diff --git a/src/HOL/Analysis/Cartesian_Space.thy b/src/HOL/Analysis/Cartesian_Space.thy --- a/src/HOL/Analysis/Cartesian_Space.thy +++ b/src/HOL/Analysis/Cartesian_Space.thy @@ -1,1441 +1,1442 @@ (* Title: HOL/Analysis/Cartesian_Space.thy Author: Amine Chaieb, University of Cambridge Author: Jose Divasón Author: Jesús Aransay Author: Johannes Hölzl, VU Amsterdam Author: Fabian Immler, TUM *) section "Linear Algebra on Finite Cartesian Products" theory Cartesian_Space imports Finite_Cartesian_Product Linear_Algebra begin subsection\<^marker>\tag unimportant\ \Type @{typ \'a ^ 'n\} and fields as vector spaces\ (*much of the following is really basic linear algebra, check for overlap? rename subsection? *) definition "cart_basis = {axis i 1 | i. i\UNIV}" lemma finite_cart_basis: "finite (cart_basis)" unfolding cart_basis_def using finite_Atleast_Atmost_nat by fastforce lemma card_cart_basis: "card (cart_basis::('a::zero_neq_one^'i) set) = CARD('i)" unfolding cart_basis_def Setcompr_eq_image by (rule card_image) (auto simp: inj_on_def axis_eq_axis) interpretation vec: vector_space "(*s) " by unfold_locales (vector algebra_simps)+ lemma independent_cart_basis: "vec.independent (cart_basis)" proof (rule vec.independent_if_scalars_zero) show "finite (cart_basis)" using finite_cart_basis . fix f::"('a, 'b) vec \ 'a" and x::"('a, 'b) vec" assume eq_0: "(\x\cart_basis. f x *s x) = 0" and x_in: "x \ cart_basis" obtain i where x: "x = axis i 1" using x_in unfolding cart_basis_def by auto have sum_eq_0: "(\x\(cart_basis) - {x}. f x * (x $ i)) = 0" proof (rule sum.neutral, rule ballI) fix xa assume xa: "xa \ cart_basis - {x}" obtain a where a: "xa = axis a 1" and a_not_i: "a \ i" using xa x unfolding cart_basis_def by auto have "xa $ i = 0" unfolding a axis_def using a_not_i by auto thus "f xa * xa $ i = 0" by simp qed have "0 = (\x\cart_basis. f x *s x) $ i" using eq_0 by simp also have "... = (\x\cart_basis. (f x *s x) $ i)" unfolding sum_component .. also have "... = (\x\cart_basis. f x * (x $ i))" unfolding vector_smult_component .. also have "... = f x * (x $ i) + (\x\(cart_basis) - {x}. f x * (x $ i))" by (rule sum.remove[OF finite_cart_basis x_in]) also have "... = f x * (x $ i)" unfolding sum_eq_0 by simp also have "... = f x" unfolding x axis_def by auto finally show "f x = 0" .. qed lemma span_cart_basis: "vec.span (cart_basis) = UNIV" proof (auto) fix x::"('a, 'b) vec" let ?f="\v. x $ (THE i. v = axis i 1)" show "x \ vec.span (cart_basis)" apply (unfold vec.span_finite[OF finite_cart_basis]) apply (rule image_eqI[of _ _ ?f]) apply (subst vec_eq_iff) apply clarify proof - fix i::'b let ?w = "axis i (1::'a)" have the_eq_i: "(THE a. ?w = axis a 1) = i" by (rule the_equality, auto simp: axis_eq_axis) have sum_eq_0: "(\v\(cart_basis) - {?w}. x $ (THE i. v = axis i 1) * v $ i) = 0" proof (rule sum.neutral, rule ballI) fix xa::"('a, 'b) vec" assume xa: "xa \ cart_basis - {?w}" obtain j where j: "xa = axis j 1" and i_not_j: "i \ j" using xa unfolding cart_basis_def by auto have the_eq_j: "(THE i. xa = axis i 1) = j" proof (rule the_equality) show "xa = axis j 1" using j . show "\i. xa = axis i 1 \ i = j" by (metis axis_eq_axis j zero_neq_one) qed show "x $ (THE i. xa = axis i 1) * xa $ i = 0" apply (subst (2) j) unfolding the_eq_j unfolding axis_def using i_not_j by simp qed have "(\v\cart_basis. x $ (THE i. v = axis i 1) *s v) $ i = (\v\cart_basis. (x $ (THE i. v = axis i 1) *s v) $ i)" unfolding sum_component .. also have "... = (\v\cart_basis. x $ (THE i. v = axis i 1) * v $ i)" unfolding vector_smult_component .. also have "... = x $ (THE a. ?w = axis a 1) * ?w $ i + (\v\(cart_basis) - {?w}. x $ (THE i. v = axis i 1) * v $ i)" by (rule sum.remove[OF finite_cart_basis], auto simp add: cart_basis_def) also have "... = x $ (THE a. ?w = axis a 1) * ?w $ i" unfolding sum_eq_0 by simp also have "... = x $ i" unfolding the_eq_i unfolding axis_def by auto finally show "x $ i = (\v\cart_basis. x $ (THE i. v = axis i 1) *s v) $ i" by simp qed simp qed (*Some interpretations:*) interpretation vec: finite_dimensional_vector_space "(*s)" "cart_basis" by (unfold_locales, auto simp add: finite_cart_basis independent_cart_basis span_cart_basis) lemma matrix_vector_mul_linear_gen[intro, simp]: "Vector_Spaces.linear (*s) (*s) ((*v) A)" by unfold_locales (vector matrix_vector_mult_def sum.distrib algebra_simps)+ lemma span_vec_eq: "vec.span X = span X" and dim_vec_eq: "vec.dim X = dim X" and dependent_vec_eq: "vec.dependent X = dependent X" and subspace_vec_eq: "vec.subspace X = subspace X" for X::"(real^'n) set" unfolding span_raw_def dim_raw_def dependent_raw_def subspace_raw_def by (auto simp: scalar_mult_eq_scaleR) lemma linear_componentwise: fixes f:: "'a::field ^'m \ 'a ^ 'n" assumes lf: "Vector_Spaces.linear (*s) (*s) f" shows "(f x)$j = sum (\i. (x$i) * (f (axis i 1)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs") proof - interpret lf: Vector_Spaces.linear "(*s)" "(*s)" f using lf . let ?M = "(UNIV :: 'm set)" let ?N = "(UNIV :: 'n set)" have fM: "finite ?M" by simp have "?rhs = (sum (\i. (x$i) *s (f (axis i 1))) ?M)$j" unfolding sum_component by simp then show ?thesis unfolding lf.sum[symmetric] lf.scale[symmetric] unfolding basis_expansion by auto qed interpretation vec: Vector_Spaces.linear "(*s)" "(*s)" "(*v) A" using matrix_vector_mul_linear_gen. interpretation vec: finite_dimensional_vector_space_pair "(*s)" cart_basis "(*s)" cart_basis .. lemma matrix_works: assumes lf: "Vector_Spaces.linear (*s) (*s) f" shows "matrix f *v x = f (x::'a::field ^ 'n)" apply (simp add: matrix_def matrix_vector_mult_def vec_eq_iff mult.commute) apply clarify apply (rule linear_componentwise[OF lf, symmetric]) done lemma matrix_of_matrix_vector_mul[simp]: "matrix(\x. A *v (x :: 'a::field ^ 'n)) = A" by (simp add: matrix_eq matrix_works) lemma matrix_compose_gen: assumes lf: "Vector_Spaces.linear (*s) (*s) (f::'a::{field}^'n \ 'a^'m)" and lg: "Vector_Spaces.linear (*s) (*s) (g::'a^'m \ 'a^_)" shows "matrix (g o f) = matrix g ** matrix f" using lf lg Vector_Spaces.linear_compose[OF lf lg] matrix_works[OF Vector_Spaces.linear_compose[OF lf lg]] by (simp add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def) lemma matrix_compose: assumes "linear (f::real^'n \ real^'m)" "linear (g::real^'m \ real^_)" shows "matrix (g o f) = matrix g ** matrix f" using matrix_compose_gen[of f g] assms by (simp add: linear_def scalar_mult_eq_scaleR) lemma left_invertible_transpose: "(\(B). B ** transpose (A) = mat (1::'a::comm_semiring_1)) \ (\(B). A ** B = mat 1)" by (metis matrix_transpose_mul transpose_mat transpose_transpose) lemma right_invertible_transpose: "(\(B). transpose (A) ** B = mat (1::'a::comm_semiring_1)) \ (\(B). B ** A = mat 1)" by (metis matrix_transpose_mul transpose_mat transpose_transpose) lemma linear_matrix_vector_mul_eq: "Vector_Spaces.linear (*s) (*s) f \ linear (f :: real^'n \ real ^'m)" by (simp add: scalar_mult_eq_scaleR linear_def) lemma matrix_vector_mul[simp]: "Vector_Spaces.linear (*s) (*s) g \ (\y. matrix g *v y) = g" "linear f \ (\x. matrix f *v x) = f" "bounded_linear f \ (\x. matrix f *v x) = f" for f :: "real^'n \ real ^'m" by (simp_all add: ext matrix_works linear_matrix_vector_mul_eq linear_linear) lemma matrix_left_invertible_injective: fixes A :: "'a::field^'n^'m" shows "(\B. B ** A = mat 1) \ inj ((*v) A)" proof safe fix B assume B: "B ** A = mat 1" show "inj ((*v) A)" unfolding inj_on_def by (metis B matrix_vector_mul_assoc matrix_vector_mul_lid) next assume "inj ((*v) A)" from vec.linear_injective_left_inverse[OF matrix_vector_mul_linear_gen this] obtain g where "Vector_Spaces.linear (*s) (*s) g" and g: "g \ (*v) A = id" by blast have "matrix g ** A = mat 1" by (metis matrix_vector_mul_linear_gen \Vector_Spaces.linear (*s) (*s) g\ g matrix_compose_gen matrix_eq matrix_id_mat_1 matrix_vector_mul(1)) then show "\B. B ** A = mat 1" by metis qed lemma matrix_left_invertible_ker: "(\B. (B::'a::{field} ^'m^'n) ** (A::'a::{field}^'n^'m) = mat 1) \ (\x. A *v x = 0 \ x = 0)" unfolding matrix_left_invertible_injective using vec.inj_on_iff_eq_0[OF vec.subspace_UNIV, of A] by (simp add: inj_on_def) lemma matrix_right_invertible_surjective: "(\B. (A::'a::field^'n^'m) ** (B::'a::field^'m^'n) = mat 1) \ surj (\x. A *v x)" proof - { fix B :: "'a ^'m^'n" assume AB: "A ** B = mat 1" { fix x :: "'a ^ 'm" have "A *v (B *v x) = x" by (simp add: matrix_vector_mul_assoc AB) } hence "surj ((*v) A)" unfolding surj_def by metis } moreover { assume sf: "surj ((*v) A)" from vec.linear_surjective_right_inverse[OF _ this] obtain g:: "'a ^'m \ 'a ^'n" where g: "Vector_Spaces.linear (*s) (*s) g" "(*v) A \ g = id" by blast have "A ** (matrix g) = mat 1" unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)] using g(2) unfolding o_def fun_eq_iff id_def . hence "\B. A ** (B::'a^'m^'n) = mat 1" by blast } ultimately show ?thesis unfolding surj_def by blast qed lemma matrix_left_invertible_independent_columns: fixes A :: "'a::{field}^'n^'m" shows "(\(B::'a ^'m^'n). B ** A = mat 1) \ (\c. sum (\i. c i *s column i A) (UNIV :: 'n set) = 0 \ (\i. c i = 0))" (is "?lhs \ ?rhs") proof - let ?U = "UNIV :: 'n set" { assume k: "\x. A *v x = 0 \ x = 0" { fix c i assume c: "sum (\i. c i *s column i A) ?U = 0" and i: "i \ ?U" let ?x = "\ i. c i" have th0:"A *v ?x = 0" using c by (vector matrix_mult_sum) from k[rule_format, OF th0] i have "c i = 0" by (vector vec_eq_iff)} hence ?rhs by blast } moreover { assume H: ?rhs { fix x assume x: "A *v x = 0" let ?c = "\i. ((x$i ):: 'a)" from H[rule_format, of ?c, unfolded matrix_mult_sum[symmetric], OF x] have "x = 0" by vector } } ultimately show ?thesis unfolding matrix_left_invertible_ker by auto qed lemma matrix_right_invertible_independent_rows: fixes A :: "'a::{field}^'n^'m" shows "(\(B::'a^'m^'n). A ** B = mat 1) \ (\c. sum (\i. c i *s row i A) (UNIV :: 'm set) = 0 \ (\i. c i = 0))" unfolding left_invertible_transpose[symmetric] matrix_left_invertible_independent_columns by (simp add:) lemma matrix_right_invertible_span_columns: "(\(B::'a::field ^'n^'m). (A::'a ^'m^'n) ** B = mat 1) \ vec.span (columns A) = UNIV" (is "?lhs = ?rhs") proof - let ?U = "UNIV :: 'm set" have fU: "finite ?U" by simp have lhseq: "?lhs \ (\y. \(x::'a^'m). sum (\i. (x$i) *s column i A) ?U = y)" unfolding matrix_right_invertible_surjective matrix_mult_sum surj_def by (simp add: eq_commute) have rhseq: "?rhs \ (\x. x \ vec.span (columns A))" by blast { assume h: ?lhs { fix x:: "'a ^'n" from h[unfolded lhseq, rule_format, of x] obtain y :: "'a ^'m" where y: "sum (\i. (y$i) *s column i A) ?U = x" by blast have "x \ vec.span (columns A)" unfolding y[symmetric] scalar_mult_eq_scaleR proof (rule vec.span_sum [OF vec.span_scale]) show "column i A \ vec.span (columns A)" for i using columns_def vec.span_superset by auto qed } then have ?rhs unfolding rhseq by blast } moreover { assume h:?rhs let ?P = "\(y::'a ^'n). \(x::'a^'m). sum (\i. (x$i) *s column i A) ?U = y" { fix y have "y \ vec.span (columns A)" unfolding h by blast then have "?P y" proof (induction rule: vec.span_induct_alt) case base then show ?case by (metis (full_types) matrix_mult_sum matrix_vector_mult_0_right) next case (step c y1 y2) from step obtain i where i: "i \ ?U" "y1 = column i A" unfolding columns_def by blast obtain x:: "'a ^'m" where x: "sum (\i. (x$i) *s column i A) ?U = y2" using step by blast let ?x = "(\ j. if j = i then c + (x$i) else (x$j))::'a^'m" show ?case proof (rule exI[where x= "?x"], vector, auto simp add: i x[symmetric] if_distrib distrib_left if_distribR cong del: if_weak_cong) fix j have th: "\xa \ ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j) else (x$xa) * ((column xa A$j))) = (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))" using i(1) by (simp add: field_simps) have "sum (\xa. if xa = i then (c + (x$i)) * ((column xa A)$j) else (x$xa) * ((column xa A$j))) ?U = sum (\xa. (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))) ?U" by (rule sum.cong[OF refl]) (use th in blast) also have "\ = sum (\xa. if xa = i then c * ((column i A)$j) else 0) ?U + sum (\xa. ((x$xa) * ((column xa A)$j))) ?U" by (simp add: sum.distrib) also have "\ = c * ((column i A)$j) + sum (\xa. ((x$xa) * ((column xa A)$j))) ?U" unfolding sum.delta[OF fU] using i(1) by simp finally show "sum (\xa. if xa = i then (c + (x$i)) * ((column xa A)$j) else (x$xa) * ((column xa A$j))) ?U = c * ((column i A)$j) + sum (\xa. ((x$xa) * ((column xa A)$j))) ?U" . qed qed } then have ?lhs unfolding lhseq .. } ultimately show ?thesis by blast qed lemma matrix_left_invertible_span_rows_gen: "(\(B::'a^'m^'n). B ** (A::'a::field^'n^'m) = mat 1) \ vec.span (rows A) = UNIV" unfolding right_invertible_transpose[symmetric] unfolding columns_transpose[symmetric] unfolding matrix_right_invertible_span_columns .. lemma matrix_left_invertible_span_rows: "(\(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \ span (rows A) = UNIV" using matrix_left_invertible_span_rows_gen[of A] by (simp add: span_vec_eq) lemma matrix_left_right_inverse: fixes A A' :: "'a::{field}^'n^'n" shows "A ** A' = mat 1 \ A' ** A = mat 1" proof - { fix A A' :: "'a ^'n^'n" assume AA': "A ** A' = mat 1" have sA: "surj ((*v) A)" using AA' matrix_right_invertible_surjective by auto from vec.linear_surjective_isomorphism[OF matrix_vector_mul_linear_gen sA] obtain f' :: "'a ^'n \ 'a ^'n" where f': "Vector_Spaces.linear (*s) (*s) f'" "\x. f' (A *v x) = x" "\x. A *v f' x = x" by blast have th: "matrix f' ** A = mat 1" by (simp add: matrix_eq matrix_works[OF f'(1)] matrix_vector_mul_assoc[symmetric] f'(2)[rule_format]) hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp hence "matrix f' = A'" by (simp add: matrix_mul_assoc[symmetric] AA') hence "matrix f' ** A = A' ** A" by simp hence "A' ** A = mat 1" by (simp add: th) } then show ?thesis by blast qed lemma invertible_left_inverse: fixes A :: "'a::{field}^'n^'n" shows "invertible A \ (\(B::'a^'n^'n). B ** A = mat 1)" by (metis invertible_def matrix_left_right_inverse) lemma invertible_right_inverse: fixes A :: "'a::{field}^'n^'n" shows "invertible A \ (\(B::'a^'n^'n). A** B = mat 1)" by (metis invertible_def matrix_left_right_inverse) lemma invertible_mult: assumes inv_A: "invertible A" and inv_B: "invertible B" shows "invertible (A**B)" proof - obtain A' where AA': "A ** A' = mat 1" and A'A: "A' ** A = mat 1" using inv_A unfolding invertible_def by blast obtain B' where BB': "B ** B' = mat 1" and B'B: "B' ** B = mat 1" using inv_B unfolding invertible_def by blast show ?thesis proof (unfold invertible_def, rule exI[of _ "B'**A'"], rule conjI) have "A ** B ** (B' ** A') = A ** (B ** (B' ** A'))" using matrix_mul_assoc[of A B "(B' ** A')", symmetric] . also have "... = A ** (B ** B' ** A')" unfolding matrix_mul_assoc[of B "B'" "A'"] .. also have "... = A ** (mat 1 ** A')" unfolding BB' .. also have "... = A ** A'" unfolding matrix_mul_lid .. also have "... = mat 1" unfolding AA' .. finally show "A ** B ** (B' ** A') = mat (1::'a)" . have "B' ** A' ** (A ** B) = B' ** (A' ** (A ** B))" using matrix_mul_assoc[of B' A' "(A ** B)", symmetric] . also have "... = B' ** (A' ** A ** B)" unfolding matrix_mul_assoc[of A' A B] .. also have "... = B' ** (mat 1 ** B)" unfolding A'A .. also have "... = B' ** B" unfolding matrix_mul_lid .. also have "... = mat 1" unfolding B'B .. finally show "B' ** A' ** (A ** B) = mat 1" . qed qed lemma transpose_invertible: fixes A :: "real^'n^'n" assumes "invertible A" shows "invertible (transpose A)" by (meson assms invertible_def matrix_left_right_inverse right_invertible_transpose) lemma vector_matrix_mul_assoc: fixes v :: "('a::comm_semiring_1)^'n" shows "(v v* M) v* N = v v* (M ** N)" proof - from matrix_vector_mul_assoc have "transpose N *v (transpose M *v v) = (transpose N ** transpose M) *v v" by fast thus "(v v* M) v* N = v v* (M ** N)" by (simp add: matrix_transpose_mul [symmetric]) qed lemma matrix_scaleR_vector_ac: fixes A :: "real^('m::finite)^'n" shows "A *v (k *\<^sub>R v) = k *\<^sub>R A *v v" by (metis matrix_vector_mult_scaleR transpose_scalar vector_scaleR_matrix_ac vector_transpose_matrix) lemma scaleR_matrix_vector_assoc: fixes A :: "real^('m::finite)^'n" shows "k *\<^sub>R (A *v v) = k *\<^sub>R A *v v" by (metis matrix_scaleR_vector_ac matrix_vector_mult_scaleR) (*Finally, some interesting theorems and interpretations that don't appear in any file of the library.*) locale linear_first_finite_dimensional_vector_space = l?: Vector_Spaces.linear scaleB scaleC f + B?: finite_dimensional_vector_space scaleB BasisB for scaleB :: "('a::field => 'b::ab_group_add => 'b)" (infixr "*b" 75) and scaleC :: "('a => 'c::ab_group_add => 'c)" (infixr "*c" 75) and BasisB :: "('b set)" and f :: "('b=>'c)" lemma vec_dim_card: "vec.dim (UNIV::('a::{field}^'n) set) = CARD ('n)" proof - let ?f="\i::'n. axis i (1::'a)" have "vec.dim (UNIV::('a::{field}^'n) set) = card (cart_basis::('a^'n) set)" unfolding vec.dim_UNIV .. also have "... = card ({i. i\ UNIV}::('n) set)" proof (rule bij_betw_same_card[of ?f, symmetric], unfold bij_betw_def, auto) show "inj (\i::'n. axis i (1::'a))" by (simp add: inj_on_def axis_eq_axis) fix i::'n show "axis i 1 \ cart_basis" unfolding cart_basis_def by auto fix x::"'a^'n" assume "x \ cart_basis" thus "x \ range (\i. axis i 1)" unfolding cart_basis_def by auto qed also have "... = CARD('n)" by auto finally show ?thesis . qed interpretation vector_space_over_itself: vector_space "(*) :: 'a::field \ 'a \ 'a" by unfold_locales (simp_all add: algebra_simps) lemmas [simp del] = vector_space_over_itself.scale_scale interpretation vector_space_over_itself: finite_dimensional_vector_space "(*) :: 'a::field => 'a => 'a" "{1}" by unfold_locales (auto simp: vector_space_over_itself.span_singleton) lemma dimension_eq_1[code_unfold]: "vector_space_over_itself.dimension TYPE('a::field)= 1" unfolding vector_space_over_itself.dimension_def by simp lemma dim_subset_UNIV_cart_gen: fixes S :: "('a::field^'n) set" shows "vec.dim S \ CARD('n)" by (metis vec.dim_eq_full vec.dim_subset_UNIV vec.span_UNIV vec_dim_card) lemma dim_subset_UNIV_cart: fixes S :: "(real^'n) set" shows "dim S \ CARD('n)" using dim_subset_UNIV_cart_gen[of S] by (simp add: dim_vec_eq) text\Two sometimes fruitful ways of looking at matrix-vector multiplication.\ lemma matrix_mult_dot: "A *v x = (\ i. inner (A$i) x)" by (simp add: matrix_vector_mult_def inner_vec_def) lemma adjoint_matrix: "adjoint(\x. (A::real^'n^'m) *v x) = (\x. transpose A *v x)" apply (rule adjoint_unique) apply (simp add: transpose_def inner_vec_def matrix_vector_mult_def sum_distrib_right sum_distrib_left) apply (subst sum.swap) apply (simp add: ac_simps) done lemma matrix_adjoint: assumes lf: "linear (f :: real^'n \ real ^'m)" shows "matrix(adjoint f) = transpose(matrix f)" proof - have "matrix(adjoint f) = matrix(adjoint ((*v) (matrix f)))" by (simp add: lf) also have "\ = transpose(matrix f)" unfolding adjoint_matrix matrix_of_matrix_vector_mul apply rule done finally show ?thesis . qed subsection\ Rank of a matrix\ text\Equivalence of row and column rank is taken from George Mackiw's paper, Mathematics Magazine 1995, p. 285.\ lemma matrix_vector_mult_in_columnspace_gen: fixes A :: "'a::field^'n^'m" shows "(A *v x) \ vec.span(columns A)" apply (simp add: matrix_vector_column columns_def transpose_def column_def) apply (intro vec.span_sum vec.span_scale) apply (force intro: vec.span_base) done lemma matrix_vector_mult_in_columnspace: fixes A :: "real^'n^'m" shows "(A *v x) \ span(columns A)" using matrix_vector_mult_in_columnspace_gen[of A x] by (simp add: span_vec_eq) lemma subspace_orthogonal_to_vector: "subspace {y. orthogonal x y}" by (simp add: subspace_def orthogonal_clauses) lemma orthogonal_nullspace_rowspace: fixes A :: "real^'n^'m" assumes 0: "A *v x = 0" and y: "y \ span(rows A)" shows "orthogonal x y" using y proof (induction rule: span_induct) case base then show ?case by (simp add: subspace_orthogonal_to_vector) next case (step v) then obtain i where "v = row i A" by (auto simp: rows_def) with 0 show ?case unfolding orthogonal_def inner_vec_def matrix_vector_mult_def row_def by (simp add: mult.commute) (metis (no_types) vec_lambda_beta zero_index) qed lemma nullspace_inter_rowspace: fixes A :: "real^'n^'m" shows "A *v x = 0 \ x \ span(rows A) \ x = 0" using orthogonal_nullspace_rowspace orthogonal_self span_zero matrix_vector_mult_0_right by blast lemma matrix_vector_mul_injective_on_rowspace: fixes A :: "real^'n^'m" shows "\A *v x = A *v y; x \ span(rows A); y \ span(rows A)\ \ x = y" using nullspace_inter_rowspace [of A "x-y"] by (metis diff_eq_diff_eq diff_self matrix_vector_mult_diff_distrib span_diff) definition\<^marker>\tag important\ rank :: "'a::field^'n^'m=>nat" where row_rank_def_gen: "rank A \ vec.dim(rows A)" lemma row_rank_def: "rank A = dim (rows A)" for A::"real^'n^'m" by (auto simp: row_rank_def_gen dim_vec_eq) lemma dim_rows_le_dim_columns: fixes A :: "real^'n^'m" shows "dim(rows A) \ dim(columns A)" proof - have "dim (span (rows A)) \ dim (span (columns A))" proof - obtain B where "independent B" "span(rows A) \ span B" and B: "B \ span(rows A)""card B = dim (span(rows A))" using basis_exists [of "span(rows A)"] by metis with span_subspace have eq: "span B = span(rows A)" by auto then have inj: "inj_on ((*v) A) (span B)" by (simp add: inj_on_def matrix_vector_mul_injective_on_rowspace) then have ind: "independent ((*v) A ` B)" by (rule linear_independent_injective_image [OF Finite_Cartesian_Product.matrix_vector_mul_linear \independent B\]) have "dim (span (rows A)) \ card ((*v) A ` B)" unfolding B(2)[symmetric] using inj by (auto simp: card_image inj_on_subset span_superset) also have "\ \ dim (span (columns A))" using _ ind by (rule independent_card_le_dim) (auto intro!: matrix_vector_mult_in_columnspace) finally show ?thesis . qed then show ?thesis - by (simp add: dim_span) + by (simp) qed lemma column_rank_def: fixes A :: "real^'n^'m" shows "rank A = dim(columns A)" unfolding row_rank_def by (metis columns_transpose dim_rows_le_dim_columns le_antisym rows_transpose) lemma rank_transpose: fixes A :: "real^'n^'m" shows "rank(transpose A) = rank A" by (metis column_rank_def row_rank_def rows_transpose) lemma matrix_vector_mult_basis: fixes A :: "real^'n^'m" shows "A *v (axis k 1) = column k A" by (simp add: cart_eq_inner_axis column_def matrix_mult_dot) lemma columns_image_basis: fixes A :: "real^'n^'m" shows "columns A = (*v) A ` (range (\i. axis i 1))" by (force simp: columns_def matrix_vector_mult_basis [symmetric]) lemma rank_dim_range: fixes A :: "real^'n^'m" shows "rank A = dim(range (\x. A *v x))" unfolding column_rank_def proof (rule span_eq_dim) have "span (columns A) \ span (range ((*v) A))" (is "?l \ ?r") by (simp add: columns_image_basis image_subsetI span_mono) then show "?l = ?r" by (metis (no_types, lifting) image_subset_iff matrix_vector_mult_in_columnspace span_eq span_span) qed lemma rank_bound: fixes A :: "real^'n^'m" shows "rank A \ min CARD('m) (CARD('n))" by (metis (mono_tags, lifting) dim_subset_UNIV_cart min.bounded_iff column_rank_def row_rank_def) lemma full_rank_injective: fixes A :: "real^'n^'m" shows "rank A = CARD('n) \ inj ((*v) A)" by (simp add: matrix_left_invertible_injective [symmetric] matrix_left_invertible_span_rows row_rank_def dim_eq_full [symmetric] card_cart_basis vec.dimension_def) lemma full_rank_surjective: fixes A :: "real^'n^'m" shows "rank A = CARD('m) \ surj ((*v) A)" by (simp add: matrix_right_invertible_surjective [symmetric] left_invertible_transpose [symmetric] matrix_left_invertible_injective full_rank_injective [symmetric] rank_transpose) lemma rank_I: "rank(mat 1::real^'n^'n) = CARD('n)" by (simp add: full_rank_injective inj_on_def) lemma less_rank_noninjective: fixes A :: "real^'n^'m" shows "rank A < CARD('n) \ \ inj ((*v) A)" using less_le rank_bound by (auto simp: full_rank_injective [symmetric]) lemma matrix_nonfull_linear_equations_eq: fixes A :: "real^'n^'m" shows "(\x. (x \ 0) \ A *v x = 0) \ rank A \ CARD('n)" by (meson matrix_left_invertible_injective full_rank_injective matrix_left_invertible_ker) lemma rank_eq_0: "rank A = 0 \ A = 0" and rank_0 [simp]: "rank (0::real^'n^'m) = 0" for A :: "real^'n^'m" by (auto simp: rank_dim_range matrix_eq) lemma rank_mul_le_right: fixes A :: "real^'n^'m" and B :: "real^'p^'n" shows "rank(A ** B) \ rank B" proof - have "rank(A ** B) \ dim ((*v) A ` range ((*v) B))" by (auto simp: rank_dim_range image_comp o_def matrix_vector_mul_assoc) also have "\ \ rank B" by (simp add: rank_dim_range dim_image_le) finally show ?thesis . qed lemma rank_mul_le_left: fixes A :: "real^'n^'m" and B :: "real^'p^'n" shows "rank(A ** B) \ rank A" by (metis matrix_transpose_mul rank_mul_le_right rank_transpose) subsection\<^marker>\tag unimportant\ \Lemmas for working on \real^1/2/3/4\\ lemma exhaust_2: fixes x :: 2 shows "x = 1 \ x = 2" proof (induct x) case (of_int z) then have "0 \ z" and "z < 2" by simp_all then have "z = 0 | z = 1" by arith then show ?case by auto qed lemma forall_2: "(\i::2. P i) \ P 1 \ P 2" by (metis exhaust_2) lemma exhaust_3: fixes x :: 3 shows "x = 1 \ x = 2 \ x = 3" proof (induct x) case (of_int z) then have "0 \ z" and "z < 3" by simp_all then have "z = 0 \ z = 1 \ z = 2" by arith then show ?case by auto qed lemma forall_3: "(\i::3. P i) \ P 1 \ P 2 \ P 3" by (metis exhaust_3) lemma exhaust_4: fixes x :: 4 shows "x = 1 \ x = 2 \ x = 3 \ x = 4" proof (induct x) case (of_int z) then have "0 \ z" and "z < 4" by simp_all then have "z = 0 \ z = 1 \ z = 2 \ z = 3" by arith then show ?case by auto qed lemma forall_4: "(\i::4. P i) \ P 1 \ P 2 \ P 3 \ P 4" by (metis exhaust_4) lemma UNIV_1 [simp]: "UNIV = {1::1}" by (auto simp add: num1_eq_iff) lemma UNIV_2: "UNIV = {1::2, 2::2}" using exhaust_2 by auto lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}" using exhaust_3 by auto lemma UNIV_4: "UNIV = {1::4, 2::4, 3::4, 4::4}" using exhaust_4 by auto lemma sum_1: "sum f (UNIV::1 set) = f 1" unfolding UNIV_1 by simp lemma sum_2: "sum f (UNIV::2 set) = f 1 + f 2" unfolding UNIV_2 by simp lemma sum_3: "sum f (UNIV::3 set) = f 1 + f 2 + f 3" unfolding UNIV_3 by (simp add: ac_simps) lemma sum_4: "sum f (UNIV::4 set) = f 1 + f 2 + f 3 + f 4" unfolding UNIV_4 by (simp add: ac_simps) subsection\<^marker>\tag unimportant\\The collapse of the general concepts to dimension one\ lemma vector_one: "(x::'a ^1) = (\ i. (x$1))" by (simp add: vec_eq_iff) lemma forall_one: "(\(x::'a ^1). P x) \ (\x. P(\ i. x))" apply auto apply (erule_tac x= "x$1" in allE) apply (simp only: vector_one[symmetric]) done lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)" by (simp add: norm_vec_def) lemma dist_vector_1: fixes x :: "'a::real_normed_vector^1" shows "dist x y = dist (x$1) (y$1)" by (simp add: dist_norm norm_vector_1) lemma norm_real: "norm(x::real ^ 1) = \x$1\" by (simp add: norm_vector_1) lemma dist_real: "dist(x::real ^ 1) y = \(x$1) - (y$1)\" by (auto simp add: norm_real dist_norm) subsection\<^marker>\tag unimportant\\Routine results connecting the types \<^typ>\real^1\ and \<^typ>\real\\ lemma vector_one_nth [simp]: fixes x :: "'a^1" shows "vec (x $ 1) = x" by (metis vec_def vector_one) lemma tendsto_at_within_vector_1: fixes S :: "'a :: metric_space set" assumes "(f \ fx) (at x within S)" shows "((\y::'a^1. \ i. f (y $ 1)) \ (vec fx::'a^1)) (at (vec x) within vec ` S)" proof (rule topological_tendstoI) fix T :: "('a^1) set" assume "open T" "vec fx \ T" have "\\<^sub>F x in at x within S. f x \ (\x. x $ 1) ` T" using \open T\ \vec fx \ T\ assms open_image_vec_nth tendsto_def by fastforce then show "\\<^sub>F x::'a^1 in at (vec x) within vec ` S. (\ i. f (x $ 1)) \ T" unfolding eventually_at dist_norm [symmetric] by (rule ex_forward) (use \open T\ in \fastforce simp: dist_norm dist_vec_def L2_set_def image_iff vector_one open_vec_def\) qed lemma has_derivative_vector_1: assumes der_g: "(g has_derivative (\x. x * g' a)) (at a within S)" shows "((\x. vec (g (x $ 1))) has_derivative (*\<^sub>R) (g' a)) (at ((vec a)::real^1) within vec ` S)" using der_g apply (auto simp: Deriv.has_derivative_within bounded_linear_scaleR_right norm_vector_1) apply (drule tendsto_at_within_vector_1, vector) apply (auto simp: algebra_simps eventually_at tendsto_def) done subsection\<^marker>\tag unimportant\\Explicit vector construction from lists\ definition "vector l = (\ i. foldr (\x f n. fun_upd (f (n+1)) n x) l (\n x. 0) 1 i)" lemma vector_1 [simp]: "(vector[x]) $1 = x" unfolding vector_def by simp lemma vector_2 [simp]: "(vector[x,y]) $1 = x" "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)" unfolding vector_def by simp_all lemma vector_3 [simp]: "(vector [x,y,z] ::('a::zero)^3)$1 = x" "(vector [x,y,z] ::('a::zero)^3)$2 = y" "(vector [x,y,z] ::('a::zero)^3)$3 = z" unfolding vector_def by simp_all lemma forall_vector_1: "(\v::'a::zero^1. P v) \ (\x. P(vector[x]))" by (metis vector_1 vector_one) lemma forall_vector_2: "(\v::'a::zero^2. P v) \ (\x y. P(vector[x, y]))" apply auto apply (erule_tac x="v$1" in allE) apply (erule_tac x="v$2" in allE) apply (subgoal_tac "vector [v$1, v$2] = v") apply simp apply (vector vector_def) apply (simp add: forall_2) done lemma forall_vector_3: "(\v::'a::zero^3. P v) \ (\x y z. P(vector[x, y, z]))" apply auto apply (erule_tac x="v$1" in allE) apply (erule_tac x="v$2" in allE) apply (erule_tac x="v$3" in allE) apply (subgoal_tac "vector [v$1, v$2, v$3] = v") apply simp apply (vector vector_def) apply (simp add: forall_3) done subsection\<^marker>\tag unimportant\ \lambda skolemization on cartesian products\ lemma lambda_skolem: "(\i. \x. P i x) \ (\x::'a ^ 'n. \i. P i (x $ i))" (is "?lhs \ ?rhs") proof - let ?S = "(UNIV :: 'n set)" { assume H: "?rhs" then have ?lhs by auto } moreover { assume H: "?lhs" then obtain f where f:"\i. P i (f i)" unfolding choice_iff by metis let ?x = "(\ i. (f i)) :: 'a ^ 'n" { fix i from f have "P i (f i)" by metis then have "P i (?x $ i)" by auto } hence "\i. P i (?x$i)" by metis hence ?rhs by metis } ultimately show ?thesis by metis qed text \The same result in terms of square matrices.\ text \Considering an n-element vector as an n-by-1 or 1-by-n matrix.\ definition "rowvector v = (\ i j. (v$j))" definition "columnvector v = (\ i j. (v$i))" lemma transpose_columnvector: "transpose(columnvector v) = rowvector v" by (simp add: transpose_def rowvector_def columnvector_def vec_eq_iff) lemma transpose_rowvector: "transpose(rowvector v) = columnvector v" by (simp add: transpose_def columnvector_def rowvector_def vec_eq_iff) lemma dot_rowvector_columnvector: "columnvector (A *v v) = A ** columnvector v" by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def) lemma dot_matrix_product: "(x::real^'n) \ y = (((rowvector x ::real^'n^1) ** (columnvector y :: real^1^'n))$1)$1" by (vector matrix_matrix_mult_def rowvector_def columnvector_def inner_vec_def) lemma dot_matrix_vector_mul: fixes A B :: "real ^'n ^'n" and x y :: "real ^'n" shows "(A *v x) \ (B *v y) = (((rowvector x :: real^'n^1) ** ((transpose A ** B) ** (columnvector y :: real ^1^'n)))$1)$1" unfolding dot_matrix_product transpose_columnvector[symmetric] dot_rowvector_columnvector matrix_transpose_mul matrix_mul_assoc .. lemma dim_substandard_cart: "vec.dim {x::'a::field^'n. \i. i \ d \ x$i = 0} = card d" (is "vec.dim ?A = _") proof (rule vec.dim_unique) let ?B = "((\x. axis x 1) ` d)" have subset_basis: "?B \ cart_basis" by (auto simp: cart_basis_def) show "?B \ ?A" by (auto simp: axis_def) show "vec.independent ((\x. axis x 1) ` d)" using subset_basis by (rule vec.independent_mono[OF vec.independent_Basis]) have "x \ vec.span ?B" if "\i. i \ d \ x $ i = 0" for x::"'a^'n" proof - have "finite ?B" using subset_basis finite_cart_basis by (rule finite_subset) have "x = (\i\UNIV. x $ i *s axis i 1)" by (rule basis_expansion[symmetric]) also have "\ = (\i\d. (x $ i) *s axis i 1)" by (rule sum.mono_neutral_cong_right) (auto simp: that) also have "\ \ vec.span ?B" by (simp add: vec.span_sum vec.span_clauses) finally show "x \ vec.span ?B" . qed then show "?A \ vec.span ?B" by auto qed (simp add: card_image inj_on_def axis_eq_axis) lemma affinity_inverses: assumes m0: "m \ (0::'a::field)" shows "(\x. m *s x + c) \ (\x. inverse(m) *s x + (-(inverse(m) *s c))) = id" "(\x. inverse(m) *s x + (-(inverse(m) *s c))) \ (\x. m *s x + c) = id" using m0 by (auto simp add: fun_eq_iff vector_add_ldistrib diff_conv_add_uminus simp del: add_uminus_conv_diff) lemma vector_affinity_eq: assumes m0: "(m::'a::field) \ 0" shows "m *s x + c = y \ x = inverse m *s y + -(inverse m *s c)" proof assume h: "m *s x + c = y" hence "m *s x = y - c" by (simp add: field_simps) hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp then show "x = inverse m *s y + - (inverse m *s c)" using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib) next assume h: "x = inverse m *s y + - (inverse m *s c)" show "m *s x + c = y" unfolding h using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib) qed lemma vector_eq_affinity: "(m::'a::field) \ 0 ==> (y = m *s x + c \ inverse(m) *s y + -(inverse(m) *s c) = x)" using vector_affinity_eq[where m=m and x=x and y=y and c=c] by metis lemma vector_cart: fixes f :: "real^'n \ real" shows "(\ i. f (axis i 1)) = (\i\Basis. f i *\<^sub>R i)" unfolding euclidean_eq_iff[where 'a="real^'n"] by simp (simp add: Basis_vec_def inner_axis) lemma const_vector_cart:"((\ i. d)::real^'n) = (\i\Basis. d *\<^sub>R i)" by (rule vector_cart) subsection\<^marker>\tag unimportant\ \Explicit formulas for low dimensions\ lemma prod_neutral_const: "prod f {(1::nat)..1} = f 1" by simp lemma prod_2: "prod f {(1::nat)..2} = f 1 * f 2" by (simp add: eval_nat_numeral atLeastAtMostSuc_conv mult.commute) lemma prod_3: "prod f {(1::nat)..3} = f 1 * f 2 * f 3" by (simp add: eval_nat_numeral atLeastAtMostSuc_conv mult.commute) subsection \Orthogonality of a matrix\ definition\<^marker>\tag important\ "orthogonal_matrix (Q::'a::semiring_1^'n^'n) \ transpose Q ** Q = mat 1 \ Q ** transpose Q = mat 1" lemma orthogonal_matrix: "orthogonal_matrix (Q:: real ^'n^'n) \ transpose Q ** Q = mat 1" by (metis matrix_left_right_inverse orthogonal_matrix_def) lemma orthogonal_matrix_id: "orthogonal_matrix (mat 1 :: _^'n^'n)" by (simp add: orthogonal_matrix_def) proposition orthogonal_matrix_mul: fixes A :: "real ^'n^'n" assumes "orthogonal_matrix A" "orthogonal_matrix B" shows "orthogonal_matrix(A ** B)" using assms by (simp add: orthogonal_matrix matrix_transpose_mul matrix_left_right_inverse matrix_mul_assoc) proposition orthogonal_transformation_matrix: fixes f:: "real^'n \ real^'n" shows "orthogonal_transformation f \ linear f \ orthogonal_matrix(matrix f)" (is "?lhs \ ?rhs") proof - let ?mf = "matrix f" let ?ot = "orthogonal_transformation f" let ?U = "UNIV :: 'n set" have fU: "finite ?U" by simp let ?m1 = "mat 1 :: real ^'n^'n" { assume ot: ?ot from ot have lf: "Vector_Spaces.linear (*s) (*s) f" and fd: "\v w. f v \ f w = v \ w" unfolding orthogonal_transformation_def orthogonal_matrix linear_def scalar_mult_eq_scaleR by blast+ { fix i j let ?A = "transpose ?mf ** ?mf" have th0: "\b (x::'a::comm_ring_1). (if b then 1 else 0)*x = (if b then x else 0)" "\b (x::'a::comm_ring_1). x*(if b then 1 else 0) = (if b then x else 0)" by simp_all from fd[of "axis i 1" "axis j 1", simplified matrix_works[OF lf, symmetric] dot_matrix_vector_mul] have "?A$i$j = ?m1 $ i $ j" by (simp add: inner_vec_def matrix_matrix_mult_def columnvector_def rowvector_def th0 sum.delta[OF fU] mat_def axis_def) } then have "orthogonal_matrix ?mf" unfolding orthogonal_matrix by vector with lf have ?rhs unfolding linear_def scalar_mult_eq_scaleR by blast } moreover { assume lf: "Vector_Spaces.linear (*s) (*s) f" and om: "orthogonal_matrix ?mf" from lf om have ?lhs unfolding orthogonal_matrix_def norm_eq orthogonal_transformation apply (simp only: matrix_works[OF lf, symmetric] dot_matrix_vector_mul) apply (simp add: dot_matrix_product linear_def scalar_mult_eq_scaleR) done } ultimately show ?thesis by (auto simp: linear_def scalar_mult_eq_scaleR) qed -subsection \ We can find an orthogonal matrix taking any unit vector to any other\ +subsection \Finding an Orthogonal Matrix\ + +text \We can find an orthogonal matrix taking any unit vector to any other.\ lemma orthogonal_matrix_transpose [simp]: "orthogonal_matrix(transpose A) \ orthogonal_matrix A" by (auto simp: orthogonal_matrix_def) lemma orthogonal_matrix_orthonormal_columns: fixes A :: "real^'n^'n" shows "orthogonal_matrix A \ (\i. norm(column i A) = 1) \ (\i j. i \ j \ orthogonal (column i A) (column j A))" by (auto simp: orthogonal_matrix matrix_mult_transpose_dot_column vec_eq_iff mat_def norm_eq_1 orthogonal_def) lemma orthogonal_matrix_orthonormal_rows: fixes A :: "real^'n^'n" shows "orthogonal_matrix A \ (\i. norm(row i A) = 1) \ (\i j. i \ j \ orthogonal (row i A) (row j A))" using orthogonal_matrix_orthonormal_columns [of "transpose A"] by simp proposition orthogonal_matrix_exists_basis: fixes a :: "real^'n" assumes "norm a = 1" obtains A where "orthogonal_matrix A" "A *v (axis k 1) = a" proof - obtain S where "a \ S" "pairwise orthogonal S" and noS: "\x. x \ S \ norm x = 1" and "independent S" "card S = CARD('n)" "span S = UNIV" using vector_in_orthonormal_basis assms by force then obtain f0 where "bij_betw f0 (UNIV::'n set) S" by (metis finite_class.finite_UNIV finite_same_card_bij finiteI_independent) then obtain f where f: "bij_betw f (UNIV::'n set) S" and a: "a = f k" using bij_swap_iff [of k "inv f0 a" f0] by (metis UNIV_I \a \ S\ bij_betw_inv_into_right bij_betw_swap_iff swap_apply(1)) show thesis proof have [simp]: "\i. norm (f i) = 1" using bij_betwE [OF \bij_betw f UNIV S\] by (blast intro: noS) have [simp]: "\i j. i \ j \ orthogonal (f i) (f j)" using \pairwise orthogonal S\ \bij_betw f UNIV S\ by (auto simp: pairwise_def bij_betw_def inj_on_def) show "orthogonal_matrix (\ i j. f j $ i)" by (simp add: orthogonal_matrix_orthonormal_columns column_def) show "(\ i j. f j $ i) *v axis k 1 = a" by (simp add: matrix_vector_mult_def axis_def a if_distrib cong: if_cong) qed qed lemma orthogonal_transformation_exists_1: fixes a b :: "real^'n" assumes "norm a = 1" "norm b = 1" obtains f where "orthogonal_transformation f" "f a = b" proof - obtain k::'n where True by simp obtain A B where AB: "orthogonal_matrix A" "orthogonal_matrix B" and eq: "A *v (axis k 1) = a" "B *v (axis k 1) = b" using orthogonal_matrix_exists_basis assms by metis let ?f = "\x. (B ** transpose A) *v x" show thesis proof show "orthogonal_transformation ?f" by (subst orthogonal_transformation_matrix) (auto simp: AB orthogonal_matrix_mul) next show "?f a = b" using \orthogonal_matrix A\ unfolding orthogonal_matrix_def by (metis eq matrix_mul_rid matrix_vector_mul_assoc) qed qed proposition orthogonal_transformation_exists: fixes a b :: "real^'n" assumes "norm a = norm b" obtains f where "orthogonal_transformation f" "f a = b" proof (cases "a = 0 \ b = 0") case True with assms show ?thesis using that by force next case False then obtain f where f: "orthogonal_transformation f" and eq: "f (a /\<^sub>R norm a) = (b /\<^sub>R norm b)" by (auto intro: orthogonal_transformation_exists_1 [of "a /\<^sub>R norm a" "b /\<^sub>R norm b"]) show ?thesis proof interpret linear f using f by (simp add: orthogonal_transformation_linear) have "f a /\<^sub>R norm a = f (a /\<^sub>R norm a)" by (simp add: scale) also have "\ = b /\<^sub>R norm a" by (simp add: eq assms [symmetric]) finally show "f a = b" using False by auto qed (use f in auto) qed -subsection \Linearity of scaling, and hence isometry, that preserves origin\ +subsection \Scaling and isometry\ -lemma scaling_linear: +proposition scaling_linear: fixes f :: "'a::real_inner \ 'a::real_inner" assumes f0: "f 0 = 0" and fd: "\x y. dist (f x) (f y) = c * dist x y" shows "linear f" proof - { fix v w have "norm (f x) = c * norm x" for x by (metis dist_0_norm f0 fd) then have "f v \ f w = c\<^sup>2 * (v \ w)" unfolding dot_norm_neg dist_norm[symmetric] by (simp add: fd power2_eq_square field_simps) } then show ?thesis unfolding linear_iff vector_eq[where 'a="'a"] scalar_mult_eq_scaleR by (simp add: inner_add field_simps) qed lemma isometry_linear: "f (0::'a::real_inner) = (0::'a) \ \x y. dist(f x) (f y) = dist x y \ linear f" by (rule scaling_linear[where c=1]) simp_all text \Hence another formulation of orthogonal transformation\ proposition orthogonal_transformation_isometry: "orthogonal_transformation f \ f(0::'a::real_inner) = (0::'a) \ (\x y. dist(f x) (f y) = dist x y)" unfolding orthogonal_transformation apply (auto simp: linear_0 isometry_linear) apply (metis (no_types, hide_lams) dist_norm linear_diff) by (metis dist_0_norm) -subsection \Can extend an isometry from unit sphere\ +text \Can extend an isometry from unit sphere:\ lemma isometry_sphere_extend: fixes f:: "'a::real_inner \ 'a" assumes f1: "\x. norm x = 1 \ norm (f x) = 1" and fd1: "\x y. \norm x = 1; norm y = 1\ \ dist (f x) (f y) = dist x y" shows "\g. orthogonal_transformation g \ (\x. norm x = 1 \ g x = f x)" proof - { fix x y x' y' u v u' v' :: "'a" assume H: "x = norm x *\<^sub>R u" "y = norm y *\<^sub>R v" "x' = norm x *\<^sub>R u'" "y' = norm y *\<^sub>R v'" and J: "norm u = 1" "norm u' = 1" "norm v = 1" "norm v' = 1" "norm(u' - v') = norm(u - v)" then have *: "u \ v = u' \ v' + v' \ u' - v \ u " by (simp add: norm_eq norm_eq_1 inner_add inner_diff) have "norm (norm x *\<^sub>R u' - norm y *\<^sub>R v') = norm (norm x *\<^sub>R u - norm y *\<^sub>R v)" using J by (simp add: norm_eq norm_eq_1 inner_diff * field_simps) then have "norm(x' - y') = norm(x - y)" using H by metis } note norm_eq = this let ?g = "\x. if x = 0 then 0 else norm x *\<^sub>R f (x /\<^sub>R norm x)" have thfg: "?g x = f x" if "norm x = 1" for x using that by auto have thd: "dist (?g x) (?g y) = dist x y" for x y proof (cases "x=0 \ y=0") case False show "dist (?g x) (?g y) = dist x y" unfolding dist_norm proof (rule norm_eq) show "x = norm x *\<^sub>R (x /\<^sub>R norm x)" "y = norm y *\<^sub>R (y /\<^sub>R norm y)" "norm (f (x /\<^sub>R norm x)) = 1" "norm (f (y /\<^sub>R norm y)) = 1" using False f1 by auto qed (use False in \auto simp: field_simps intro: f1 fd1[unfolded dist_norm]\) qed (auto simp: f1) show ?thesis unfolding orthogonal_transformation_isometry by (rule exI[where x= ?g]) (metis thfg thd) qed subsection\Induction on matrix row operations\ lemma induct_matrix_row_operations: fixes P :: "real^'n^'n \ bool" assumes zero_row: "\A i. row i A = 0 \ P A" and diagonal: "\A. (\i j. i \ j \ A$i$j = 0) \ P A" and swap_cols: "\A m n. \P A; m \ n\ \ P(\ i j. A $ i $ Fun.swap m n id j)" and row_op: "\A m n c. \P A; m \ n\ \ P(\ i. if i = m then row m A + c *\<^sub>R row n A else row i A)" shows "P A" proof - have "P A" if "(\i j. \j \ -K; i \ j\ \ A$i$j = 0)" for A K proof - have "finite K" by simp then show ?thesis using that proof (induction arbitrary: A rule: finite_induct) case empty with diagonal show ?case by simp next case (insert k K) note insertK = insert have "P A" if kk: "A$k$k \ 0" and 0: "\i j. \j \ - insert k K; i \ j\ \ A$i$j = 0" "\i. \i \ -L; i \ k\ \ A$i$k = 0" for A L proof - have "finite L" by simp then show ?thesis using 0 kk proof (induction arbitrary: A rule: finite_induct) case (empty B) show ?case proof (rule insertK) fix i j assume "i \ - K" "j \ i" show "B $ j $ i = 0" using \j \ i\ \i \ - K\ empty by (metis ComplD ComplI Compl_eq_Diff_UNIV Diff_empty UNIV_I insert_iff) qed next case (insert l L B) show ?case proof (cases "k = l") case True with insert show ?thesis by auto next case False let ?C = "\ i. if i = l then row l B - (B $ l $ k / B $ k $ k) *\<^sub>R row k B else row i B" have 1: "\j \ - insert k K; i \ j\ \ ?C $ i $ j = 0" for j i by (auto simp: insert.prems(1) row_def) have 2: "?C $ i $ k = 0" if "i \ - L" "i \ k" for i proof (cases "i=l") case True with that insert.prems show ?thesis by (simp add: row_def) next case False with that show ?thesis by (simp add: insert.prems(2) row_def) qed have 3: "?C $ k $ k \ 0" by (auto simp: insert.prems row_def \k \ l\) have PC: "P ?C" using insert.IH [OF 1 2 3] by auto have eqB: "(\ i. if i = l then row l ?C + (B $ l $ k / B $ k $ k) *\<^sub>R row k ?C else row i ?C) = B" using \k \ l\ by (simp add: vec_eq_iff row_def) show ?thesis using row_op [OF PC, of l k, where c = "B$l$k / B$k$k"] eqB \k \ l\ by (simp add: cong: if_cong) qed qed qed then have nonzero_hyp: "P A" if kk: "A$k$k \ 0" and zeroes: "\i j. j \ - insert k K \ i\j \ A$i$j = 0" for A by (auto simp: intro!: kk zeroes) show ?case proof (cases "row k A = 0") case True with zero_row show ?thesis by auto next case False then obtain l where l: "A$k$l \ 0" by (auto simp: row_def zero_vec_def vec_eq_iff) show ?thesis proof (cases "k = l") case True with l nonzero_hyp insert.prems show ?thesis by blast next case False have *: "A $ i $ Fun.swap k l id j = 0" if "j \ k" "j \ K" "i \ j" for i j using False l insert.prems that by (auto simp: swap_def insert split: if_split_asm) have "P (\ i j. (\ i j. A $ i $ Fun.swap k l id j) $ i $ Fun.swap k l id j)" by (rule swap_cols [OF nonzero_hyp False]) (auto simp: l *) moreover have "(\ i j. (\ i j. A $ i $ Fun.swap k l id j) $ i $ Fun.swap k l id j) = A" by (vector Fun.swap_def) ultimately show ?thesis by simp qed qed qed qed then show ?thesis by blast qed lemma induct_matrix_elementary: fixes P :: "real^'n^'n \ bool" assumes mult: "\A B. \P A; P B\ \ P(A ** B)" and zero_row: "\A i. row i A = 0 \ P A" and diagonal: "\A. (\i j. i \ j \ A$i$j = 0) \ P A" and swap1: "\m n. m \ n \ P(\ i j. mat 1 $ i $ Fun.swap m n id j)" and idplus: "\m n c. m \ n \ P(\ i j. if i = m \ j = n then c else of_bool (i = j))" shows "P A" proof - have swap: "P (\ i j. A $ i $ Fun.swap m n id j)" (is "P ?C") if "P A" "m \ n" for A m n proof - have "A ** (\ i j. mat 1 $ i $ Fun.swap m n id j) = ?C" by (simp add: matrix_matrix_mult_def mat_def vec_eq_iff if_distrib sum.delta_remove) then show ?thesis using mult swap1 that by metis qed have row: "P (\ i. if i = m then row m A + c *\<^sub>R row n A else row i A)" (is "P ?C") if "P A" "m \ n" for A m n c proof - let ?B = "\ i j. if i = m \ j = n then c else of_bool (i = j)" have "?B ** A = ?C" using \m \ n\ unfolding matrix_matrix_mult_def row_def of_bool_def by (auto simp: vec_eq_iff if_distrib [of "\x. x * y" for y] sum.remove cong: if_cong) then show ?thesis by (rule subst) (auto simp: that mult idplus) qed show ?thesis by (rule induct_matrix_row_operations [OF zero_row diagonal swap row]) qed lemma induct_matrix_elementary_alt: fixes P :: "real^'n^'n \ bool" assumes mult: "\A B. \P A; P B\ \ P(A ** B)" and zero_row: "\A i. row i A = 0 \ P A" and diagonal: "\A. (\i j. i \ j \ A$i$j = 0) \ P A" and swap1: "\m n. m \ n \ P(\ i j. mat 1 $ i $ Fun.swap m n id j)" and idplus: "\m n. m \ n \ P(\ i j. of_bool (i = m \ j = n \ i = j))" shows "P A" proof - have *: "P (\ i j. if i = m \ j = n then c else of_bool (i = j))" if "m \ n" for m n c proof (cases "c = 0") case True with diagonal show ?thesis by auto next case False then have eq: "(\ i j. if i = m \ j = n then c else of_bool (i = j)) = (\ i j. if i = j then (if j = n then inverse c else 1) else 0) ** (\ i j. of_bool (i = m \ j = n \ i = j)) ** (\ i j. if i = j then if j = n then c else 1 else 0)" using \m \ n\ apply (simp add: matrix_matrix_mult_def vec_eq_iff of_bool_def if_distrib [of "\x. y * x" for y] cong: if_cong) apply (simp add: if_if_eq_conj sum.neutral conj_commute cong: conj_cong) done show ?thesis apply (subst eq) apply (intro mult idplus that) apply (auto intro: diagonal) done qed show ?thesis by (rule induct_matrix_elementary) (auto intro: assms *) qed lemma matrix_vector_mult_matrix_matrix_mult_compose: "(*v) (A ** B) = (*v) A \ (*v) B" by (auto simp: matrix_vector_mul_assoc) lemma induct_linear_elementary: fixes f :: "real^'n \ real^'n" assumes "linear f" and comp: "\f g. \linear f; linear g; P f; P g\ \ P(f \ g)" and zeroes: "\f i. \linear f; \x. (f x) $ i = 0\ \ P f" and const: "\c. P(\x. \ i. c i * x$i)" and swap: "\m n::'n. m \ n \ P(\x. \ i. x $ Fun.swap m n id i)" and idplus: "\m n::'n. m \ n \ P(\x. \ i. if i = m then x$m + x$n else x$i)" shows "P f" proof - have "P ((*v) A)" for A proof (rule induct_matrix_elementary_alt) fix A B assume "P ((*v) A)" and "P ((*v) B)" then show "P ((*v) (A ** B))" - by (auto simp add: matrix_vector_mult_matrix_matrix_mult_compose matrix_vector_mul_linear - intro!: comp) + by (auto simp add: matrix_vector_mult_matrix_matrix_mult_compose intro!: comp) next fix A :: "real^'n^'n" and i assume "row i A = 0" show "P ((*v) A)" using matrix_vector_mul_linear by (rule zeroes[where i=i]) (metis \row i A = 0\ inner_zero_left matrix_vector_mul_component row_def vec_lambda_eta) next fix A :: "real^'n^'n" assume 0: "\i j. i \ j \ A $ i $ j = 0" have "A $ i $ i * x $ i = (\j\UNIV. A $ i $ j * x $ j)" for x and i :: "'n" by (simp add: 0 comm_monoid_add_class.sum.remove [where x=i]) then have "(\x. \ i. A $ i $ i * x $ i) = ((*v) A)" by (auto simp: 0 matrix_vector_mult_def) then show "P ((*v) A)" using const [of "\i. A $ i $ i"] by simp next fix m n :: "'n" assume "m \ n" have eq: "(\j\UNIV. if i = Fun.swap m n id j then x $ j else 0) = (\j\UNIV. if j = Fun.swap m n id i then x $ j else 0)" for i and x :: "real^'n" unfolding swap_def by (rule sum.cong) auto have "(\x::real^'n. \ i. x $ Fun.swap m n id i) = ((*v) (\ i j. if i = Fun.swap m n id j then 1 else 0))" by (auto simp: mat_def matrix_vector_mult_def eq if_distrib [of "\x. x * y" for y] cong: if_cong) with swap [OF \m \ n\] show "P ((*v) (\ i j. mat 1 $ i $ Fun.swap m n id j))" by (simp add: mat_def matrix_vector_mult_def) next fix m n :: "'n" assume "m \ n" then have "x $ m + x $ n = (\j\UNIV. of_bool (j = n \ m = j) * x $ j)" for x :: "real^'n" by (auto simp: of_bool_def if_distrib [of "\x. x * y" for y] sum.remove cong: if_cong) then have "(\x::real^'n. \ i. if i = m then x $ m + x $ n else x $ i) = ((*v) (\ i j. of_bool (i = m \ j = n \ i = j)))" unfolding matrix_vector_mult_def of_bool_def by (auto simp: vec_eq_iff if_distrib [of "\x. x * y" for y] cong: if_cong) then show "P ((*v) (\ i j. of_bool (i = m \ j = n \ i = j)))" using idplus [OF \m \ n\] by simp qed then show ?thesis by (metis \linear f\ matrix_vector_mul) qed end \ No newline at end of file diff --git a/src/HOL/Analysis/Convex.thy b/src/HOL/Analysis/Convex.thy --- a/src/HOL/Analysis/Convex.thy +++ b/src/HOL/Analysis/Convex.thy @@ -1,4140 +1,4140 @@ (* Title: HOL/Analysis/Convex.thy Author: L C Paulson, University of Cambridge Author: Robert Himmelmann, TU Muenchen Author: Bogdan Grechuk, University of Edinburgh Author: Armin Heller, TU Muenchen Author: Johannes Hoelzl, TU Muenchen *) section \Convex Sets and Functions\ theory Convex imports Linear_Algebra "HOL-Library.Set_Algebras" begin -subsection \Convexity\ +subsection \Convex Sets\ definition\<^marker>\tag important\ convex :: "'a::real_vector set \ bool" where "convex s \ (\x\s. \y\s. \u\0. \v\0. u + v = 1 \ u *\<^sub>R x + v *\<^sub>R y \ s)" lemma convexI: assumes "\x y u v. x \ s \ y \ s \ 0 \ u \ 0 \ v \ u + v = 1 \ u *\<^sub>R x + v *\<^sub>R y \ s" shows "convex s" using assms unfolding convex_def by fast lemma convexD: assumes "convex s" and "x \ s" and "y \ s" and "0 \ u" and "0 \ v" and "u + v = 1" shows "u *\<^sub>R x + v *\<^sub>R y \ s" using assms unfolding convex_def by fast lemma convex_alt: "convex s \ (\x\s. \y\s. \u. 0 \ u \ u \ 1 \ ((1 - u) *\<^sub>R x + u *\<^sub>R y) \ s)" (is "_ \ ?alt") proof show "convex s" if alt: ?alt proof - { fix x y and u v :: real assume mem: "x \ s" "y \ s" assume "0 \ u" "0 \ v" moreover assume "u + v = 1" then have "u = 1 - v" by auto ultimately have "u *\<^sub>R x + v *\<^sub>R y \ s" using alt [rule_format, OF mem] by auto } then show ?thesis unfolding convex_def by auto qed show ?alt if "convex s" using that by (auto simp: convex_def) qed lemma convexD_alt: assumes "convex s" "a \ s" "b \ s" "0 \ u" "u \ 1" shows "((1 - u) *\<^sub>R a + u *\<^sub>R b) \ s" using assms unfolding convex_alt by auto lemma mem_convex_alt: assumes "convex S" "x \ S" "y \ S" "u \ 0" "v \ 0" "u + v > 0" shows "((u/(u+v)) *\<^sub>R x + (v/(u+v)) *\<^sub>R y) \ S" apply (rule convexD) using assms apply (simp_all add: zero_le_divide_iff add_divide_distrib [symmetric]) done lemma convex_empty[intro,simp]: "convex {}" unfolding convex_def by simp lemma convex_singleton[intro,simp]: "convex {a}" unfolding convex_def by (auto simp: scaleR_left_distrib[symmetric]) lemma convex_UNIV[intro,simp]: "convex UNIV" unfolding convex_def by auto lemma convex_Inter: "(\s. s\f \ convex s) \ convex(\f)" unfolding convex_def by auto lemma convex_Int: "convex s \ convex t \ convex (s \ t)" unfolding convex_def by auto lemma convex_INT: "(\i. i \ A \ convex (B i)) \ convex (\i\A. B i)" unfolding convex_def by auto lemma convex_Times: "convex s \ convex t \ convex (s \ t)" unfolding convex_def by auto lemma convex_halfspace_le: "convex {x. inner a x \ b}" unfolding convex_def by (auto simp: inner_add intro!: convex_bound_le) lemma convex_halfspace_ge: "convex {x. inner a x \ b}" proof - have *: "{x. inner a x \ b} = {x. inner (-a) x \ -b}" by auto show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto qed lemma convex_halfspace_abs_le: "convex {x. \inner a x\ \ b}" proof - have *: "{x. \inner a x\ \ b} = {x. inner a x \ b} \ {x. -b \ inner a x}" by auto show ?thesis unfolding * by (simp add: convex_Int convex_halfspace_ge convex_halfspace_le) qed lemma convex_hyperplane: "convex {x. inner a x = b}" proof - have *: "{x. inner a x = b} = {x. inner a x \ b} \ {x. inner a x \ b}" by auto show ?thesis using convex_halfspace_le convex_halfspace_ge by (auto intro!: convex_Int simp: *) qed lemma convex_halfspace_lt: "convex {x. inner a x < b}" unfolding convex_def by (auto simp: convex_bound_lt inner_add) lemma convex_halfspace_gt: "convex {x. inner a x > b}" using convex_halfspace_lt[of "-a" "-b"] by auto lemma convex_halfspace_Re_ge: "convex {x. Re x \ b}" using convex_halfspace_ge[of b "1::complex"] by simp lemma convex_halfspace_Re_le: "convex {x. Re x \ b}" using convex_halfspace_le[of "1::complex" b] by simp lemma convex_halfspace_Im_ge: "convex {x. Im x \ b}" using convex_halfspace_ge[of b \] by simp lemma convex_halfspace_Im_le: "convex {x. Im x \ b}" using convex_halfspace_le[of \ b] by simp lemma convex_halfspace_Re_gt: "convex {x. Re x > b}" using convex_halfspace_gt[of b "1::complex"] by simp lemma convex_halfspace_Re_lt: "convex {x. Re x < b}" using convex_halfspace_lt[of "1::complex" b] by simp lemma convex_halfspace_Im_gt: "convex {x. Im x > b}" using convex_halfspace_gt[of b \] by simp lemma convex_halfspace_Im_lt: "convex {x. Im x < b}" using convex_halfspace_lt[of \ b] by simp lemma convex_real_interval [iff]: fixes a b :: "real" shows "convex {a..}" and "convex {..b}" and "convex {a<..}" and "convex {.. inner 1 x}" by auto then show 1: "convex {a..}" by (simp only: convex_halfspace_ge) have "{..b} = {x. inner 1 x \ b}" by auto then show 2: "convex {..b}" by (simp only: convex_halfspace_le) have "{a<..} = {x. a < inner 1 x}" by auto then show 3: "convex {a<..}" by (simp only: convex_halfspace_gt) have "{.. {..b}" by auto then show "convex {a..b}" by (simp only: convex_Int 1 2) have "{a<..b} = {a<..} \ {..b}" by auto then show "convex {a<..b}" by (simp only: convex_Int 3 2) have "{a.. {.. {.." by (simp add: convex_def scaleR_conv_of_real) subsection\<^marker>\tag unimportant\ \Explicit expressions for convexity in terms of arbitrary sums\ lemma convex_sum: fixes C :: "'a::real_vector set" assumes "finite s" and "convex C" and "(\ i \ s. a i) = 1" assumes "\i. i \ s \ a i \ 0" and "\i. i \ s \ y i \ C" shows "(\ j \ s. a j *\<^sub>R y j) \ C" using assms(1,3,4,5) proof (induct arbitrary: a set: finite) case empty then show ?case by simp next case (insert i s) note IH = this(3) have "a i + sum a s = 1" and "0 \ a i" and "\j\s. 0 \ a j" and "y i \ C" and "\j\s. y j \ C" using insert.hyps(1,2) insert.prems by simp_all then have "0 \ sum a s" by (simp add: sum_nonneg) have "a i *\<^sub>R y i + (\j\s. a j *\<^sub>R y j) \ C" proof (cases "sum a s = 0") case True with \a i + sum a s = 1\ have "a i = 1" by simp from sum_nonneg_0 [OF \finite s\ _ True] \\j\s. 0 \ a j\ have "\j\s. a j = 0" by simp show ?thesis using \a i = 1\ and \\j\s. a j = 0\ and \y i \ C\ by simp next case False with \0 \ sum a s\ have "0 < sum a s" by simp then have "(\j\s. (a j / sum a s) *\<^sub>R y j) \ C" using \\j\s. 0 \ a j\ and \\j\s. y j \ C\ by (simp add: IH sum_divide_distrib [symmetric]) from \convex C\ and \y i \ C\ and this and \0 \ a i\ and \0 \ sum a s\ and \a i + sum a s = 1\ have "a i *\<^sub>R y i + sum a s *\<^sub>R (\j\s. (a j / sum a s) *\<^sub>R y j) \ C" by (rule convexD) then show ?thesis by (simp add: scaleR_sum_right False) qed then show ?case using \finite s\ and \i \ s\ by simp qed lemma convex: "convex s \ (\(k::nat) u x. (\i. 1\i \ i\k \ 0 \ u i \ x i \s) \ (sum u {1..k} = 1) \ sum (\i. u i *\<^sub>R x i) {1..k} \ s)" proof safe fix k :: nat fix u :: "nat \ real" fix x assume "convex s" "\i. 1 \ i \ i \ k \ 0 \ u i \ x i \ s" "sum u {1..k} = 1" with convex_sum[of "{1 .. k}" s] show "(\j\{1 .. k}. u j *\<^sub>R x j) \ s" by auto next assume *: "\k u x. (\ i :: nat. 1 \ i \ i \ k \ 0 \ u i \ x i \ s) \ sum u {1..k} = 1 \ (\i = 1..k. u i *\<^sub>R (x i :: 'a)) \ s" { fix \ :: real fix x y :: 'a assume xy: "x \ s" "y \ s" assume mu: "\ \ 0" "\ \ 1" let ?u = "\i. if (i :: nat) = 1 then \ else 1 - \" let ?x = "\i. if (i :: nat) = 1 then x else y" have "{1 :: nat .. 2} \ - {x. x = 1} = {2}" by auto then have card: "card ({1 :: nat .. 2} \ - {x. x = 1}) = 1" by simp then have "sum ?u {1 .. 2} = 1" using sum.If_cases[of "{(1 :: nat) .. 2}" "\ x. x = 1" "\ x. \" "\ x. 1 - \"] by auto with *[rule_format, of "2" ?u ?x] have s: "(\j \ {1..2}. ?u j *\<^sub>R ?x j) \ s" using mu xy by auto have grarr: "(\j \ {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \) *\<^sub>R y" using sum.atLeast_Suc_atMost[of "Suc (Suc 0)" 2 "\ j. (1 - \) *\<^sub>R y"] by auto from sum.atLeast_Suc_atMost[of "Suc 0" 2 "\ j. ?u j *\<^sub>R ?x j", simplified this] have "(\j \ {1..2}. ?u j *\<^sub>R ?x j) = \ *\<^sub>R x + (1 - \) *\<^sub>R y" by auto then have "(1 - \) *\<^sub>R y + \ *\<^sub>R x \ s" using s by (auto simp: add.commute) } then show "convex s" unfolding convex_alt by auto qed lemma convex_explicit: fixes s :: "'a::real_vector set" shows "convex s \ (\t u. finite t \ t \ s \ (\x\t. 0 \ u x) \ sum u t = 1 \ sum (\x. u x *\<^sub>R x) t \ s)" proof safe fix t fix u :: "'a \ real" assume "convex s" and "finite t" and "t \ s" "\x\t. 0 \ u x" "sum u t = 1" then show "(\x\t. u x *\<^sub>R x) \ s" using convex_sum[of t s u "\ x. x"] by auto next assume *: "\t. \ u. finite t \ t \ s \ (\x\t. 0 \ u x) \ sum u t = 1 \ (\x\t. u x *\<^sub>R x) \ s" show "convex s" unfolding convex_alt proof safe fix x y fix \ :: real assume **: "x \ s" "y \ s" "0 \ \" "\ \ 1" show "(1 - \) *\<^sub>R x + \ *\<^sub>R y \ s" proof (cases "x = y") case False then show ?thesis using *[rule_format, of "{x, y}" "\ z. if z = x then 1 - \ else \"] ** by auto next case True then show ?thesis using *[rule_format, of "{x, y}" "\ z. 1"] ** by (auto simp: field_simps real_vector.scale_left_diff_distrib) qed qed qed lemma convex_finite: assumes "finite s" shows "convex s \ (\u. (\x\s. 0 \ u x) \ sum u s = 1 \ sum (\x. u x *\<^sub>R x) s \ s)" unfolding convex_explicit apply safe subgoal for u by (erule allE [where x=s], erule allE [where x=u]) auto subgoal for t u proof - have if_distrib_arg: "\P f g x. (if P then f else g) x = (if P then f x else g x)" by simp assume sum: "\u. (\x\s. 0 \ u x) \ sum u s = 1 \ (\x\s. u x *\<^sub>R x) \ s" assume *: "\x\t. 0 \ u x" "sum u t = 1" assume "t \ s" then have "s \ t = t" by auto with sum[THEN spec[where x="\x. if x\t then u x else 0"]] * show "(\x\t. u x *\<^sub>R x) \ s" by (auto simp: assms sum.If_cases if_distrib if_distrib_arg) qed done -subsection \Functions that are convex on a set\ +subsection \Convex Functions on a Set\ definition\<^marker>\tag important\ convex_on :: "'a::real_vector set \ ('a \ real) \ bool" where "convex_on s f \ (\x\s. \y\s. \u\0. \v\0. u + v = 1 \ f (u *\<^sub>R x + v *\<^sub>R y) \ u * f x + v * f y)" lemma convex_onI [intro?]: assumes "\t x y. t > 0 \ t < 1 \ x \ A \ y \ A \ f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \ (1 - t) * f x + t * f y" shows "convex_on A f" unfolding convex_on_def proof clarify fix x y fix u v :: real assume A: "x \ A" "y \ A" "u \ 0" "v \ 0" "u + v = 1" from A(5) have [simp]: "v = 1 - u" by (simp add: algebra_simps) from A(1-4) show "f (u *\<^sub>R x + v *\<^sub>R y) \ u * f x + v * f y" using assms[of u y x] by (cases "u = 0 \ u = 1") (auto simp: algebra_simps) qed lemma convex_on_linorderI [intro?]: fixes A :: "('a::{linorder,real_vector}) set" assumes "\t x y. t > 0 \ t < 1 \ x \ A \ y \ A \ x < y \ f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \ (1 - t) * f x + t * f y" shows "convex_on A f" proof fix x y fix t :: real assume A: "x \ A" "y \ A" "t > 0" "t < 1" with assms [of t x y] assms [of "1 - t" y x] show "f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \ (1 - t) * f x + t * f y" by (cases x y rule: linorder_cases) (auto simp: algebra_simps) qed lemma convex_onD: assumes "convex_on A f" shows "\t x y. t \ 0 \ t \ 1 \ x \ A \ y \ A \ f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \ (1 - t) * f x + t * f y" using assms by (auto simp: convex_on_def) lemma convex_onD_Icc: assumes "convex_on {x..y} f" "x \ (y :: _ :: {real_vector,preorder})" shows "\t. t \ 0 \ t \ 1 \ f ((1 - t) *\<^sub>R x + t *\<^sub>R y) \ (1 - t) * f x + t * f y" using assms(2) by (intro convex_onD [OF assms(1)]) simp_all lemma convex_on_subset: "convex_on t f \ s \ t \ convex_on s f" unfolding convex_on_def by auto lemma convex_on_add [intro]: assumes "convex_on s f" and "convex_on s g" shows "convex_on s (\x. f x + g x)" proof - { fix x y assume "x \ s" "y \ s" moreover fix u v :: real assume "0 \ u" "0 \ v" "u + v = 1" ultimately have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \ (u * f x + v * f y) + (u * g x + v * g y)" using assms unfolding convex_on_def by (auto simp: add_mono) then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \ u * (f x + g x) + v * (f y + g y)" by (simp add: field_simps) } then show ?thesis unfolding convex_on_def by auto qed lemma convex_on_cmul [intro]: fixes c :: real assumes "0 \ c" and "convex_on s f" shows "convex_on s (\x. c * f x)" proof - have *: "u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)" for u c fx v fy :: real by (simp add: field_simps) show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)] unfolding convex_on_def and * by auto qed lemma convex_lower: assumes "convex_on s f" and "x \ s" and "y \ s" and "0 \ u" and "0 \ v" and "u + v = 1" shows "f (u *\<^sub>R x + v *\<^sub>R y) \ max (f x) (f y)" proof - let ?m = "max (f x) (f y)" have "u * f x + v * f y \ u * max (f x) (f y) + v * max (f x) (f y)" using assms(4,5) by (auto simp: mult_left_mono add_mono) also have "\ = max (f x) (f y)" using assms(6) by (simp add: distrib_right [symmetric]) finally show ?thesis using assms unfolding convex_on_def by fastforce qed lemma convex_on_dist [intro]: fixes s :: "'a::real_normed_vector set" shows "convex_on s (\x. dist a x)" proof (auto simp: convex_on_def dist_norm) fix x y assume "x \ s" "y \ s" fix u v :: real assume "0 \ u" assume "0 \ v" assume "u + v = 1" have "a = u *\<^sub>R a + v *\<^sub>R a" unfolding scaleR_left_distrib[symmetric] and \u + v = 1\ by simp then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))" by (auto simp: algebra_simps) show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \ u * norm (a - x) + v * norm (a - y)" unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"] using \0 \ u\ \0 \ v\ by auto qed subsection\<^marker>\tag unimportant\ \Arithmetic operations on sets preserve convexity\ lemma convex_linear_image: assumes "linear f" and "convex s" shows "convex (f ` s)" proof - interpret f: linear f by fact from \convex s\ show "convex (f ` s)" by (simp add: convex_def f.scaleR [symmetric] f.add [symmetric]) qed lemma convex_linear_vimage: assumes "linear f" and "convex s" shows "convex (f -` s)" proof - interpret f: linear f by fact from \convex s\ show "convex (f -` s)" by (simp add: convex_def f.add f.scaleR) qed lemma convex_scaling: assumes "convex s" shows "convex ((\x. c *\<^sub>R x) ` s)" proof - have "linear (\x. c *\<^sub>R x)" by (simp add: linearI scaleR_add_right) then show ?thesis using \convex s\ by (rule convex_linear_image) qed lemma convex_scaled: assumes "convex S" shows "convex ((\x. x *\<^sub>R c) ` S)" proof - have "linear (\x. x *\<^sub>R c)" by (simp add: linearI scaleR_add_left) then show ?thesis using \convex S\ by (rule convex_linear_image) qed lemma convex_negations: assumes "convex S" shows "convex ((\x. - x) ` S)" proof - have "linear (\x. - x)" by (simp add: linearI) then show ?thesis using \convex S\ by (rule convex_linear_image) qed lemma convex_sums: assumes "convex S" and "convex T" shows "convex (\x\ S. \y \ T. {x + y})" proof - have "linear (\(x, y). x + y)" by (auto intro: linearI simp: scaleR_add_right) with assms have "convex ((\(x, y). x + y) ` (S \ T))" by (intro convex_linear_image convex_Times) also have "((\(x, y). x + y) ` (S \ T)) = (\x\ S. \y \ T. {x + y})" by auto finally show ?thesis . qed lemma convex_differences: assumes "convex S" "convex T" shows "convex (\x\ S. \y \ T. {x - y})" proof - have "{x - y| x y. x \ S \ y \ T} = {x + y |x y. x \ S \ y \ uminus ` T}" by (auto simp: diff_conv_add_uminus simp del: add_uminus_conv_diff) then show ?thesis using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto qed lemma convex_translation: "convex ((+) a ` S)" if "convex S" proof - have "(\ x\ {a}. \y \ S. {x + y}) = (+) a ` S" by auto then show ?thesis using convex_sums [OF convex_singleton [of a] that] by auto qed lemma convex_translation_subtract: "convex ((\b. b - a) ` S)" if "convex S" using convex_translation [of S "- a"] that by (simp cong: image_cong_simp) lemma convex_affinity: assumes "convex S" shows "convex ((\x. a + c *\<^sub>R x) ` S)" proof - have "(\x. a + c *\<^sub>R x) ` S = (+) a ` (*\<^sub>R) c ` S" by auto then show ?thesis using convex_translation[OF convex_scaling[OF assms], of a c] by auto qed lemma pos_is_convex: "convex {0 :: real <..}" unfolding convex_alt proof safe fix y x \ :: real assume *: "y > 0" "x > 0" "\ \ 0" "\ \ 1" { assume "\ = 0" then have "\ *\<^sub>R x + (1 - \) *\<^sub>R y = y" by simp then have "\ *\<^sub>R x + (1 - \) *\<^sub>R y > 0" using * by simp } moreover { assume "\ = 1" then have "\ *\<^sub>R x + (1 - \) *\<^sub>R y > 0" using * by simp } moreover { assume "\ \ 1" "\ \ 0" then have "\ > 0" "(1 - \) > 0" using * by auto then have "\ *\<^sub>R x + (1 - \) *\<^sub>R y > 0" using * by (auto simp: add_pos_pos) } ultimately show "(1 - \) *\<^sub>R y + \ *\<^sub>R x > 0" by fastforce qed lemma convex_on_sum: fixes a :: "'a \ real" and y :: "'a \ 'b::real_vector" and f :: "'b \ real" assumes "finite s" "s \ {}" and "convex_on C f" and "convex C" and "(\ i \ s. a i) = 1" and "\i. i \ s \ a i \ 0" and "\i. i \ s \ y i \ C" shows "f (\ i \ s. a i *\<^sub>R y i) \ (\ i \ s. a i * f (y i))" using assms proof (induct s arbitrary: a rule: finite_ne_induct) case (singleton i) then have ai: "a i = 1" by auto then show ?case by auto next case (insert i s) then have "convex_on C f" by simp from this[unfolded convex_on_def, rule_format] have conv: "\x y \. x \ C \ y \ C \ 0 \ \ \ \ \ 1 \ f (\ *\<^sub>R x + (1 - \) *\<^sub>R y) \ \ * f x + (1 - \) * f y" by simp show ?case proof (cases "a i = 1") case True then have "(\ j \ s. a j) = 0" using insert by auto then have "\j. j \ s \ a j = 0" using insert by (fastforce simp: sum_nonneg_eq_0_iff) then show ?thesis using insert by auto next case False from insert have yai: "y i \ C" "a i \ 0" by auto have fis: "finite (insert i s)" using insert by auto then have ai1: "a i \ 1" using sum_nonneg_leq_bound[of "insert i s" a] insert by simp then have "a i < 1" using False by auto then have i0: "1 - a i > 0" by auto let ?a = "\j. a j / (1 - a i)" have a_nonneg: "?a j \ 0" if "j \ s" for j using i0 insert that by fastforce have "(\ j \ insert i s. a j) = 1" using insert by auto then have "(\ j \ s. a j) = 1 - a i" using sum.insert insert by fastforce then have "(\ j \ s. a j) / (1 - a i) = 1" using i0 by auto then have a1: "(\ j \ s. ?a j) = 1" unfolding sum_divide_distrib by simp have "convex C" using insert by auto then have asum: "(\ j \ s. ?a j *\<^sub>R y j) \ C" using insert convex_sum [OF \finite s\ \convex C\ a1 a_nonneg] by auto have asum_le: "f (\ j \ s. ?a j *\<^sub>R y j) \ (\ j \ s. ?a j * f (y j))" using a_nonneg a1 insert by blast have "f (\ j \ insert i s. a j *\<^sub>R y j) = f ((\ j \ s. a j *\<^sub>R y j) + a i *\<^sub>R y i)" using sum.insert[of s i "\ j. a j *\<^sub>R y j", OF \finite s\ \i \ s\] insert by (auto simp only: add.commute) also have "\ = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\ j \ s. a j *\<^sub>R y j) + a i *\<^sub>R y i)" using i0 by auto also have "\ = f ((1 - a i) *\<^sub>R (\ j \ s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)" using scaleR_right.sum[of "inverse (1 - a i)" "\ j. a j *\<^sub>R y j" s, symmetric] by (auto simp: algebra_simps) also have "\ = f ((1 - a i) *\<^sub>R (\ j \ s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)" by (auto simp: divide_inverse) also have "\ \ (1 - a i) *\<^sub>R f ((\ j \ s. ?a j *\<^sub>R y j)) + a i * f (y i)" using conv[of "y i" "(\ j \ s. ?a j *\<^sub>R y j)" "a i", OF yai(1) asum yai(2) ai1] by (auto simp: add.commute) also have "\ \ (1 - a i) * (\ j \ s. ?a j * f (y j)) + a i * f (y i)" using add_right_mono [OF mult_left_mono [of _ _ "1 - a i", OF asum_le less_imp_le[OF i0]], of "a i * f (y i)"] by simp also have "\ = (\ j \ s. (1 - a i) * ?a j * f (y j)) + a i * f (y i)" unfolding sum_distrib_left[of "1 - a i" "\ j. ?a j * f (y j)"] using i0 by auto also have "\ = (\ j \ s. a j * f (y j)) + a i * f (y i)" using i0 by auto also have "\ = (\ j \ insert i s. a j * f (y j))" using insert by auto finally show ?thesis by simp qed qed lemma convex_on_alt: fixes C :: "'a::real_vector set" assumes "convex C" shows "convex_on C f \ (\x \ C. \ y \ C. \ \ :: real. \ \ 0 \ \ \ 1 \ f (\ *\<^sub>R x + (1 - \) *\<^sub>R y) \ \ * f x + (1 - \) * f y)" proof safe fix x y fix \ :: real assume *: "convex_on C f" "x \ C" "y \ C" "0 \ \" "\ \ 1" from this[unfolded convex_on_def, rule_format] have "0 \ u \ 0 \ v \ u + v = 1 \ f (u *\<^sub>R x + v *\<^sub>R y) \ u * f x + v * f y" for u v by auto from this [of "\" "1 - \", simplified] * show "f (\ *\<^sub>R x + (1 - \) *\<^sub>R y) \ \ * f x + (1 - \) * f y" by auto next assume *: "\x\C. \y\C. \\. 0 \ \ \ \ \ 1 \ f (\ *\<^sub>R x + (1 - \) *\<^sub>R y) \ \ * f x + (1 - \) * f y" { fix x y fix u v :: real assume **: "x \ C" "y \ C" "u \ 0" "v \ 0" "u + v = 1" then have[simp]: "1 - u = v" by auto from *[rule_format, of x y u] have "f (u *\<^sub>R x + v *\<^sub>R y) \ u * f x + v * f y" using ** by auto } then show "convex_on C f" unfolding convex_on_def by auto qed lemma convex_on_diff: fixes f :: "real \ real" assumes f: "convex_on I f" and I: "x \ I" "y \ I" and t: "x < t" "t < y" shows "(f x - f t) / (x - t) \ (f x - f y) / (x - y)" and "(f x - f y) / (x - y) \ (f t - f y) / (t - y)" proof - define a where "a \ (t - y) / (x - y)" with t have "0 \ a" "0 \ 1 - a" by (auto simp: field_simps) with f \x \ I\ \y \ I\ have cvx: "f (a * x + (1 - a) * y) \ a * f x + (1 - a) * f y" by (auto simp: convex_on_def) have "a * x + (1 - a) * y = a * (x - y) + y" by (simp add: field_simps) also have "\ = t" unfolding a_def using \x < t\ \t < y\ by simp finally have "f t \ a * f x + (1 - a) * f y" using cvx by simp also have "\ = a * (f x - f y) + f y" by (simp add: field_simps) finally have "f t - f y \ a * (f x - f y)" by simp with t show "(f x - f t) / (x - t) \ (f x - f y) / (x - y)" by (simp add: le_divide_eq divide_le_eq field_simps a_def) with t show "(f x - f y) / (x - y) \ (f t - f y) / (t - y)" by (simp add: le_divide_eq divide_le_eq field_simps) qed lemma pos_convex_function: fixes f :: "real \ real" assumes "convex C" and leq: "\x y. x \ C \ y \ C \ f' x * (y - x) \ f y - f x" shows "convex_on C f" unfolding convex_on_alt[OF assms(1)] using assms proof safe fix x y \ :: real let ?x = "\ *\<^sub>R x + (1 - \) *\<^sub>R y" assume *: "convex C" "x \ C" "y \ C" "\ \ 0" "\ \ 1" then have "1 - \ \ 0" by auto then have xpos: "?x \ C" using * unfolding convex_alt by fastforce have geq: "\ * (f x - f ?x) + (1 - \) * (f y - f ?x) \ \ * f' ?x * (x - ?x) + (1 - \) * f' ?x * (y - ?x)" using add_mono [OF mult_left_mono [OF leq [OF xpos *(2)] \\ \ 0\] mult_left_mono [OF leq [OF xpos *(3)] \1 - \ \ 0\]] by auto then have "\ * f x + (1 - \) * f y - f ?x \ 0" by (auto simp: field_simps) then show "f (\ *\<^sub>R x + (1 - \) *\<^sub>R y) \ \ * f x + (1 - \) * f y" using convex_on_alt by auto qed lemma atMostAtLeast_subset_convex: fixes C :: "real set" assumes "convex C" and "x \ C" "y \ C" "x < y" shows "{x .. y} \ C" proof safe fix z assume z: "z \ {x .. y}" have less: "z \ C" if *: "x < z" "z < y" proof - let ?\ = "(y - z) / (y - x)" have "0 \ ?\" "?\ \ 1" using assms * by (auto simp: field_simps) then have comb: "?\ * x + (1 - ?\) * y \ C" using assms iffD1[OF convex_alt, rule_format, of C y x ?\] by (simp add: algebra_simps) have "?\ * x + (1 - ?\) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y" by (auto simp: field_simps) also have "\ = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)" using assms by (simp only: add_divide_distrib) (auto simp: field_simps) also have "\ = z" using assms by (auto simp: field_simps) finally show ?thesis using comb by auto qed show "z \ C" using z less assms by (auto simp: le_less) qed lemma f''_imp_f': fixes f :: "real \ real" assumes "convex C" and f': "\x. x \ C \ DERIV f x :> (f' x)" and f'': "\x. x \ C \ DERIV f' x :> (f'' x)" and pos: "\x. x \ C \ f'' x \ 0" and x: "x \ C" and y: "y \ C" shows "f' x * (y - x) \ f y - f x" using assms proof - have less_imp: "f y - f x \ f' x * (y - x)" "f' y * (x - y) \ f x - f y" if *: "x \ C" "y \ C" "y > x" for x y :: real proof - from * have ge: "y - x > 0" "y - x \ 0" by auto from * have le: "x - y < 0" "x - y \ 0" by auto then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1" using subsetD[OF atMostAtLeast_subset_convex[OF \convex C\ \x \ C\ \y \ C\ \x < y\], THEN f', THEN MVT2[OF \x < y\, rule_format, unfolded atLeastAtMost_iff[symmetric]]] by auto then have "z1 \ C" using atMostAtLeast_subset_convex \convex C\ \x \ C\ \y \ C\ \x < y\ by fastforce from z1 have z1': "f x - f y = (x - y) * f' z1" by (simp add: field_simps) obtain z2 where z2: "z2 > x" "z2 < z1" "f' z1 - f' x = (z1 - x) * f'' z2" using subsetD[OF atMostAtLeast_subset_convex[OF \convex C\ \x \ C\ \z1 \ C\ \x < z1\], THEN f'', THEN MVT2[OF \x < z1\, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1 by auto obtain z3 where z3: "z3 > z1" "z3 < y" "f' y - f' z1 = (y - z1) * f'' z3" using subsetD[OF atMostAtLeast_subset_convex[OF \convex C\ \z1 \ C\ \y \ C\ \z1 < y\], THEN f'', THEN MVT2[OF \z1 < y\, rule_format, unfolded atLeastAtMost_iff[symmetric]]] z1 by auto have "f' y - (f x - f y) / (x - y) = f' y - f' z1" using * z1' by auto also have "\ = (y - z1) * f'' z3" using z3 by auto finally have cool': "f' y - (f x - f y) / (x - y) = (y - z1) * f'' z3" by simp have A': "y - z1 \ 0" using z1 by auto have "z3 \ C" using z3 * atMostAtLeast_subset_convex \convex C\ \x \ C\ \z1 \ C\ \x < z1\ by fastforce then have B': "f'' z3 \ 0" using assms by auto from A' B' have "(y - z1) * f'' z3 \ 0" by auto from cool' this have "f' y - (f x - f y) / (x - y) \ 0" by auto from mult_right_mono_neg[OF this le(2)] have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \ 0 * (x - y)" by (simp add: algebra_simps) then have "f' y * (x - y) - (f x - f y) \ 0" using le by auto then have res: "f' y * (x - y) \ f x - f y" by auto have "(f y - f x) / (y - x) - f' x = f' z1 - f' x" using * z1 by auto also have "\ = (z1 - x) * f'' z2" using z2 by auto finally have cool: "(f y - f x) / (y - x) - f' x = (z1 - x) * f'' z2" by simp have A: "z1 - x \ 0" using z1 by auto have "z2 \ C" using z2 z1 * atMostAtLeast_subset_convex \convex C\ \z1 \ C\ \y \ C\ \z1 < y\ by fastforce then have B: "f'' z2 \ 0" using assms by auto from A B have "(z1 - x) * f'' z2 \ 0" by auto with cool have "(f y - f x) / (y - x) - f' x \ 0" by auto from mult_right_mono[OF this ge(2)] have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \ 0 * (y - x)" by (simp add: algebra_simps) then have "f y - f x - f' x * (y - x) \ 0" using ge by auto then show "f y - f x \ f' x * (y - x)" "f' y * (x - y) \ f x - f y" using res by auto qed show ?thesis proof (cases "x = y") case True with x y show ?thesis by auto next case False with less_imp x y show ?thesis by (auto simp: neq_iff) qed qed lemma f''_ge0_imp_convex: fixes f :: "real \ real" assumes conv: "convex C" and f': "\x. x \ C \ DERIV f x :> (f' x)" and f'': "\x. x \ C \ DERIV f' x :> (f'' x)" and pos: "\x. x \ C \ f'' x \ 0" shows "convex_on C f" using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce lemma minus_log_convex: fixes b :: real assumes "b > 1" shows "convex_on {0 <..} (\ x. - log b x)" proof - have "\z. z > 0 \ DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto then have f': "\z. z > 0 \ DERIV (\ z. - log b z) z :> - 1 / (ln b * z)" by (auto simp: DERIV_minus) have "\z::real. z > 0 \ DERIV inverse z :> - (inverse z ^ Suc (Suc 0))" using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto from this[THEN DERIV_cmult, of _ "- 1 / ln b"] have "\z::real. z > 0 \ DERIV (\ z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))" by auto then have f''0: "\z::real. z > 0 \ DERIV (\ z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)" unfolding inverse_eq_divide by (auto simp: mult.assoc) have f''_ge0: "\z::real. z > 0 \ 1 / (ln b * z * z) \ 0" using \b > 1\ by (auto intro!: less_imp_le) from f''_ge0_imp_convex[OF pos_is_convex, unfolded greaterThan_iff, OF f' f''0 f''_ge0] show ?thesis by auto qed subsection\<^marker>\tag unimportant\ \Convexity of real functions\ lemma convex_on_realI: assumes "connected A" and "\x. x \ A \ (f has_real_derivative f' x) (at x)" and "\x y. x \ A \ y \ A \ x \ y \ f' x \ f' y" shows "convex_on A f" proof (rule convex_on_linorderI) fix t x y :: real assume t: "t > 0" "t < 1" assume xy: "x \ A" "y \ A" "x < y" define z where "z = (1 - t) * x + t * y" with \connected A\ and xy have ivl: "{x..y} \ A" using connected_contains_Icc by blast from xy t have xz: "z > x" by (simp add: z_def algebra_simps) have "y - z = (1 - t) * (y - x)" by (simp add: z_def algebra_simps) also from xy t have "\ > 0" by (intro mult_pos_pos) simp_all finally have yz: "z < y" by simp from assms xz yz ivl t have "\\. \ > x \ \ < z \ f z - f x = (z - x) * f' \" by (intro MVT2) (auto intro!: assms(2)) then obtain \ where \: "\ > x" "\ < z" "f' \ = (f z - f x) / (z - x)" by auto from assms xz yz ivl t have "\\. \ > z \ \ < y \ f y - f z = (y - z) * f' \" by (intro MVT2) (auto intro!: assms(2)) then obtain \ where \: "\ > z" "\ < y" "f' \ = (f y - f z) / (y - z)" by auto from \(3) have "(f y - f z) / (y - z) = f' \" .. also from \ \ ivl have "\ \ A" "\ \ A" by auto with \ \ have "f' \ \ f' \" by (intro assms(3)) auto also from \(3) have "f' \ = (f z - f x) / (z - x)" . finally have "(f y - f z) * (z - x) \ (f z - f x) * (y - z)" using xz yz by (simp add: field_simps) also have "z - x = t * (y - x)" by (simp add: z_def algebra_simps) also have "y - z = (1 - t) * (y - x)" by (simp add: z_def algebra_simps) finally have "(f y - f z) * t \ (f z - f x) * (1 - t)" using xy by simp then show "(1 - t) * f x + t * f y \ f ((1 - t) *\<^sub>R x + t *\<^sub>R y)" by (simp add: z_def algebra_simps) qed lemma convex_on_inverse: assumes "A \ {0<..}" shows "convex_on A (inverse :: real \ real)" proof (rule convex_on_subset[OF _ assms], intro convex_on_realI[of _ _ "\x. -inverse (x^2)"]) fix u v :: real assume "u \ {0<..}" "v \ {0<..}" "u \ v" with assms show "-inverse (u^2) \ -inverse (v^2)" by (intro le_imp_neg_le le_imp_inverse_le power_mono) (simp_all) qed (insert assms, auto intro!: derivative_eq_intros simp: field_split_simps power2_eq_square) lemma convex_onD_Icc': assumes "convex_on {x..y} f" "c \ {x..y}" defines "d \ y - x" shows "f c \ (f y - f x) / d * (c - x) + f x" proof (cases x y rule: linorder_cases) case less then have d: "d > 0" by (simp add: d_def) from assms(2) less have A: "0 \ (c - x) / d" "(c - x) / d \ 1" by (simp_all add: d_def field_split_simps) have "f c = f (x + (c - x) * 1)" by simp also from less have "1 = ((y - x) / d)" by (simp add: d_def) also from d have "x + (c - x) * \ = (1 - (c - x) / d) *\<^sub>R x + ((c - x) / d) *\<^sub>R y" by (simp add: field_simps) also have "f \ \ (1 - (c - x) / d) * f x + (c - x) / d * f y" using assms less by (intro convex_onD_Icc) simp_all also from d have "\ = (f y - f x) / d * (c - x) + f x" by (simp add: field_simps) finally show ?thesis . qed (insert assms(2), simp_all) lemma convex_onD_Icc'': assumes "convex_on {x..y} f" "c \ {x..y}" defines "d \ y - x" shows "f c \ (f x - f y) / d * (y - c) + f y" proof (cases x y rule: linorder_cases) case less then have d: "d > 0" by (simp add: d_def) from assms(2) less have A: "0 \ (y - c) / d" "(y - c) / d \ 1" by (simp_all add: d_def field_split_simps) have "f c = f (y - (y - c) * 1)" by simp also from less have "1 = ((y - x) / d)" by (simp add: d_def) also from d have "y - (y - c) * \ = (1 - (1 - (y - c) / d)) *\<^sub>R x + (1 - (y - c) / d) *\<^sub>R y" by (simp add: field_simps) also have "f \ \ (1 - (1 - (y - c) / d)) * f x + (1 - (y - c) / d) * f y" using assms less by (intro convex_onD_Icc) (simp_all add: field_simps) also from d have "\ = (f x - f y) / d * (y - c) + f y" by (simp add: field_simps) finally show ?thesis . qed (insert assms(2), simp_all) lemma convex_translation_eq [simp]: "convex ((+) a ` s) \ convex s" by (metis convex_translation translation_galois) lemma convex_translation_subtract_eq [simp]: "convex ((\b. b - a) ` s) \ convex s" using convex_translation_eq [of "- a"] by (simp cong: image_cong_simp) lemma convex_linear_image_eq [simp]: fixes f :: "'a::real_vector \ 'b::real_vector" shows "\linear f; inj f\ \ convex (f ` s) \ convex s" by (metis (no_types) convex_linear_image convex_linear_vimage inj_vimage_image_eq) lemma fst_linear: "linear fst" unfolding linear_iff by (simp add: algebra_simps) lemma snd_linear: "linear snd" unfolding linear_iff by (simp add: algebra_simps) lemma fst_snd_linear: "linear (\(x,y). x + y)" unfolding linear_iff by (simp add: algebra_simps) lemma vector_choose_size: assumes "0 \ c" obtains x :: "'a::{real_normed_vector, perfect_space}" where "norm x = c" proof - obtain a::'a where "a \ 0" using UNIV_not_singleton UNIV_eq_I set_zero singletonI by fastforce then show ?thesis by (rule_tac x="scaleR (c / norm a) a" in that) (simp add: assms) qed lemma vector_choose_dist: assumes "0 \ c" obtains y :: "'a::{real_normed_vector, perfect_space}" where "dist x y = c" by (metis add_diff_cancel_left' assms dist_commute dist_norm vector_choose_size) lemma sum_delta_notmem: assumes "x \ s" shows "sum (\y. if (y = x) then P x else Q y) s = sum Q s" and "sum (\y. if (x = y) then P x else Q y) s = sum Q s" and "sum (\y. if (y = x) then P y else Q y) s = sum Q s" and "sum (\y. if (x = y) then P y else Q y) s = sum Q s" apply (rule_tac [!] sum.cong) using assms apply auto done lemma sum_delta'': fixes s::"'a::real_vector set" assumes "finite s" shows "(\x\s. (if y = x then f x else 0) *\<^sub>R x) = (if y\s then (f y) *\<^sub>R y else 0)" proof - have *: "\x y. (if y = x then f x else (0::real)) *\<^sub>R x = (if x=y then (f x) *\<^sub>R x else 0)" by auto show ?thesis unfolding * using sum.delta[OF assms, of y "\x. f x *\<^sub>R x"] by auto qed lemma if_smult: "(if P then x else (y::real)) *\<^sub>R v = (if P then x *\<^sub>R v else y *\<^sub>R v)" by (fact if_distrib) lemma dist_triangle_eq: fixes x y z :: "'a::real_inner" shows "dist x z = dist x y + dist y z \ norm (x - y) *\<^sub>R (y - z) = norm (y - z) *\<^sub>R (x - y)" proof - have *: "x - y + (y - z) = x - z" by auto show ?thesis unfolding dist_norm norm_triangle_eq[of "x - y" "y - z", unfolded *] by (auto simp:norm_minus_commute) qed subsection \Affine set and affine hull\ definition\<^marker>\tag important\ affine :: "'a::real_vector set \ bool" where "affine s \ (\x\s. \y\s. \u v. u + v = 1 \ u *\<^sub>R x + v *\<^sub>R y \ s)" lemma affine_alt: "affine s \ (\x\s. \y\s. \u::real. (1 - u) *\<^sub>R x + u *\<^sub>R y \ s)" unfolding affine_def by (metis eq_diff_eq') lemma affine_empty [iff]: "affine {}" unfolding affine_def by auto lemma affine_sing [iff]: "affine {x}" unfolding affine_alt by (auto simp: scaleR_left_distrib [symmetric]) lemma affine_UNIV [iff]: "affine UNIV" unfolding affine_def by auto lemma affine_Inter [intro]: "(\s. s\f \ affine s) \ affine (\f)" unfolding affine_def by auto lemma affine_Int[intro]: "affine s \ affine t \ affine (s \ t)" unfolding affine_def by auto lemma affine_scaling: "affine s \ affine (image (\x. c *\<^sub>R x) s)" apply (clarsimp simp add: affine_def) apply (rule_tac x="u *\<^sub>R x + v *\<^sub>R y" in image_eqI) apply (auto simp: algebra_simps) done lemma affine_affine_hull [simp]: "affine(affine hull s)" unfolding hull_def using affine_Inter[of "{t. affine t \ s \ t}"] by auto lemma affine_hull_eq[simp]: "(affine hull s = s) \ affine s" by (metis affine_affine_hull hull_same) lemma affine_hyperplane: "affine {x. a \ x = b}" by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral) subsubsection\<^marker>\tag unimportant\ \Some explicit formulations\ text "Formalized by Lars Schewe." lemma affine: fixes V::"'a::real_vector set" shows "affine V \ (\S u. finite S \ S \ {} \ S \ V \ sum u S = 1 \ (\x\S. u x *\<^sub>R x) \ V)" proof - have "u *\<^sub>R x + v *\<^sub>R y \ V" if "x \ V" "y \ V" "u + v = (1::real)" and *: "\S u. \finite S; S \ {}; S \ V; sum u S = 1\ \ (\x\S. u x *\<^sub>R x) \ V" for x y u v proof (cases "x = y") case True then show ?thesis using that by (metis scaleR_add_left scaleR_one) next case False then show ?thesis using that *[of "{x,y}" "\w. if w = x then u else v"] by auto qed moreover have "(\x\S. u x *\<^sub>R x) \ V" if *: "\x y u v. \x\V; y\V; u + v = 1\ \ u *\<^sub>R x + v *\<^sub>R y \ V" and "finite S" "S \ {}" "S \ V" "sum u S = 1" for S u proof - define n where "n = card S" consider "card S = 0" | "card S = 1" | "card S = 2" | "card S > 2" by linarith then show "(\x\S. u x *\<^sub>R x) \ V" proof cases assume "card S = 1" then obtain a where "S={a}" by (auto simp: card_Suc_eq) then show ?thesis using that by simp next assume "card S = 2" then obtain a b where "S = {a, b}" by (metis Suc_1 card_1_singletonE card_Suc_eq) then show ?thesis using *[of a b] that by (auto simp: sum_clauses(2)) next assume "card S > 2" then show ?thesis using that n_def proof (induct n arbitrary: u S) case 0 then show ?case by auto next case (Suc n u S) have "sum u S = card S" if "\ (\x\S. u x \ 1)" using that unfolding card_eq_sum by auto with Suc.prems obtain x where "x \ S" and x: "u x \ 1" by force have c: "card (S - {x}) = card S - 1" by (simp add: Suc.prems(3) \x \ S\) have "sum u (S - {x}) = 1 - u x" by (simp add: Suc.prems sum_diff1 \x \ S\) with x have eq1: "inverse (1 - u x) * sum u (S - {x}) = 1" by auto have inV: "(\y\S - {x}. (inverse (1 - u x) * u y) *\<^sub>R y) \ V" proof (cases "card (S - {x}) > 2") case True then have S: "S - {x} \ {}" "card (S - {x}) = n" using Suc.prems c by force+ show ?thesis proof (rule Suc.hyps) show "(\a\S - {x}. inverse (1 - u x) * u a) = 1" by (auto simp: eq1 sum_distrib_left[symmetric]) qed (use S Suc.prems True in auto) next case False then have "card (S - {x}) = Suc (Suc 0)" using Suc.prems c by auto then obtain a b where ab: "(S - {x}) = {a, b}" "a\b" unfolding card_Suc_eq by auto then show ?thesis using eq1 \S \ V\ by (auto simp: sum_distrib_left distrib_left intro!: Suc.prems(2)[of a b]) qed have "u x + (1 - u x) = 1 \ u x *\<^sub>R x + (1 - u x) *\<^sub>R ((\y\S - {x}. u y *\<^sub>R y) /\<^sub>R (1 - u x)) \ V" by (rule Suc.prems) (use \x \ S\ Suc.prems inV in \auto simp: scaleR_right.sum\) moreover have "(\a\S. u a *\<^sub>R a) = u x *\<^sub>R x + (\a\S - {x}. u a *\<^sub>R a)" by (meson Suc.prems(3) sum.remove \x \ S\) ultimately show "(\x\S. u x *\<^sub>R x) \ V" by (simp add: x) qed qed (use \S\{}\ \finite S\ in auto) qed ultimately show ?thesis unfolding affine_def by meson qed lemma affine_hull_explicit: "affine hull p = {y. \S u. finite S \ S \ {} \ S \ p \ sum u S = 1 \ sum (\v. u v *\<^sub>R v) S = y}" (is "_ = ?rhs") proof (rule hull_unique) show "p \ ?rhs" proof (intro subsetI CollectI exI conjI) show "\x. sum (\z. 1) {x} = 1" by auto qed auto show "?rhs \ T" if "p \ T" "affine T" for T using that unfolding affine by blast show "affine ?rhs" unfolding affine_def proof clarify fix u v :: real and sx ux sy uy assume uv: "u + v = 1" and x: "finite sx" "sx \ {}" "sx \ p" "sum ux sx = (1::real)" and y: "finite sy" "sy \ {}" "sy \ p" "sum uy sy = (1::real)" have **: "(sx \ sy) \ sx = sx" "(sx \ sy) \ sy = sy" by auto show "\S w. finite S \ S \ {} \ S \ p \ sum w S = 1 \ (\v\S. w v *\<^sub>R v) = u *\<^sub>R (\v\sx. ux v *\<^sub>R v) + v *\<^sub>R (\v\sy. uy v *\<^sub>R v)" proof (intro exI conjI) show "finite (sx \ sy)" using x y by auto show "sum (\i. (if i\sx then u * ux i else 0) + (if i\sy then v * uy i else 0)) (sx \ sy) = 1" using x y uv by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric] sum_distrib_left [symmetric] **) have "(\i\sx \ sy. ((if i \ sx then u * ux i else 0) + (if i \ sy then v * uy i else 0)) *\<^sub>R i) = (\i\sx. (u * ux i) *\<^sub>R i) + (\i\sy. (v * uy i) *\<^sub>R i)" using x y unfolding scaleR_left_distrib scaleR_zero_left if_smult by (simp add: sum_Un sum.distrib sum.inter_restrict[symmetric] **) also have "\ = u *\<^sub>R (\v\sx. ux v *\<^sub>R v) + v *\<^sub>R (\v\sy. uy v *\<^sub>R v)" unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] by blast finally show "(\i\sx \ sy. ((if i \ sx then u * ux i else 0) + (if i \ sy then v * uy i else 0)) *\<^sub>R i) = u *\<^sub>R (\v\sx. ux v *\<^sub>R v) + v *\<^sub>R (\v\sy. uy v *\<^sub>R v)" . qed (use x y in auto) qed qed lemma affine_hull_finite: assumes "finite S" shows "affine hull S = {y. \u. sum u S = 1 \ sum (\v. u v *\<^sub>R v) S = y}" proof - have *: "\h. sum h S = 1 \ (\v\S. h v *\<^sub>R v) = x" if "F \ S" "finite F" "F \ {}" and sum: "sum u F = 1" and x: "(\v\F. u v *\<^sub>R v) = x" for x F u proof - have "S \ F = F" using that by auto show ?thesis proof (intro exI conjI) show "(\x\S. if x \ F then u x else 0) = 1" by (metis (mono_tags, lifting) \S \ F = F\ assms sum.inter_restrict sum) show "(\v\S. (if v \ F then u v else 0) *\<^sub>R v) = x" by (simp add: if_smult cong: if_cong) (metis (no_types) \S \ F = F\ assms sum.inter_restrict x) qed qed show ?thesis unfolding affine_hull_explicit using assms by (fastforce dest: *) qed subsubsection\<^marker>\tag unimportant\ \Stepping theorems and hence small special cases\ lemma affine_hull_empty[simp]: "affine hull {} = {}" by simp lemma affine_hull_finite_step: fixes y :: "'a::real_vector" shows "finite S \ (\u. sum u (insert a S) = w \ sum (\x. u x *\<^sub>R x) (insert a S) = y) \ (\v u. sum u S = w - v \ sum (\x. u x *\<^sub>R x) S = y - v *\<^sub>R a)" (is "_ \ ?lhs = ?rhs") proof - assume fin: "finite S" show "?lhs = ?rhs" proof assume ?lhs then obtain u where u: "sum u (insert a S) = w \ (\x\insert a S. u x *\<^sub>R x) = y" by auto show ?rhs proof (cases "a \ S") case True then show ?thesis using u by (simp add: insert_absorb) (metis diff_zero real_vector.scale_zero_left) next case False show ?thesis by (rule exI [where x="u a"]) (use u fin False in auto) qed next assume ?rhs then obtain v u where vu: "sum u S = w - v" "(\x\S. u x *\<^sub>R x) = y - v *\<^sub>R a" by auto have *: "\x M. (if x = a then v else M) *\<^sub>R x = (if x = a then v *\<^sub>R x else M *\<^sub>R x)" by auto show ?lhs proof (cases "a \ S") case True show ?thesis by (rule exI [where x="\x. (if x=a then v else 0) + u x"]) (simp add: True scaleR_left_distrib sum.distrib sum_clauses fin vu * cong: if_cong) next case False then show ?thesis apply (rule_tac x="\x. if x=a then v else u x" in exI) apply (simp add: vu sum_clauses(2)[OF fin] *) by (simp add: sum_delta_notmem(3) vu) qed qed qed lemma affine_hull_2: fixes a b :: "'a::real_vector" shows "affine hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b| u v. (u + v = 1)}" (is "?lhs = ?rhs") proof - have *: "\x y z. z = x - y \ y + z = (x::real)" "\x y z. z = x - y \ y + z = (x::'a)" by auto have "?lhs = {y. \u. sum u {a, b} = 1 \ (\v\{a, b}. u v *\<^sub>R v) = y}" using affine_hull_finite[of "{a,b}"] by auto also have "\ = {y. \v u. u b = 1 - v \ u b *\<^sub>R b = y - v *\<^sub>R a}" by (simp add: affine_hull_finite_step[of "{b}" a]) also have "\ = ?rhs" unfolding * by auto finally show ?thesis by auto qed lemma affine_hull_3: fixes a b c :: "'a::real_vector" shows "affine hull {a,b,c} = { u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c| u v w. u + v + w = 1}" proof - have *: "\x y z. z = x - y \ y + z = (x::real)" "\x y z. z = x - y \ y + z = (x::'a)" by auto show ?thesis apply (simp add: affine_hull_finite affine_hull_finite_step) unfolding * apply safe apply (metis add.assoc) apply (rule_tac x=u in exI, force) done qed lemma mem_affine: assumes "affine S" "x \ S" "y \ S" "u + v = 1" shows "u *\<^sub>R x + v *\<^sub>R y \ S" using assms affine_def[of S] by auto lemma mem_affine_3: assumes "affine S" "x \ S" "y \ S" "z \ S" "u + v + w = 1" shows "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \ S" proof - have "u *\<^sub>R x + v *\<^sub>R y + w *\<^sub>R z \ affine hull {x, y, z}" using affine_hull_3[of x y z] assms by auto moreover have "affine hull {x, y, z} \ affine hull S" using hull_mono[of "{x, y, z}" "S"] assms by auto moreover have "affine hull S = S" using assms affine_hull_eq[of S] by auto ultimately show ?thesis by auto qed lemma mem_affine_3_minus: assumes "affine S" "x \ S" "y \ S" "z \ S" shows "x + v *\<^sub>R (y-z) \ S" using mem_affine_3[of S x y z 1 v "-v"] assms by (simp add: algebra_simps) corollary mem_affine_3_minus2: "\affine S; x \ S; y \ S; z \ S\ \ x - v *\<^sub>R (y-z) \ S" by (metis add_uminus_conv_diff mem_affine_3_minus real_vector.scale_minus_left) subsubsection\<^marker>\tag unimportant\ \Some relations between affine hull and subspaces\ lemma affine_hull_insert_subset_span: "affine hull (insert a S) \ {a + v| v . v \ span {x - a | x . x \ S}}" proof - have "\v T u. x = a + v \ (finite T \ T \ {x - a |x. x \ S} \ (\v\T. u v *\<^sub>R v) = v)" if "finite F" "F \ {}" "F \ insert a S" "sum u F = 1" "(\v\F. u v *\<^sub>R v) = x" for x F u proof - have *: "(\x. x - a) ` (F - {a}) \ {x - a |x. x \ S}" using that by auto show ?thesis proof (intro exI conjI) show "finite ((\x. x - a) ` (F - {a}))" by (simp add: that(1)) show "(\v\(\x. x - a) ` (F - {a}). u(v+a) *\<^sub>R v) = x-a" by (simp add: sum.reindex[unfolded inj_on_def] algebra_simps sum_subtractf scaleR_left.sum[symmetric] sum_diff1 that) qed (use \F \ insert a S\ in auto) qed then show ?thesis unfolding affine_hull_explicit span_explicit by blast qed lemma affine_hull_insert_span: assumes "a \ S" shows "affine hull (insert a S) = {a + v | v . v \ span {x - a | x. x \ S}}" proof - have *: "\G u. finite G \ G \ {} \ G \ insert a S \ sum u G = 1 \ (\v\G. u v *\<^sub>R v) = y" if "v \ span {x - a |x. x \ S}" "y = a + v" for y v proof - from that obtain T u where u: "finite T" "T \ {x - a |x. x \ S}" "a + (\v\T. u v *\<^sub>R v) = y" unfolding span_explicit by auto define F where "F = (\x. x + a) ` T" have F: "finite F" "F \ S" "(\v\F. u (v - a) *\<^sub>R (v - a)) = y - a" unfolding F_def using u by (auto simp: sum.reindex[unfolded inj_on_def]) have *: "F \ {a} = {}" "F \ - {a} = F" using F assms by auto show "\G u. finite G \ G \ {} \ G \ insert a S \ sum u G = 1 \ (\v\G. u v *\<^sub>R v) = y" apply (rule_tac x = "insert a F" in exI) apply (rule_tac x = "\x. if x=a then 1 - sum (\x. u (x - a)) F else u (x - a)" in exI) using assms F apply (auto simp: sum_clauses sum.If_cases if_smult sum_subtractf scaleR_left.sum algebra_simps *) done qed show ?thesis by (intro subset_antisym affine_hull_insert_subset_span) (auto simp: affine_hull_explicit dest!: *) qed lemma affine_hull_span: assumes "a \ S" shows "affine hull S = {a + v | v. v \ span {x - a | x. x \ S - {a}}}" using affine_hull_insert_span[of a "S - {a}", unfolded insert_Diff[OF assms]] by auto subsubsection\<^marker>\tag unimportant\ \Parallel affine sets\ definition affine_parallel :: "'a::real_vector set \ 'a::real_vector set \ bool" where "affine_parallel S T \ (\a. T = (\x. a + x) ` S)" lemma affine_parallel_expl_aux: fixes S T :: "'a::real_vector set" assumes "\x. x \ S \ a + x \ T" shows "T = (\x. a + x) ` S" proof - have "x \ ((\x. a + x) ` S)" if "x \ T" for x using that by (simp add: image_iff) (metis add.commute diff_add_cancel assms) moreover have "T \ (\x. a + x) ` S" using assms by auto ultimately show ?thesis by auto qed lemma affine_parallel_expl: "affine_parallel S T \ (\a. \x. x \ S \ a + x \ T)" by (auto simp add: affine_parallel_def) (use affine_parallel_expl_aux [of S _ T] in blast) lemma affine_parallel_reflex: "affine_parallel S S" unfolding affine_parallel_def using image_add_0 by blast lemma affine_parallel_commut: assumes "affine_parallel A B" shows "affine_parallel B A" proof - from assms obtain a where B: "B = (\x. a + x) ` A" unfolding affine_parallel_def by auto have [simp]: "(\x. x - a) = plus (- a)" by (simp add: fun_eq_iff) from B show ?thesis using translation_galois [of B a A] unfolding affine_parallel_def by blast qed lemma affine_parallel_assoc: assumes "affine_parallel A B" and "affine_parallel B C" shows "affine_parallel A C" proof - from assms obtain ab where "B = (\x. ab + x) ` A" unfolding affine_parallel_def by auto moreover from assms obtain bc where "C = (\x. bc + x) ` B" unfolding affine_parallel_def by auto ultimately show ?thesis using translation_assoc[of bc ab A] unfolding affine_parallel_def by auto qed lemma affine_translation_aux: fixes a :: "'a::real_vector" assumes "affine ((\x. a + x) ` S)" shows "affine S" proof - { fix x y u v assume xy: "x \ S" "y \ S" "(u :: real) + v = 1" then have "(a + x) \ ((\x. a + x) ` S)" "(a + y) \ ((\x. a + x) ` S)" by auto then have h1: "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) \ (\x. a + x) ` S" using xy assms unfolding affine_def by auto have "u *\<^sub>R (a + x) + v *\<^sub>R (a + y) = (u + v) *\<^sub>R a + (u *\<^sub>R x + v *\<^sub>R y)" by (simp add: algebra_simps) also have "\ = a + (u *\<^sub>R x + v *\<^sub>R y)" using \u + v = 1\ by auto ultimately have "a + (u *\<^sub>R x + v *\<^sub>R y) \ (\x. a + x) ` S" using h1 by auto then have "u *\<^sub>R x + v *\<^sub>R y \ S" by auto } then show ?thesis unfolding affine_def by auto qed lemma affine_translation: "affine S \ affine ((+) a ` S)" for a :: "'a::real_vector" proof show "affine ((+) a ` S)" if "affine S" using that translation_assoc [of "- a" a S] by (auto intro: affine_translation_aux [of "- a" "((+) a ` S)"]) show "affine S" if "affine ((+) a ` S)" using that by (rule affine_translation_aux) qed lemma parallel_is_affine: fixes S T :: "'a::real_vector set" assumes "affine S" "affine_parallel S T" shows "affine T" proof - from assms obtain a where "T = (\x. a + x) ` S" unfolding affine_parallel_def by auto then show ?thesis using affine_translation assms by auto qed lemma subspace_imp_affine: "subspace s \ affine s" unfolding subspace_def affine_def by auto subsubsection\<^marker>\tag unimportant\ \Subspace parallel to an affine set\ lemma subspace_affine: "subspace S \ affine S \ 0 \ S" proof - have h0: "subspace S \ affine S \ 0 \ S" using subspace_imp_affine[of S] subspace_0 by auto { assume assm: "affine S \ 0 \ S" { fix c :: real fix x assume x: "x \ S" have "c *\<^sub>R x = (1-c) *\<^sub>R 0 + c *\<^sub>R x" by auto moreover have "(1 - c) *\<^sub>R 0 + c *\<^sub>R x \ S" using affine_alt[of S] assm x by auto ultimately have "c *\<^sub>R x \ S" by auto } then have h1: "\c. \x \ S. c *\<^sub>R x \ S" by auto { fix x y assume xy: "x \ S" "y \ S" define u where "u = (1 :: real)/2" have "(1/2) *\<^sub>R (x+y) = (1/2) *\<^sub>R (x+y)" by auto moreover have "(1/2) *\<^sub>R (x+y)=(1/2) *\<^sub>R x + (1-(1/2)) *\<^sub>R y" by (simp add: algebra_simps) moreover have "(1 - u) *\<^sub>R x + u *\<^sub>R y \ S" using affine_alt[of S] assm xy by auto ultimately have "(1/2) *\<^sub>R (x+y) \ S" using u_def by auto moreover have "x + y = 2 *\<^sub>R ((1/2) *\<^sub>R (x+y))" by auto ultimately have "x + y \ S" using h1[rule_format, of "(1/2) *\<^sub>R (x+y)" "2"] by auto } then have "\x \ S. \y \ S. x + y \ S" by auto then have "subspace S" using h1 assm unfolding subspace_def by auto } then show ?thesis using h0 by metis qed lemma affine_diffs_subspace: assumes "affine S" "a \ S" shows "subspace ((\x. (-a)+x) ` S)" proof - have [simp]: "(\x. x - a) = plus (- a)" by (simp add: fun_eq_iff) have "affine ((\x. (-a)+x) ` S)" using affine_translation assms by blast moreover have "0 \ ((\x. (-a)+x) ` S)" using assms exI[of "(\x. x\S \ -a+x = 0)" a] by auto ultimately show ?thesis using subspace_affine by auto qed lemma affine_diffs_subspace_subtract: "subspace ((\x. x - a) ` S)" if "affine S" "a \ S" using that affine_diffs_subspace [of _ a] by simp lemma parallel_subspace_explicit: assumes "affine S" and "a \ S" assumes "L \ {y. \x \ S. (-a) + x = y}" shows "subspace L \ affine_parallel S L" proof - from assms have "L = plus (- a) ` S" by auto then have par: "affine_parallel S L" unfolding affine_parallel_def .. then have "affine L" using assms parallel_is_affine by auto moreover have "0 \ L" using assms by auto ultimately show ?thesis using subspace_affine par by auto qed lemma parallel_subspace_aux: assumes "subspace A" and "subspace B" and "affine_parallel A B" shows "A \ B" proof - from assms obtain a where a: "\x. x \ A \ a + x \ B" using affine_parallel_expl[of A B] by auto then have "-a \ A" using assms subspace_0[of B] by auto then have "a \ A" using assms subspace_neg[of A "-a"] by auto then show ?thesis using assms a unfolding subspace_def by auto qed lemma parallel_subspace: assumes "subspace A" and "subspace B" and "affine_parallel A B" shows "A = B" proof show "A \ B" using assms parallel_subspace_aux by auto show "A \ B" using assms parallel_subspace_aux[of B A] affine_parallel_commut by auto qed lemma affine_parallel_subspace: assumes "affine S" "S \ {}" shows "\!L. subspace L \ affine_parallel S L" proof - have ex: "\L. subspace L \ affine_parallel S L" using assms parallel_subspace_explicit by auto { fix L1 L2 assume ass: "subspace L1 \ affine_parallel S L1" "subspace L2 \ affine_parallel S L2" then have "affine_parallel L1 L2" using affine_parallel_commut[of S L1] affine_parallel_assoc[of L1 S L2] by auto then have "L1 = L2" using ass parallel_subspace by auto } then show ?thesis using ex by auto qed subsection \Cones\ definition\<^marker>\tag important\ cone :: "'a::real_vector set \ bool" where "cone s \ (\x\s. \c\0. c *\<^sub>R x \ s)" lemma cone_empty[intro, simp]: "cone {}" unfolding cone_def by auto lemma cone_univ[intro, simp]: "cone UNIV" unfolding cone_def by auto lemma cone_Inter[intro]: "\s\f. cone s \ cone (\f)" unfolding cone_def by auto lemma subspace_imp_cone: "subspace S \ cone S" by (simp add: cone_def subspace_scale) subsubsection \Conic hull\ lemma cone_cone_hull: "cone (cone hull s)" unfolding hull_def by auto lemma cone_hull_eq: "cone hull s = s \ cone s" apply (rule hull_eq) using cone_Inter unfolding subset_eq apply auto done lemma mem_cone: assumes "cone S" "x \ S" "c \ 0" shows "c *\<^sub>R x \ S" using assms cone_def[of S] by auto lemma cone_contains_0: assumes "cone S" shows "S \ {} \ 0 \ S" proof - { assume "S \ {}" then obtain a where "a \ S" by auto then have "0 \ S" using assms mem_cone[of S a 0] by auto } then show ?thesis by auto qed lemma cone_0: "cone {0}" unfolding cone_def by auto lemma cone_Union[intro]: "(\s\f. cone s) \ cone (\f)" unfolding cone_def by blast lemma cone_iff: assumes "S \ {}" shows "cone S \ 0 \ S \ (\c. c > 0 \ ((*\<^sub>R) c) ` S = S)" proof - { assume "cone S" { fix c :: real assume "c > 0" { fix x assume "x \ S" then have "x \ ((*\<^sub>R) c) ` S" unfolding image_def using \cone S\ \c>0\ mem_cone[of S x "1/c"] exI[of "(\t. t \ S \ x = c *\<^sub>R t)" "(1 / c) *\<^sub>R x"] by auto } moreover { fix x assume "x \ ((*\<^sub>R) c) ` S" then have "x \ S" using \cone S\ \c > 0\ unfolding cone_def image_def \c > 0\ by auto } ultimately have "((*\<^sub>R) c) ` S = S" by blast } then have "0 \ S \ (\c. c > 0 \ ((*\<^sub>R) c) ` S = S)" using \cone S\ cone_contains_0[of S] assms by auto } moreover { assume a: "0 \ S \ (\c. c > 0 \ ((*\<^sub>R) c) ` S = S)" { fix x assume "x \ S" fix c1 :: real assume "c1 \ 0" then have "c1 = 0 \ c1 > 0" by auto then have "c1 *\<^sub>R x \ S" using a \x \ S\ by auto } then have "cone S" unfolding cone_def by auto } ultimately show ?thesis by blast qed lemma cone_hull_empty: "cone hull {} = {}" by (metis cone_empty cone_hull_eq) lemma cone_hull_empty_iff: "S = {} \ cone hull S = {}" by (metis bot_least cone_hull_empty hull_subset xtrans(5)) lemma cone_hull_contains_0: "S \ {} \ 0 \ cone hull S" using cone_cone_hull[of S] cone_contains_0[of "cone hull S"] cone_hull_empty_iff[of S] by auto lemma mem_cone_hull: assumes "x \ S" "c \ 0" shows "c *\<^sub>R x \ cone hull S" by (metis assms cone_cone_hull hull_inc mem_cone) proposition cone_hull_expl: "cone hull S = {c *\<^sub>R x | c x. c \ 0 \ x \ S}" (is "?lhs = ?rhs") proof - { fix x assume "x \ ?rhs" then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \ 0" "xx \ S" by auto fix c :: real assume c: "c \ 0" then have "c *\<^sub>R x = (c * cx) *\<^sub>R xx" using x by (simp add: algebra_simps) moreover have "c * cx \ 0" using c x by auto ultimately have "c *\<^sub>R x \ ?rhs" using x by auto } then have "cone ?rhs" unfolding cone_def by auto then have "?rhs \ Collect cone" unfolding mem_Collect_eq by auto { fix x assume "x \ S" then have "1 *\<^sub>R x \ ?rhs" apply auto apply (rule_tac x = 1 in exI, auto) done then have "x \ ?rhs" by auto } then have "S \ ?rhs" by auto then have "?lhs \ ?rhs" using \?rhs \ Collect cone\ hull_minimal[of S "?rhs" "cone"] by auto moreover { fix x assume "x \ ?rhs" then obtain cx :: real and xx where x: "x = cx *\<^sub>R xx" "cx \ 0" "xx \ S" by auto then have "xx \ cone hull S" using hull_subset[of S] by auto then have "x \ ?lhs" using x cone_cone_hull[of S] cone_def[of "cone hull S"] by auto } ultimately show ?thesis by auto qed -subsection \Affine dependence and consequential theorems\ +subsection \Affine Dependence\ text "Formalized by Lars Schewe." definition\<^marker>\tag important\ affine_dependent :: "'a::real_vector set \ bool" where "affine_dependent s \ (\x\s. x \ affine hull (s - {x}))" lemma affine_dependent_subset: "\affine_dependent s; s \ t\ \ affine_dependent t" apply (simp add: affine_dependent_def Bex_def) apply (blast dest: hull_mono [OF Diff_mono [OF _ subset_refl]]) done lemma affine_independent_subset: shows "\\ affine_dependent t; s \ t\ \ \ affine_dependent s" by (metis affine_dependent_subset) lemma affine_independent_Diff: "\ affine_dependent s \ \ affine_dependent(s - t)" by (meson Diff_subset affine_dependent_subset) proposition affine_dependent_explicit: "affine_dependent p \ (\S u. finite S \ S \ p \ sum u S = 0 \ (\v\S. u v \ 0) \ sum (\v. u v *\<^sub>R v) S = 0)" proof - have "\S u. finite S \ S \ p \ sum u S = 0 \ (\v\S. u v \ 0) \ (\w\S. u w *\<^sub>R w) = 0" if "(\w\S. u w *\<^sub>R w) = x" "x \ p" "finite S" "S \ {}" "S \ p - {x}" "sum u S = 1" for x S u proof (intro exI conjI) have "x \ S" using that by auto then show "(\v \ insert x S. if v = x then - 1 else u v) = 0" using that by (simp add: sum_delta_notmem) show "(\w \ insert x S. (if w = x then - 1 else u w) *\<^sub>R w) = 0" using that \x \ S\ by (simp add: if_smult sum_delta_notmem cong: if_cong) qed (use that in auto) moreover have "\x\p. \S u. finite S \ S \ {} \ S \ p - {x} \ sum u S = 1 \ (\v\S. u v *\<^sub>R v) = x" if "(\v\S. u v *\<^sub>R v) = 0" "finite S" "S \ p" "sum u S = 0" "v \ S" "u v \ 0" for S u v proof (intro bexI exI conjI) have "S \ {v}" using that by auto then show "S - {v} \ {}" using that by auto show "(\x \ S - {v}. - (1 / u v) * u x) = 1" unfolding sum_distrib_left[symmetric] sum_diff1[OF \finite S\] by (simp add: that) show "(\x\S - {v}. (- (1 / u v) * u x) *\<^sub>R x) = v" unfolding sum_distrib_left [symmetric] scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] sum_diff1[OF \finite S\] using that by auto show "S - {v} \ p - {v}" using that by auto qed (use that in auto) ultimately show ?thesis unfolding affine_dependent_def affine_hull_explicit by auto qed lemma affine_dependent_explicit_finite: fixes S :: "'a::real_vector set" assumes "finite S" shows "affine_dependent S \ (\u. sum u S = 0 \ (\v\S. u v \ 0) \ sum (\v. u v *\<^sub>R v) S = 0)" (is "?lhs = ?rhs") proof have *: "\vt u v. (if vt then u v else 0) *\<^sub>R v = (if vt then (u v) *\<^sub>R v else 0::'a)" by auto assume ?lhs then obtain t u v where "finite t" "t \ S" "sum u t = 0" "v\t" "u v \ 0" "(\v\t. u v *\<^sub>R v) = 0" unfolding affine_dependent_explicit by auto then show ?rhs apply (rule_tac x="\x. if x\t then u x else 0" in exI) apply (auto simp: * sum.inter_restrict[OF assms, symmetric] Int_absorb1[OF \t\S\]) done next assume ?rhs then obtain u v where "sum u S = 0" "v\S" "u v \ 0" "(\v\S. u v *\<^sub>R v) = 0" by auto then show ?lhs unfolding affine_dependent_explicit using assms by auto qed subsection\<^marker>\tag unimportant\ \Connectedness of convex sets\ lemma connectedD: "connected S \ open A \ open B \ S \ A \ B \ A \ B \ S = {} \ A \ S = {} \ B \ S = {}" by (rule Topological_Spaces.topological_space_class.connectedD) lemma convex_connected: fixes S :: "'a::real_normed_vector set" assumes "convex S" shows "connected S" proof (rule connectedI) fix A B assume "open A" "open B" "A \ B \ S = {}" "S \ A \ B" moreover assume "A \ S \ {}" "B \ S \ {}" then obtain a b where a: "a \ A" "a \ S" and b: "b \ B" "b \ S" by auto define f where [abs_def]: "f u = u *\<^sub>R a + (1 - u) *\<^sub>R b" for u then have "continuous_on {0 .. 1} f" by (auto intro!: continuous_intros) then have "connected (f ` {0 .. 1})" by (auto intro!: connected_continuous_image) note connectedD[OF this, of A B] moreover have "a \ A \ f ` {0 .. 1}" using a by (auto intro!: image_eqI[of _ _ 1] simp: f_def) moreover have "b \ B \ f ` {0 .. 1}" using b by (auto intro!: image_eqI[of _ _ 0] simp: f_def) moreover have "f ` {0 .. 1} \ S" using \convex S\ a b unfolding convex_def f_def by auto ultimately show False by auto qed corollary connected_UNIV[intro]: "connected (UNIV :: 'a::real_normed_vector set)" by (simp add: convex_connected) lemma convex_prod: assumes "\i. i \ Basis \ convex {x. P i x}" shows "convex {x. \i\Basis. P i (x\i)}" using assms unfolding convex_def by (auto simp: inner_add_left) lemma convex_positive_orthant: "convex {x::'a::euclidean_space. (\i\Basis. 0 \ x\i)}" by (rule convex_prod) (simp add: atLeast_def[symmetric] convex_real_interval) subsection \Convex hull\ lemma convex_convex_hull [iff]: "convex (convex hull s)" unfolding hull_def using convex_Inter[of "{t. convex t \ s \ t}"] by auto lemma convex_hull_subset: "s \ convex hull t \ convex hull s \ convex hull t" by (simp add: convex_convex_hull subset_hull) lemma convex_hull_eq: "convex hull s = s \ convex s" by (metis convex_convex_hull hull_same) subsubsection\<^marker>\tag unimportant\ \Convex hull is "preserved" by a linear function\ lemma convex_hull_linear_image: assumes f: "linear f" shows "f ` (convex hull s) = convex hull (f ` s)" proof show "convex hull (f ` s) \ f ` (convex hull s)" by (intro hull_minimal image_mono hull_subset convex_linear_image assms convex_convex_hull) show "f ` (convex hull s) \ convex hull (f ` s)" proof (unfold image_subset_iff_subset_vimage, rule hull_minimal) show "s \ f -` (convex hull (f ` s))" by (fast intro: hull_inc) show "convex (f -` (convex hull (f ` s)))" by (intro convex_linear_vimage [OF f] convex_convex_hull) qed qed lemma in_convex_hull_linear_image: assumes "linear f" and "x \ convex hull s" shows "f x \ convex hull (f ` s)" using convex_hull_linear_image[OF assms(1)] assms(2) by auto lemma convex_hull_Times: "convex hull (s \ t) = (convex hull s) \ (convex hull t)" proof show "convex hull (s \ t) \ (convex hull s) \ (convex hull t)" by (intro hull_minimal Sigma_mono hull_subset convex_Times convex_convex_hull) have "(x, y) \ convex hull (s \ t)" if x: "x \ convex hull s" and y: "y \ convex hull t" for x y proof (rule hull_induct [OF x], rule hull_induct [OF y]) fix x y assume "x \ s" and "y \ t" then show "(x, y) \ convex hull (s \ t)" by (simp add: hull_inc) next fix x let ?S = "((\y. (0, y)) -` (\p. (- x, 0) + p) ` (convex hull s \ t))" have "convex ?S" by (intro convex_linear_vimage convex_translation convex_convex_hull, simp add: linear_iff) also have "?S = {y. (x, y) \ convex hull (s \ t)}" by (auto simp: image_def Bex_def) finally show "convex {y. (x, y) \ convex hull (s \ t)}" . next show "convex {x. (x, y) \ convex hull s \ t}" proof - fix y let ?S = "((\x. (x, 0)) -` (\p. (0, - y) + p) ` (convex hull s \ t))" have "convex ?S" by (intro convex_linear_vimage convex_translation convex_convex_hull, simp add: linear_iff) also have "?S = {x. (x, y) \ convex hull (s \ t)}" by (auto simp: image_def Bex_def) finally show "convex {x. (x, y) \ convex hull (s \ t)}" . qed qed then show "(convex hull s) \ (convex hull t) \ convex hull (s \ t)" unfolding subset_eq split_paired_Ball_Sigma by blast qed subsubsection\<^marker>\tag unimportant\ \Stepping theorems for convex hulls of finite sets\ lemma convex_hull_empty[simp]: "convex hull {} = {}" by (rule hull_unique) auto lemma convex_hull_singleton[simp]: "convex hull {a} = {a}" by (rule hull_unique) auto lemma convex_hull_insert: fixes S :: "'a::real_vector set" assumes "S \ {}" shows "convex hull (insert a S) = {x. \u\0. \v\0. \b. (u + v = 1) \ b \ (convex hull S) \ (x = u *\<^sub>R a + v *\<^sub>R b)}" (is "_ = ?hull") proof (intro equalityI hull_minimal subsetI) fix x assume "x \ insert a S" then have "\u\0. \v\0. u + v = 1 \ (\b. b \ convex hull S \ x = u *\<^sub>R a + v *\<^sub>R b)" unfolding insert_iff proof assume "x = a" then show ?thesis by (rule_tac x=1 in exI) (use assms hull_subset in fastforce) next assume "x \ S" with hull_subset[of S convex] show ?thesis by force qed then show "x \ ?hull" by simp next fix x assume "x \ ?hull" then obtain u v b where obt: "u\0" "v\0" "u + v = 1" "b \ convex hull S" "x = u *\<^sub>R a + v *\<^sub>R b" by auto have "a \ convex hull insert a S" "b \ convex hull insert a S" using hull_mono[of S "insert a S" convex] hull_mono[of "{a}" "insert a S" convex] and obt(4) by auto then show "x \ convex hull insert a S" unfolding obt(5) using obt(1-3) by (rule convexD [OF convex_convex_hull]) next show "convex ?hull" proof (rule convexI) fix x y u v assume as: "(0::real) \ u" "0 \ v" "u + v = 1" and x: "x \ ?hull" and y: "y \ ?hull" from x obtain u1 v1 b1 where obt1: "u1\0" "v1\0" "u1 + v1 = 1" "b1 \ convex hull S" and xeq: "x = u1 *\<^sub>R a + v1 *\<^sub>R b1" by auto from y obtain u2 v2 b2 where obt2: "u2\0" "v2\0" "u2 + v2 = 1" "b2 \ convex hull S" and yeq: "y = u2 *\<^sub>R a + v2 *\<^sub>R b2" by auto have *: "\(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x" by (auto simp: algebra_simps) have "\b \ convex hull S. u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)" proof (cases "u * v1 + v * v2 = 0") case True have *: "\(x::'a) s1 s2. x - s1 *\<^sub>R x - s2 *\<^sub>R x = ((1::real) - (s1 + s2)) *\<^sub>R x" by (auto simp: algebra_simps) have eq0: "u * v1 = 0" "v * v2 = 0" using True mult_nonneg_nonneg[OF \u\0\ \v1\0\] mult_nonneg_nonneg[OF \v\0\ \v2\0\] by arith+ then have "u * u1 + v * u2 = 1" using as(3) obt1(3) obt2(3) by auto then show ?thesis using "*" eq0 as obt1(4) xeq yeq by auto next case False have "1 - (u * u1 + v * u2) = (u + v) - (u * u1 + v * u2)" using as(3) obt1(3) obt2(3) by (auto simp: field_simps) also have "\ = u * (v1 + u1 - u1) + v * (v2 + u2 - u2)" using as(3) obt1(3) obt2(3) by (auto simp: field_simps) also have "\ = u * v1 + v * v2" by simp finally have **:"1 - (u * u1 + v * u2) = u * v1 + v * v2" by auto let ?b = "((u * v1) / (u * v1 + v * v2)) *\<^sub>R b1 + ((v * v2) / (u * v1 + v * v2)) *\<^sub>R b2" have zeroes: "0 \ u * v1 + v * v2" "0 \ u * v1" "0 \ u * v1 + v * v2" "0 \ v * v2" using as(1,2) obt1(1,2) obt2(1,2) by auto show ?thesis proof show "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (?b - (u * u1) *\<^sub>R ?b - (v * u2) *\<^sub>R ?b)" unfolding xeq yeq * ** using False by (auto simp: scaleR_left_distrib scaleR_right_distrib) show "?b \ convex hull S" using False zeroes obt1(4) obt2(4) by (auto simp: convexD [OF convex_convex_hull] scaleR_left_distrib scaleR_right_distrib add_divide_distrib[symmetric] zero_le_divide_iff) qed qed then obtain b where b: "b \ convex hull S" "u *\<^sub>R x + v *\<^sub>R y = (u * u1) *\<^sub>R a + (v * u2) *\<^sub>R a + (b - (u * u1) *\<^sub>R b - (v * u2) *\<^sub>R b)" .. have u1: "u1 \ 1" unfolding obt1(3)[symmetric] and not_le using obt1(2) by auto have u2: "u2 \ 1" unfolding obt2(3)[symmetric] and not_le using obt2(2) by auto have "u1 * u + u2 * v \ max u1 u2 * u + max u1 u2 * v" proof (rule add_mono) show "u1 * u \ max u1 u2 * u" "u2 * v \ max u1 u2 * v" by (simp_all add: as mult_right_mono) qed also have "\ \ 1" unfolding distrib_left[symmetric] and as(3) using u1 u2 by auto finally have le1: "u1 * u + u2 * v \ 1" . show "u *\<^sub>R x + v *\<^sub>R y \ ?hull" proof (intro CollectI exI conjI) show "0 \ u * u1 + v * u2" by (simp add: as(1) as(2) obt1(1) obt2(1)) show "0 \ 1 - u * u1 - v * u2" by (simp add: le1 diff_diff_add mult.commute) qed (use b in \auto simp: algebra_simps\) qed qed lemma convex_hull_insert_alt: "convex hull (insert a S) = (if S = {} then {a} else {(1 - u) *\<^sub>R a + u *\<^sub>R x |x u. 0 \ u \ u \ 1 \ x \ convex hull S})" apply (auto simp: convex_hull_insert) using diff_eq_eq apply fastforce by (metis add.group_left_neutral add_le_imp_le_diff diff_add_cancel) subsubsection\<^marker>\tag unimportant\ \Explicit expression for convex hull\ proposition convex_hull_indexed: fixes S :: "'a::real_vector set" shows "convex hull S = {y. \k u x. (\i\{1::nat .. k}. 0 \ u i \ x i \ S) \ (sum u {1..k} = 1) \ (\i = 1..k. u i *\<^sub>R x i) = y}" (is "?xyz = ?hull") proof (rule hull_unique [OF _ convexI]) show "S \ ?hull" by (clarsimp, rule_tac x=1 in exI, rule_tac x="\x. 1" in exI, auto) next fix T assume "S \ T" "convex T" then show "?hull \ T" by (blast intro: convex_sum) next fix x y u v assume uv: "0 \ u" "0 \ v" "u + v = (1::real)" assume xy: "x \ ?hull" "y \ ?hull" from xy obtain k1 u1 x1 where x [rule_format]: "\i\{1::nat..k1}. 0\u1 i \ x1 i \ S" "sum u1 {Suc 0..k1} = 1" "(\i = Suc 0..k1. u1 i *\<^sub>R x1 i) = x" by auto from xy obtain k2 u2 x2 where y [rule_format]: "\i\{1::nat..k2}. 0\u2 i \ x2 i \ S" "sum u2 {Suc 0..k2} = 1" "(\i = Suc 0..k2. u2 i *\<^sub>R x2 i) = y" by auto have *: "\P (x::'a) y s t i. (if P i then s else t) *\<^sub>R (if P i then x else y) = (if P i then s *\<^sub>R x else t *\<^sub>R y)" "{1..k1 + k2} \ {1..k1} = {1..k1}" "{1..k1 + k2} \ - {1..k1} = (\i. i + k1) ` {1..k2}" by auto have inj: "inj_on (\i. i + k1) {1..k2}" unfolding inj_on_def by auto let ?uu = "\i. if i \ {1..k1} then u * u1 i else v * u2 (i - k1)" let ?xx = "\i. if i \ {1..k1} then x1 i else x2 (i - k1)" show "u *\<^sub>R x + v *\<^sub>R y \ ?hull" proof (intro CollectI exI conjI ballI) show "0 \ ?uu i" "?xx i \ S" if "i \ {1..k1+k2}" for i using that by (auto simp add: le_diff_conv uv(1) x(1) uv(2) y(1)) show "(\i = 1..k1 + k2. ?uu i) = 1" "(\i = 1..k1 + k2. ?uu i *\<^sub>R ?xx i) = u *\<^sub>R x + v *\<^sub>R y" unfolding * sum.If_cases[OF finite_atLeastAtMost[of 1 "k1 + k2"]] sum.reindex[OF inj] Collect_mem_eq o_def unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] sum_distrib_left[symmetric] by (simp_all add: sum_distrib_left[symmetric] x(2,3) y(2,3) uv(3)) qed qed lemma convex_hull_finite: fixes S :: "'a::real_vector set" assumes "finite S" shows "convex hull S = {y. \u. (\x\S. 0 \ u x) \ sum u S = 1 \ sum (\x. u x *\<^sub>R x) S = y}" (is "?HULL = _") proof (rule hull_unique [OF _ convexI]; clarify) fix x assume "x \ S" then show "\u. (\x\S. 0 \ u x) \ sum u S = 1 \ (\x\S. u x *\<^sub>R x) = x" by (rule_tac x="\y. if x=y then 1 else 0" in exI) (auto simp: sum.delta'[OF assms] sum_delta''[OF assms]) next fix u v :: real assume uv: "0 \ u" "0 \ v" "u + v = 1" fix ux assume ux [rule_format]: "\x\S. 0 \ ux x" "sum ux S = (1::real)" fix uy assume uy [rule_format]: "\x\S. 0 \ uy x" "sum uy S = (1::real)" have "0 \ u * ux x + v * uy x" if "x\S" for x by (simp add: that uv ux(1) uy(1)) moreover have "(\x\S. u * ux x + v * uy x) = 1" unfolding sum.distrib and sum_distrib_left[symmetric] ux(2) uy(2) using uv(3) by auto moreover have "(\x\S. (u * ux x + v * uy x) *\<^sub>R x) = u *\<^sub>R (\x\S. ux x *\<^sub>R x) + v *\<^sub>R (\x\S. uy x *\<^sub>R x)" unfolding scaleR_left_distrib sum.distrib scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] by auto ultimately show "\uc. (\x\S. 0 \ uc x) \ sum uc S = 1 \ (\x\S. uc x *\<^sub>R x) = u *\<^sub>R (\x\S. ux x *\<^sub>R x) + v *\<^sub>R (\x\S. uy x *\<^sub>R x)" by (rule_tac x="\x. u * ux x + v * uy x" in exI, auto) qed (use assms in \auto simp: convex_explicit\) subsubsection\<^marker>\tag unimportant\ \Another formulation\ text "Formalized by Lars Schewe." lemma convex_hull_explicit: fixes p :: "'a::real_vector set" shows "convex hull p = {y. \S u. finite S \ S \ p \ (\x\S. 0 \ u x) \ sum u S = 1 \ sum (\v. u v *\<^sub>R v) S = y}" (is "?lhs = ?rhs") proof - { fix x assume "x\?lhs" then obtain k u y where obt: "\i\{1::nat..k}. 0 \ u i \ y i \ p" "sum u {1..k} = 1" "(\i = 1..k. u i *\<^sub>R y i) = x" unfolding convex_hull_indexed by auto have fin: "finite {1..k}" by auto have fin': "\v. finite {i \ {1..k}. y i = v}" by auto { fix j assume "j\{1..k}" then have "y j \ p" "0 \ sum u {i. Suc 0 \ i \ i \ k \ y i = y j}" using obt(1)[THEN bspec[where x=j]] and obt(2) apply simp apply (rule sum_nonneg) using obt(1) apply auto done } moreover have "(\v\y ` {1..k}. sum u {i \ {1..k}. y i = v}) = 1" unfolding sum.image_gen[OF fin, symmetric] using obt(2) by auto moreover have "(\v\y ` {1..k}. sum u {i \ {1..k}. y i = v} *\<^sub>R v) = x" using sum.image_gen[OF fin, of "\i. u i *\<^sub>R y i" y, symmetric] unfolding scaleR_left.sum using obt(3) by auto ultimately have "\S u. finite S \ S \ p \ (\x\S. 0 \ u x) \ sum u S = 1 \ (\v\S. u v *\<^sub>R v) = x" apply (rule_tac x="y ` {1..k}" in exI) apply (rule_tac x="\v. sum u {i\{1..k}. y i = v}" in exI, auto) done then have "x\?rhs" by auto } moreover { fix y assume "y\?rhs" then obtain S u where obt: "finite S" "S \ p" "\x\S. 0 \ u x" "sum u S = 1" "(\v\S. u v *\<^sub>R v) = y" by auto obtain f where f: "inj_on f {1..card S}" "f ` {1..card S} = S" using ex_bij_betw_nat_finite_1[OF obt(1)] unfolding bij_betw_def by auto { fix i :: nat assume "i\{1..card S}" then have "f i \ S" using f(2) by blast then have "0 \ u (f i)" "f i \ p" using obt(2,3) by auto } moreover have *: "finite {1..card S}" by auto { fix y assume "y\S" then obtain i where "i\{1..card S}" "f i = y" using f using image_iff[of y f "{1..card S}"] by auto then have "{x. Suc 0 \ x \ x \ card S \ f x = y} = {i}" apply auto using f(1)[unfolded inj_on_def] by (metis One_nat_def atLeastAtMost_iff) then have "card {x. Suc 0 \ x \ x \ card S \ f x = y} = 1" by auto then have "(\x\{x \ {1..card S}. f x = y}. u (f x)) = u y" "(\x\{x \ {1..card S}. f x = y}. u (f x) *\<^sub>R f x) = u y *\<^sub>R y" by (auto simp: sum_constant_scaleR) } then have "(\x = 1..card S. u (f x)) = 1" "(\i = 1..card S. u (f i) *\<^sub>R f i) = y" unfolding sum.image_gen[OF *(1), of "\x. u (f x) *\<^sub>R f x" f] and sum.image_gen[OF *(1), of "\x. u (f x)" f] unfolding f using sum.cong [of S S "\y. (\x\{x \ {1..card S}. f x = y}. u (f x) *\<^sub>R f x)" "\v. u v *\<^sub>R v"] using sum.cong [of S S "\y. (\x\{x \ {1..card S}. f x = y}. u (f x))" u] unfolding obt(4,5) by auto ultimately have "\k u x. (\i\{1..k}. 0 \ u i \ x i \ p) \ sum u {1..k} = 1 \ (\i::nat = 1..k. u i *\<^sub>R x i) = y" apply (rule_tac x="card S" in exI) apply (rule_tac x="u \ f" in exI) apply (rule_tac x=f in exI, fastforce) done then have "y \ ?lhs" unfolding convex_hull_indexed by auto } ultimately show ?thesis unfolding set_eq_iff by blast qed subsubsection\<^marker>\tag unimportant\ \A stepping theorem for that expansion\ lemma convex_hull_finite_step: fixes S :: "'a::real_vector set" assumes "finite S" shows "(\u. (\x\insert a S. 0 \ u x) \ sum u (insert a S) = w \ sum (\x. u x *\<^sub>R x) (insert a S) = y) \ (\v\0. \u. (\x\S. 0 \ u x) \ sum u S = w - v \ sum (\x. u x *\<^sub>R x) S = y - v *\<^sub>R a)" (is "?lhs = ?rhs") proof (rule, case_tac[!] "a\S") assume "a \ S" then have *: "insert a S = S" by auto assume ?lhs then show ?rhs unfolding * by (rule_tac x=0 in exI, auto) next assume ?lhs then obtain u where u: "\x\insert a S. 0 \ u x" "sum u (insert a S) = w" "(\x\insert a S. u x *\<^sub>R x) = y" by auto assume "a \ S" then show ?rhs apply (rule_tac x="u a" in exI) using u(1)[THEN bspec[where x=a]] apply simp apply (rule_tac x=u in exI) using u[unfolded sum_clauses(2)[OF assms]] and \a\S\ apply auto done next assume "a \ S" then have *: "insert a S = S" by auto have fin: "finite (insert a S)" using assms by auto assume ?rhs then obtain v u where uv: "v\0" "\x\S. 0 \ u x" "sum u S = w - v" "(\x\S. u x *\<^sub>R x) = y - v *\<^sub>R a" by auto show ?lhs apply (rule_tac x = "\x. (if a = x then v else 0) + u x" in exI) unfolding scaleR_left_distrib and sum.distrib and sum_delta''[OF fin] and sum.delta'[OF fin] unfolding sum_clauses(2)[OF assms] using uv and uv(2)[THEN bspec[where x=a]] and \a\S\ apply auto done next assume ?rhs then obtain v u where uv: "v\0" "\x\S. 0 \ u x" "sum u S = w - v" "(\x\S. u x *\<^sub>R x) = y - v *\<^sub>R a" by auto moreover assume "a \ S" moreover have "(\x\S. if a = x then v else u x) = sum u S" "(\x\S. (if a = x then v else u x) *\<^sub>R x) = (\x\S. u x *\<^sub>R x)" using \a \ S\ by (auto simp: intro!: sum.cong) ultimately show ?lhs by (rule_tac x="\x. if a = x then v else u x" in exI) (auto simp: sum_clauses(2)[OF assms]) qed subsubsection\<^marker>\tag unimportant\ \Hence some special cases\ lemma convex_hull_2: "convex hull {a,b} = {u *\<^sub>R a + v *\<^sub>R b | u v. 0 \ u \ 0 \ v \ u + v = 1}" proof - have *: "\u. (\x\{a, b}. 0 \ u x) \ 0 \ u a \ 0 \ u b" by auto have **: "finite {b}" by auto show ?thesis apply (simp add: convex_hull_finite) unfolding convex_hull_finite_step[OF **, of a 1, unfolded * conj_assoc] apply auto apply (rule_tac x=v in exI) apply (rule_tac x="1 - v" in exI, simp) apply (rule_tac x=u in exI, simp) apply (rule_tac x="\x. v" in exI, simp) done qed lemma convex_hull_2_alt: "convex hull {a,b} = {a + u *\<^sub>R (b - a) | u. 0 \ u \ u \ 1}" unfolding convex_hull_2 proof (rule Collect_cong) have *: "\x y ::real. x + y = 1 \ x = 1 - y" by auto fix x show "(\v u. x = v *\<^sub>R a + u *\<^sub>R b \ 0 \ v \ 0 \ u \ v + u = 1) \ (\u. x = a + u *\<^sub>R (b - a) \ 0 \ u \ u \ 1)" unfolding * apply auto apply (rule_tac[!] x=u in exI) apply (auto simp: algebra_simps) done qed lemma convex_hull_3: "convex hull {a,b,c} = { u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c | u v w. 0 \ u \ 0 \ v \ 0 \ w \ u + v + w = 1}" proof - have fin: "finite {a,b,c}" "finite {b,c}" "finite {c}" by auto have *: "\x y z ::real. x + y + z = 1 \ x = 1 - y - z" by (auto simp: field_simps) show ?thesis unfolding convex_hull_finite[OF fin(1)] and convex_hull_finite_step[OF fin(2)] and * unfolding convex_hull_finite_step[OF fin(3)] apply (rule Collect_cong, simp) apply auto apply (rule_tac x=va in exI) apply (rule_tac x="u c" in exI, simp) apply (rule_tac x="1 - v - w" in exI, simp) apply (rule_tac x=v in exI, simp) apply (rule_tac x="\x. w" in exI, simp) done qed lemma convex_hull_3_alt: "convex hull {a,b,c} = {a + u *\<^sub>R (b - a) + v *\<^sub>R (c - a) | u v. 0 \ u \ 0 \ v \ u + v \ 1}" proof - have *: "\x y z ::real. x + y + z = 1 \ x = 1 - y - z" by auto show ?thesis unfolding convex_hull_3 apply (auto simp: *) apply (rule_tac x=v in exI) apply (rule_tac x=w in exI) apply (simp add: algebra_simps) apply (rule_tac x=u in exI) apply (rule_tac x=v in exI) apply (simp add: algebra_simps) done qed subsection\<^marker>\tag unimportant\ \Relations among closure notions and corresponding hulls\ lemma affine_imp_convex: "affine s \ convex s" unfolding affine_def convex_def by auto lemma convex_affine_hull [simp]: "convex (affine hull S)" by (simp add: affine_imp_convex) lemma subspace_imp_convex: "subspace s \ convex s" using subspace_imp_affine affine_imp_convex by auto lemma affine_hull_subset_span: "(affine hull s) \ (span s)" by (metis hull_minimal span_superset subspace_imp_affine subspace_span) lemma convex_hull_subset_span: "(convex hull s) \ (span s)" by (metis hull_minimal span_superset subspace_imp_convex subspace_span) lemma convex_hull_subset_affine_hull: "(convex hull s) \ (affine hull s)" by (metis affine_affine_hull affine_imp_convex hull_minimal hull_subset) lemma affine_dependent_imp_dependent: "affine_dependent s \ dependent s" unfolding affine_dependent_def dependent_def using affine_hull_subset_span by auto lemma dependent_imp_affine_dependent: assumes "dependent {x - a| x . x \ s}" and "a \ s" shows "affine_dependent (insert a s)" proof - from assms(1)[unfolded dependent_explicit] obtain S u v where obt: "finite S" "S \ {x - a |x. x \ s}" "v\S" "u v \ 0" "(\v\S. u v *\<^sub>R v) = 0" by auto define t where "t = (\x. x + a) ` S" have inj: "inj_on (\x. x + a) S" unfolding inj_on_def by auto have "0 \ S" using obt(2) assms(2) unfolding subset_eq by auto have fin: "finite t" and "t \ s" unfolding t_def using obt(1,2) by auto then have "finite (insert a t)" and "insert a t \ insert a s" by auto moreover have *: "\P Q. (\x\t. (if x = a then P x else Q x)) = (\x\t. Q x)" apply (rule sum.cong) using \a\s\ \t\s\ apply auto done have "(\x\insert a t. if x = a then - (\x\t. u (x - a)) else u (x - a)) = 0" unfolding sum_clauses(2)[OF fin] * using \a\s\ \t\s\ by auto moreover have "\v\insert a t. (if v = a then - (\x\t. u (x - a)) else u (v - a)) \ 0" using obt(3,4) \0\S\ by (rule_tac x="v + a" in bexI) (auto simp: t_def) moreover have *: "\P Q. (\x\t. (if x = a then P x else Q x) *\<^sub>R x) = (\x\t. Q x *\<^sub>R x)" using \a\s\ \t\s\ by (auto intro!: sum.cong) have "(\x\t. u (x - a)) *\<^sub>R a = (\v\t. u (v - a) *\<^sub>R v)" unfolding scaleR_left.sum unfolding t_def and sum.reindex[OF inj] and o_def using obt(5) by (auto simp: sum.distrib scaleR_right_distrib) then have "(\v\insert a t. (if v = a then - (\x\t. u (x - a)) else u (v - a)) *\<^sub>R v) = 0" unfolding sum_clauses(2)[OF fin] using \a\s\ \t\s\ by (auto simp: *) ultimately show ?thesis unfolding affine_dependent_explicit apply (rule_tac x="insert a t" in exI, auto) done qed lemma convex_cone: "convex s \ cone s \ (\x\s. \y\s. (x + y) \ s) \ (\x\s. \c\0. (c *\<^sub>R x) \ s)" (is "?lhs = ?rhs") proof - { fix x y assume "x\s" "y\s" and ?lhs then have "2 *\<^sub>R x \s" "2 *\<^sub>R y \ s" unfolding cone_def by auto then have "x + y \ s" using \?lhs\[unfolded convex_def, THEN conjunct1] apply (erule_tac x="2*\<^sub>R x" in ballE) apply (erule_tac x="2*\<^sub>R y" in ballE) apply (erule_tac x="1/2" in allE, simp) apply (erule_tac x="1/2" in allE, auto) done } then show ?thesis unfolding convex_def cone_def by blast qed lemma affine_dependent_biggerset: fixes s :: "'a::euclidean_space set" assumes "finite s" "card s \ DIM('a) + 2" shows "affine_dependent s" proof - have "s \ {}" using assms by auto then obtain a where "a\s" by auto have *: "{x - a |x. x \ s - {a}} = (\x. x - a) ` (s - {a})" by auto have "card {x - a |x. x \ s - {a}} = card (s - {a})" unfolding * by (simp add: card_image inj_on_def) also have "\ > DIM('a)" using assms(2) unfolding card_Diff_singleton[OF assms(1) \a\s\] by auto finally show ?thesis apply (subst insert_Diff[OF \a\s\, symmetric]) apply (rule dependent_imp_affine_dependent) apply (rule dependent_biggerset, auto) done qed lemma affine_dependent_biggerset_general: assumes "finite (S :: 'a::euclidean_space set)" and "card S \ dim S + 2" shows "affine_dependent S" proof - from assms(2) have "S \ {}" by auto then obtain a where "a\S" by auto have *: "{x - a |x. x \ S - {a}} = (\x. x - a) ` (S - {a})" by auto have **: "card {x - a |x. x \ S - {a}} = card (S - {a})" by (metis (no_types, lifting) "*" card_image diff_add_cancel inj_on_def) have "dim {x - a |x. x \ S - {a}} \ dim S" using \a\S\ by (auto simp: span_base span_diff intro: subset_le_dim) also have "\ < dim S + 1" by auto also have "\ \ card (S - {a})" using assms using card_Diff_singleton[OF assms(1) \a\S\] by auto finally show ?thesis apply (subst insert_Diff[OF \a\S\, symmetric]) apply (rule dependent_imp_affine_dependent) apply (rule dependent_biggerset_general) unfolding ** apply auto done qed subsection\<^marker>\tag unimportant\ \Some Properties of Affine Dependent Sets\ lemma affine_independent_0 [simp]: "\ affine_dependent {}" by (simp add: affine_dependent_def) lemma affine_independent_1 [simp]: "\ affine_dependent {a}" by (simp add: affine_dependent_def) lemma affine_independent_2 [simp]: "\ affine_dependent {a,b}" by (simp add: affine_dependent_def insert_Diff_if hull_same) lemma affine_hull_translation: "affine hull ((\x. a + x) ` S) = (\x. a + x) ` (affine hull S)" proof - have "affine ((\x. a + x) ` (affine hull S))" using affine_translation affine_affine_hull by blast moreover have "(\x. a + x) ` S \ (\x. a + x) ` (affine hull S)" using hull_subset[of S] by auto ultimately have h1: "affine hull ((\x. a + x) ` S) \ (\x. a + x) ` (affine hull S)" by (metis hull_minimal) have "affine((\x. -a + x) ` (affine hull ((\x. a + x) ` S)))" using affine_translation affine_affine_hull by blast moreover have "(\x. -a + x) ` (\x. a + x) ` S \ (\x. -a + x) ` (affine hull ((\x. a + x) ` S))" using hull_subset[of "(\x. a + x) ` S"] by auto moreover have "S = (\x. -a + x) ` (\x. a + x) ` S" using translation_assoc[of "-a" a] by auto ultimately have "(\x. -a + x) ` (affine hull ((\x. a + x) ` S)) >= (affine hull S)" by (metis hull_minimal) then have "affine hull ((\x. a + x) ` S) >= (\x. a + x) ` (affine hull S)" by auto then show ?thesis using h1 by auto qed lemma affine_dependent_translation: assumes "affine_dependent S" shows "affine_dependent ((\x. a + x) ` S)" proof - obtain x where x: "x \ S \ x \ affine hull (S - {x})" using assms affine_dependent_def by auto have "(+) a ` (S - {x}) = (+) a ` S - {a + x}" by auto then have "a + x \ affine hull ((\x. a + x) ` S - {a + x})" using affine_hull_translation[of a "S - {x}"] x by auto moreover have "a + x \ (\x. a + x) ` S" using x by auto ultimately show ?thesis unfolding affine_dependent_def by auto qed lemma affine_dependent_translation_eq: "affine_dependent S \ affine_dependent ((\x. a + x) ` S)" proof - { assume "affine_dependent ((\x. a + x) ` S)" then have "affine_dependent S" using affine_dependent_translation[of "((\x. a + x) ` S)" "-a"] translation_assoc[of "-a" a] by auto } then show ?thesis using affine_dependent_translation by auto qed lemma affine_hull_0_dependent: assumes "0 \ affine hull S" shows "dependent S" proof - obtain s u where s_u: "finite s \ s \ {} \ s \ S \ sum u s = 1 \ (\v\s. u v *\<^sub>R v) = 0" using assms affine_hull_explicit[of S] by auto then have "\v\s. u v \ 0" by auto then have "finite s \ s \ S \ (\v\s. u v \ 0 \ (\v\s. u v *\<^sub>R v) = 0)" using s_u by auto then show ?thesis unfolding dependent_explicit[of S] by auto qed lemma affine_dependent_imp_dependent2: assumes "affine_dependent (insert 0 S)" shows "dependent S" proof - obtain x where x: "x \ insert 0 S \ x \ affine hull (insert 0 S - {x})" using affine_dependent_def[of "(insert 0 S)"] assms by blast then have "x \ span (insert 0 S - {x})" using affine_hull_subset_span by auto moreover have "span (insert 0 S - {x}) = span (S - {x})" using insert_Diff_if[of "0" S "{x}"] span_insert_0[of "S-{x}"] by auto ultimately have "x \ span (S - {x})" by auto then have "x \ 0 \ dependent S" using x dependent_def by auto moreover { assume "x = 0" then have "0 \ affine hull S" using x hull_mono[of "S - {0}" S] by auto then have "dependent S" using affine_hull_0_dependent by auto } ultimately show ?thesis by auto qed lemma affine_dependent_iff_dependent: assumes "a \ S" shows "affine_dependent (insert a S) \ dependent ((\x. -a + x) ` S)" proof - have "((+) (- a) ` S) = {x - a| x . x \ S}" by auto then show ?thesis using affine_dependent_translation_eq[of "(insert a S)" "-a"] affine_dependent_imp_dependent2 assms dependent_imp_affine_dependent[of a S] by (auto simp del: uminus_add_conv_diff) qed lemma affine_dependent_iff_dependent2: assumes "a \ S" shows "affine_dependent S \ dependent ((\x. -a + x) ` (S-{a}))" proof - have "insert a (S - {a}) = S" using assms by auto then show ?thesis using assms affine_dependent_iff_dependent[of a "S-{a}"] by auto qed lemma affine_hull_insert_span_gen: "affine hull (insert a s) = (\x. a + x) ` span ((\x. - a + x) ` s)" proof - have h1: "{x - a |x. x \ s} = ((\x. -a+x) ` s)" by auto { assume "a \ s" then have ?thesis using affine_hull_insert_span[of a s] h1 by auto } moreover { assume a1: "a \ s" have "\x. x \ s \ -a+x=0" apply (rule exI[of _ a]) using a1 apply auto done then have "insert 0 ((\x. -a+x) ` (s - {a})) = (\x. -a+x) ` s" by auto then have "span ((\x. -a+x) ` (s - {a}))=span ((\x. -a+x) ` s)" using span_insert_0[of "(+) (- a) ` (s - {a})"] by (auto simp del: uminus_add_conv_diff) moreover have "{x - a |x. x \ (s - {a})} = ((\x. -a+x) ` (s - {a}))" by auto moreover have "insert a (s - {a}) = insert a s" by auto ultimately have ?thesis using affine_hull_insert_span[of "a" "s-{a}"] by auto } ultimately show ?thesis by auto qed lemma affine_hull_span2: assumes "a \ s" shows "affine hull s = (\x. a+x) ` span ((\x. -a+x) ` (s-{a}))" using affine_hull_insert_span_gen[of a "s - {a}", unfolded insert_Diff[OF assms]] by auto lemma affine_hull_span_gen: assumes "a \ affine hull s" shows "affine hull s = (\x. a+x) ` span ((\x. -a+x) ` s)" proof - have "affine hull (insert a s) = affine hull s" using hull_redundant[of a affine s] assms by auto then show ?thesis using affine_hull_insert_span_gen[of a "s"] by auto qed lemma affine_hull_span_0: assumes "0 \ affine hull S" shows "affine hull S = span S" using affine_hull_span_gen[of "0" S] assms by auto lemma extend_to_affine_basis_nonempty: fixes S V :: "'n::euclidean_space set" assumes "\ affine_dependent S" "S \ V" "S \ {}" shows "\T. \ affine_dependent T \ S \ T \ T \ V \ affine hull T = affine hull V" proof - obtain a where a: "a \ S" using assms by auto then have h0: "independent ((\x. -a + x) ` (S-{a}))" using affine_dependent_iff_dependent2 assms by auto obtain B where B: "(\x. -a+x) ` (S - {a}) \ B \ B \ (\x. -a+x) ` V \ independent B \ (\x. -a+x) ` V \ span B" using assms by (blast intro: maximal_independent_subset_extend[OF _ h0, of "(\x. -a + x) ` V"]) define T where "T = (\x. a+x) ` insert 0 B" then have "T = insert a ((\x. a+x) ` B)" by auto then have "affine hull T = (\x. a+x) ` span B" using affine_hull_insert_span_gen[of a "((\x. a+x) ` B)"] translation_assoc[of "-a" a B] by auto then have "V \ affine hull T" using B assms translation_inverse_subset[of a V "span B"] by auto moreover have "T \ V" using T_def B a assms by auto ultimately have "affine hull T = affine hull V" by (metis Int_absorb1 Int_absorb2 hull_hull hull_mono) moreover have "S \ T" using T_def B translation_inverse_subset[of a "S-{a}" B] by auto moreover have "\ affine_dependent T" using T_def affine_dependent_translation_eq[of "insert 0 B"] affine_dependent_imp_dependent2 B by auto ultimately show ?thesis using \T \ V\ by auto qed lemma affine_basis_exists: fixes V :: "'n::euclidean_space set" shows "\B. B \ V \ \ affine_dependent B \ affine hull V = affine hull B" proof (cases "V = {}") case True then show ?thesis using affine_independent_0 by auto next case False then obtain x where "x \ V" by auto then show ?thesis using affine_dependent_def[of "{x}"] extend_to_affine_basis_nonempty[of "{x}" V] by auto qed proposition extend_to_affine_basis: fixes S V :: "'n::euclidean_space set" assumes "\ affine_dependent S" "S \ V" obtains T where "\ affine_dependent T" "S \ T" "T \ V" "affine hull T = affine hull V" proof (cases "S = {}") case True then show ?thesis using affine_basis_exists by (metis empty_subsetI that) next case False then show ?thesis by (metis assms extend_to_affine_basis_nonempty that) qed subsection \Affine Dimension of a Set\ definition\<^marker>\tag important\ aff_dim :: "('a::euclidean_space) set \ int" where "aff_dim V = (SOME d :: int. \B. affine hull B = affine hull V \ \ affine_dependent B \ of_nat (card B) = d + 1)" lemma aff_dim_basis_exists: fixes V :: "('n::euclidean_space) set" shows "\B. affine hull B = affine hull V \ \ affine_dependent B \ of_nat (card B) = aff_dim V + 1" proof - obtain B where "\ affine_dependent B \ affine hull B = affine hull V" using affine_basis_exists[of V] by auto then show ?thesis unfolding aff_dim_def some_eq_ex[of "\d. \B. affine hull B = affine hull V \ \ affine_dependent B \ of_nat (card B) = d + 1"] apply auto apply (rule exI[of _ "int (card B) - (1 :: int)"]) apply (rule exI[of _ "B"], auto) done qed lemma affine_hull_nonempty: "S \ {} \ affine hull S \ {}" proof - have "S = {} \ affine hull S = {}" using affine_hull_empty by auto moreover have "affine hull S = {} \ S = {}" unfolding hull_def by auto ultimately show ?thesis by blast qed lemma aff_dim_parallel_subspace_aux: fixes B :: "'n::euclidean_space set" assumes "\ affine_dependent B" "a \ B" shows "finite B \ ((card B) - 1 = dim (span ((\x. -a+x) ` (B-{a}))))" proof - have "independent ((\x. -a + x) ` (B-{a}))" using affine_dependent_iff_dependent2 assms by auto then have fin: "dim (span ((\x. -a+x) ` (B-{a}))) = card ((\x. -a + x) ` (B-{a}))" "finite ((\x. -a + x) ` (B - {a}))" using indep_card_eq_dim_span[of "(\x. -a+x) ` (B-{a})"] by auto show ?thesis proof (cases "(\x. -a + x) ` (B - {a}) = {}") case True have "B = insert a ((\x. a + x) ` (\x. -a + x) ` (B - {a}))" using translation_assoc[of "a" "-a" "(B - {a})"] assms by auto then have "B = {a}" using True by auto then show ?thesis using assms fin by auto next case False then have "card ((\x. -a + x) ` (B - {a})) > 0" using fin by auto moreover have h1: "card ((\x. -a + x) ` (B-{a})) = card (B-{a})" by (rule card_image) (use translate_inj_on in blast) ultimately have "card (B-{a}) > 0" by auto then have *: "finite (B - {a})" using card_gt_0_iff[of "(B - {a})"] by auto then have "card (B - {a}) = card B - 1" using card_Diff_singleton assms by auto with * show ?thesis using fin h1 by auto qed qed lemma aff_dim_parallel_subspace: fixes V L :: "'n::euclidean_space set" assumes "V \ {}" and "subspace L" and "affine_parallel (affine hull V) L" shows "aff_dim V = int (dim L)" proof - obtain B where B: "affine hull B = affine hull V \ \ affine_dependent B \ int (card B) = aff_dim V + 1" using aff_dim_basis_exists by auto then have "B \ {}" using assms B affine_hull_nonempty[of V] affine_hull_nonempty[of B] by auto then obtain a where a: "a \ B" by auto define Lb where "Lb = span ((\x. -a+x) ` (B-{a}))" moreover have "affine_parallel (affine hull B) Lb" using Lb_def B assms affine_hull_span2[of a B] a affine_parallel_commut[of "Lb" "(affine hull B)"] unfolding affine_parallel_def by auto moreover have "subspace Lb" using Lb_def subspace_span by auto moreover have "affine hull B \ {}" using assms B affine_hull_nonempty[of V] by auto ultimately have "L = Lb" using assms affine_parallel_subspace[of "affine hull B"] affine_affine_hull[of B] B by auto then have "dim L = dim Lb" by auto moreover have "card B - 1 = dim Lb" and "finite B" using Lb_def aff_dim_parallel_subspace_aux a B by auto ultimately show ?thesis using B \B \ {}\ card_gt_0_iff[of B] by auto qed lemma aff_independent_finite: fixes B :: "'n::euclidean_space set" assumes "\ affine_dependent B" shows "finite B" proof - { assume "B \ {}" then obtain a where "a \ B" by auto then have ?thesis using aff_dim_parallel_subspace_aux assms by auto } then show ?thesis by auto qed lemmas independent_finite = independent_imp_finite lemma span_substd_basis: assumes d: "d \ Basis" shows "span d = {x. \i\Basis. i \ d \ x\i = 0}" (is "_ = ?B") proof - have "d \ ?B" using d by (auto simp: inner_Basis) moreover have s: "subspace ?B" using subspace_substandard[of "\i. i \ d"] . ultimately have "span d \ ?B" using span_mono[of d "?B"] span_eq_iff[of "?B"] by blast moreover have *: "card d \ dim (span d)" using independent_card_le_dim[of d "span d"] independent_substdbasis[OF assms] span_superset[of d] by auto moreover from * have "dim ?B \ dim (span d)" using dim_substandard[OF assms] by auto ultimately show ?thesis using s subspace_dim_equal[of "span d" "?B"] subspace_span[of d] by auto qed lemma basis_to_substdbasis_subspace_isomorphism: fixes B :: "'a::euclidean_space set" assumes "independent B" shows "\f d::'a set. card d = card B \ linear f \ f ` B = d \ f ` span B = {x. \i\Basis. i \ d \ x \ i = 0} \ inj_on f (span B) \ d \ Basis" proof - have B: "card B = dim B" using dim_unique[of B B "card B"] assms span_superset[of B] by auto have "dim B \ card (Basis :: 'a set)" using dim_subset_UNIV[of B] by simp from ex_card[OF this] obtain d :: "'a set" where d: "d \ Basis" and t: "card d = dim B" by auto let ?t = "{x::'a::euclidean_space. \i\Basis. i \ d \ x\i = 0}" have "\f. linear f \ f ` B = d \ f ` span B = ?t \ inj_on f (span B)" proof (intro basis_to_basis_subspace_isomorphism subspace_span subspace_substandard span_superset) show "d \ {x. \i\Basis. i \ d \ x \ i = 0}" using d inner_not_same_Basis by blast qed (auto simp: span_substd_basis independent_substdbasis dim_substandard d t B assms) with t \card B = dim B\ d show ?thesis by auto qed lemma aff_dim_empty: fixes S :: "'n::euclidean_space set" shows "S = {} \ aff_dim S = -1" proof - obtain B where *: "affine hull B = affine hull S" and "\ affine_dependent B" and "int (card B) = aff_dim S + 1" using aff_dim_basis_exists by auto moreover from * have "S = {} \ B = {}" using affine_hull_nonempty[of B] affine_hull_nonempty[of S] by auto ultimately show ?thesis using aff_independent_finite[of B] card_gt_0_iff[of B] by auto qed lemma aff_dim_empty_eq [simp]: "aff_dim ({}::'a::euclidean_space set) = -1" by (simp add: aff_dim_empty [symmetric]) lemma aff_dim_affine_hull [simp]: "aff_dim (affine hull S) = aff_dim S" unfolding aff_dim_def using hull_hull[of _ S] by auto lemma aff_dim_affine_hull2: assumes "affine hull S = affine hull T" shows "aff_dim S = aff_dim T" unfolding aff_dim_def using assms by auto lemma aff_dim_unique: fixes B V :: "'n::euclidean_space set" assumes "affine hull B = affine hull V \ \ affine_dependent B" shows "of_nat (card B) = aff_dim V + 1" proof (cases "B = {}") case True then have "V = {}" using affine_hull_nonempty[of V] affine_hull_nonempty[of B] assms by auto then have "aff_dim V = (-1::int)" using aff_dim_empty by auto then show ?thesis using \B = {}\ by auto next case False then obtain a where a: "a \ B" by auto define Lb where "Lb = span ((\x. -a+x) ` (B-{a}))" have "affine_parallel (affine hull B) Lb" using Lb_def affine_hull_span2[of a B] a affine_parallel_commut[of "Lb" "(affine hull B)"] unfolding affine_parallel_def by auto moreover have "subspace Lb" using Lb_def subspace_span by auto ultimately have "aff_dim B = int(dim Lb)" using aff_dim_parallel_subspace[of B Lb] \B \ {}\ by auto moreover have "(card B) - 1 = dim Lb" "finite B" using Lb_def aff_dim_parallel_subspace_aux a assms by auto ultimately have "of_nat (card B) = aff_dim B + 1" using \B \ {}\ card_gt_0_iff[of B] by auto then show ?thesis using aff_dim_affine_hull2 assms by auto qed lemma aff_dim_affine_independent: fixes B :: "'n::euclidean_space set" assumes "\ affine_dependent B" shows "of_nat (card B) = aff_dim B + 1" using aff_dim_unique[of B B] assms by auto lemma affine_independent_iff_card: fixes s :: "'a::euclidean_space set" shows "\ affine_dependent s \ finite s \ aff_dim s = int(card s) - 1" apply (rule iffI) apply (simp add: aff_dim_affine_independent aff_independent_finite) by (metis affine_basis_exists [of s] aff_dim_unique card_subset_eq diff_add_cancel of_nat_eq_iff) lemma aff_dim_sing [simp]: fixes a :: "'n::euclidean_space" shows "aff_dim {a} = 0" using aff_dim_affine_independent[of "{a}"] affine_independent_1 by auto lemma aff_dim_2 [simp]: "aff_dim {a,b} = (if a = b then 0 else 1)" proof (clarsimp) assume "a \ b" then have "aff_dim{a,b} = card{a,b} - 1" using affine_independent_2 [of a b] aff_dim_affine_independent by fastforce also have "\ = 1" using \a \ b\ by simp finally show "aff_dim {a, b} = 1" . qed lemma aff_dim_inner_basis_exists: fixes V :: "('n::euclidean_space) set" shows "\B. B \ V \ affine hull B = affine hull V \ \ affine_dependent B \ of_nat (card B) = aff_dim V + 1" proof - obtain B where B: "\ affine_dependent B" "B \ V" "affine hull B = affine hull V" using affine_basis_exists[of V] by auto then have "of_nat(card B) = aff_dim V+1" using aff_dim_unique by auto with B show ?thesis by auto qed lemma aff_dim_le_card: fixes V :: "'n::euclidean_space set" assumes "finite V" shows "aff_dim V \ of_nat (card V) - 1" proof - obtain B where B: "B \ V" "of_nat (card B) = aff_dim V + 1" using aff_dim_inner_basis_exists[of V] by auto then have "card B \ card V" using assms card_mono by auto with B show ?thesis by auto qed lemma aff_dim_parallel_eq: fixes S T :: "'n::euclidean_space set" assumes "affine_parallel (affine hull S) (affine hull T)" shows "aff_dim S = aff_dim T" proof - { assume "T \ {}" "S \ {}" then obtain L where L: "subspace L \ affine_parallel (affine hull T) L" using affine_parallel_subspace[of "affine hull T"] affine_affine_hull[of T] affine_hull_nonempty by auto then have "aff_dim T = int (dim L)" using aff_dim_parallel_subspace \T \ {}\ by auto moreover have *: "subspace L \ affine_parallel (affine hull S) L" using L affine_parallel_assoc[of "affine hull S" "affine hull T" L] assms by auto moreover from * have "aff_dim S = int (dim L)" using aff_dim_parallel_subspace \S \ {}\ by auto ultimately have ?thesis by auto } moreover { assume "S = {}" then have "S = {}" and "T = {}" using assms affine_hull_nonempty unfolding affine_parallel_def by auto then have ?thesis using aff_dim_empty by auto } moreover { assume "T = {}" then have "S = {}" and "T = {}" using assms affine_hull_nonempty unfolding affine_parallel_def by auto then have ?thesis using aff_dim_empty by auto } ultimately show ?thesis by blast qed lemma aff_dim_translation_eq: "aff_dim ((+) a ` S) = aff_dim S" for a :: "'n::euclidean_space" proof - have "affine_parallel (affine hull S) (affine hull ((\x. a + x) ` S))" unfolding affine_parallel_def apply (rule exI[of _ "a"]) using affine_hull_translation[of a S] apply auto done then show ?thesis using aff_dim_parallel_eq[of S "(\x. a + x) ` S"] by auto qed lemma aff_dim_translation_eq_subtract: "aff_dim ((\x. x - a) ` S) = aff_dim S" for a :: "'n::euclidean_space" using aff_dim_translation_eq [of "- a"] by (simp cong: image_cong_simp) lemma aff_dim_affine: fixes S L :: "'n::euclidean_space set" assumes "S \ {}" and "affine S" and "subspace L" and "affine_parallel S L" shows "aff_dim S = int (dim L)" proof - have *: "affine hull S = S" using assms affine_hull_eq[of S] by auto then have "affine_parallel (affine hull S) L" using assms by (simp add: *) then show ?thesis using assms aff_dim_parallel_subspace[of S L] by blast qed lemma dim_affine_hull: fixes S :: "'n::euclidean_space set" shows "dim (affine hull S) = dim S" proof - have "dim (affine hull S) \ dim S" using dim_subset by auto moreover have "dim (span S) \ dim (affine hull S)" using dim_subset affine_hull_subset_span by blast moreover have "dim (span S) = dim S" using dim_span by auto ultimately show ?thesis by auto qed lemma aff_dim_subspace: fixes S :: "'n::euclidean_space set" assumes "subspace S" shows "aff_dim S = int (dim S)" proof (cases "S={}") case True with assms show ?thesis by (simp add: subspace_affine) next case False with aff_dim_affine[of S S] assms subspace_imp_affine[of S] affine_parallel_reflex[of S] subspace_affine show ?thesis by auto qed lemma aff_dim_zero: fixes S :: "'n::euclidean_space set" assumes "0 \ affine hull S" shows "aff_dim S = int (dim S)" proof - have "subspace (affine hull S)" using subspace_affine[of "affine hull S"] affine_affine_hull assms by auto then have "aff_dim (affine hull S) = int (dim (affine hull S))" using assms aff_dim_subspace[of "affine hull S"] by auto then show ?thesis using aff_dim_affine_hull[of S] dim_affine_hull[of S] by auto qed lemma aff_dim_eq_dim: "aff_dim S = int (dim ((+) (- a) ` S))" if "a \ affine hull S" for S :: "'n::euclidean_space set" proof - have "0 \ affine hull (+) (- a) ` S" unfolding affine_hull_translation using that by (simp add: ac_simps) with aff_dim_zero show ?thesis by (metis aff_dim_translation_eq) qed lemma aff_dim_eq_dim_subtract: "aff_dim S = int (dim ((\x. x - a) ` S))" if "a \ affine hull S" for S :: "'n::euclidean_space set" using aff_dim_eq_dim [of a] that by (simp cong: image_cong_simp) lemma aff_dim_UNIV [simp]: "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))" using aff_dim_subspace[of "(UNIV :: 'n::euclidean_space set)"] dim_UNIV[where 'a="'n::euclidean_space"] by auto lemma aff_dim_geq: fixes V :: "'n::euclidean_space set" shows "aff_dim V \ -1" proof - obtain B where "affine hull B = affine hull V" and "\ affine_dependent B" and "int (card B) = aff_dim V + 1" using aff_dim_basis_exists by auto then show ?thesis by auto qed lemma aff_dim_negative_iff [simp]: fixes S :: "'n::euclidean_space set" shows "aff_dim S < 0 \S = {}" by (metis aff_dim_empty aff_dim_geq diff_0 eq_iff zle_diff1_eq) lemma aff_lowdim_subset_hyperplane: fixes S :: "'a::euclidean_space set" assumes "aff_dim S < DIM('a)" obtains a b where "a \ 0" "S \ {x. a \ x = b}" proof (cases "S={}") case True moreover have "(SOME b. b \ Basis) \ 0" by (metis norm_some_Basis norm_zero zero_neq_one) ultimately show ?thesis using that by blast next case False then obtain c S' where "c \ S'" "S = insert c S'" by (meson equals0I mk_disjoint_insert) have "dim ((+) (-c) ` S) < DIM('a)" by (metis \S = insert c S'\ aff_dim_eq_dim assms hull_inc insertI1 of_nat_less_imp_less) then obtain a where "a \ 0" "span ((+) (-c) ` S) \ {x. a \ x = 0}" using lowdim_subset_hyperplane by blast moreover have "a \ w = a \ c" if "span ((+) (- c) ` S) \ {x. a \ x = 0}" "w \ S" for w proof - have "w-c \ span ((+) (- c) ` S)" by (simp add: span_base \w \ S\) with that have "w-c \ {x. a \ x = 0}" by blast then show ?thesis by (auto simp: algebra_simps) qed ultimately have "S \ {x. a \ x = a \ c}" by blast then show ?thesis by (rule that[OF \a \ 0\]) qed lemma affine_independent_card_dim_diffs: fixes S :: "'a :: euclidean_space set" assumes "\ affine_dependent S" "a \ S" shows "card S = dim {x - a|x. x \ S} + 1" proof - have 1: "{b - a|b. b \ (S - {a})} \ {x - a|x. x \ S}" by auto have 2: "x - a \ span {b - a |b. b \ S - {a}}" if "x \ S" for x proof (cases "x = a") case True then show ?thesis by (simp add: span_clauses) next case False then show ?thesis using assms by (blast intro: span_base that) qed have "\ affine_dependent (insert a S)" by (simp add: assms insert_absorb) then have 3: "independent {b - a |b. b \ S - {a}}" using dependent_imp_affine_dependent by fastforce have "{b - a |b. b \ S - {a}} = (\b. b-a) ` (S - {a})" by blast then have "card {b - a |b. b \ S - {a}} = card ((\b. b-a) ` (S - {a}))" by simp also have "\ = card (S - {a})" by (metis (no_types, lifting) card_image diff_add_cancel inj_onI) also have "\ = card S - 1" by (simp add: aff_independent_finite assms) finally have 4: "card {b - a |b. b \ S - {a}} = card S - 1" . have "finite S" by (meson assms aff_independent_finite) with \a \ S\ have "card S \ 0" by auto moreover have "dim {x - a |x. x \ S} = card S - 1" using 2 by (blast intro: dim_unique [OF 1 _ 3 4]) ultimately show ?thesis by auto qed lemma independent_card_le_aff_dim: fixes B :: "'n::euclidean_space set" assumes "B \ V" assumes "\ affine_dependent B" shows "int (card B) \ aff_dim V + 1" proof - obtain T where T: "\ affine_dependent T \ B \ T \ T \ V \ affine hull T = affine hull V" by (metis assms extend_to_affine_basis[of B V]) then have "of_nat (card T) = aff_dim V + 1" using aff_dim_unique by auto then show ?thesis using T card_mono[of T B] aff_independent_finite[of T] by auto qed lemma aff_dim_subset: fixes S T :: "'n::euclidean_space set" assumes "S \ T" shows "aff_dim S \ aff_dim T" proof - obtain B where B: "\ affine_dependent B" "B \ S" "affine hull B = affine hull S" "of_nat (card B) = aff_dim S + 1" using aff_dim_inner_basis_exists[of S] by auto then have "int (card B) \ aff_dim T + 1" using assms independent_card_le_aff_dim[of B T] by auto with B show ?thesis by auto qed lemma aff_dim_le_DIM: fixes S :: "'n::euclidean_space set" shows "aff_dim S \ int (DIM('n))" proof - have "aff_dim (UNIV :: 'n::euclidean_space set) = int(DIM('n))" using aff_dim_UNIV by auto then show "aff_dim (S:: 'n::euclidean_space set) \ int(DIM('n))" using aff_dim_subset[of S "(UNIV :: ('n::euclidean_space) set)"] subset_UNIV by auto qed lemma affine_dim_equal: fixes S :: "'n::euclidean_space set" assumes "affine S" "affine T" "S \ {}" "S \ T" "aff_dim S = aff_dim T" shows "S = T" proof - obtain a where "a \ S" using assms by auto then have "a \ T" using assms by auto define LS where "LS = {y. \x \ S. (-a) + x = y}" then have ls: "subspace LS" "affine_parallel S LS" using assms parallel_subspace_explicit[of S a LS] \a \ S\ by auto then have h1: "int(dim LS) = aff_dim S" using assms aff_dim_affine[of S LS] by auto have "T \ {}" using assms by auto define LT where "LT = {y. \x \ T. (-a) + x = y}" then have lt: "subspace LT \ affine_parallel T LT" using assms parallel_subspace_explicit[of T a LT] \a \ T\ by auto then have "int(dim LT) = aff_dim T" using assms aff_dim_affine[of T LT] \T \ {}\ by auto then have "dim LS = dim LT" using h1 assms by auto moreover have "LS \ LT" using LS_def LT_def assms by auto ultimately have "LS = LT" using subspace_dim_equal[of LS LT] ls lt by auto moreover have "S = {x. \y \ LS. a+y=x}" using LS_def by auto moreover have "T = {x. \y \ LT. a+y=x}" using LT_def by auto ultimately show ?thesis by auto qed lemma aff_dim_eq_0: fixes S :: "'a::euclidean_space set" shows "aff_dim S = 0 \ (\a. S = {a})" proof (cases "S = {}") case True then show ?thesis by auto next case False then obtain a where "a \ S" by auto show ?thesis proof safe assume 0: "aff_dim S = 0" have "\ {a,b} \ S" if "b \ a" for b by (metis "0" aff_dim_2 aff_dim_subset not_one_le_zero that) then show "\a. S = {a}" using \a \ S\ by blast qed auto qed lemma affine_hull_UNIV: fixes S :: "'n::euclidean_space set" assumes "aff_dim S = int(DIM('n))" shows "affine hull S = (UNIV :: ('n::euclidean_space) set)" proof - have "S \ {}" using assms aff_dim_empty[of S] by auto have h0: "S \ affine hull S" using hull_subset[of S _] by auto have h1: "aff_dim (UNIV :: ('n::euclidean_space) set) = aff_dim S" using aff_dim_UNIV assms by auto then have h2: "aff_dim (affine hull S) \ aff_dim (UNIV :: ('n::euclidean_space) set)" using aff_dim_le_DIM[of "affine hull S"] assms h0 by auto have h3: "aff_dim S \ aff_dim (affine hull S)" using h0 aff_dim_subset[of S "affine hull S"] assms by auto then have h4: "aff_dim (affine hull S) = aff_dim (UNIV :: ('n::euclidean_space) set)" using h0 h1 h2 by auto then show ?thesis using affine_dim_equal[of "affine hull S" "(UNIV :: ('n::euclidean_space) set)"] affine_affine_hull[of S] affine_UNIV assms h4 h0 \S \ {}\ by auto qed lemma disjoint_affine_hull: fixes s :: "'n::euclidean_space set" assumes "\ affine_dependent s" "t \ s" "u \ s" "t \ u = {}" shows "(affine hull t) \ (affine hull u) = {}" proof - have "finite s" using assms by (simp add: aff_independent_finite) then have "finite t" "finite u" using assms finite_subset by blast+ { fix y assume yt: "y \ affine hull t" and yu: "y \ affine hull u" then obtain a b where a1 [simp]: "sum a t = 1" and [simp]: "sum (\v. a v *\<^sub>R v) t = y" and [simp]: "sum b u = 1" "sum (\v. b v *\<^sub>R v) u = y" by (auto simp: affine_hull_finite \finite t\ \finite u\) define c where "c x = (if x \ t then a x else if x \ u then -(b x) else 0)" for x have [simp]: "s \ t = t" "s \ - t \ u = u" using assms by auto have "sum c s = 0" by (simp add: c_def comm_monoid_add_class.sum.If_cases \finite s\ sum_negf) moreover have "\ (\v\s. c v = 0)" by (metis (no_types) IntD1 \s \ t = t\ a1 c_def sum.neutral zero_neq_one) moreover have "(\v\s. c v *\<^sub>R v) = 0" by (simp add: c_def if_smult sum_negf comm_monoid_add_class.sum.If_cases \finite s\) ultimately have False using assms \finite s\ by (auto simp: affine_dependent_explicit) } then show ?thesis by blast qed lemma aff_dim_convex_hull: fixes S :: "'n::euclidean_space set" shows "aff_dim (convex hull S) = aff_dim S" using aff_dim_affine_hull[of S] convex_hull_subset_affine_hull[of S] hull_subset[of S "convex"] aff_dim_subset[of S "convex hull S"] aff_dim_subset[of "convex hull S" "affine hull S"] by auto subsection \Caratheodory's theorem\ lemma convex_hull_caratheodory_aff_dim: fixes p :: "('a::euclidean_space) set" shows "convex hull p = {y. \s u. finite s \ s \ p \ card s \ aff_dim p + 1 \ (\x\s. 0 \ u x) \ sum u s = 1 \ sum (\v. u v *\<^sub>R v) s = y}" unfolding convex_hull_explicit set_eq_iff mem_Collect_eq proof (intro allI iffI) fix y let ?P = "\n. \s u. finite s \ card s = n \ s \ p \ (\x\s. 0 \ u x) \ sum u s = 1 \ (\v\s. u v *\<^sub>R v) = y" assume "\s u. finite s \ s \ p \ (\x\s. 0 \ u x) \ sum u s = 1 \ (\v\s. u v *\<^sub>R v) = y" then obtain N where "?P N" by auto then have "\n\N. (\k ?P k) \ ?P n" apply (rule_tac ex_least_nat_le, auto) done then obtain n where "?P n" and smallest: "\k ?P k" by blast then obtain s u where obt: "finite s" "card s = n" "s\p" "\x\s. 0 \ u x" "sum u s = 1" "(\v\s. u v *\<^sub>R v) = y" by auto have "card s \ aff_dim p + 1" proof (rule ccontr, simp only: not_le) assume "aff_dim p + 1 < card s" then have "affine_dependent s" using affine_dependent_biggerset[OF obt(1)] independent_card_le_aff_dim not_less obt(3) by blast then obtain w v where wv: "sum w s = 0" "v\s" "w v \ 0" "(\v\s. w v *\<^sub>R v) = 0" using affine_dependent_explicit_finite[OF obt(1)] by auto define i where "i = (\v. (u v) / (- w v)) ` {v\s. w v < 0}" define t where "t = Min i" have "\x\s. w x < 0" proof (rule ccontr, simp add: not_less) assume as:"\x\s. 0 \ w x" then have "sum w (s - {v}) \ 0" apply (rule_tac sum_nonneg, auto) done then have "sum w s > 0" unfolding sum.remove[OF obt(1) \v\s\] using as[THEN bspec[where x=v]] \v\s\ \w v \ 0\ by auto then show False using wv(1) by auto qed then have "i \ {}" unfolding i_def by auto then have "t \ 0" using Min_ge_iff[of i 0 ] and obt(1) unfolding t_def i_def using obt(4)[unfolded le_less] by (auto simp: divide_le_0_iff) have t: "\v\s. u v + t * w v \ 0" proof fix v assume "v \ s" then have v: "0 \ u v" using obt(4)[THEN bspec[where x=v]] by auto show "0 \ u v + t * w v" proof (cases "w v < 0") case False thus ?thesis using v \t\0\ by auto next case True then have "t \ u v / (- w v)" using \v\s\ unfolding t_def i_def apply (rule_tac Min_le) using obt(1) apply auto done then show ?thesis unfolding real_0_le_add_iff using pos_le_divide_eq[OF True[unfolded neg_0_less_iff_less[symmetric]]] by auto qed qed obtain a where "a \ s" and "t = (\v. (u v) / (- w v)) a" and "w a < 0" using Min_in[OF _ \i\{}\] and obt(1) unfolding i_def t_def by auto then have a: "a \ s" "u a + t * w a = 0" by auto have *: "\f. sum f (s - {a}) = sum f s - ((f a)::'b::ab_group_add)" unfolding sum.remove[OF obt(1) \a\s\] by auto have "(\v\s. u v + t * w v) = 1" unfolding sum.distrib wv(1) sum_distrib_left[symmetric] obt(5) by auto moreover have "(\v\s. u v *\<^sub>R v + (t * w v) *\<^sub>R v) - (u a *\<^sub>R a + (t * w a) *\<^sub>R a) = y" unfolding sum.distrib obt(6) scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] wv(4) using a(2) [THEN eq_neg_iff_add_eq_0 [THEN iffD2]] by simp ultimately have "?P (n - 1)" apply (rule_tac x="(s - {a})" in exI) apply (rule_tac x="\v. u v + t * w v" in exI) using obt(1-3) and t and a apply (auto simp: * scaleR_left_distrib) done then show False using smallest[THEN spec[where x="n - 1"]] by auto qed then show "\s u. finite s \ s \ p \ card s \ aff_dim p + 1 \ (\x\s. 0 \ u x) \ sum u s = 1 \ (\v\s. u v *\<^sub>R v) = y" using obt by auto qed auto lemma caratheodory_aff_dim: fixes p :: "('a::euclidean_space) set" shows "convex hull p = {x. \s. finite s \ s \ p \ card s \ aff_dim p + 1 \ x \ convex hull s}" (is "?lhs = ?rhs") proof show "?lhs \ ?rhs" apply (subst convex_hull_caratheodory_aff_dim, clarify) apply (rule_tac x=s in exI) apply (simp add: hull_subset convex_explicit [THEN iffD1, OF convex_convex_hull]) done next show "?rhs \ ?lhs" using hull_mono by blast qed lemma convex_hull_caratheodory: fixes p :: "('a::euclidean_space) set" shows "convex hull p = {y. \s u. finite s \ s \ p \ card s \ DIM('a) + 1 \ (\x\s. 0 \ u x) \ sum u s = 1 \ sum (\v. u v *\<^sub>R v) s = y}" (is "?lhs = ?rhs") proof (intro set_eqI iffI) fix x assume "x \ ?lhs" then show "x \ ?rhs" apply (simp only: convex_hull_caratheodory_aff_dim Set.mem_Collect_eq) apply (erule ex_forward)+ using aff_dim_le_DIM [of p] apply simp done next fix x assume "x \ ?rhs" then show "x \ ?lhs" by (auto simp: convex_hull_explicit) qed theorem caratheodory: "convex hull p = {x::'a::euclidean_space. \s. finite s \ s \ p \ card s \ DIM('a) + 1 \ x \ convex hull s}" proof safe fix x assume "x \ convex hull p" then obtain s u where "finite s" "s \ p" "card s \ DIM('a) + 1" "\x\s. 0 \ u x" "sum u s = 1" "(\v\s. u v *\<^sub>R v) = x" unfolding convex_hull_caratheodory by auto then show "\s. finite s \ s \ p \ card s \ DIM('a) + 1 \ x \ convex hull s" apply (rule_tac x=s in exI) using hull_subset[of s convex] using convex_convex_hull[simplified convex_explicit, of s, THEN spec[where x=s], THEN spec[where x=u]] apply auto done next fix x s assume "finite s" "s \ p" "card s \ DIM('a) + 1" "x \ convex hull s" then show "x \ convex hull p" using hull_mono[OF \s\p\] by auto qed subsection\<^marker>\tag unimportant\\Some Properties of subset of standard basis\ lemma affine_hull_substd_basis: assumes "d \ Basis" shows "affine hull (insert 0 d) = {x::'a::euclidean_space. \i\Basis. i \ d \ x\i = 0}" (is "affine hull (insert 0 ?A) = ?B") proof - have *: "\A. (+) (0::'a) ` A = A" "\A. (+) (- (0::'a)) ` A = A" by auto show ?thesis unfolding affine_hull_insert_span_gen span_substd_basis[OF assms,symmetric] * .. qed lemma affine_hull_convex_hull [simp]: "affine hull (convex hull S) = affine hull S" by (metis Int_absorb1 Int_absorb2 convex_hull_subset_affine_hull hull_hull hull_mono hull_subset) subsection\<^marker>\tag unimportant\ \Moving and scaling convex hulls\ lemma convex_hull_set_plus: "convex hull (S + T) = convex hull S + convex hull T" unfolding set_plus_image apply (subst convex_hull_linear_image [symmetric]) apply (simp add: linear_iff scaleR_right_distrib) apply (simp add: convex_hull_Times) done lemma translation_eq_singleton_plus: "(\x. a + x) ` T = {a} + T" unfolding set_plus_def by auto lemma convex_hull_translation: "convex hull ((\x. a + x) ` S) = (\x. a + x) ` (convex hull S)" unfolding translation_eq_singleton_plus by (simp only: convex_hull_set_plus convex_hull_singleton) lemma convex_hull_scaling: "convex hull ((\x. c *\<^sub>R x) ` S) = (\x. c *\<^sub>R x) ` (convex hull S)" using linear_scaleR by (rule convex_hull_linear_image [symmetric]) lemma convex_hull_affinity: "convex hull ((\x. a + c *\<^sub>R x) ` S) = (\x. a + c *\<^sub>R x) ` (convex hull S)" by(simp only: image_image[symmetric] convex_hull_scaling convex_hull_translation) subsection\<^marker>\tag unimportant\ \Convexity of cone hulls\ lemma convex_cone_hull: assumes "convex S" shows "convex (cone hull S)" proof (rule convexI) fix x y assume xy: "x \ cone hull S" "y \ cone hull S" then have "S \ {}" using cone_hull_empty_iff[of S] by auto fix u v :: real assume uv: "u \ 0" "v \ 0" "u + v = 1" then have *: "u *\<^sub>R x \ cone hull S" "v *\<^sub>R y \ cone hull S" using cone_cone_hull[of S] xy cone_def[of "cone hull S"] by auto from * obtain cx :: real and xx where x: "u *\<^sub>R x = cx *\<^sub>R xx" "cx \ 0" "xx \ S" using cone_hull_expl[of S] by auto from * obtain cy :: real and yy where y: "v *\<^sub>R y = cy *\<^sub>R yy" "cy \ 0" "yy \ S" using cone_hull_expl[of S] by auto { assume "cx + cy \ 0" then have "u *\<^sub>R x = 0" and "v *\<^sub>R y = 0" using x y by auto then have "u *\<^sub>R x + v *\<^sub>R y = 0" by auto then have "u *\<^sub>R x + v *\<^sub>R y \ cone hull S" using cone_hull_contains_0[of S] \S \ {}\ by auto } moreover { assume "cx + cy > 0" then have "(cx / (cx + cy)) *\<^sub>R xx + (cy / (cx + cy)) *\<^sub>R yy \ S" using assms mem_convex_alt[of S xx yy cx cy] x y by auto then have "cx *\<^sub>R xx + cy *\<^sub>R yy \ cone hull S" using mem_cone_hull[of "(cx/(cx+cy)) *\<^sub>R xx + (cy/(cx+cy)) *\<^sub>R yy" S "cx+cy"] \cx+cy>0\ by (auto simp: scaleR_right_distrib) then have "u *\<^sub>R x + v *\<^sub>R y \ cone hull S" using x y by auto } moreover have "cx + cy \ 0 \ cx + cy > 0" by auto ultimately show "u *\<^sub>R x + v *\<^sub>R y \ cone hull S" by blast qed lemma cone_convex_hull: assumes "cone S" shows "cone (convex hull S)" proof (cases "S = {}") case True then show ?thesis by auto next case False then have *: "0 \ S \ (\c. c > 0 \ (*\<^sub>R) c ` S = S)" using cone_iff[of S] assms by auto { fix c :: real assume "c > 0" then have "(*\<^sub>R) c ` (convex hull S) = convex hull ((*\<^sub>R) c ` S)" using convex_hull_scaling[of _ S] by auto also have "\ = convex hull S" using * \c > 0\ by auto finally have "(*\<^sub>R) c ` (convex hull S) = convex hull S" by auto } then have "0 \ convex hull S" "\c. c > 0 \ ((*\<^sub>R) c ` (convex hull S)) = (convex hull S)" using * hull_subset[of S convex] by auto then show ?thesis using \S \ {}\ cone_iff[of "convex hull S"] by auto qed subsection \Radon's theorem\ text "Formalized by Lars Schewe." lemma Radon_ex_lemma: assumes "finite c" "affine_dependent c" shows "\u. sum u c = 0 \ (\v\c. u v \ 0) \ sum (\v. u v *\<^sub>R v) c = 0" proof - from assms(2)[unfolded affine_dependent_explicit] obtain s u where "finite s" "s \ c" "sum u s = 0" "\v\s. u v \ 0" "(\v\s. u v *\<^sub>R v) = 0" by blast then show ?thesis apply (rule_tac x="\v. if v\s then u v else 0" in exI) unfolding if_smult scaleR_zero_left and sum.inter_restrict[OF assms(1), symmetric] apply (auto simp: Int_absorb1) done qed lemma Radon_s_lemma: assumes "finite s" and "sum f s = (0::real)" shows "sum f {x\s. 0 < f x} = - sum f {x\s. f x < 0}" proof - have *: "\x. (if f x < 0 then f x else 0) + (if 0 < f x then f x else 0) = f x" by auto show ?thesis unfolding add_eq_0_iff[symmetric] and sum.inter_filter[OF assms(1)] and sum.distrib[symmetric] and * using assms(2) by assumption qed lemma Radon_v_lemma: assumes "finite s" and "sum f s = 0" and "\x. g x = (0::real) \ f x = (0::'a::euclidean_space)" shows "(sum f {x\s. 0 < g x}) = - sum f {x\s. g x < 0}" proof - have *: "\x. (if 0 < g x then f x else 0) + (if g x < 0 then f x else 0) = f x" using assms(3) by auto show ?thesis unfolding eq_neg_iff_add_eq_0 and sum.inter_filter[OF assms(1)] and sum.distrib[symmetric] and * using assms(2) apply assumption done qed lemma Radon_partition: assumes "finite c" "affine_dependent c" shows "\m p. m \ p = {} \ m \ p = c \ (convex hull m) \ (convex hull p) \ {}" proof - obtain u v where uv: "sum u c = 0" "v\c" "u v \ 0" "(\v\c. u v *\<^sub>R v) = 0" using Radon_ex_lemma[OF assms] by auto have fin: "finite {x \ c. 0 < u x}" "finite {x \ c. 0 > u x}" using assms(1) by auto define z where "z = inverse (sum u {x\c. u x > 0}) *\<^sub>R sum (\x. u x *\<^sub>R x) {x\c. u x > 0}" have "sum u {x \ c. 0 < u x} \ 0" proof (cases "u v \ 0") case False then have "u v < 0" by auto then show ?thesis proof (cases "\w\{x \ c. 0 < u x}. u w > 0") case True then show ?thesis using sum_nonneg_eq_0_iff[of _ u, OF fin(1)] by auto next case False then have "sum u c \ sum (\x. if x=v then u v else 0) c" apply (rule_tac sum_mono, auto) done then show ?thesis unfolding sum.delta[OF assms(1)] using uv(2) and \u v < 0\ and uv(1) by auto qed qed (insert sum_nonneg_eq_0_iff[of _ u, OF fin(1)] uv(2-3), auto) then have *: "sum u {x\c. u x > 0} > 0" unfolding less_le apply (rule_tac conjI) apply (rule_tac sum_nonneg, auto) done moreover have "sum u ({x \ c. 0 < u x} \ {x \ c. u x < 0}) = sum u c" "(\x\{x \ c. 0 < u x} \ {x \ c. u x < 0}. u x *\<^sub>R x) = (\x\c. u x *\<^sub>R x)" using assms(1) apply (rule_tac[!] sum.mono_neutral_left, auto) done then have "sum u {x \ c. 0 < u x} = - sum u {x \ c. 0 > u x}" "(\x\{x \ c. 0 < u x}. u x *\<^sub>R x) = - (\x\{x \ c. 0 > u x}. u x *\<^sub>R x)" unfolding eq_neg_iff_add_eq_0 using uv(1,4) by (auto simp: sum.union_inter_neutral[OF fin, symmetric]) moreover have "\x\{v \ c. u v < 0}. 0 \ inverse (sum u {x \ c. 0 < u x}) * - u x" apply rule apply (rule mult_nonneg_nonneg) using * apply auto done ultimately have "z \ convex hull {v \ c. u v \ 0}" unfolding convex_hull_explicit mem_Collect_eq apply (rule_tac x="{v \ c. u v < 0}" in exI) apply (rule_tac x="\y. inverse (sum u {x\c. u x > 0}) * - u y" in exI) using assms(1) unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] and z_def apply (auto simp: sum_negf sum_distrib_left[symmetric]) done moreover have "\x\{v \ c. 0 < u v}. 0 \ inverse (sum u {x \ c. 0 < u x}) * u x" apply rule apply (rule mult_nonneg_nonneg) using * apply auto done then have "z \ convex hull {v \ c. u v > 0}" unfolding convex_hull_explicit mem_Collect_eq apply (rule_tac x="{v \ c. 0 < u v}" in exI) apply (rule_tac x="\y. inverse (sum u {x\c. u x > 0}) * u y" in exI) using assms(1) unfolding scaleR_scaleR[symmetric] scaleR_right.sum [symmetric] and z_def using * apply (auto simp: sum_negf sum_distrib_left[symmetric]) done ultimately show ?thesis apply (rule_tac x="{v\c. u v \ 0}" in exI) apply (rule_tac x="{v\c. u v > 0}" in exI, auto) done qed theorem Radon: assumes "affine_dependent c" obtains m p where "m \ c" "p \ c" "m \ p = {}" "(convex hull m) \ (convex hull p) \ {}" proof - from assms[unfolded affine_dependent_explicit] obtain s u where "finite s" "s \ c" "sum u s = 0" "\v\s. u v \ 0" "(\v\s. u v *\<^sub>R v) = 0" by blast then have *: "finite s" "affine_dependent s" and s: "s \ c" unfolding affine_dependent_explicit by auto from Radon_partition[OF *] obtain m p where "m \ p = {}" "m \ p = s" "convex hull m \ convex hull p \ {}" by blast then show ?thesis apply (rule_tac that[of p m]) using s apply auto done qed subsection \Helly's theorem\ lemma Helly_induct: fixes f :: "'a::euclidean_space set set" assumes "card f = n" and "n \ DIM('a) + 1" and "\s\f. convex s" "\t\f. card t = DIM('a) + 1 \ \t \ {}" shows "\f \ {}" using assms proof (induction n arbitrary: f) case 0 then show ?case by auto next case (Suc n) have "finite f" using \card f = Suc n\ by (auto intro: card_ge_0_finite) show "\f \ {}" proof (cases "n = DIM('a)") case True then show ?thesis by (simp add: Suc.prems(1) Suc.prems(4)) next case False have "\(f - {s}) \ {}" if "s \ f" for s proof (rule Suc.IH[rule_format]) show "card (f - {s}) = n" by (simp add: Suc.prems(1) \finite f\ that) show "DIM('a) + 1 \ n" using False Suc.prems(2) by linarith show "\t. \t \ f - {s}; card t = DIM('a) + 1\ \ \t \ {}" by (simp add: Suc.prems(4) subset_Diff_insert) qed (use Suc in auto) then have "\s\f. \x. x \ \(f - {s})" by blast then obtain X where X: "\s. s\f \ X s \ \(f - {s})" by metis show ?thesis proof (cases "inj_on X f") case False then obtain s t where "s\t" and st: "s\f" "t\f" "X s = X t" unfolding inj_on_def by auto then have *: "\f = \(f - {s}) \ \(f - {t})" by auto show ?thesis by (metis "*" X disjoint_iff_not_equal st) next case True then obtain m p where mp: "m \ p = {}" "m \ p = X ` f" "convex hull m \ convex hull p \ {}" using Radon_partition[of "X ` f"] and affine_dependent_biggerset[of "X ` f"] unfolding card_image[OF True] and \card f = Suc n\ using Suc(3) \finite f\ and False by auto have "m \ X ` f" "p \ X ` f" using mp(2) by auto then obtain g h where gh:"m = X ` g" "p = X ` h" "g \ f" "h \ f" unfolding subset_image_iff by auto then have "f \ (g \ h) = f" by auto then have f: "f = g \ h" using inj_on_Un_image_eq_iff[of X f "g \ h"] and True unfolding mp(2)[unfolded image_Un[symmetric] gh] by auto have *: "g \ h = {}" using mp(1) unfolding gh using inj_on_image_Int[OF True gh(3,4)] by auto have "convex hull (X ` h) \ \g" "convex hull (X ` g) \ \h" by (rule hull_minimal; use X * f in \auto simp: Suc.prems(3) convex_Inter\)+ then show ?thesis unfolding f using mp(3)[unfolded gh] by blast qed qed qed theorem Helly: fixes f :: "'a::euclidean_space set set" assumes "card f \ DIM('a) + 1" "\s\f. convex s" and "\t. \t\f; card t = DIM('a) + 1\ \ \t \ {}" shows "\f \ {}" apply (rule Helly_induct) using assms apply auto done subsection \Epigraphs of convex functions\ definition\<^marker>\tag important\ "epigraph S (f :: _ \ real) = {xy. fst xy \ S \ f (fst xy) \ snd xy}" lemma mem_epigraph: "(x, y) \ epigraph S f \ x \ S \ f x \ y" unfolding epigraph_def by auto lemma convex_epigraph: "convex (epigraph S f) \ convex_on S f \ convex S" proof safe assume L: "convex (epigraph S f)" then show "convex_on S f" by (auto simp: convex_def convex_on_def epigraph_def) show "convex S" using L apply (clarsimp simp: convex_def convex_on_def epigraph_def) apply (erule_tac x=x in allE) apply (erule_tac x="f x" in allE, safe) apply (erule_tac x=y in allE) apply (erule_tac x="f y" in allE) apply (auto simp: ) done next assume "convex_on S f" "convex S" then show "convex (epigraph S f)" unfolding convex_def convex_on_def epigraph_def apply safe apply (rule_tac [2] y="u * f a + v * f aa" in order_trans) apply (auto intro!:mult_left_mono add_mono) done qed lemma convex_epigraphI: "convex_on S f \ convex S \ convex (epigraph S f)" unfolding convex_epigraph by auto lemma convex_epigraph_convex: "convex S \ convex_on S f \ convex(epigraph S f)" by (simp add: convex_epigraph) subsubsection\<^marker>\tag unimportant\ \Use this to derive general bound property of convex function\ lemma convex_on: assumes "convex S" shows "convex_on S f \ (\k u x. (\i\{1..k::nat}. 0 \ u i \ x i \ S) \ sum u {1..k} = 1 \ f (sum (\i. u i *\<^sub>R x i) {1..k}) \ sum (\i. u i * f(x i)) {1..k})" unfolding convex_epigraph_convex[OF assms] convex epigraph_def Ball_def mem_Collect_eq unfolding fst_sum snd_sum fst_scaleR snd_scaleR apply safe apply (drule_tac x=k in spec) apply (drule_tac x=u in spec) apply (drule_tac x="\i. (x i, f (x i))" in spec) apply simp using assms[unfolded convex] apply simp apply (rule_tac y="\i = 1..k. u i * f (fst (x i))" in order_trans, force) apply (rule sum_mono) apply (erule_tac x=i in allE) unfolding real_scaleR_def apply (rule mult_left_mono) using assms[unfolded convex] apply auto done subsection\<^marker>\tag unimportant\ \A bound within a convex hull\ lemma convex_on_convex_hull_bound: assumes "convex_on (convex hull s) f" and "\x\s. f x \ b" shows "\x\ convex hull s. f x \ b" proof fix x assume "x \ convex hull s" then obtain k u v where obt: "\i\{1..k::nat}. 0 \ u i \ v i \ s" "sum u {1..k} = 1" "(\i = 1..k. u i *\<^sub>R v i) = x" unfolding convex_hull_indexed mem_Collect_eq by auto have "(\i = 1..k. u i * f (v i)) \ b" using sum_mono[of "{1..k}" "\i. u i * f (v i)" "\i. u i * b"] unfolding sum_distrib_right[symmetric] obt(2) mult_1 apply (drule_tac meta_mp) apply (rule mult_left_mono) using assms(2) obt(1) apply auto done then show "f x \ b" using assms(1)[unfolded convex_on[OF convex_convex_hull], rule_format, of k u v] unfolding obt(2-3) using obt(1) and hull_subset[unfolded subset_eq, rule_format, of _ s] by auto qed lemma inner_sum_Basis[simp]: "i \ Basis \ (\Basis) \ i = 1" by (simp add: inner_sum_left sum.If_cases inner_Basis) lemma convex_set_plus: assumes "convex S" and "convex T" shows "convex (S + T)" proof - have "convex (\x\ S. \y \ T. {x + y})" using assms by (rule convex_sums) moreover have "(\x\ S. \y \ T. {x + y}) = S + T" unfolding set_plus_def by auto finally show "convex (S + T)" . qed lemma convex_set_sum: assumes "\i. i \ A \ convex (B i)" shows "convex (\i\A. B i)" proof (cases "finite A") case True then show ?thesis using assms by induct (auto simp: convex_set_plus) qed auto lemma finite_set_sum: assumes "finite A" and "\i\A. finite (B i)" shows "finite (\i\A. B i)" using assms by (induct set: finite, simp, simp add: finite_set_plus) lemma box_eq_set_sum_Basis: shows "{x. \i\Basis. x\i \ B i} = (\i\Basis. image (\x. x *\<^sub>R i) (B i))" apply (subst set_sum_alt [OF finite_Basis], safe) apply (fast intro: euclidean_representation [symmetric]) apply (subst inner_sum_left) apply (rename_tac f) apply (subgoal_tac "(\x\Basis. f x \ i) = f i \ i") apply (drule (1) bspec) apply clarsimp apply (frule sum.remove [OF finite_Basis]) apply (erule trans, simp) apply (rule sum.neutral, clarsimp) apply (frule_tac x=i in bspec, assumption) apply (drule_tac x=x in bspec, assumption, clarsimp) apply (cut_tac u=x and v=i in inner_Basis, assumption+) apply (rule ccontr, simp) done lemma convex_hull_set_sum: "convex hull (\i\A. B i) = (\i\A. convex hull (B i))" proof (cases "finite A") assume "finite A" then show ?thesis by (induct set: finite, simp, simp add: convex_hull_set_plus) qed simp end \ No newline at end of file diff --git a/src/HOL/Analysis/Determinants.thy b/src/HOL/Analysis/Determinants.thy --- a/src/HOL/Analysis/Determinants.thy +++ b/src/HOL/Analysis/Determinants.thy @@ -1,1123 +1,1123 @@ (* Title: HOL/Analysis/Determinants.thy Author: Amine Chaieb, University of Cambridge; proofs reworked by LCP *) -section \Traces, Determinant of square matrices and some properties\ +section \Traces and Determinants of Square Matrices\ theory Determinants imports Cartesian_Space "HOL-Library.Permutations" begin subsection \Trace\ definition\<^marker>\tag important\ trace :: "'a::semiring_1^'n^'n \ 'a" where "trace A = sum (\i. ((A$i)$i)) (UNIV::'n set)" lemma trace_0: "trace (mat 0) = 0" by (simp add: trace_def mat_def) lemma trace_I: "trace (mat 1 :: 'a::semiring_1^'n^'n) = of_nat(CARD('n))" by (simp add: trace_def mat_def) lemma trace_add: "trace ((A::'a::comm_semiring_1^'n^'n) + B) = trace A + trace B" by (simp add: trace_def sum.distrib) lemma trace_sub: "trace ((A::'a::comm_ring_1^'n^'n) - B) = trace A - trace B" by (simp add: trace_def sum_subtractf) lemma trace_mul_sym: "trace ((A::'a::comm_semiring_1^'n^'m) ** B) = trace (B**A)" apply (simp add: trace_def matrix_matrix_mult_def) apply (subst sum.swap) apply (simp add: mult.commute) done subsubsection\<^marker>\tag important\ \Definition of determinant\ definition\<^marker>\tag important\ det:: "'a::comm_ring_1^'n^'n \ 'a" where "det A = sum (\p. of_int (sign p) * prod (\i. A$i$p i) (UNIV :: 'n set)) {p. p permutes (UNIV :: 'n set)}" text \Basic determinant properties\ lemma det_transpose [simp]: "det (transpose A) = det (A::'a::comm_ring_1 ^'n^'n)" proof - let ?di = "\A i j. A$i$j" let ?U = "(UNIV :: 'n set)" have fU: "finite ?U" by simp { fix p assume p: "p \ {p. p permutes ?U}" from p have pU: "p permutes ?U" by blast have sth: "sign (inv p) = sign p" by (metis sign_inverse fU p mem_Collect_eq permutation_permutes) from permutes_inj[OF pU] have pi: "inj_on p ?U" by (blast intro: subset_inj_on) from permutes_image[OF pU] have "prod (\i. ?di (transpose A) i (inv p i)) ?U = prod (\i. ?di (transpose A) i (inv p i)) (p ` ?U)" by simp also have "\ = prod ((\i. ?di (transpose A) i (inv p i)) \ p) ?U" unfolding prod.reindex[OF pi] .. also have "\ = prod (\i. ?di A i (p i)) ?U" proof - have "((\i. ?di (transpose A) i (inv p i)) \ p) i = ?di A i (p i)" if "i \ ?U" for i using that permutes_inv_o[OF pU] permutes_in_image[OF pU] unfolding transpose_def by (simp add: fun_eq_iff) then show "prod ((\i. ?di (transpose A) i (inv p i)) \ p) ?U = prod (\i. ?di A i (p i)) ?U" by (auto intro: prod.cong) qed finally have "of_int (sign (inv p)) * (prod (\i. ?di (transpose A) i (inv p i)) ?U) = of_int (sign p) * (prod (\i. ?di A i (p i)) ?U)" using sth by simp } then show ?thesis unfolding det_def by (subst sum_permutations_inverse) (blast intro: sum.cong) qed lemma det_lowerdiagonal: fixes A :: "'a::comm_ring_1^('n::{finite,wellorder})^('n::{finite,wellorder})" assumes ld: "\i j. i < j \ A$i$j = 0" shows "det A = prod (\i. A$i$i) (UNIV:: 'n set)" proof - let ?U = "UNIV:: 'n set" let ?PU = "{p. p permutes ?U}" let ?pp = "\p. of_int (sign p) * prod (\i. A$i$p i) (UNIV :: 'n set)" have fU: "finite ?U" by simp have id0: "{id} \ ?PU" by (auto simp: permutes_id) have p0: "\p \ ?PU - {id}. ?pp p = 0" proof fix p assume "p \ ?PU - {id}" then obtain i where i: "p i > i" by clarify (meson leI permutes_natset_le) from ld[OF i] have "\i \ ?U. A$i$p i = 0" by blast with prod_zero[OF fU] show "?pp p = 0" by force qed from sum.mono_neutral_cong_left[OF finite_permutations[OF fU] id0 p0] show ?thesis unfolding det_def by (simp add: sign_id) qed lemma det_upperdiagonal: fixes A :: "'a::comm_ring_1^'n::{finite,wellorder}^'n::{finite,wellorder}" assumes ld: "\i j. i > j \ A$i$j = 0" shows "det A = prod (\i. A$i$i) (UNIV:: 'n set)" proof - let ?U = "UNIV:: 'n set" let ?PU = "{p. p permutes ?U}" let ?pp = "(\p. of_int (sign p) * prod (\i. A$i$p i) (UNIV :: 'n set))" have fU: "finite ?U" by simp have id0: "{id} \ ?PU" by (auto simp: permutes_id) have p0: "\p \ ?PU -{id}. ?pp p = 0" proof fix p assume p: "p \ ?PU - {id}" then obtain i where i: "p i < i" by clarify (meson leI permutes_natset_ge) from ld[OF i] have "\i \ ?U. A$i$p i = 0" by blast with prod_zero[OF fU] show "?pp p = 0" by force qed from sum.mono_neutral_cong_left[OF finite_permutations[OF fU] id0 p0] show ?thesis unfolding det_def by (simp add: sign_id) qed proposition det_diagonal: fixes A :: "'a::comm_ring_1^'n^'n" assumes ld: "\i j. i \ j \ A$i$j = 0" shows "det A = prod (\i. A$i$i) (UNIV::'n set)" proof - let ?U = "UNIV:: 'n set" let ?PU = "{p. p permutes ?U}" let ?pp = "\p. of_int (sign p) * prod (\i. A$i$p i) (UNIV :: 'n set)" have fU: "finite ?U" by simp from finite_permutations[OF fU] have fPU: "finite ?PU" . have id0: "{id} \ ?PU" by (auto simp: permutes_id) have p0: "\p \ ?PU - {id}. ?pp p = 0" proof fix p assume p: "p \ ?PU - {id}" then obtain i where i: "p i \ i" by fastforce with ld have "\i \ ?U. A$i$p i = 0" by (metis UNIV_I) with prod_zero [OF fU] show "?pp p = 0" by force qed from sum.mono_neutral_cong_left[OF fPU id0 p0] show ?thesis unfolding det_def by (simp add: sign_id) qed lemma det_I [simp]: "det (mat 1 :: 'a::comm_ring_1^'n^'n) = 1" by (simp add: det_diagonal mat_def) lemma det_0 [simp]: "det (mat 0 :: 'a::comm_ring_1^'n^'n) = 0" by (simp add: det_def prod_zero power_0_left) lemma det_permute_rows: fixes A :: "'a::comm_ring_1^'n^'n" assumes p: "p permutes (UNIV :: 'n::finite set)" shows "det (\ i. A$p i :: 'a^'n^'n) = of_int (sign p) * det A" proof - let ?U = "UNIV :: 'n set" let ?PU = "{p. p permutes ?U}" have *: "(\q\?PU. of_int (sign (q \ p)) * (\i\?U. A $ p i $ (q \ p) i)) = (\n\?PU. of_int (sign p) * of_int (sign n) * (\i\?U. A $ i $ n i))" proof (rule sum.cong) fix q assume qPU: "q \ ?PU" have fU: "finite ?U" by simp from qPU have q: "q permutes ?U" by blast have "prod (\i. A$p i$ (q \ p) i) ?U = prod ((\i. A$p i$(q \ p) i) \ inv p) ?U" by (simp only: prod.permute[OF permutes_inv[OF p], symmetric]) also have "\ = prod (\i. A $ (p \ inv p) i $ (q \ (p \ inv p)) i) ?U" by (simp only: o_def) also have "\ = prod (\i. A$i$q i) ?U" by (simp only: o_def permutes_inverses[OF p]) finally have thp: "prod (\i. A$p i$ (q \ p) i) ?U = prod (\i. A$i$q i) ?U" by blast from p q have pp: "permutation p" and qp: "permutation q" by (metis fU permutation_permutes)+ show "of_int (sign (q \ p)) * prod (\i. A$ p i$ (q \ p) i) ?U = of_int (sign p) * of_int (sign q) * prod (\i. A$i$q i) ?U" by (simp only: thp sign_compose[OF qp pp] mult.commute of_int_mult) qed auto show ?thesis apply (simp add: det_def sum_distrib_left mult.assoc[symmetric]) apply (subst sum_permutations_compose_right[OF p]) apply (rule *) done qed lemma det_permute_columns: fixes A :: "'a::comm_ring_1^'n^'n" assumes p: "p permutes (UNIV :: 'n set)" shows "det(\ i j. A$i$ p j :: 'a^'n^'n) = of_int (sign p) * det A" proof - let ?Ap = "\ i j. A$i$ p j :: 'a^'n^'n" let ?At = "transpose A" have "of_int (sign p) * det A = det (transpose (\ i. transpose A $ p i))" unfolding det_permute_rows[OF p, of ?At] det_transpose .. moreover have "?Ap = transpose (\ i. transpose A $ p i)" by (simp add: transpose_def vec_eq_iff) ultimately show ?thesis by simp qed lemma det_identical_columns: fixes A :: "'a::comm_ring_1^'n^'n" assumes jk: "j \ k" and r: "column j A = column k A" shows "det A = 0" proof - let ?U="UNIV::'n set" let ?t_jk="Fun.swap j k id" let ?PU="{p. p permutes ?U}" let ?S1="{p. p\?PU \ evenperm p}" let ?S2="{(?t_jk \ p) |p. p \?S1}" let ?f="\p. of_int (sign p) * (\i\UNIV. A $ i $ p i)" let ?g="\p. ?t_jk \ p" have g_S1: "?S2 = ?g` ?S1" by auto have inj_g: "inj_on ?g ?S1" proof (unfold inj_on_def, auto) fix x y assume x: "x permutes ?U" and even_x: "evenperm x" and y: "y permutes ?U" and even_y: "evenperm y" and eq: "?t_jk \ x = ?t_jk \ y" show "x = y" by (metis (hide_lams, no_types) comp_assoc eq id_comp swap_id_idempotent) qed have tjk_permutes: "?t_jk permutes ?U" unfolding permutes_def swap_id_eq by (auto,metis) have tjk_eq: "\i l. A $ i $ ?t_jk l = A $ i $ l" using r jk unfolding column_def vec_eq_iff swap_id_eq by fastforce have sign_tjk: "sign ?t_jk = -1" using sign_swap_id[of j k] jk by auto {fix x assume x: "x\ ?S1" have "sign (?t_jk \ x) = sign (?t_jk) * sign x" by (metis (lifting) finite_class.finite_UNIV mem_Collect_eq permutation_permutes permutation_swap_id sign_compose x) also have "\ = - sign x" using sign_tjk by simp also have "\ \ sign x" unfolding sign_def by simp finally have "sign (?t_jk \ x) \ sign x" and "(?t_jk \ x) \ ?S2" using x by force+ } hence disjoint: "?S1 \ ?S2 = {}" by (force simp: sign_def) have PU_decomposition: "?PU = ?S1 \ ?S2" proof (auto) fix x assume x: "x permutes ?U" and "\p. p permutes ?U \ x = Fun.swap j k id \ p \ \ evenperm p" then obtain p where p: "p permutes UNIV" and x_eq: "x = Fun.swap j k id \ p" and odd_p: "\ evenperm p" by (metis (mono_tags) id_o o_assoc permutes_compose swap_id_idempotent tjk_permutes) thus "evenperm x" by (meson evenperm_comp evenperm_swap finite_class.finite_UNIV jk permutation_permutes permutation_swap_id) next fix p assume p: "p permutes ?U" show "Fun.swap j k id \ p permutes UNIV" by (metis p permutes_compose tjk_permutes) qed have "sum ?f ?S2 = sum ((\p. of_int (sign p) * (\i\UNIV. A $ i $ p i)) \ (\) (Fun.swap j k id)) {p \ {p. p permutes UNIV}. evenperm p}" unfolding g_S1 by (rule sum.reindex[OF inj_g]) also have "\ = sum (\p. of_int (sign (?t_jk \ p)) * (\i\UNIV. A $ i $ p i)) ?S1" unfolding o_def by (rule sum.cong, auto simp: tjk_eq) also have "\ = sum (\p. - ?f p) ?S1" proof (rule sum.cong, auto) fix x assume x: "x permutes ?U" and even_x: "evenperm x" hence perm_x: "permutation x" and perm_tjk: "permutation ?t_jk" using permutation_permutes[of x] permutation_permutes[of ?t_jk] permutation_swap_id by (metis finite_code)+ have "(sign (?t_jk \ x)) = - (sign x)" unfolding sign_compose[OF perm_tjk perm_x] sign_tjk by auto thus "of_int (sign (?t_jk \ x)) * (\i\UNIV. A $ i $ x i) = - (of_int (sign x) * (\i\UNIV. A $ i $ x i))" by auto qed also have "\= - sum ?f ?S1" unfolding sum_negf .. finally have *: "sum ?f ?S2 = - sum ?f ?S1" . have "det A = (\p | p permutes UNIV. of_int (sign p) * (\i\UNIV. A $ i $ p i))" unfolding det_def .. also have "\= sum ?f ?S1 + sum ?f ?S2" by (subst PU_decomposition, rule sum.union_disjoint[OF _ _ disjoint], auto) also have "\= sum ?f ?S1 - sum ?f ?S1 " unfolding * by auto also have "\= 0" by simp finally show "det A = 0" by simp qed lemma det_identical_rows: fixes A :: "'a::comm_ring_1^'n^'n" assumes ij: "i \ j" and r: "row i A = row j A" shows "det A = 0" by (metis column_transpose det_identical_columns det_transpose ij r) lemma det_zero_row: fixes A :: "'a::{idom, ring_char_0}^'n^'n" and F :: "'b::{field}^'m^'m" shows "row i A = 0 \ det A = 0" and "row j F = 0 \ det F = 0" by (force simp: row_def det_def vec_eq_iff sign_nz intro!: sum.neutral)+ lemma det_zero_column: fixes A :: "'a::{idom, ring_char_0}^'n^'n" and F :: "'b::{field}^'m^'m" shows "column i A = 0 \ det A = 0" and "column j F = 0 \ det F = 0" unfolding atomize_conj atomize_imp by (metis det_transpose det_zero_row row_transpose) lemma det_row_add: fixes a b c :: "'n::finite \ _ ^ 'n" shows "det((\ i. if i = k then a i + b i else c i)::'a::comm_ring_1^'n^'n) = det((\ i. if i = k then a i else c i)::'a::comm_ring_1^'n^'n) + det((\ i. if i = k then b i else c i)::'a::comm_ring_1^'n^'n)" unfolding det_def vec_lambda_beta sum.distrib[symmetric] proof (rule sum.cong) let ?U = "UNIV :: 'n set" let ?pU = "{p. p permutes ?U}" let ?f = "(\i. if i = k then a i + b i else c i)::'n \ 'a::comm_ring_1^'n" let ?g = "(\ i. if i = k then a i else c i)::'n \ 'a::comm_ring_1^'n" let ?h = "(\ i. if i = k then b i else c i)::'n \ 'a::comm_ring_1^'n" fix p assume p: "p \ ?pU" let ?Uk = "?U - {k}" from p have pU: "p permutes ?U" by blast have kU: "?U = insert k ?Uk" by blast have eq: "prod (\i. ?f i $ p i) ?Uk = prod (\i. ?g i $ p i) ?Uk" "prod (\i. ?f i $ p i) ?Uk = prod (\i. ?h i $ p i) ?Uk" by auto have Uk: "finite ?Uk" "k \ ?Uk" by auto have "prod (\i. ?f i $ p i) ?U = prod (\i. ?f i $ p i) (insert k ?Uk)" unfolding kU[symmetric] .. also have "\ = ?f k $ p k * prod (\i. ?f i $ p i) ?Uk" by (rule prod.insert) auto also have "\ = (a k $ p k * prod (\i. ?f i $ p i) ?Uk) + (b k$ p k * prod (\i. ?f i $ p i) ?Uk)" by (simp add: field_simps) also have "\ = (a k $ p k * prod (\i. ?g i $ p i) ?Uk) + (b k$ p k * prod (\i. ?h i $ p i) ?Uk)" by (metis eq) also have "\ = prod (\i. ?g i $ p i) (insert k ?Uk) + prod (\i. ?h i $ p i) (insert k ?Uk)" unfolding prod.insert[OF Uk] by simp finally have "prod (\i. ?f i $ p i) ?U = prod (\i. ?g i $ p i) ?U + prod (\i. ?h i $ p i) ?U" unfolding kU[symmetric] . then show "of_int (sign p) * prod (\i. ?f i $ p i) ?U = of_int (sign p) * prod (\i. ?g i $ p i) ?U + of_int (sign p) * prod (\i. ?h i $ p i) ?U" by (simp add: field_simps) qed auto lemma det_row_mul: fixes a b :: "'n::finite \ _ ^ 'n" shows "det((\ i. if i = k then c *s a i else b i)::'a::comm_ring_1^'n^'n) = c * det((\ i. if i = k then a i else b i)::'a::comm_ring_1^'n^'n)" unfolding det_def vec_lambda_beta sum_distrib_left proof (rule sum.cong) let ?U = "UNIV :: 'n set" let ?pU = "{p. p permutes ?U}" let ?f = "(\i. if i = k then c*s a i else b i)::'n \ 'a::comm_ring_1^'n" let ?g = "(\ i. if i = k then a i else b i)::'n \ 'a::comm_ring_1^'n" fix p assume p: "p \ ?pU" let ?Uk = "?U - {k}" from p have pU: "p permutes ?U" by blast have kU: "?U = insert k ?Uk" by blast have eq: "prod (\i. ?f i $ p i) ?Uk = prod (\i. ?g i $ p i) ?Uk" by auto have Uk: "finite ?Uk" "k \ ?Uk" by auto have "prod (\i. ?f i $ p i) ?U = prod (\i. ?f i $ p i) (insert k ?Uk)" unfolding kU[symmetric] .. also have "\ = ?f k $ p k * prod (\i. ?f i $ p i) ?Uk" by (rule prod.insert) auto also have "\ = (c*s a k) $ p k * prod (\i. ?f i $ p i) ?Uk" by (simp add: field_simps) also have "\ = c* (a k $ p k * prod (\i. ?g i $ p i) ?Uk)" unfolding eq by (simp add: ac_simps) also have "\ = c* (prod (\i. ?g i $ p i) (insert k ?Uk))" unfolding prod.insert[OF Uk] by simp finally have "prod (\i. ?f i $ p i) ?U = c* (prod (\i. ?g i $ p i) ?U)" unfolding kU[symmetric] . then show "of_int (sign p) * prod (\i. ?f i $ p i) ?U = c * (of_int (sign p) * prod (\i. ?g i $ p i) ?U)" by (simp add: field_simps) qed auto lemma det_row_0: fixes b :: "'n::finite \ _ ^ 'n" shows "det((\ i. if i = k then 0 else b i)::'a::comm_ring_1^'n^'n) = 0" using det_row_mul[of k 0 "\i. 1" b] apply simp apply (simp only: vector_smult_lzero) done lemma det_row_operation: fixes A :: "'a::{comm_ring_1}^'n^'n" assumes ij: "i \ j" shows "det (\ k. if k = i then row i A + c *s row j A else row k A) = det A" proof - let ?Z = "(\ k. if k = i then row j A else row k A) :: 'a ^'n^'n" have th: "row i ?Z = row j ?Z" by (vector row_def) have th2: "((\ k. if k = i then row i A else row k A) :: 'a^'n^'n) = A" by (vector row_def) show ?thesis unfolding det_row_add [of i] det_row_mul[of i] det_identical_rows[OF ij th] th2 by simp qed lemma det_row_span: fixes A :: "'a::{field}^'n^'n" assumes x: "x \ vec.span {row j A |j. j \ i}" shows "det (\ k. if k = i then row i A + x else row k A) = det A" using x proof (induction rule: vec.span_induct_alt) case base have "(if k = i then row i A + 0 else row k A) = row k A" for k by simp then show ?case by (simp add: row_def) next case (step c z y) then obtain j where j: "z = row j A" "i \ j" by blast let ?w = "row i A + y" have th0: "row i A + (c*s z + y) = ?w + c*s z" by vector let ?d = "\x. det (\ k. if k = i then x else row k A)" have thz: "?d z = 0" apply (rule det_identical_rows[OF j(2)]) using j apply (vector row_def) done have "?d (row i A + (c*s z + y)) = ?d (?w + c*s z)" unfolding th0 .. then have "?d (row i A + (c*s z + y)) = det A" unfolding thz step.IH det_row_mul[of i] det_row_add[of i] by simp then show ?case unfolding scalar_mult_eq_scaleR . qed lemma matrix_id [simp]: "det (matrix id) = 1" by (simp add: matrix_id_mat_1) proposition det_matrix_scaleR [simp]: "det (matrix (((*\<^sub>R) r)) :: real^'n^'n) = r ^ CARD('n::finite)" apply (subst det_diagonal) apply (auto simp: matrix_def mat_def) apply (simp add: cart_eq_inner_axis inner_axis_axis) done text \ May as well do this, though it's a bit unsatisfactory since it ignores exact duplicates by considering the rows/columns as a set. \ lemma det_dependent_rows: fixes A:: "'a::{field}^'n^'n" assumes d: "vec.dependent (rows A)" shows "det A = 0" proof - let ?U = "UNIV :: 'n set" from d obtain i where i: "row i A \ vec.span (rows A - {row i A})" unfolding vec.dependent_def rows_def by blast show ?thesis proof (cases "\i j. i \ j \ row i A \ row j A") case True with i have "vec.span (rows A - {row i A}) \ vec.span {row j A |j. j \ i}" by (auto simp: rows_def intro!: vec.span_mono) then have "- row i A \ vec.span {row j A|j. j \ i}" by (meson i subsetCE vec.span_neg) from det_row_span[OF this] have "det A = det (\ k. if k = i then 0 *s 1 else row k A)" unfolding right_minus vector_smult_lzero .. with det_row_mul[of i 0 "\i. 1"] show ?thesis by simp next case False then obtain j k where jk: "j \ k" "row j A = row k A" by auto from det_identical_rows[OF jk] show ?thesis . qed qed lemma det_dependent_columns: assumes d: "vec.dependent (columns (A::real^'n^'n))" shows "det A = 0" by (metis d det_dependent_rows rows_transpose det_transpose) text \Multilinearity and the multiplication formula\ lemma Cart_lambda_cong: "(\x. f x = g x) \ (vec_lambda f::'a^'n) = (vec_lambda g :: 'a^'n)" by auto lemma det_linear_row_sum: assumes fS: "finite S" shows "det ((\ i. if i = k then sum (a i) S else c i)::'a::comm_ring_1^'n^'n) = sum (\j. det ((\ i. if i = k then a i j else c i)::'a^'n^'n)) S" using fS by (induct rule: finite_induct; simp add: det_row_0 det_row_add cong: if_cong) lemma finite_bounded_functions: assumes fS: "finite S" shows "finite {f. (\i \ {1.. (k::nat)}. f i \ S) \ (\i. i \ {1 .. k} \ f i = i)}" proof (induct k) case 0 have *: "{f. \i. f i = i} = {id}" by auto show ?case by (auto simp: *) next case (Suc k) let ?f = "\(y::nat,g) i. if i = Suc k then y else g i" let ?S = "?f ` (S \ {f. (\i\{1..k}. f i \ S) \ (\i. i \ {1..k} \ f i = i)})" have "?S = {f. (\i\{1.. Suc k}. f i \ S) \ (\i. i \ {1.. Suc k} \ f i = i)}" apply (auto simp: image_iff) apply (rename_tac f) apply (rule_tac x="f (Suc k)" in bexI) apply (rule_tac x = "\i. if i = Suc k then i else f i" in exI, auto) done with finite_imageI[OF finite_cartesian_product[OF fS Suc.hyps(1)], of ?f] show ?case by metis qed lemma det_linear_rows_sum_lemma: assumes fS: "finite S" and fT: "finite T" shows "det ((\ i. if i \ T then sum (a i) S else c i):: 'a::comm_ring_1^'n^'n) = sum (\f. det((\ i. if i \ T then a i (f i) else c i)::'a^'n^'n)) {f. (\i \ T. f i \ S) \ (\i. i \ T \ f i = i)}" using fT proof (induct T arbitrary: a c set: finite) case empty have th0: "\x y. (\ i. if i \ {} then x i else y i) = (\ i. y i)" by vector from empty.prems show ?case unfolding th0 by (simp add: eq_id_iff) next case (insert z T a c) let ?F = "\T. {f. (\i \ T. f i \ S) \ (\i. i \ T \ f i = i)}" let ?h = "\(y,g) i. if i = z then y else g i" let ?k = "\h. (h(z),(\i. if i = z then i else h i))" let ?s = "\ k a c f. det((\ i. if i \ T then a i (f i) else c i)::'a^'n^'n)" let ?c = "\j i. if i = z then a i j else c i" have thif: "\a b c d. (if a \ b then c else d) = (if a then c else if b then c else d)" by simp have thif2: "\a b c d e. (if a then b else if c then d else e) = (if c then (if a then b else d) else (if a then b else e))" by simp from \z \ T\ have nz: "\i. i \ T \ i \ z" by auto have "det (\ i. if i \ insert z T then sum (a i) S else c i) = det (\ i. if i = z then sum (a i) S else if i \ T then sum (a i) S else c i)" unfolding insert_iff thif .. also have "\ = (\j\S. det (\ i. if i \ T then sum (a i) S else if i = z then a i j else c i))" unfolding det_linear_row_sum[OF fS] by (subst thif2) (simp add: nz cong: if_cong) finally have tha: "det (\ i. if i \ insert z T then sum (a i) S else c i) = (\(j, f)\S \ ?F T. det (\ i. if i \ T then a i (f i) else if i = z then a i j else c i))" unfolding insert.hyps unfolding sum.cartesian_product by blast show ?case unfolding tha using \z \ T\ by (intro sum.reindex_bij_witness[where i="?k" and j="?h"]) (auto intro!: cong[OF refl[of det]] simp: vec_eq_iff) qed lemma det_linear_rows_sum: fixes S :: "'n::finite set" assumes fS: "finite S" shows "det (\ i. sum (a i) S) = sum (\f. det (\ i. a i (f i) :: 'a::comm_ring_1 ^ 'n^'n)) {f. \i. f i \ S}" proof - have th0: "\x y. ((\ i. if i \ (UNIV:: 'n set) then x i else y i) :: 'a^'n^'n) = (\ i. x i)" by vector from det_linear_rows_sum_lemma[OF fS, of "UNIV :: 'n set" a, unfolded th0, OF finite] show ?thesis by simp qed lemma matrix_mul_sum_alt: fixes A B :: "'a::comm_ring_1^'n^'n" shows "A ** B = (\ i. sum (\k. A$i$k *s B $ k) (UNIV :: 'n set))" by (vector matrix_matrix_mult_def sum_component) lemma det_rows_mul: "det((\ i. c i *s a i)::'a::comm_ring_1^'n^'n) = prod (\i. c i) (UNIV:: 'n set) * det((\ i. a i)::'a^'n^'n)" proof (simp add: det_def sum_distrib_left cong add: prod.cong, rule sum.cong) let ?U = "UNIV :: 'n set" let ?PU = "{p. p permutes ?U}" fix p assume pU: "p \ ?PU" let ?s = "of_int (sign p)" from pU have p: "p permutes ?U" by blast have "prod (\i. c i * a i $ p i) ?U = prod c ?U * prod (\i. a i $ p i) ?U" unfolding prod.distrib .. then show "?s * (\xa\?U. c xa * a xa $ p xa) = prod c ?U * (?s* (\xa\?U. a xa $ p xa))" by (simp add: field_simps) qed rule proposition det_mul: fixes A B :: "'a::comm_ring_1^'n^'n" shows "det (A ** B) = det A * det B" proof - let ?U = "UNIV :: 'n set" let ?F = "{f. (\i \ ?U. f i \ ?U) \ (\i. i \ ?U \ f i = i)}" let ?PU = "{p. p permutes ?U}" have "p \ ?F" if "p permutes ?U" for p by simp then have PUF: "?PU \ ?F" by blast { fix f assume fPU: "f \ ?F - ?PU" have fUU: "f ` ?U \ ?U" using fPU by auto from fPU have f: "\i \ ?U. f i \ ?U" "\i. i \ ?U \ f i = i" "\(\y. \!x. f x = y)" unfolding permutes_def by auto let ?A = "(\ i. A$i$f i *s B$f i) :: 'a^'n^'n" let ?B = "(\ i. B$f i) :: 'a^'n^'n" { assume fni: "\ inj_on f ?U" then obtain i j where ij: "f i = f j" "i \ j" unfolding inj_on_def by blast then have "row i ?B = row j ?B" by (vector row_def) with det_identical_rows[OF ij(2)] have "det (\ i. A$i$f i *s B$f i) = 0" unfolding det_rows_mul by force } moreover { assume fi: "inj_on f ?U" from f fi have fith: "\i j. f i = f j \ i = j" unfolding inj_on_def by metis note fs = fi[unfolded surjective_iff_injective_gen[OF finite finite refl fUU, symmetric]] have "\!x. f x = y" for y using fith fs by blast with f(3) have "det (\ i. A$i$f i *s B$f i) = 0" by blast } ultimately have "det (\ i. A$i$f i *s B$f i) = 0" by blast } then have zth: "\ f\ ?F - ?PU. det (\ i. A$i$f i *s B$f i) = 0" by simp { fix p assume pU: "p \ ?PU" from pU have p: "p permutes ?U" by blast let ?s = "\p. of_int (sign p)" let ?f = "\q. ?s p * (\i\ ?U. A $ i $ p i) * (?s q * (\i\ ?U. B $ i $ q i))" have "(sum (\q. ?s q * (\i\ ?U. (\ i. A $ i $ p i *s B $ p i :: 'a^'n^'n) $ i $ q i)) ?PU) = (sum (\q. ?s p * (\i\ ?U. A $ i $ p i) * (?s q * (\i\ ?U. B $ i $ q i))) ?PU)" unfolding sum_permutations_compose_right[OF permutes_inv[OF p], of ?f] proof (rule sum.cong) fix q assume qU: "q \ ?PU" then have q: "q permutes ?U" by blast from p q have pp: "permutation p" and pq: "permutation q" unfolding permutation_permutes by auto have th00: "of_int (sign p) * of_int (sign p) = (1::'a)" "\a. of_int (sign p) * (of_int (sign p) * a) = a" unfolding mult.assoc[symmetric] unfolding of_int_mult[symmetric] by (simp_all add: sign_idempotent) have ths: "?s q = ?s p * ?s (q \ inv p)" using pp pq permutation_inverse[OF pp] sign_inverse[OF pp] by (simp add: th00 ac_simps sign_idempotent sign_compose) have th001: "prod (\i. B$i$ q (inv p i)) ?U = prod ((\i. B$i$ q (inv p i)) \ p) ?U" by (rule prod.permute[OF p]) have thp: "prod (\i. (\ i. A$i$p i *s B$p i :: 'a^'n^'n) $i $ q i) ?U = prod (\i. A$i$p i) ?U * prod (\i. B$i$ q (inv p i)) ?U" unfolding th001 prod.distrib[symmetric] o_def permutes_inverses[OF p] apply (rule prod.cong[OF refl]) using permutes_in_image[OF q] apply vector done show "?s q * prod (\i. (((\ i. A$i$p i *s B$p i) :: 'a^'n^'n)$i$q i)) ?U = ?s p * (prod (\i. A$i$p i) ?U) * (?s (q \ inv p) * prod (\i. B$i$(q \ inv p) i) ?U)" using ths thp pp pq permutation_inverse[OF pp] sign_inverse[OF pp] by (simp add: sign_nz th00 field_simps sign_idempotent sign_compose) qed rule } then have th2: "sum (\f. det (\ i. A$i$f i *s B$f i)) ?PU = det A * det B" unfolding det_def sum_product by (rule sum.cong [OF refl]) have "det (A**B) = sum (\f. det (\ i. A $ i $ f i *s B $ f i)) ?F" unfolding matrix_mul_sum_alt det_linear_rows_sum[OF finite] by simp also have "\ = sum (\f. det (\ i. A$i$f i *s B$f i)) ?PU" using sum.mono_neutral_cong_left[OF finite PUF zth, symmetric] unfolding det_rows_mul by auto finally show ?thesis unfolding th2 . qed subsection \Relation to invertibility\ proposition invertible_det_nz: fixes A::"'a::{field}^'n^'n" shows "invertible A \ det A \ 0" proof (cases "invertible A") case True then obtain B :: "'a^'n^'n" where B: "A ** B = mat 1" unfolding invertible_right_inverse by blast then have "det (A ** B) = det (mat 1 :: 'a^'n^'n)" by simp then show ?thesis by (metis True det_I det_mul mult_zero_left one_neq_zero) next case False let ?U = "UNIV :: 'n set" have fU: "finite ?U" by simp from False obtain c i where c: "sum (\i. c i *s row i A) ?U = 0" and iU: "i \ ?U" and ci: "c i \ 0" unfolding invertible_right_inverse matrix_right_invertible_independent_rows by blast have thr0: "- row i A = sum (\j. (1/ c i) *s (c j *s row j A)) (?U - {i})" unfolding sum_cmul using c ci by (auto simp: sum.remove[OF fU iU] eq_vector_fraction_iff add_eq_0_iff) have thr: "- row i A \ vec.span {row j A| j. j \ i}" unfolding thr0 by (auto intro: vec.span_base vec.span_scale vec.span_sum) let ?B = "(\ k. if k = i then 0 else row k A) :: 'a^'n^'n" have thrb: "row i ?B = 0" using iU by (vector row_def) have "det A = 0" unfolding det_row_span[OF thr, symmetric] right_minus unfolding det_zero_row(2)[OF thrb] .. then show ?thesis by (simp add: False) qed lemma det_nz_iff_inj_gen: fixes f :: "'a::field^'n \ 'a::field^'n" assumes "Vector_Spaces.linear (*s) (*s) f" shows "det (matrix f) \ 0 \ inj f" proof assume "det (matrix f) \ 0" then show "inj f" using assms invertible_det_nz inj_matrix_vector_mult by force next assume "inj f" show "det (matrix f) \ 0" using vec.linear_injective_left_inverse [OF assms \inj f\] by (metis assms invertible_det_nz invertible_left_inverse matrix_compose_gen matrix_id_mat_1) qed lemma det_nz_iff_inj: fixes f :: "real^'n \ real^'n" assumes "linear f" shows "det (matrix f) \ 0 \ inj f" using det_nz_iff_inj_gen[of f] assms unfolding linear_matrix_vector_mul_eq . lemma det_eq_0_rank: fixes A :: "real^'n^'n" shows "det A = 0 \ rank A < CARD('n)" using invertible_det_nz [of A] by (auto simp: matrix_left_invertible_injective invertible_left_inverse less_rank_noninjective) subsubsection\<^marker>\tag important\ \Invertibility of matrices and corresponding linear functions\ lemma matrix_left_invertible_gen: fixes f :: "'a::field^'m \ 'a::field^'n" assumes "Vector_Spaces.linear (*s) (*s) f" shows "((\B. B ** matrix f = mat 1) \ (\g. Vector_Spaces.linear (*s) (*s) g \ g \ f = id))" proof safe fix B assume 1: "B ** matrix f = mat 1" show "\g. Vector_Spaces.linear (*s) (*s) g \ g \ f = id" proof (intro exI conjI) show "Vector_Spaces.linear (*s) (*s) (\y. B *v y)" by simp show "((*v) B) \ f = id" unfolding o_def by (metis assms 1 eq_id_iff matrix_vector_mul(1) matrix_vector_mul_assoc matrix_vector_mul_lid) qed next fix g assume "Vector_Spaces.linear (*s) (*s) g" "g \ f = id" then have "matrix g ** matrix f = mat 1" by (metis assms matrix_compose_gen matrix_id_mat_1) then show "\B. B ** matrix f = mat 1" .. qed lemma matrix_left_invertible: "linear f \ ((\B. B ** matrix f = mat 1) \ (\g. linear g \ g \ f = id))" for f::"real^'m \ real^'n" using matrix_left_invertible_gen[of f] by (auto simp: linear_matrix_vector_mul_eq) lemma matrix_right_invertible_gen: fixes f :: "'a::field^'m \ 'a^'n" assumes "Vector_Spaces.linear (*s) (*s) f" shows "((\B. matrix f ** B = mat 1) \ (\g. Vector_Spaces.linear (*s) (*s) g \ f \ g = id))" proof safe fix B assume 1: "matrix f ** B = mat 1" show "\g. Vector_Spaces.linear (*s) (*s) g \ f \ g = id" proof (intro exI conjI) show "Vector_Spaces.linear (*s) (*s) ((*v) B)" by simp show "f \ (*v) B = id" using 1 assms comp_apply eq_id_iff vec.linear_id matrix_id_mat_1 matrix_vector_mul_assoc matrix_works by (metis (no_types, hide_lams)) qed next fix g assume "Vector_Spaces.linear (*s) (*s) g" and "f \ g = id" then have "matrix f ** matrix g = mat 1" by (metis assms matrix_compose_gen matrix_id_mat_1) then show "\B. matrix f ** B = mat 1" .. qed lemma matrix_right_invertible: "linear f \ ((\B. matrix f ** B = mat 1) \ (\g. linear g \ f \ g = id))" for f::"real^'m \ real^'n" using matrix_right_invertible_gen[of f] by (auto simp: linear_matrix_vector_mul_eq) lemma matrix_invertible_gen: fixes f :: "'a::field^'m \ 'a::field^'n" assumes "Vector_Spaces.linear (*s) (*s) f" shows "invertible (matrix f) \ (\g. Vector_Spaces.linear (*s) (*s) g \ f \ g = id \ g \ f = id)" (is "?lhs = ?rhs") proof assume ?lhs then show ?rhs by (metis assms invertible_def left_right_inverse_eq matrix_left_invertible_gen matrix_right_invertible_gen) next assume ?rhs then show ?lhs by (metis assms invertible_def matrix_compose_gen matrix_id_mat_1) qed lemma matrix_invertible: "linear f \ invertible (matrix f) \ (\g. linear g \ f \ g = id \ g \ f = id)" for f::"real^'m \ real^'n" using matrix_invertible_gen[of f] by (auto simp: linear_matrix_vector_mul_eq) lemma invertible_eq_bij: fixes m :: "'a::field^'m^'n" shows "invertible m \ bij ((*v) m)" using matrix_invertible_gen[OF matrix_vector_mul_linear_gen, of m, simplified matrix_of_matrix_vector_mul] by (metis bij_betw_def left_right_inverse_eq matrix_vector_mul_linear_gen o_bij vec.linear_injective_left_inverse vec.linear_surjective_right_inverse) subsection \Cramer's rule\ lemma cramer_lemma_transpose: fixes A:: "'a::{field}^'n^'n" and x :: "'a::{field}^'n" shows "det ((\ i. if i = k then sum (\i. x$i *s row i A) (UNIV::'n set) else row i A)::'a::{field}^'n^'n) = x$k * det A" (is "?lhs = ?rhs") proof - let ?U = "UNIV :: 'n set" let ?Uk = "?U - {k}" have U: "?U = insert k ?Uk" by blast have kUk: "k \ ?Uk" by simp have th00: "\k s. x$k *s row k A + s = (x$k - 1) *s row k A + row k A + s" by (vector field_simps) have th001: "\f k . (\x. if x = k then f k else f x) = f" by auto have "(\ i. row i A) = A" by (vector row_def) then have thd1: "det (\ i. row i A) = det A" by simp have thd0: "det (\ i. if i = k then row k A + (\i \ ?Uk. x $ i *s row i A) else row i A) = det A" by (force intro: det_row_span vec.span_sum vec.span_scale vec.span_base) show "?lhs = x$k * det A" apply (subst U) unfolding sum.insert[OF finite kUk] apply (subst th00) unfolding add.assoc apply (subst det_row_add) unfolding thd0 unfolding det_row_mul unfolding th001[of k "\i. row i A"] unfolding thd1 apply (simp add: field_simps) done qed proposition cramer_lemma: fixes A :: "'a::{field}^'n^'n" shows "det((\ i j. if j = k then (A *v x)$i else A$i$j):: 'a::{field}^'n^'n) = x$k * det A" proof - let ?U = "UNIV :: 'n set" have *: "\c. sum (\i. c i *s row i (transpose A)) ?U = sum (\i. c i *s column i A) ?U" by (auto intro: sum.cong) show ?thesis unfolding matrix_mult_sum unfolding cramer_lemma_transpose[of k x "transpose A", unfolded det_transpose, symmetric] unfolding *[of "\i. x$i"] apply (subst det_transpose[symmetric]) apply (rule cong[OF refl[of det]]) apply (vector transpose_def column_def row_def) done qed proposition cramer: fixes A ::"'a::{field}^'n^'n" assumes d0: "det A \ 0" shows "A *v x = b \ x = (\ k. det(\ i j. if j=k then b$i else A$i$j) / det A)" proof - from d0 obtain B where B: "A ** B = mat 1" "B ** A = mat 1" unfolding invertible_det_nz[symmetric] invertible_def by blast have "(A ** B) *v b = b" by (simp add: B) then have "A *v (B *v b) = b" by (simp add: matrix_vector_mul_assoc) then have xe: "\x. A *v x = b" by blast { fix x assume x: "A *v x = b" have "x = (\ k. det(\ i j. if j=k then b$i else A$i$j) / det A)" unfolding x[symmetric] using d0 by (simp add: vec_eq_iff cramer_lemma field_simps) } with xe show ?thesis by auto qed lemma det_1: "det (A::'a::comm_ring_1^1^1) = A$1$1" by (simp add: det_def sign_id) lemma det_2: "det (A::'a::comm_ring_1^2^2) = A$1$1 * A$2$2 - A$1$2 * A$2$1" proof - have f12: "finite {2::2}" "1 \ {2::2}" by auto show ?thesis unfolding det_def UNIV_2 unfolding sum_over_permutations_insert[OF f12] unfolding permutes_sing by (simp add: sign_swap_id sign_id swap_id_eq) qed lemma det_3: "det (A::'a::comm_ring_1^3^3) = A$1$1 * A$2$2 * A$3$3 + A$1$2 * A$2$3 * A$3$1 + A$1$3 * A$2$1 * A$3$2 - A$1$1 * A$2$3 * A$3$2 - A$1$2 * A$2$1 * A$3$3 - A$1$3 * A$2$2 * A$3$1" proof - have f123: "finite {2::3, 3}" "1 \ {2::3, 3}" by auto have f23: "finite {3::3}" "2 \ {3::3}" by auto show ?thesis unfolding det_def UNIV_3 unfolding sum_over_permutations_insert[OF f123] unfolding sum_over_permutations_insert[OF f23] unfolding permutes_sing by (simp add: sign_swap_id permutation_swap_id sign_compose sign_id swap_id_eq) qed proposition det_orthogonal_matrix: fixes Q:: "'a::linordered_idom^'n^'n" assumes oQ: "orthogonal_matrix Q" shows "det Q = 1 \ det Q = - 1" proof - have "Q ** transpose Q = mat 1" by (metis oQ orthogonal_matrix_def) then have "det (Q ** transpose Q) = det (mat 1:: 'a^'n^'n)" by simp then have "det Q * det Q = 1" by (simp add: det_mul) then show ?thesis by (simp add: square_eq_1_iff) qed proposition orthogonal_transformation_det [simp]: fixes f :: "real^'n \ real^'n" shows "orthogonal_transformation f \ \det (matrix f)\ = 1" using det_orthogonal_matrix orthogonal_transformation_matrix by fastforce subsection \Rotation, reflection, rotoinversion\ definition\<^marker>\tag important\ "rotation_matrix Q \ orthogonal_matrix Q \ det Q = 1" definition\<^marker>\tag important\ "rotoinversion_matrix Q \ orthogonal_matrix Q \ det Q = - 1" lemma orthogonal_rotation_or_rotoinversion: fixes Q :: "'a::linordered_idom^'n^'n" shows " orthogonal_matrix Q \ rotation_matrix Q \ rotoinversion_matrix Q" by (metis rotoinversion_matrix_def rotation_matrix_def det_orthogonal_matrix) text\ Slightly stronger results giving rotation, but only in two or more dimensions\ lemma rotation_matrix_exists_basis: fixes a :: "real^'n" assumes 2: "2 \ CARD('n)" and "norm a = 1" obtains A where "rotation_matrix A" "A *v (axis k 1) = a" proof - obtain A where "orthogonal_matrix A" and A: "A *v (axis k 1) = a" using orthogonal_matrix_exists_basis assms by metis with orthogonal_rotation_or_rotoinversion consider "rotation_matrix A" | "rotoinversion_matrix A" by metis then show thesis proof cases assume "rotation_matrix A" then show ?thesis using \A *v axis k 1 = a\ that by auto next from ex_card[OF 2] obtain h i::'n where "h \ i" by (auto simp add: eval_nat_numeral card_Suc_eq) then obtain j where "j \ k" by (metis (full_types)) let ?TA = "transpose A" let ?A = "\ i. if i = j then - 1 *\<^sub>R (?TA $ i) else ?TA $i" assume "rotoinversion_matrix A" then have [simp]: "det A = -1" by (simp add: rotoinversion_matrix_def) show ?thesis proof have [simp]: "row i (\ i. if i = j then - 1 *\<^sub>R ?TA $ i else ?TA $ i) = (if i = j then - row i ?TA else row i ?TA)" for i by (auto simp: row_def) have "orthogonal_matrix ?A" unfolding orthogonal_matrix_orthonormal_rows using \orthogonal_matrix A\ by (auto simp: orthogonal_matrix_orthonormal_columns orthogonal_clauses) then show "rotation_matrix (transpose ?A)" unfolding rotation_matrix_def by (simp add: det_row_mul[of j _ "\i. ?TA $ i", unfolded scalar_mult_eq_scaleR]) show "transpose ?A *v axis k 1 = a" using \j \ k\ A by (simp add: matrix_vector_column axis_def scalar_mult_eq_scaleR if_distrib [of "\z. z *\<^sub>R c" for c] cong: if_cong) qed qed qed lemma rotation_exists_1: fixes a :: "real^'n" assumes "2 \ CARD('n)" "norm a = 1" "norm b = 1" obtains f where "orthogonal_transformation f" "det(matrix f) = 1" "f a = b" proof - obtain k::'n where True by simp obtain A B where AB: "rotation_matrix A" "rotation_matrix B" and eq: "A *v (axis k 1) = a" "B *v (axis k 1) = b" using rotation_matrix_exists_basis assms by metis let ?f = "\x. (B ** transpose A) *v x" show thesis proof show "orthogonal_transformation ?f" using AB orthogonal_matrix_mul orthogonal_transformation_matrix rotation_matrix_def matrix_vector_mul_linear by force show "det (matrix ?f) = 1" using AB by (auto simp: det_mul rotation_matrix_def) show "?f a = b" using AB unfolding orthogonal_matrix_def rotation_matrix_def by (metis eq matrix_mul_rid matrix_vector_mul_assoc) qed qed lemma rotation_exists: fixes a :: "real^'n" assumes 2: "2 \ CARD('n)" and eq: "norm a = norm b" obtains f where "orthogonal_transformation f" "det(matrix f) = 1" "f a = b" proof (cases "a = 0 \ b = 0") case True with assms have "a = 0" "b = 0" by auto then show ?thesis by (metis eq_id_iff matrix_id orthogonal_transformation_id that) next case False then obtain f where f: "orthogonal_transformation f" "det (matrix f) = 1" and f': "f (a /\<^sub>R norm a) = b /\<^sub>R norm b" using rotation_exists_1 [of "a /\<^sub>R norm a" "b /\<^sub>R norm b", OF 2] by auto then interpret linear f by (simp add: orthogonal_transformation) have "f a = b" using f' False by (simp add: eq scale) with f show thesis .. qed lemma rotation_rightward_line: fixes a :: "real^'n" obtains f where "orthogonal_transformation f" "2 \ CARD('n) \ det(matrix f) = 1" "f(norm a *\<^sub>R axis k 1) = a" proof (cases "CARD('n) = 1") case True obtain f where "orthogonal_transformation f" "f (norm a *\<^sub>R axis k (1::real)) = a" proof (rule orthogonal_transformation_exists) show "norm (norm a *\<^sub>R axis k (1::real)) = norm a" by simp qed auto then show thesis using True that by auto next case False obtain f where "orthogonal_transformation f" "det(matrix f) = 1" "f (norm a *\<^sub>R axis k 1) = a" proof (rule rotation_exists) show "2 \ CARD('n)" using False one_le_card_finite [where 'a='n] by linarith show "norm (norm a *\<^sub>R axis k (1::real)) = norm a" by simp qed auto then show thesis using that by blast qed end diff --git a/src/HOL/Analysis/Elementary_Topology.thy b/src/HOL/Analysis/Elementary_Topology.thy --- a/src/HOL/Analysis/Elementary_Topology.thy +++ b/src/HOL/Analysis/Elementary_Topology.thy @@ -1,2755 +1,2666 @@ (* Author: L C Paulson, University of Cambridge Author: Amine Chaieb, University of Cambridge Author: Robert Himmelmann, TU Muenchen Author: Brian Huffman, Portland State University *) chapter \Topology\ theory Elementary_Topology imports "HOL-Library.Set_Idioms" "HOL-Library.Disjoint_Sets" Product_Vector begin section \Elementary Topology\ subsection \TODO: move?\ lemma open_subopen: "open S \ (\x\S. \T. open T \ x \ T \ T \ S)" using openI by auto -subsubsection\<^marker>\tag unimportant\ \Archimedean properties and useful consequences\ - -text\Bernoulli's inequality\ -proposition Bernoulli_inequality: - fixes x :: real - assumes "-1 \ x" - shows "1 + n * x \ (1 + x) ^ n" -proof (induct n) - case 0 - then show ?case by simp -next - case (Suc n) - have "1 + Suc n * x \ 1 + (Suc n)*x + n * x^2" - by (simp add: algebra_simps) - also have "... = (1 + x) * (1 + n*x)" - by (auto simp: power2_eq_square algebra_simps of_nat_Suc) - also have "... \ (1 + x) ^ Suc n" - using Suc.hyps assms mult_left_mono by fastforce - finally show ?case . -qed - -corollary Bernoulli_inequality_even: - fixes x :: real - assumes "even n" - shows "1 + n * x \ (1 + x) ^ n" -proof (cases "-1 \ x \ n=0") - case True - then show ?thesis - by (auto simp: Bernoulli_inequality) -next - case False - then have "real n \ 1" - by simp - with False have "n * x \ -1" - by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one) - then have "1 + n * x \ 0" - by auto - also have "... \ (1 + x) ^ n" - using assms - using zero_le_even_power by blast - finally show ?thesis . -qed - -corollary real_arch_pow: - fixes x :: real - assumes x: "1 < x" - shows "\n. y < x^n" -proof - - from x have x0: "x - 1 > 0" - by arith - from reals_Archimedean3[OF x0, rule_format, of y] - obtain n :: nat where n: "y < real n * (x - 1)" by metis - from x0 have x00: "x- 1 \ -1" by arith - from Bernoulli_inequality[OF x00, of n] n - have "y < x^n" by auto - then show ?thesis by metis -qed - -corollary real_arch_pow_inv: - fixes x y :: real - assumes y: "y > 0" - and x1: "x < 1" - shows "\n. x^n < y" -proof (cases "x > 0") - case True - with x1 have ix: "1 < 1/x" by (simp add: field_simps) - from real_arch_pow[OF ix, of "1/y"] - obtain n where n: "1/y < (1/x)^n" by blast - then show ?thesis using y \x > 0\ - by (auto simp add: field_simps) -next - case False - with y x1 show ?thesis - by (metis less_le_trans not_less power_one_right) -qed - -lemma forall_pos_mono: - "(\d e::real. d < e \ P d \ P e) \ - (\n::nat. n \ 0 \ P (inverse (real n))) \ (\e. 0 < e \ P e)" - by (metis real_arch_inverse) - -lemma forall_pos_mono_1: - "(\d e::real. d < e \ P d \ P e) \ - (\n. P (inverse (real (Suc n)))) \ 0 < e \ P e" - apply (rule forall_pos_mono) - apply auto - apply (metis Suc_pred of_nat_Suc) - done - subsubsection\<^marker>\tag unimportant\ \Affine transformations of intervals\ lemma real_affinity_le: "0 < m \ m * x + c \ y \ x \ inverse m * y + - (c / m)" for m :: "'a::linordered_field" by (simp add: field_simps) lemma real_le_affinity: "0 < m \ y \ m * x + c \ inverse m * y + - (c / m) \ x" for m :: "'a::linordered_field" by (simp add: field_simps) lemma real_affinity_lt: "0 < m \ m * x + c < y \ x < inverse m * y + - (c / m)" for m :: "'a::linordered_field" by (simp add: field_simps) lemma real_lt_affinity: "0 < m \ y < m * x + c \ inverse m * y + - (c / m) < x" for m :: "'a::linordered_field" by (simp add: field_simps) lemma real_affinity_eq: "m \ 0 \ m * x + c = y \ x = inverse m * y + - (c / m)" for m :: "'a::linordered_field" by (simp add: field_simps) lemma real_eq_affinity: "m \ 0 \ y = m * x + c \ inverse m * y + - (c / m) = x" for m :: "'a::linordered_field" by (simp add: field_simps) subsection \Topological Basis\ context topological_space begin definition\<^marker>\tag important\ "topological_basis B \ (\b\B. open b) \ (\x. open x \ (\B'. B' \ B \ \B' = x))" lemma topological_basis: "topological_basis B \ (\x. open x \ (\B'. B' \ B \ \B' = x))" unfolding topological_basis_def apply safe apply fastforce apply fastforce apply (erule_tac x=x in allE, simp) apply (rule_tac x="{x}" in exI, auto) done lemma topological_basis_iff: assumes "\B'. B' \ B \ open B'" shows "topological_basis B \ (\O'. open O' \ (\x\O'. \B'\B. x \ B' \ B' \ O'))" (is "_ \ ?rhs") proof safe fix O' and x::'a assume H: "topological_basis B" "open O'" "x \ O'" then have "(\B'\B. \B' = O')" by (simp add: topological_basis_def) then obtain B' where "B' \ B" "O' = \B'" by auto then show "\B'\B. x \ B' \ B' \ O'" using H by auto next assume H: ?rhs show "topological_basis B" using assms unfolding topological_basis_def proof safe fix O' :: "'a set" assume "open O'" with H obtain f where "\x\O'. f x \ B \ x \ f x \ f x \ O'" by (force intro: bchoice simp: Bex_def) then show "\B'\B. \B' = O'" by (auto intro: exI[where x="{f x |x. x \ O'}"]) qed qed lemma topological_basisI: assumes "\B'. B' \ B \ open B'" and "\O' x. open O' \ x \ O' \ \B'\B. x \ B' \ B' \ O'" shows "topological_basis B" using assms by (subst topological_basis_iff) auto lemma topological_basisE: fixes O' assumes "topological_basis B" and "open O'" and "x \ O'" obtains B' where "B' \ B" "x \ B'" "B' \ O'" proof atomize_elim from assms have "\B'. B'\B \ open B'" by (simp add: topological_basis_def) with topological_basis_iff assms show "\B'. B' \ B \ x \ B' \ B' \ O'" using assms by (simp add: Bex_def) qed lemma topological_basis_open: assumes "topological_basis B" and "X \ B" shows "open X" using assms by (simp add: topological_basis_def) lemma topological_basis_imp_subbasis: assumes B: "topological_basis B" shows "open = generate_topology B" proof (intro ext iffI) fix S :: "'a set" assume "open S" with B obtain B' where "B' \ B" "S = \B'" unfolding topological_basis_def by blast then show "generate_topology B S" by (auto intro: generate_topology.intros dest: topological_basis_open) next fix S :: "'a set" assume "generate_topology B S" then show "open S" by induct (auto dest: topological_basis_open[OF B]) qed lemma basis_dense: fixes B :: "'a set set" and f :: "'a set \ 'a" assumes "topological_basis B" and choosefrom_basis: "\B'. B' \ {} \ f B' \ B'" shows "\X. open X \ X \ {} \ (\B' \ B. f B' \ X)" proof (intro allI impI) fix X :: "'a set" assume "open X" and "X \ {}" from topological_basisE[OF \topological_basis B\ \open X\ choosefrom_basis[OF \X \ {}\]] obtain B' where "B' \ B" "f X \ B'" "B' \ X" . then show "\B'\B. f B' \ X" by (auto intro!: choosefrom_basis) qed end lemma topological_basis_prod: assumes A: "topological_basis A" and B: "topological_basis B" shows "topological_basis ((\(a, b). a \ b) ` (A \ B))" unfolding topological_basis_def proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric]) fix S :: "('a \ 'b) set" assume "open S" then show "\X\A \ B. (\(a,b)\X. a \ b) = S" proof (safe intro!: exI[of _ "{x\A \ B. fst x \ snd x \ S}"]) fix x y assume "(x, y) \ S" from open_prod_elim[OF \open S\ this] obtain a b where a: "open a""x \ a" and b: "open b" "y \ b" and "a \ b \ S" by (metis mem_Sigma_iff) moreover from A a obtain A0 where "A0 \ A" "x \ A0" "A0 \ a" by (rule topological_basisE) moreover from B b obtain B0 where "B0 \ B" "y \ B0" "B0 \ b" by (rule topological_basisE) ultimately show "(x, y) \ (\(a, b)\{X \ A \ B. fst X \ snd X \ S}. a \ b)" by (intro UN_I[of "(A0, B0)"]) auto qed auto qed (metis A B topological_basis_open open_Times) subsection \Countable Basis\ locale\<^marker>\tag important\ countable_basis = topological_space p for p::"'a set \ bool" + fixes B :: "'a set set" assumes is_basis: "topological_basis B" and countable_basis: "countable B" begin lemma open_countable_basis_ex: assumes "p X" shows "\B' \ B. X = \B'" using assms countable_basis is_basis unfolding topological_basis_def by blast lemma open_countable_basisE: assumes "p X" obtains B' where "B' \ B" "X = \B'" using assms open_countable_basis_ex by atomize_elim simp lemma countable_dense_exists: "\D::'a set. countable D \ (\X. p X \ X \ {} \ (\d \ D. d \ X))" proof - let ?f = "(\B'. SOME x. x \ B')" have "countable (?f ` B)" using countable_basis by simp with basis_dense[OF is_basis, of ?f] show ?thesis by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI) qed lemma countable_dense_setE: obtains D :: "'a set" where "countable D" "\X. p X \ X \ {} \ \d \ D. d \ X" using countable_dense_exists by blast end lemma countable_basis_openI: "countable_basis open B" if "countable B" "topological_basis B" using that by unfold_locales (simp_all add: topological_basis topological_space.topological_basis topological_space_axioms) lemma (in first_countable_topology) first_countable_basisE: fixes x :: 'a obtains \ where "countable \" "\A. A \ \ \ x \ A" "\A. A \ \ \ open A" "\S. open S \ x \ S \ (\A\\. A \ S)" proof - obtain \ where \: "(\i::nat. x \ \ i \ open (\ i))" "(\S. open S \ x \ S \ (\i. \ i \ S))" using first_countable_basis[of x] by metis show thesis proof show "countable (range \)" by simp qed (use \ in auto) qed lemma (in first_countable_topology) first_countable_basis_Int_stableE: obtains \ where "countable \" "\A. A \ \ \ x \ A" "\A. A \ \ \ open A" "\S. open S \ x \ S \ (\A\\. A \ S)" "\A B. A \ \ \ B \ \ \ A \ B \ \" proof atomize_elim obtain \ where \: "countable \" "\B. B \ \ \ x \ B" "\B. B \ \ \ open B" "\S. open S \ x \ S \ \B\\. B \ S" by (rule first_countable_basisE) blast define \ where [abs_def]: "\ = (\N. \((\n. from_nat_into \ n) ` N)) ` (Collect finite::nat set set)" then show "\\. countable \ \ (\A. A \ \ \ x \ A) \ (\A. A \ \ \ open A) \ (\S. open S \ x \ S \ (\A\\. A \ S)) \ (\A B. A \ \ \ B \ \ \ A \ B \ \)" proof (safe intro!: exI[where x=\]) show "countable \" unfolding \_def by (intro countable_image countable_Collect_finite) fix A assume "A \ \" then show "x \ A" "open A" using \(4)[OF open_UNIV] by (auto simp: \_def intro: \ from_nat_into) next let ?int = "\N. \(from_nat_into \ ` N)" fix A B assume "A \ \" "B \ \" then obtain N M where "A = ?int N" "B = ?int M" "finite (N \ M)" by (auto simp: \_def) then show "A \ B \ \" by (auto simp: \_def intro!: image_eqI[where x="N \ M"]) next fix S assume "open S" "x \ S" then obtain a where a: "a\\" "a \ S" using \ by blast then show "\a\\. a \ S" using a \ by (intro bexI[where x=a]) (auto simp: \_def intro: image_eqI[where x="{to_nat_on \ a}"]) qed qed lemma (in topological_space) first_countableI: assumes "countable \" and 1: "\A. A \ \ \ x \ A" "\A. A \ \ \ open A" and 2: "\S. open S \ x \ S \ \A\\. A \ S" shows "\\::nat \ 'a set. (\i. x \ \ i \ open (\ i)) \ (\S. open S \ x \ S \ (\i. \ i \ S))" proof (safe intro!: exI[of _ "from_nat_into \"]) fix i have "\ \ {}" using 2[of UNIV] by auto show "x \ from_nat_into \ i" "open (from_nat_into \ i)" using range_from_nat_into_subset[OF \\ \ {}\] 1 by auto next fix S assume "open S" "x\S" from 2[OF this] show "\i. from_nat_into \ i \ S" using subset_range_from_nat_into[OF \countable \\] by auto qed instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology proof fix x :: "'a \ 'b" obtain \ where \: "countable \" "\a. a \ \ \ fst x \ a" "\a. a \ \ \ open a" "\S. open S \ fst x \ S \ \a\\. a \ S" by (rule first_countable_basisE[of "fst x"]) blast obtain B where B: "countable B" "\a. a \ B \ snd x \ a" "\a. a \ B \ open a" "\S. open S \ snd x \ S \ \a\B. a \ S" by (rule first_countable_basisE[of "snd x"]) blast show "\\::nat \ ('a \ 'b) set. (\i. x \ \ i \ open (\ i)) \ (\S. open S \ x \ S \ (\i. \ i \ S))" proof (rule first_countableI[of "(\(a, b). a \ b) ` (\ \ B)"], safe) fix a b assume x: "a \ \" "b \ B" show "x \ a \ b" by (simp add: \(2) B(2) mem_Times_iff x) show "open (a \ b)" by (simp add: \(3) B(3) open_Times x) next fix S assume "open S" "x \ S" then obtain a' b' where a'b': "open a'" "open b'" "x \ a' \ b'" "a' \ b' \ S" by (rule open_prod_elim) moreover from a'b' \(4)[of a'] B(4)[of b'] obtain a b where "a \ \" "a \ a'" "b \ B" "b \ b'" by auto ultimately show "\a\(\(a, b). a \ b) ` (\ \ B). a \ S" by (auto intro!: bexI[of _ "a \ b"] bexI[of _ a] bexI[of _ b]) qed (simp add: \ B) qed class second_countable_topology = topological_space + assumes ex_countable_subbasis: "\B::'a set set. countable B \ open = generate_topology B" begin lemma ex_countable_basis: "\B::'a set set. countable B \ topological_basis B" proof - from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B" by blast let ?B = "Inter ` {b. finite b \ b \ B }" show ?thesis proof (intro exI conjI) show "countable ?B" by (intro countable_image countable_Collect_finite_subset B) { fix S assume "open S" then have "\B'\{b. finite b \ b \ B}. (\b\B'. \b) = S" unfolding B proof induct case UNIV show ?case by (intro exI[of _ "{{}}"]) simp next case (Int a b) then obtain x y where x: "a = \(Inter ` x)" "\i. i \ x \ finite i \ i \ B" and y: "b = \(Inter ` y)" "\i. i \ y \ finite i \ i \ B" by blast show ?case unfolding x y Int_UN_distrib2 by (intro exI[of _ "{i \ j| i j. i \ x \ j \ y}"]) (auto dest: x(2) y(2)) next case (UN K) then have "\k\K. \B'\{b. finite b \ b \ B}. \ (Inter ` B') = k" by auto then obtain k where "\ka\K. k ka \ {b. finite b \ b \ B} \ \(Inter ` (k ka)) = ka" unfolding bchoice_iff .. then show "\B'\{b. finite b \ b \ B}. \ (Inter ` B') = \K" by (intro exI[of _ "\(k ` K)"]) auto next case (Basis S) then show ?case by (intro exI[of _ "{{S}}"]) auto qed then have "(\B'\Inter ` {b. finite b \ b \ B}. \B' = S)" unfolding subset_image_iff by blast } then show "topological_basis ?B" unfolding topological_basis_def by (safe intro!: open_Inter) (simp_all add: B generate_topology.Basis subset_eq) qed qed end lemma univ_second_countable: obtains \ :: "'a::second_countable_topology set set" where "countable \" "\C. C \ \ \ open C" "\S. open S \ \U. U \ \ \ S = \U" by (metis ex_countable_basis topological_basis_def) proposition Lindelof: fixes \ :: "'a::second_countable_topology set set" assumes \: "\S. S \ \ \ open S" obtains \' where "\' \ \" "countable \'" "\\' = \\" proof - obtain \ :: "'a set set" where "countable \" "\C. C \ \ \ open C" and \: "\S. open S \ \U. U \ \ \ S = \U" using univ_second_countable by blast define \ where "\ \ {S. S \ \ \ (\U. U \ \ \ S \ U)}" have "countable \" apply (rule countable_subset [OF _ \countable \\]) apply (force simp: \_def) done have "\S. \U. S \ \ \ U \ \ \ S \ U" by (simp add: \_def) then obtain G where G: "\S. S \ \ \ G S \ \ \ S \ G S" by metis have "\\ \ \\" unfolding \_def by (blast dest: \ \) moreover have "\\ \ \\" using \_def by blast ultimately have eq1: "\\ = \\" .. have eq2: "\\ = \ (G ` \)" using G eq1 by auto show ?thesis apply (rule_tac \' = "G ` \" in that) using G \countable \\ by (auto simp: eq1 eq2) qed lemma countable_disjoint_open_subsets: fixes \ :: "'a::second_countable_topology set set" assumes "\S. S \ \ \ open S" and pw: "pairwise disjnt \" shows "countable \" proof - obtain \' where "\' \ \" "countable \'" "\\' = \\" by (meson assms Lindelof) with pw have "\ \ insert {} \'" by (fastforce simp add: pairwise_def disjnt_iff) then show ?thesis by (simp add: \countable \'\ countable_subset) qed sublocale second_countable_topology < countable_basis "open" "SOME B. countable B \ topological_basis B" using someI_ex[OF ex_countable_basis] by unfold_locales safe instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology proof obtain A :: "'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto moreover obtain B :: "'b set set" where "countable B" "topological_basis B" using ex_countable_basis by auto ultimately show "\B::('a \ 'b) set set. countable B \ open = generate_topology B" by (auto intro!: exI[of _ "(\(a, b). a \ b) ` (A \ B)"] topological_basis_prod topological_basis_imp_subbasis) qed instance second_countable_topology \ first_countable_topology proof fix x :: 'a define B :: "'a set set" where "B = (SOME B. countable B \ topological_basis B)" then have B: "countable B" "topological_basis B" using countable_basis is_basis by (auto simp: countable_basis is_basis) then show "\A::nat \ 'a set. (\i. x \ A i \ open (A i)) \ (\S. open S \ x \ S \ (\i. A i \ S))" by (intro first_countableI[of "{b\B. x \ b}"]) (fastforce simp: topological_space_class.topological_basis_def)+ qed instance nat :: second_countable_topology proof show "\B::nat set set. countable B \ open = generate_topology B" by (intro exI[of _ "range lessThan \ range greaterThan"]) (auto simp: open_nat_def) qed lemma countable_separating_set_linorder1: shows "\B::('a::{linorder_topology, second_countable_topology} set). countable B \ (\x y. x < y \ (\b \ B. x < b \ b \ y))" proof - obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto define B1 where "B1 = {(LEAST x. x \ U)| U. U \ A}" then have "countable B1" using \countable A\ by (simp add: Setcompr_eq_image) define B2 where "B2 = {(SOME x. x \ U)| U. U \ A}" then have "countable B2" using \countable A\ by (simp add: Setcompr_eq_image) have "\b \ B1 \ B2. x < b \ b \ y" if "x < y" for x y proof (cases) assume "\z. x < z \ z < y" then obtain z where z: "x < z \ z < y" by auto define U where "U = {x<.. U" using z U_def by simp ultimately obtain V where "V \ A" "z \ V" "V \ U" using topological_basisE[OF \topological_basis A\] by auto define w where "w = (SOME x. x \ V)" then have "w \ V" using \z \ V\ by (metis someI2) then have "x < w \ w \ y" using \w \ V\ \V \ U\ U_def by fastforce moreover have "w \ B1 \ B2" using w_def B2_def \V \ A\ by auto ultimately show ?thesis by auto next assume "\(\z. x < z \ z < y)" then have *: "\z. z > x \ z \ y" by auto define U where "U = {x<..}" then have "open U" by simp moreover have "y \ U" using \x < y\ U_def by simp ultimately obtain "V" where "V \ A" "y \ V" "V \ U" using topological_basisE[OF \topological_basis A\] by auto have "U = {y..}" unfolding U_def using * \x < y\ by auto then have "V \ {y..}" using \V \ U\ by simp then have "(LEAST w. w \ V) = y" using \y \ V\ by (meson Least_equality atLeast_iff subsetCE) then have "y \ B1 \ B2" using \V \ A\ B1_def by auto moreover have "x < y \ y \ y" using \x < y\ by simp ultimately show ?thesis by auto qed moreover have "countable (B1 \ B2)" using \countable B1\ \countable B2\ by simp ultimately show ?thesis by auto qed lemma countable_separating_set_linorder2: shows "\B::('a::{linorder_topology, second_countable_topology} set). countable B \ (\x y. x < y \ (\b \ B. x \ b \ b < y))" proof - obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto define B1 where "B1 = {(GREATEST x. x \ U) | U. U \ A}" then have "countable B1" using \countable A\ by (simp add: Setcompr_eq_image) define B2 where "B2 = {(SOME x. x \ U)| U. U \ A}" then have "countable B2" using \countable A\ by (simp add: Setcompr_eq_image) have "\b \ B1 \ B2. x \ b \ b < y" if "x < y" for x y proof (cases) assume "\z. x < z \ z < y" then obtain z where z: "x < z \ z < y" by auto define U where "U = {x<.. U" using z U_def by simp ultimately obtain "V" where "V \ A" "z \ V" "V \ U" using topological_basisE[OF \topological_basis A\] by auto define w where "w = (SOME x. x \ V)" then have "w \ V" using \z \ V\ by (metis someI2) then have "x \ w \ w < y" using \w \ V\ \V \ U\ U_def by fastforce moreover have "w \ B1 \ B2" using w_def B2_def \V \ A\ by auto ultimately show ?thesis by auto next assume "\(\z. x < z \ z < y)" then have *: "\z. z < y \ z \ x" using leI by blast define U where "U = {.. U" using \x < y\ U_def by simp ultimately obtain "V" where "V \ A" "x \ V" "V \ U" using topological_basisE[OF \topological_basis A\] by auto have "U = {..x}" unfolding U_def using * \x < y\ by auto then have "V \ {..x}" using \V \ U\ by simp then have "(GREATEST x. x \ V) = x" using \x \ V\ by (meson Greatest_equality atMost_iff subsetCE) then have "x \ B1 \ B2" using \V \ A\ B1_def by auto moreover have "x \ x \ x < y" using \x < y\ by simp ultimately show ?thesis by auto qed moreover have "countable (B1 \ B2)" using \countable B1\ \countable B2\ by simp ultimately show ?thesis by auto qed lemma countable_separating_set_dense_linorder: shows "\B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \ (\x y. x < y \ (\b \ B. x < b \ b < y))" proof - obtain B::"'a set" where B: "countable B" "\x y. x < y \ (\b \ B. x < b \ b \ y)" using countable_separating_set_linorder1 by auto have "\b \ B. x < b \ b < y" if "x < y" for x y proof - obtain z where "x < z" "z < y" using \x < y\ dense by blast then obtain b where "b \ B" "x < b \ b \ z" using B(2) by auto then have "x < b \ b < y" using \z < y\ by auto then show ?thesis using \b \ B\ by auto qed then show ?thesis using B(1) by auto qed subsection \Polish spaces\ text \Textbooks define Polish spaces as completely metrizable. We assume the topology to be complete for a given metric.\ class polish_space = complete_space + second_countable_topology subsection \Limit Points\ definition\<^marker>\tag important\ (in topological_space) islimpt:: "'a \ 'a set \ bool" (infixr "islimpt" 60) where "x islimpt S \ (\T. x\T \ open T \ (\y\S. y\T \ y\x))" lemma islimptI: assumes "\T. x \ T \ open T \ \y\S. y \ T \ y \ x" shows "x islimpt S" using assms unfolding islimpt_def by auto lemma islimptE: assumes "x islimpt S" and "x \ T" and "open T" obtains y where "y \ S" and "y \ T" and "y \ x" using assms unfolding islimpt_def by auto lemma islimpt_iff_eventually: "x islimpt S \ \ eventually (\y. y \ S) (at x)" unfolding islimpt_def eventually_at_topological by auto lemma islimpt_subset: "x islimpt S \ S \ T \ x islimpt T" unfolding islimpt_def by fast lemma islimpt_UNIV_iff: "x islimpt UNIV \ \ open {x}" unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast) lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})" unfolding islimpt_def by blast text \A perfect space has no isolated points.\ lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV" for x :: "'a::perfect_space" unfolding islimpt_UNIV_iff by (rule not_open_singleton) lemma closed_limpt: "closed S \ (\x. x islimpt S \ x \ S)" unfolding closed_def apply (subst open_subopen) apply (simp add: islimpt_def subset_eq) apply (metis ComplE ComplI) done lemma islimpt_EMPTY[simp]: "\ x islimpt {}" by (auto simp: islimpt_def) lemma islimpt_Un: "x islimpt (S \ T) \ x islimpt S \ x islimpt T" by (simp add: islimpt_iff_eventually eventually_conj_iff) lemma islimpt_insert: fixes x :: "'a::t1_space" shows "x islimpt (insert a s) \ x islimpt s" proof assume *: "x islimpt (insert a s)" show "x islimpt s" proof (rule islimptI) fix t assume t: "x \ t" "open t" show "\y\s. y \ t \ y \ x" proof (cases "x = a") case True obtain y where "y \ insert a s" "y \ t" "y \ x" using * t by (rule islimptE) with \x = a\ show ?thesis by auto next case False with t have t': "x \ t - {a}" "open (t - {a})" by (simp_all add: open_Diff) obtain y where "y \ insert a s" "y \ t - {a}" "y \ x" using * t' by (rule islimptE) then show ?thesis by auto qed qed next assume "x islimpt s" then show "x islimpt (insert a s)" by (rule islimpt_subset) auto qed lemma islimpt_finite: fixes x :: "'a::t1_space" shows "finite s \ \ x islimpt s" by (induct set: finite) (simp_all add: islimpt_insert) lemma islimpt_Un_finite: fixes x :: "'a::t1_space" shows "finite s \ x islimpt (s \ t) \ x islimpt t" by (simp add: islimpt_Un islimpt_finite) lemma islimpt_eq_acc_point: fixes l :: "'a :: t1_space" shows "l islimpt S \ (\U. l\U \ open U \ infinite (U \ S))" proof (safe intro!: islimptI) fix U assume "l islimpt S" "l \ U" "open U" "finite (U \ S)" then have "l islimpt S" "l \ (U - (U \ S - {l}))" "open (U - (U \ S - {l}))" by (auto intro: finite_imp_closed) then show False by (rule islimptE) auto next fix T assume *: "\U. l\U \ open U \ infinite (U \ S)" "l \ T" "open T" then have "infinite (T \ S - {l})" by auto then have "\x. x \ (T \ S - {l})" unfolding ex_in_conv by (intro notI) simp then show "\y\S. y \ T \ y \ l" by auto qed lemma acc_point_range_imp_convergent_subsequence: fixes l :: "'a :: first_countable_topology" assumes l: "\U. l\U \ open U \ infinite (U \ range f)" shows "\r::nat\nat. strict_mono r \ (f \ r) \ l" proof - from countable_basis_at_decseq[of l] obtain A where A: "\i. open (A i)" "\i. l \ A i" "\S. open S \ l \ S \ eventually (\i. A i \ S) sequentially" by blast define s where "s n i = (SOME j. i < j \ f j \ A (Suc n))" for n i { fix n i have "infinite (A (Suc n) \ range f - f`{.. i})" using l A by auto then have "\x. x \ A (Suc n) \ range f - f`{.. i}" unfolding ex_in_conv by (intro notI) simp then have "\j. f j \ A (Suc n) \ j \ {.. i}" by auto then have "\a. i < a \ f a \ A (Suc n)" by (auto simp: not_le) then have "i < s n i" "f (s n i) \ A (Suc n)" unfolding s_def by (auto intro: someI2_ex) } note s = this define r where "r = rec_nat (s 0 0) s" have "strict_mono r" by (auto simp: r_def s strict_mono_Suc_iff) moreover have "(\n. f (r n)) \ l" proof (rule topological_tendstoI) fix S assume "open S" "l \ S" with A(3) have "eventually (\i. A i \ S) sequentially" by auto moreover { fix i assume "Suc 0 \ i" then have "f (r i) \ A i" by (cases i) (simp_all add: r_def s) } then have "eventually (\i. f (r i) \ A i) sequentially" by (auto simp: eventually_sequentially) ultimately show "eventually (\i. f (r i) \ S) sequentially" by eventually_elim auto qed ultimately show "\r::nat\nat. strict_mono r \ (f \ r) \ l" by (auto simp: convergent_def comp_def) qed lemma islimpt_range_imp_convergent_subsequence: fixes l :: "'a :: {t1_space, first_countable_topology}" assumes l: "l islimpt (range f)" shows "\r::nat\nat. strict_mono r \ (f \ r) \ l" using l unfolding islimpt_eq_acc_point by (rule acc_point_range_imp_convergent_subsequence) lemma sequence_unique_limpt: fixes f :: "nat \ 'a::t2_space" assumes "(f \ l) sequentially" and "l' islimpt (range f)" shows "l' = l" proof (rule ccontr) assume "l' \ l" obtain s t where "open s" "open t" "l' \ s" "l \ t" "s \ t = {}" using hausdorff [OF \l' \ l\] by auto have "eventually (\n. f n \ t) sequentially" using assms(1) \open t\ \l \ t\ by (rule topological_tendstoD) then obtain N where "\n\N. f n \ t" unfolding eventually_sequentially by auto have "UNIV = {.. {N..}" by auto then have "l' islimpt (f ` ({.. {N..}))" using assms(2) by simp then have "l' islimpt (f ` {.. f ` {N..})" by (simp add: image_Un) then have "l' islimpt (f ` {N..})" by (simp add: islimpt_Un_finite) then obtain y where "y \ f ` {N..}" "y \ s" "y \ l'" using \l' \ s\ \open s\ by (rule islimptE) then obtain n where "N \ n" "f n \ s" "f n \ l'" by auto with \\n\N. f n \ t\ have "f n \ s \ t" by simp with \s \ t = {}\ show False by simp qed subsection \Interior of a Set\ definition\<^marker>\tag important\ interior :: "('a::topological_space) set \ 'a set" where "interior S = \{T. open T \ T \ S}" lemma interiorI [intro?]: assumes "open T" and "x \ T" and "T \ S" shows "x \ interior S" using assms unfolding interior_def by fast lemma interiorE [elim?]: assumes "x \ interior S" obtains T where "open T" and "x \ T" and "T \ S" using assms unfolding interior_def by fast lemma open_interior [simp, intro]: "open (interior S)" by (simp add: interior_def open_Union) lemma interior_subset: "interior S \ S" by (auto simp: interior_def) lemma interior_maximal: "T \ S \ open T \ T \ interior S" by (auto simp: interior_def) lemma interior_open: "open S \ interior S = S" by (intro equalityI interior_subset interior_maximal subset_refl) lemma interior_eq: "interior S = S \ open S" by (metis open_interior interior_open) lemma open_subset_interior: "open S \ S \ interior T \ S \ T" by (metis interior_maximal interior_subset subset_trans) lemma interior_empty [simp]: "interior {} = {}" using open_empty by (rule interior_open) lemma interior_UNIV [simp]: "interior UNIV = UNIV" using open_UNIV by (rule interior_open) lemma interior_interior [simp]: "interior (interior S) = interior S" using open_interior by (rule interior_open) lemma interior_mono: "S \ T \ interior S \ interior T" by (auto simp: interior_def) lemma interior_unique: assumes "T \ S" and "open T" assumes "\T'. T' \ S \ open T' \ T' \ T" shows "interior S = T" by (intro equalityI assms interior_subset open_interior interior_maximal) lemma interior_singleton [simp]: "interior {a} = {}" for a :: "'a::perfect_space" apply (rule interior_unique, simp_all) using not_open_singleton subset_singletonD apply fastforce done lemma interior_Int [simp]: "interior (S \ T) = interior S \ interior T" by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1 Int_lower2 interior_maximal interior_subset open_Int open_interior) lemma eventually_nhds_in_nhd: "x \ interior s \ eventually (\y. y \ s) (nhds x)" using interior_subset[of s] by (subst eventually_nhds) blast lemma interior_limit_point [intro]: fixes x :: "'a::perfect_space" assumes x: "x \ interior S" shows "x islimpt S" using x islimpt_UNIV [of x] unfolding interior_def islimpt_def apply (clarsimp, rename_tac T T') apply (drule_tac x="T \ T'" in spec) apply (auto simp: open_Int) done lemma interior_closed_Un_empty_interior: assumes cS: "closed S" and iT: "interior T = {}" shows "interior (S \ T) = interior S" proof show "interior S \ interior (S \ T)" by (rule interior_mono) (rule Un_upper1) show "interior (S \ T) \ interior S" proof fix x assume "x \ interior (S \ T)" then obtain R where "open R" "x \ R" "R \ S \ T" .. show "x \ interior S" proof (rule ccontr) assume "x \ interior S" with \x \ R\ \open R\ obtain y where "y \ R - S" unfolding interior_def by fast from \open R\ \closed S\ have "open (R - S)" by (rule open_Diff) from \R \ S \ T\ have "R - S \ T" by fast from \y \ R - S\ \open (R - S)\ \R - S \ T\ \interior T = {}\ show False unfolding interior_def by fast qed qed qed lemma interior_Times: "interior (A \ B) = interior A \ interior B" proof (rule interior_unique) show "interior A \ interior B \ A \ B" by (intro Sigma_mono interior_subset) show "open (interior A \ interior B)" by (intro open_Times open_interior) fix T assume "T \ A \ B" and "open T" then show "T \ interior A \ interior B" proof safe fix x y assume "(x, y) \ T" then obtain C D where "open C" "open D" "C \ D \ T" "x \ C" "y \ D" using \open T\ unfolding open_prod_def by fast then have "open C" "open D" "C \ A" "D \ B" "x \ C" "y \ D" using \T \ A \ B\ by auto then show "x \ interior A" and "y \ interior B" by (auto intro: interiorI) qed qed lemma interior_Ici: fixes x :: "'a :: {dense_linorder,linorder_topology}" assumes "b < x" shows "interior {x ..} = {x <..}" proof (rule interior_unique) fix T assume "T \ {x ..}" "open T" moreover have "x \ T" proof assume "x \ T" obtain y where "y < x" "{y <.. x} \ T" using open_left[OF \open T\ \x \ T\ \b < x\] by auto with dense[OF \y < x\] obtain z where "z \ T" "z < x" by (auto simp: subset_eq Ball_def) with \T \ {x ..}\ show False by auto qed ultimately show "T \ {x <..}" by (auto simp: subset_eq less_le) qed auto lemma interior_Iic: fixes x :: "'a ::{dense_linorder,linorder_topology}" assumes "x < b" shows "interior {.. x} = {..< x}" proof (rule interior_unique) fix T assume "T \ {.. x}" "open T" moreover have "x \ T" proof assume "x \ T" obtain y where "x < y" "{x ..< y} \ T" using open_right[OF \open T\ \x \ T\ \x < b\] by auto with dense[OF \x < y\] obtain z where "z \ T" "x < z" by (auto simp: subset_eq Ball_def less_le) with \T \ {.. x}\ show False by auto qed ultimately show "T \ {..< x}" by (auto simp: subset_eq less_le) qed auto lemma countable_disjoint_nonempty_interior_subsets: fixes \ :: "'a::second_countable_topology set set" assumes pw: "pairwise disjnt \" and int: "\S. \S \ \; interior S = {}\ \ S = {}" shows "countable \" proof (rule countable_image_inj_on) have "disjoint (interior ` \)" using pw by (simp add: disjoint_image_subset interior_subset) then show "countable (interior ` \)" by (auto intro: countable_disjoint_open_subsets) show "inj_on interior \" using pw apply (clarsimp simp: inj_on_def pairwise_def) apply (metis disjnt_def disjnt_subset1 inf.orderE int interior_subset) done qed subsection \Closure of a Set\ definition\<^marker>\tag important\ closure :: "('a::topological_space) set \ 'a set" where "closure S = S \ {x . x islimpt S}" lemma interior_closure: "interior S = - (closure (- S))" by (auto simp: interior_def closure_def islimpt_def) lemma closure_interior: "closure S = - interior (- S)" by (simp add: interior_closure) lemma closed_closure[simp, intro]: "closed (closure S)" by (simp add: closure_interior closed_Compl) lemma closure_subset: "S \ closure S" by (simp add: closure_def) lemma closure_hull: "closure S = closed hull S" by (auto simp: hull_def closure_interior interior_def) lemma closure_eq: "closure S = S \ closed S" unfolding closure_hull using closed_Inter by (rule hull_eq) lemma closure_closed [simp]: "closed S \ closure S = S" by (simp only: closure_eq) lemma closure_closure [simp]: "closure (closure S) = closure S" unfolding closure_hull by (rule hull_hull) lemma closure_mono: "S \ T \ closure S \ closure T" unfolding closure_hull by (rule hull_mono) lemma closure_minimal: "S \ T \ closed T \ closure S \ T" unfolding closure_hull by (rule hull_minimal) lemma closure_unique: assumes "S \ T" and "closed T" and "\T'. S \ T' \ closed T' \ T \ T'" shows "closure S = T" using assms unfolding closure_hull by (rule hull_unique) lemma closure_empty [simp]: "closure {} = {}" using closed_empty by (rule closure_closed) lemma closure_UNIV [simp]: "closure UNIV = UNIV" using closed_UNIV by (rule closure_closed) lemma closure_Un [simp]: "closure (S \ T) = closure S \ closure T" by (simp add: closure_interior) lemma closure_eq_empty [iff]: "closure S = {} \ S = {}" using closure_empty closure_subset[of S] by blast lemma closure_subset_eq: "closure S \ S \ closed S" using closure_eq[of S] closure_subset[of S] by simp lemma open_Int_closure_eq_empty: "open S \ (S \ closure T) = {} \ S \ T = {}" using open_subset_interior[of S "- T"] using interior_subset[of "- T"] by (auto simp: closure_interior) lemma open_Int_closure_subset: "open S \ S \ closure T \ closure (S \ T)" proof fix x assume *: "open S" "x \ S \ closure T" have "x islimpt (S \ T)" if **: "x islimpt T" proof (rule islimptI) fix A assume "x \ A" "open A" with * have "x \ A \ S" "open (A \ S)" by (simp_all add: open_Int) with ** obtain y where "y \ T" "y \ A \ S" "y \ x" by (rule islimptE) then have "y \ S \ T" "y \ A \ y \ x" by simp_all then show "\y\(S \ T). y \ A \ y \ x" .. qed with * show "x \ closure (S \ T)" unfolding closure_def by blast qed lemma closure_complement: "closure (- S) = - interior S" by (simp add: closure_interior) lemma interior_complement: "interior (- S) = - closure S" by (simp add: closure_interior) lemma interior_diff: "interior(S - T) = interior S - closure T" by (simp add: Diff_eq interior_complement) lemma closure_Times: "closure (A \ B) = closure A \ closure B" proof (rule closure_unique) show "A \ B \ closure A \ closure B" by (intro Sigma_mono closure_subset) show "closed (closure A \ closure B)" by (intro closed_Times closed_closure) fix T assume "A \ B \ T" and "closed T" then show "closure A \ closure B \ T" apply (simp add: closed_def open_prod_def, clarify) apply (rule ccontr) apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D) apply (simp add: closure_interior interior_def) apply (drule_tac x=C in spec) apply (drule_tac x=D in spec, auto) done qed lemma islimpt_in_closure: "(x islimpt S) = (x\closure(S-{x}))" unfolding closure_def using islimpt_punctured by blast lemma connected_imp_connected_closure: "connected S \ connected (closure S)" by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD) lemma bdd_below_closure: fixes A :: "real set" assumes "bdd_below A" shows "bdd_below (closure A)" proof - from assms obtain m where "\x. x \ A \ m \ x" by (auto simp: bdd_below_def) then have "A \ {m..}" by auto then have "closure A \ {m..}" using closed_real_atLeast by (rule closure_minimal) then show ?thesis by (auto simp: bdd_below_def) qed subsection \Frontier (also known as boundary)\ definition\<^marker>\tag important\ frontier :: "('a::topological_space) set \ 'a set" where "frontier S = closure S - interior S" lemma frontier_closed [iff]: "closed (frontier S)" by (simp add: frontier_def closed_Diff) lemma frontier_closures: "frontier S = closure S \ closure (- S)" by (auto simp: frontier_def interior_closure) lemma frontier_Int: "frontier(S \ T) = closure(S \ T) \ (frontier S \ frontier T)" proof - have "closure (S \ T) \ closure S" "closure (S \ T) \ closure T" by (simp_all add: closure_mono) then show ?thesis by (auto simp: frontier_closures) qed lemma frontier_Int_subset: "frontier(S \ T) \ frontier S \ frontier T" by (auto simp: frontier_Int) lemma frontier_Int_closed: assumes "closed S" "closed T" shows "frontier(S \ T) = (frontier S \ T) \ (S \ frontier T)" proof - have "closure (S \ T) = T \ S" using assms by (simp add: Int_commute closed_Int) moreover have "T \ (closure S \ closure (- S)) = frontier S \ T" by (simp add: Int_commute frontier_closures) ultimately show ?thesis by (simp add: Int_Un_distrib Int_assoc Int_left_commute assms frontier_closures) qed lemma frontier_subset_closed: "closed S \ frontier S \ S" by (metis frontier_def closure_closed Diff_subset) lemma frontier_empty [simp]: "frontier {} = {}" by (simp add: frontier_def) lemma frontier_subset_eq: "frontier S \ S \ closed S" proof - { assume "frontier S \ S" then have "closure S \ S" using interior_subset unfolding frontier_def by auto then have "closed S" using closure_subset_eq by auto } then show ?thesis using frontier_subset_closed[of S] .. qed lemma frontier_complement [simp]: "frontier (- S) = frontier S" by (auto simp: frontier_def closure_complement interior_complement) lemma frontier_Un_subset: "frontier(S \ T) \ frontier S \ frontier T" by (metis compl_sup frontier_Int_subset frontier_complement) lemma frontier_disjoint_eq: "frontier S \ S = {} \ open S" using frontier_complement frontier_subset_eq[of "- S"] unfolding open_closed by auto lemma frontier_UNIV [simp]: "frontier UNIV = {}" using frontier_complement frontier_empty by fastforce lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)" by (simp add: Int_commute frontier_def interior_closure) lemma frontier_interior_subset: "frontier(interior S) \ frontier S" by (simp add: Diff_mono frontier_interiors interior_mono interior_subset) lemma closure_Un_frontier: "closure S = S \ frontier S" proof - have "S \ interior S = S" using interior_subset by auto then show ?thesis using closure_subset by (auto simp: frontier_def) qed subsection\<^marker>\tag unimportant\ \Filters and the ``eventually true'' quantifier\ text \Identify Trivial limits, where we can't approach arbitrarily closely.\ lemma trivial_limit_within: "trivial_limit (at a within S) \ \ a islimpt S" proof assume "trivial_limit (at a within S)" then show "\ a islimpt S" unfolding trivial_limit_def unfolding eventually_at_topological unfolding islimpt_def apply (clarsimp simp add: set_eq_iff) apply (rename_tac T, rule_tac x=T in exI) apply (clarsimp, drule_tac x=y in bspec, simp_all) done next assume "\ a islimpt S" then show "trivial_limit (at a within S)" unfolding trivial_limit_def eventually_at_topological islimpt_def by metis qed lemma trivial_limit_at_iff: "trivial_limit (at a) \ \ a islimpt UNIV" using trivial_limit_within [of a UNIV] by simp lemma trivial_limit_at: "\ trivial_limit (at a)" for a :: "'a::perfect_space" by (rule at_neq_bot) lemma not_trivial_limit_within: "\ trivial_limit (at x within S) = (x \ closure (S - {x}))" using islimpt_in_closure by (metis trivial_limit_within) lemma not_in_closure_trivial_limitI: "x \ closure s \ trivial_limit (at x within s)" using not_trivial_limit_within[of x s] by safe (metis Diff_empty Diff_insert0 closure_subset contra_subsetD) lemma filterlim_at_within_closure_implies_filterlim: "filterlim f l (at x within s)" if "x \ closure s \ filterlim f l (at x within s)" by (metis bot.extremum filterlim_filtercomap filterlim_mono not_in_closure_trivial_limitI that) lemma at_within_eq_bot_iff: "at c within A = bot \ c \ closure (A - {c})" using not_trivial_limit_within[of c A] by blast text \Some property holds "sufficiently close" to the limit point.\ lemma trivial_limit_eventually: "trivial_limit net \ eventually P net" by simp lemma trivial_limit_eq: "trivial_limit net \ (\P. eventually P net)" by (simp add: filter_eq_iff) lemma Lim_topological: "(f \ l) net \ trivial_limit net \ (\S. open S \ l \ S \ eventually (\x. f x \ S) net)" unfolding tendsto_def trivial_limit_eq by auto lemma eventually_within_Un: "eventually P (at x within (s \ t)) \ eventually P (at x within s) \ eventually P (at x within t)" unfolding eventually_at_filter by (auto elim!: eventually_rev_mp) lemma Lim_within_union: "(f \ l) (at x within (s \ t)) \ (f \ l) (at x within s) \ (f \ l) (at x within t)" unfolding tendsto_def by (auto simp: eventually_within_Un) subsection \Limits\ text \The expected monotonicity property.\ lemma Lim_Un: assumes "(f \ l) (at x within S)" "(f \ l) (at x within T)" shows "(f \ l) (at x within (S \ T))" using assms unfolding at_within_union by (rule filterlim_sup) lemma Lim_Un_univ: "(f \ l) (at x within S) \ (f \ l) (at x within T) \ S \ T = UNIV \ (f \ l) (at x)" by (metis Lim_Un) text \Interrelations between restricted and unrestricted limits.\ lemma Lim_at_imp_Lim_at_within: "(f \ l) (at x) \ (f \ l) (at x within S)" by (metis order_refl filterlim_mono subset_UNIV at_le) lemma eventually_within_interior: assumes "x \ interior S" shows "eventually P (at x within S) \ eventually P (at x)" (is "?lhs = ?rhs") proof from assms obtain T where T: "open T" "x \ T" "T \ S" .. { assume ?lhs then obtain A where "open A" and "x \ A" and "\y\A. y \ x \ y \ S \ P y" by (auto simp: eventually_at_topological) with T have "open (A \ T)" and "x \ A \ T" and "\y \ A \ T. y \ x \ P y" by auto then show ?rhs by (auto simp: eventually_at_topological) next assume ?rhs then show ?lhs by (auto elim: eventually_mono simp: eventually_at_filter) } qed lemma at_within_interior: "x \ interior S \ at x within S = at x" unfolding filter_eq_iff by (intro allI eventually_within_interior) lemma Lim_within_LIMSEQ: fixes a :: "'a::first_countable_topology" assumes "\S. (\n. S n \ a \ S n \ T) \ S \ a \ (\n. X (S n)) \ L" shows "(X \ L) (at a within T)" using assms unfolding tendsto_def [where l=L] by (simp add: sequentially_imp_eventually_within) lemma Lim_right_bound: fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \ 'b::{linorder_topology, conditionally_complete_linorder}" assumes mono: "\a b. a \ I \ b \ I \ x < a \ a \ b \ f a \ f b" and bnd: "\a. a \ I \ x < a \ K \ f a" shows "(f \ Inf (f ` ({x<..} \ I))) (at x within ({x<..} \ I))" proof (cases "{x<..} \ I = {}") case True then show ?thesis by simp next case False show ?thesis proof (rule order_tendstoI) fix a assume a: "a < Inf (f ` ({x<..} \ I))" { fix y assume "y \ {x<..} \ I" with False bnd have "Inf (f ` ({x<..} \ I)) \ f y" by (auto intro!: cInf_lower bdd_belowI2) with a have "a < f y" by (blast intro: less_le_trans) } then show "eventually (\x. a < f x) (at x within ({x<..} \ I))" by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one) next fix a assume "Inf (f ` ({x<..} \ I)) < a" from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \ I" "f y < a" by auto then have "eventually (\x. x \ I \ f x < a) (at_right x)" unfolding eventually_at_right[OF \x < y\] by (metis less_imp_le le_less_trans mono) then show "eventually (\x. f x < a) (at x within ({x<..} \ I))" unfolding eventually_at_filter by eventually_elim simp qed qed (*could prove directly from islimpt_sequential_inj, but only for metric spaces*) lemma islimpt_sequential: fixes x :: "'a::first_countable_topology" shows "x islimpt S \ (\f. (\n::nat. f n \ S - {x}) \ (f \ x) sequentially)" (is "?lhs = ?rhs") proof assume ?lhs from countable_basis_at_decseq[of x] obtain A where A: "\i. open (A i)" "\i. x \ A i" "\S. open S \ x \ S \ eventually (\i. A i \ S) sequentially" by blast define f where "f n = (SOME y. y \ S \ y \ A n \ x \ y)" for n { fix n from \?lhs\ have "\y. y \ S \ y \ A n \ x \ y" unfolding islimpt_def using A(1,2)[of n] by auto then have "f n \ S \ f n \ A n \ x \ f n" unfolding f_def by (rule someI_ex) then have "f n \ S" "f n \ A n" "x \ f n" by auto } then have "\n. f n \ S - {x}" by auto moreover have "(\n. f n) \ x" proof (rule topological_tendstoI) fix S assume "open S" "x \ S" from A(3)[OF this] \\n. f n \ A n\ show "eventually (\x. f x \ S) sequentially" by (auto elim!: eventually_mono) qed ultimately show ?rhs by fast next assume ?rhs then obtain f :: "nat \ 'a" where f: "\n. f n \ S - {x}" and lim: "f \ x" by auto show ?lhs unfolding islimpt_def proof safe fix T assume "open T" "x \ T" from lim[THEN topological_tendstoD, OF this] f show "\y\S. y \ T \ y \ x" unfolding eventually_sequentially by auto qed qed text\These are special for limits out of the same topological space.\ lemma Lim_within_id: "(id \ a) (at a within s)" unfolding id_def by (rule tendsto_ident_at) lemma Lim_at_id: "(id \ a) (at a)" unfolding id_def by (rule tendsto_ident_at) text\It's also sometimes useful to extract the limit point from the filter.\ abbreviation netlimit :: "'a::t2_space filter \ 'a" where "netlimit F \ Lim F (\x. x)" lemma netlimit_at [simp]: fixes a :: "'a::{perfect_space,t2_space}" shows "netlimit (at a) = a" using Lim_ident_at [of a UNIV] by simp lemma lim_within_interior: "x \ interior S \ (f \ l) (at x within S) \ (f \ l) (at x)" by (metis at_within_interior) lemma netlimit_within_interior: fixes x :: "'a::{t2_space,perfect_space}" assumes "x \ interior S" shows "netlimit (at x within S) = x" using assms by (metis at_within_interior netlimit_at) text\Useful lemmas on closure and set of possible sequential limits.\ lemma closure_sequential: fixes l :: "'a::first_countable_topology" shows "l \ closure S \ (\x. (\n. x n \ S) \ (x \ l) sequentially)" (is "?lhs = ?rhs") proof assume "?lhs" moreover { assume "l \ S" then have "?rhs" using tendsto_const[of l sequentially] by auto } moreover { assume "l islimpt S" then have "?rhs" unfolding islimpt_sequential by auto } ultimately show "?rhs" unfolding closure_def by auto next assume "?rhs" then show "?lhs" unfolding closure_def islimpt_sequential by auto qed lemma closed_sequential_limits: fixes S :: "'a::first_countable_topology set" shows "closed S \ (\x l. (\n. x n \ S) \ (x \ l) sequentially \ l \ S)" by (metis closure_sequential closure_subset_eq subset_iff) lemma tendsto_If_within_closures: assumes f: "x \ s \ (closure s \ closure t) \ (f \ l x) (at x within s \ (closure s \ closure t))" assumes g: "x \ t \ (closure s \ closure t) \ (g \ l x) (at x within t \ (closure s \ closure t))" assumes "x \ s \ t" shows "((\x. if x \ s then f x else g x) \ l x) (at x within s \ t)" proof - have *: "(s \ t) \ {x. x \ s} = s" "(s \ t) \ {x. x \ s} = t - s" by auto have "(f \ l x) (at x within s)" by (rule filterlim_at_within_closure_implies_filterlim) (use \x \ _\ in \auto simp: inf_commute closure_def intro: tendsto_within_subset[OF f]\) moreover have "(g \ l x) (at x within t - s)" by (rule filterlim_at_within_closure_implies_filterlim) (use \x \ _\ in \auto intro!: tendsto_within_subset[OF g] simp: closure_def intro: islimpt_subset\) ultimately show ?thesis by (intro filterlim_at_within_If) (simp_all only: *) qed subsection \Compactness\ lemma brouwer_compactness_lemma: fixes f :: "'a::topological_space \ 'b::real_normed_vector" assumes "compact s" and "continuous_on s f" and "\ (\x\s. f x = 0)" obtains d where "0 < d" and "\x\s. d \ norm (f x)" proof (cases "s = {}") case True show thesis by (rule that [of 1]) (auto simp: True) next case False have "continuous_on s (norm \ f)" by (rule continuous_intros continuous_on_norm assms(2))+ with False obtain x where x: "x \ s" "\y\s. (norm \ f) x \ (norm \ f) y" using continuous_attains_inf[OF assms(1), of "norm \ f"] unfolding o_def by auto have "(norm \ f) x > 0" using assms(3) and x(1) by auto then show ?thesis by (rule that) (insert x(2), auto simp: o_def) qed subsubsection \Bolzano-Weierstrass property\ proposition Heine_Borel_imp_Bolzano_Weierstrass: assumes "compact s" and "infinite t" and "t \ s" shows "\x \ s. x islimpt t" proof (rule ccontr) assume "\ (\x \ s. x islimpt t)" then obtain f where f: "\x\s. x \ f x \ open (f x) \ (\y\t. y \ f x \ y = x)" unfolding islimpt_def using bchoice[of s "\ x T. x \ T \ open T \ (\y\t. y \ T \ y = x)"] by auto obtain g where g: "g \ {t. \x. x \ s \ t = f x}" "finite g" "s \ \g" using assms(1)[unfolded compact_eq_Heine_Borel, THEN spec[where x="{t. \x. x\s \ t = f x}"]] using f by auto from g(1,3) have g':"\x\g. \xa \ s. x = f xa" by auto { fix x y assume "x \ t" "y \ t" "f x = f y" then have "x \ f x" "y \ f x \ y = x" using f[THEN bspec[where x=x]] and \t \ s\ by auto then have "x = y" using \f x = f y\ and f[THEN bspec[where x=y]] and \y \ t\ and \t \ s\ by auto } then have "inj_on f t" unfolding inj_on_def by simp then have "infinite (f ` t)" using assms(2) using finite_imageD by auto moreover { fix x assume "x \ t" "f x \ g" from g(3) assms(3) \x \ t\ obtain h where "h \ g" and "x \ h" by auto then obtain y where "y \ s" "h = f y" using g'[THEN bspec[where x=h]] by auto then have "y = x" using f[THEN bspec[where x=y]] and \x\t\ and \x\h\[unfolded \h = f y\] by auto then have False using \f x \ g\ \h \ g\ unfolding \h = f y\ by auto } then have "f ` t \ g" by auto ultimately show False using g(2) using finite_subset by auto qed lemma sequence_infinite_lemma: fixes f :: "nat \ 'a::t1_space" assumes "\n. f n \ l" and "(f \ l) sequentially" shows "infinite (range f)" proof assume "finite (range f)" then have "closed (range f)" by (rule finite_imp_closed) then have "open (- range f)" by (rule open_Compl) from assms(1) have "l \ - range f" by auto from assms(2) have "eventually (\n. f n \ - range f) sequentially" using \open (- range f)\ \l \ - range f\ by (rule topological_tendstoD) then show False unfolding eventually_sequentially by auto qed lemma Bolzano_Weierstrass_imp_closed: fixes s :: "'a::{first_countable_topology,t2_space} set" assumes "\t. infinite t \ t \ s --> (\x \ s. x islimpt t)" shows "closed s" proof - { fix x l assume as: "\n::nat. x n \ s" "(x \ l) sequentially" then have "l \ s" proof (cases "\n. x n \ l") case False then show "l\s" using as(1) by auto next case True note cas = this with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto then obtain l' where "l'\s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto then show "l\s" using sequence_unique_limpt[of x l l'] using as cas by auto qed } then show ?thesis unfolding closed_sequential_limits by fast qed lemma closure_insert: fixes x :: "'a::t1_space" shows "closure (insert x s) = insert x (closure s)" apply (rule closure_unique) apply (rule insert_mono [OF closure_subset]) apply (rule closed_insert [OF closed_closure]) apply (simp add: closure_minimal) done text\In particular, some common special cases.\ lemma compact_Un [intro]: assumes "compact s" and "compact t" shows " compact (s \ t)" proof (rule compactI) fix f assume *: "Ball f open" "s \ t \ \f" from * \compact s\ obtain s' where "s' \ f \ finite s' \ s \ \s'" unfolding compact_eq_Heine_Borel by (auto elim!: allE[of _ f]) moreover from * \compact t\ obtain t' where "t' \ f \ finite t' \ t \ \t'" unfolding compact_eq_Heine_Borel by (auto elim!: allE[of _ f]) ultimately show "\f'\f. finite f' \ s \ t \ \f'" by (auto intro!: exI[of _ "s' \ t'"]) qed lemma compact_Union [intro]: "finite S \ (\T. T \ S \ compact T) \ compact (\S)" by (induct set: finite) auto lemma compact_UN [intro]: "finite A \ (\x. x \ A \ compact (B x)) \ compact (\x\A. B x)" by (rule compact_Union) auto lemma closed_Int_compact [intro]: assumes "closed s" and "compact t" shows "compact (s \ t)" using compact_Int_closed [of t s] assms by (simp add: Int_commute) lemma compact_Int [intro]: fixes s t :: "'a :: t2_space set" assumes "compact s" and "compact t" shows "compact (s \ t)" using assms by (intro compact_Int_closed compact_imp_closed) lemma compact_sing [simp]: "compact {a}" unfolding compact_eq_Heine_Borel by auto lemma compact_insert [simp]: assumes "compact s" shows "compact (insert x s)" proof - have "compact ({x} \ s)" using compact_sing assms by (rule compact_Un) then show ?thesis by simp qed lemma finite_imp_compact: "finite s \ compact s" by (induct set: finite) simp_all lemma open_delete: fixes s :: "'a::t1_space set" shows "open s \ open (s - {x})" by (simp add: open_Diff) text\Compactness expressed with filters\ lemma closure_iff_nhds_not_empty: "x \ closure X \ (\A. \S\A. open S \ x \ S \ X \ A \ {})" proof safe assume x: "x \ closure X" fix S A assume "open S" "x \ S" "X \ A = {}" "S \ A" then have "x \ closure (-S)" by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI) with x have "x \ closure X - closure (-S)" by auto also have "\ \ closure (X \ S)" using \open S\ open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps) finally have "X \ S \ {}" by auto then show False using \X \ A = {}\ \S \ A\ by auto next assume "\A S. S \ A \ open S \ x \ S \ X \ A \ {}" from this[THEN spec, of "- X", THEN spec, of "- closure X"] show "x \ closure X" by (simp add: closure_subset open_Compl) qed lemma compact_filter: "compact U \ (\F. F \ bot \ eventually (\x. x \ U) F \ (\x\U. inf (nhds x) F \ bot))" proof (intro allI iffI impI compact_fip[THEN iffD2] notI) fix F assume "compact U" assume F: "F \ bot" "eventually (\x. x \ U) F" then have "U \ {}" by (auto simp: eventually_False) define Z where "Z = closure ` {A. eventually (\x. x \ A) F}" then have "\z\Z. closed z" by auto moreover have ev_Z: "\z. z \ Z \ eventually (\x. x \ z) F" unfolding Z_def by (auto elim: eventually_mono intro: subsetD[OF closure_subset]) have "(\B \ Z. finite B \ U \ \B \ {})" proof (intro allI impI) fix B assume "finite B" "B \ Z" with \finite B\ ev_Z F(2) have "eventually (\x. x \ U \ (\B)) F" by (auto simp: eventually_ball_finite_distrib eventually_conj_iff) with F show "U \ \B \ {}" by (intro notI) (simp add: eventually_False) qed ultimately have "U \ \Z \ {}" using \compact U\ unfolding compact_fip by blast then obtain x where "x \ U" and x: "\z. z \ Z \ x \ z" by auto have "\P. eventually P (inf (nhds x) F) \ P \ bot" unfolding eventually_inf eventually_nhds proof safe fix P Q R S assume "eventually R F" "open S" "x \ S" with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"] have "S \ {x. R x} \ {}" by (auto simp: Z_def) moreover assume "Ball S Q" "\x. Q x \ R x \ bot x" ultimately show False by (auto simp: set_eq_iff) qed with \x \ U\ show "\x\U. inf (nhds x) F \ bot" by (metis eventually_bot) next fix A assume A: "\a\A. closed a" "\B\A. finite B \ U \ \B \ {}" "U \ \A = {}" define F where "F = (INF a\insert U A. principal a)" have "F \ bot" unfolding F_def proof (rule INF_filter_not_bot) fix X assume X: "X \ insert U A" "finite X" with A(2)[THEN spec, of "X - {U}"] have "U \ \(X - {U}) \ {}" by auto with X show "(INF a\X. principal a) \ bot" by (auto simp: INF_principal_finite principal_eq_bot_iff) qed moreover have "F \ principal U" unfolding F_def by auto then have "eventually (\x. x \ U) F" by (auto simp: le_filter_def eventually_principal) moreover assume "\F. F \ bot \ eventually (\x. x \ U) F \ (\x\U. inf (nhds x) F \ bot)" ultimately obtain x where "x \ U" and x: "inf (nhds x) F \ bot" by auto { fix V assume "V \ A" then have "F \ principal V" unfolding F_def by (intro INF_lower2[of V]) auto then have V: "eventually (\x. x \ V) F" by (auto simp: le_filter_def eventually_principal) have "x \ closure V" unfolding closure_iff_nhds_not_empty proof (intro impI allI) fix S A assume "open S" "x \ S" "S \ A" then have "eventually (\x. x \ A) (nhds x)" by (auto simp: eventually_nhds) with V have "eventually (\x. x \ V \ A) (inf (nhds x) F)" by (auto simp: eventually_inf) with x show "V \ A \ {}" by (auto simp del: Int_iff simp add: trivial_limit_def) qed then have "x \ V" using \V \ A\ A(1) by simp } with \x\U\ have "x \ U \ \A" by auto with \U \ \A = {}\ show False by auto qed definition\<^marker>\tag important\ countably_compact :: "('a::topological_space) set \ bool" where "countably_compact U \ (\A. countable A \ (\a\A. open a) \ U \ \A \ (\T\A. finite T \ U \ \T))" lemma countably_compactE: assumes "countably_compact s" and "\t\C. open t" and "s \ \C" "countable C" obtains C' where "C' \ C" and "finite C'" and "s \ \C'" using assms unfolding countably_compact_def by metis lemma countably_compactI: assumes "\C. \t\C. open t \ s \ \C \ countable C \ (\C'\C. finite C' \ s \ \C')" shows "countably_compact s" using assms unfolding countably_compact_def by metis lemma compact_imp_countably_compact: "compact U \ countably_compact U" by (auto simp: compact_eq_Heine_Borel countably_compact_def) lemma countably_compact_imp_compact: assumes "countably_compact U" and ccover: "countable B" "\b\B. open b" and basis: "\T x. open T \ x \ T \ x \ U \ \b\B. x \ b \ b \ U \ T" shows "compact U" using \countably_compact U\ unfolding compact_eq_Heine_Borel countably_compact_def proof safe fix A assume A: "\a\A. open a" "U \ \A" assume *: "\A. countable A \ (\a\A. open a) \ U \ \A \ (\T\A. finite T \ U \ \T)" moreover define C where "C = {b\B. \a\A. b \ U \ a}" ultimately have "countable C" "\a\C. open a" unfolding C_def using ccover by auto moreover have "\A \ U \ \C" proof safe fix x a assume "x \ U" "x \ a" "a \ A" with basis[of a x] A obtain b where "b \ B" "x \ b" "b \ U \ a" by blast with \a \ A\ show "x \ \C" unfolding C_def by auto qed then have "U \ \C" using \U \ \A\ by auto ultimately obtain T where T: "T\C" "finite T" "U \ \T" using * by metis then have "\t\T. \a\A. t \ U \ a" by (auto simp: C_def) then obtain f where "\t\T. f t \ A \ t \ U \ f t" unfolding bchoice_iff Bex_def .. with T show "\T\A. finite T \ U \ \T" unfolding C_def by (intro exI[of _ "f`T"]) fastforce qed proposition countably_compact_imp_compact_second_countable: "countably_compact U \ compact (U :: 'a :: second_countable_topology set)" proof (rule countably_compact_imp_compact) fix T and x :: 'a assume "open T" "x \ T" from topological_basisE[OF is_basis this] obtain b where "b \ (SOME B. countable B \ topological_basis B)" "x \ b" "b \ T" . then show "\b\SOME B. countable B \ topological_basis B. x \ b \ b \ U \ T" by blast qed (insert countable_basis topological_basis_open[OF is_basis], auto) lemma countably_compact_eq_compact: "countably_compact U \ compact (U :: 'a :: second_countable_topology set)" using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast subsubsection\Sequential compactness\ definition\<^marker>\tag important\ seq_compact :: "'a::topological_space set \ bool" where "seq_compact S \ (\f. (\n. f n \ S) \ (\l\S. \r::nat\nat. strict_mono r \ ((f \ r) \ l) sequentially))" lemma seq_compactI: assumes "\f. \n. f n \ S \ \l\S. \r::nat\nat. strict_mono r \ ((f \ r) \ l) sequentially" shows "seq_compact S" unfolding seq_compact_def using assms by fast lemma seq_compactE: assumes "seq_compact S" "\n. f n \ S" obtains l r where "l \ S" "strict_mono (r :: nat \ nat)" "((f \ r) \ l) sequentially" using assms unfolding seq_compact_def by fast lemma closed_sequentially: (* TODO: move upwards *) assumes "closed s" and "\n. f n \ s" and "f \ l" shows "l \ s" proof (rule ccontr) assume "l \ s" with \closed s\ and \f \ l\ have "eventually (\n. f n \ - s) sequentially" by (fast intro: topological_tendstoD) with \\n. f n \ s\ show "False" by simp qed lemma seq_compact_Int_closed: assumes "seq_compact s" and "closed t" shows "seq_compact (s \ t)" proof (rule seq_compactI) fix f assume "\n::nat. f n \ s \ t" hence "\n. f n \ s" and "\n. f n \ t" by simp_all from \seq_compact s\ and \\n. f n \ s\ obtain l r where "l \ s" and r: "strict_mono r" and l: "(f \ r) \ l" by (rule seq_compactE) from \\n. f n \ t\ have "\n. (f \ r) n \ t" by simp from \closed t\ and this and l have "l \ t" by (rule closed_sequentially) with \l \ s\ and r and l show "\l\s \ t. \r. strict_mono r \ (f \ r) \ l" by fast qed lemma seq_compact_closed_subset: assumes "closed s" and "s \ t" and "seq_compact t" shows "seq_compact s" using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1) lemma seq_compact_imp_countably_compact: fixes U :: "'a :: first_countable_topology set" assumes "seq_compact U" shows "countably_compact U" proof (safe intro!: countably_compactI) fix A assume A: "\a\A. open a" "U \ \A" "countable A" have subseq: "\X. range X \ U \ \r x. x \ U \ strict_mono (r :: nat \ nat) \ (X \ r) \ x" using \seq_compact U\ by (fastforce simp: seq_compact_def subset_eq) show "\T\A. finite T \ U \ \T" proof cases assume "finite A" with A show ?thesis by auto next assume "infinite A" then have "A \ {}" by auto show ?thesis proof (rule ccontr) assume "\ (\T\A. finite T \ U \ \T)" then have "\T. \x. T \ A \ finite T \ (x \ U - \T)" by auto then obtain X' where T: "\T. T \ A \ finite T \ X' T \ U - \T" by metis define X where "X n = X' (from_nat_into A ` {.. n})" for n have X: "\n. X n \ U - (\i\n. from_nat_into A i)" using \A \ {}\ unfolding X_def by (intro T) (auto intro: from_nat_into) then have "range X \ U" by auto with subseq[of X] obtain r x where "x \ U" and r: "strict_mono r" "(X \ r) \ x" by auto from \x\U\ \U \ \A\ from_nat_into_surj[OF \countable A\] obtain n where "x \ from_nat_into A n" by auto with r(2) A(1) from_nat_into[OF \A \ {}\, of n] have "eventually (\i. X (r i) \ from_nat_into A n) sequentially" unfolding tendsto_def by (auto simp: comp_def) then obtain N where "\i. N \ i \ X (r i) \ from_nat_into A n" by (auto simp: eventually_sequentially) moreover from X have "\i. n \ r i \ X (r i) \ from_nat_into A n" by auto moreover from \strict_mono r\[THEN seq_suble, of "max n N"] have "\i. n \ r i \ N \ i" by (auto intro!: exI[of _ "max n N"]) ultimately show False by auto qed qed qed lemma compact_imp_seq_compact: fixes U :: "'a :: first_countable_topology set" assumes "compact U" shows "seq_compact U" unfolding seq_compact_def proof safe fix X :: "nat \ 'a" assume "\n. X n \ U" then have "eventually (\x. x \ U) (filtermap X sequentially)" by (auto simp: eventually_filtermap) moreover have "filtermap X sequentially \ bot" by (simp add: trivial_limit_def eventually_filtermap) ultimately obtain x where "x \ U" and x: "inf (nhds x) (filtermap X sequentially) \ bot" (is "?F \ _") using \compact U\ by (auto simp: compact_filter) from countable_basis_at_decseq[of x] obtain A where A: "\i. open (A i)" "\i. x \ A i" "\S. open S \ x \ S \ eventually (\i. A i \ S) sequentially" by blast define s where "s n i = (SOME j. i < j \ X j \ A (Suc n))" for n i { fix n i have "\a. i < a \ X a \ A (Suc n)" proof (rule ccontr) assume "\ (\a>i. X a \ A (Suc n))" then have "\a. Suc i \ a \ X a \ A (Suc n)" by auto then have "eventually (\x. x \ A (Suc n)) (filtermap X sequentially)" by (auto simp: eventually_filtermap eventually_sequentially) moreover have "eventually (\x. x \ A (Suc n)) (nhds x)" using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds) ultimately have "eventually (\x. False) ?F" by (auto simp: eventually_inf) with x show False by (simp add: eventually_False) qed then have "i < s n i" "X (s n i) \ A (Suc n)" unfolding s_def by (auto intro: someI2_ex) } note s = this define r where "r = rec_nat (s 0 0) s" have "strict_mono r" by (auto simp: r_def s strict_mono_Suc_iff) moreover have "(\n. X (r n)) \ x" proof (rule topological_tendstoI) fix S assume "open S" "x \ S" with A(3) have "eventually (\i. A i \ S) sequentially" by auto moreover { fix i assume "Suc 0 \ i" then have "X (r i) \ A i" by (cases i) (simp_all add: r_def s) } then have "eventually (\i. X (r i) \ A i) sequentially" by (auto simp: eventually_sequentially) ultimately show "eventually (\i. X (r i) \ S) sequentially" by eventually_elim auto qed ultimately show "\x \ U. \r. strict_mono r \ (X \ r) \ x" using \x \ U\ by (auto simp: convergent_def comp_def) qed lemma countably_compact_imp_acc_point: assumes "countably_compact s" and "countable t" and "infinite t" and "t \ s" shows "\x\s. \U. x\U \ open U \ infinite (U \ t)" proof (rule ccontr) define C where "C = (\F. interior (F \ (- t))) ` {F. finite F \ F \ t }" note \countably_compact s\ moreover have "\t\C. open t" by (auto simp: C_def) moreover assume "\ (\x\s. \U. x\U \ open U \ infinite (U \ t))" then have s: "\x. x \ s \ \U. x\U \ open U \ finite (U \ t)" by metis have "s \ \C" using \t \ s\ unfolding C_def apply (safe dest!: s) apply (rule_tac a="U \ t" in UN_I) apply (auto intro!: interiorI simp add: finite_subset) done moreover from \countable t\ have "countable C" unfolding C_def by (auto intro: countable_Collect_finite_subset) ultimately obtain D where "D \ C" "finite D" "s \ \D" by (rule countably_compactE) then obtain E where E: "E \ {F. finite F \ F \ t }" "finite E" and s: "s \ (\F\E. interior (F \ (- t)))" by (metis (lifting) finite_subset_image C_def) from s \t \ s\ have "t \ \E" using interior_subset by blast moreover have "finite (\E)" using E by auto ultimately show False using \infinite t\ by (auto simp: finite_subset) qed lemma countable_acc_point_imp_seq_compact: fixes s :: "'a::first_countable_topology set" assumes "\t. infinite t \ countable t \ t \ s \ (\x\s. \U. x\U \ open U \ infinite (U \ t))" shows "seq_compact s" proof - { fix f :: "nat \ 'a" assume f: "\n. f n \ s" have "\l\s. \r. strict_mono r \ ((f \ r) \ l) sequentially" proof (cases "finite (range f)") case True obtain l where "infinite {n. f n = f l}" using pigeonhole_infinite[OF _ True] by auto then obtain r :: "nat \ nat" where "strict_mono r" and fr: "\n. f (r n) = f l" using infinite_enumerate by blast then have "strict_mono r \ (f \ r) \ f l" by (simp add: fr o_def) with f show "\l\s. \r. strict_mono r \ (f \ r) \ l" by auto next case False with f assms have "\x\s. \U. x\U \ open U \ infinite (U \ range f)" by auto then obtain l where "l \ s" "\U. l\U \ open U \ infinite (U \ range f)" .. from this(2) have "\r. strict_mono r \ ((f \ r) \ l) sequentially" using acc_point_range_imp_convergent_subsequence[of l f] by auto with \l \ s\ show "\l\s. \r. strict_mono r \ ((f \ r) \ l) sequentially" .. qed } then show ?thesis unfolding seq_compact_def by auto qed lemma seq_compact_eq_countably_compact: fixes U :: "'a :: first_countable_topology set" shows "seq_compact U \ countably_compact U" using countable_acc_point_imp_seq_compact countably_compact_imp_acc_point seq_compact_imp_countably_compact by metis lemma seq_compact_eq_acc_point: fixes s :: "'a :: first_countable_topology set" shows "seq_compact s \ (\t. infinite t \ countable t \ t \ s --> (\x\s. \U. x\U \ open U \ infinite (U \ t)))" using countable_acc_point_imp_seq_compact[of s] countably_compact_imp_acc_point[of s] seq_compact_imp_countably_compact[of s] by metis lemma seq_compact_eq_compact: fixes U :: "'a :: second_countable_topology set" shows "seq_compact U \ compact U" using seq_compact_eq_countably_compact countably_compact_eq_compact by blast proposition Bolzano_Weierstrass_imp_seq_compact: fixes s :: "'a::{t1_space, first_countable_topology} set" shows "\t. infinite t \ t \ s \ (\x \ s. x islimpt t) \ seq_compact s" by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point) subsection\<^marker>\tag unimportant\ \Cartesian products\ lemma seq_compact_Times: "seq_compact s \ seq_compact t \ seq_compact (s \ t)" unfolding seq_compact_def apply clarify apply (drule_tac x="fst \ f" in spec) apply (drule mp, simp add: mem_Times_iff) apply (clarify, rename_tac l1 r1) apply (drule_tac x="snd \ f \ r1" in spec) apply (drule mp, simp add: mem_Times_iff) apply (clarify, rename_tac l2 r2) apply (rule_tac x="(l1, l2)" in rev_bexI, simp) apply (rule_tac x="r1 \ r2" in exI) apply (rule conjI, simp add: strict_mono_def) apply (drule_tac f=r2 in LIMSEQ_subseq_LIMSEQ, assumption) apply (drule (1) tendsto_Pair) back apply (simp add: o_def) done lemma compact_Times: assumes "compact s" "compact t" shows "compact (s \ t)" proof (rule compactI) fix C assume C: "\t\C. open t" "s \ t \ \C" have "\x\s. \a. open a \ x \ a \ (\d\C. finite d \ a \ t \ \d)" proof fix x assume "x \ s" have "\y\t. \a b c. c \ C \ open a \ open b \ x \ a \ y \ b \ a \ b \ c" (is "\y\t. ?P y") proof fix y assume "y \ t" with \x \ s\ C obtain c where "c \ C" "(x, y) \ c" "open c" by auto then show "?P y" by (auto elim!: open_prod_elim) qed then obtain a b c where b: "\y. y \ t \ open (b y)" and c: "\y. y \ t \ c y \ C \ open (a y) \ open (b y) \ x \ a y \ y \ b y \ a y \ b y \ c y" by metis then have "\y\t. open (b y)" "t \ (\y\t. b y)" by auto with compactE_image[OF \compact t\] obtain D where D: "D \ t" "finite D" "t \ (\y\D. b y)" by metis moreover from D c have "(\y\D. a y) \ t \ (\y\D. c y)" by (fastforce simp: subset_eq) ultimately show "\a. open a \ x \ a \ (\d\C. finite d \ a \ t \ \d)" using c by (intro exI[of _ "c`D"] exI[of _ "\(a`D)"] conjI) (auto intro!: open_INT) qed then obtain a d where a: "\x. x\s \ open (a x)" "s \ (\x\s. a x)" and d: "\x. x \ s \ d x \ C \ finite (d x) \ a x \ t \ \(d x)" unfolding subset_eq UN_iff by metis moreover from compactE_image[OF \compact s\ a] obtain e where e: "e \ s" "finite e" and s: "s \ (\x\e. a x)" by auto moreover { from s have "s \ t \ (\x\e. a x \ t)" by auto also have "\ \ (\x\e. \(d x))" using d \e \ s\ by (intro UN_mono) auto finally have "s \ t \ (\x\e. \(d x))" . } ultimately show "\C'\C. finite C' \ s \ t \ \C'" by (intro exI[of _ "(\x\e. d x)"]) (auto simp: subset_eq) qed lemma tube_lemma: assumes "compact K" assumes "open W" assumes "{x0} \ K \ W" shows "\X0. x0 \ X0 \ open X0 \ X0 \ K \ W" proof - { fix y assume "y \ K" then have "(x0, y) \ W" using assms by auto with \open W\ have "\X0 Y. open X0 \ open Y \ x0 \ X0 \ y \ Y \ X0 \ Y \ W" by (rule open_prod_elim) blast } then obtain X0 Y where *: "\y \ K. open (X0 y) \ open (Y y) \ x0 \ X0 y \ y \ Y y \ X0 y \ Y y \ W" by metis from * have "\t\Y ` K. open t" "K \ \(Y ` K)" by auto with \compact K\ obtain CC where CC: "CC \ Y ` K" "finite CC" "K \ \CC" by (meson compactE) then obtain c where c: "\C. C \ CC \ c C \ K \ C = Y (c C)" by (force intro!: choice) with * CC show ?thesis by (force intro!: exI[where x="\C\CC. X0 (c C)"]) (* SLOW *) qed lemma continuous_on_prod_compactE: fixes fx::"'a::topological_space \ 'b::topological_space \ 'c::metric_space" and e::real assumes cont_fx: "continuous_on (U \ C) fx" assumes "compact C" assumes [intro]: "x0 \ U" notes [continuous_intros] = continuous_on_compose2[OF cont_fx] assumes "e > 0" obtains X0 where "x0 \ X0" "open X0" "\x\X0 \ U. \t \ C. dist (fx (x, t)) (fx (x0, t)) \ e" proof - define psi where "psi = (\(x, t). dist (fx (x, t)) (fx (x0, t)))" define W0 where "W0 = {(x, t) \ U \ C. psi (x, t) < e}" have W0_eq: "W0 = psi -` {.. U \ C" by (auto simp: vimage_def W0_def) have "open {.. C) psi" by (auto intro!: continuous_intros simp: psi_def split_beta') from this[unfolded continuous_on_open_invariant, rule_format, OF \open {..] obtain W where W: "open W" "W \ U \ C = W0 \ U \ C" unfolding W0_eq by blast have "{x0} \ C \ W \ U \ C" unfolding W by (auto simp: W0_def psi_def \0 < e\) then have "{x0} \ C \ W" by blast from tube_lemma[OF \compact C\ \open W\ this] obtain X0 where X0: "x0 \ X0" "open X0" "X0 \ C \ W" by blast have "\x\X0 \ U. \t \ C. dist (fx (x, t)) (fx (x0, t)) \ e" proof safe fix x assume x: "x \ X0" "x \ U" fix t assume t: "t \ C" have "dist (fx (x, t)) (fx (x0, t)) = psi (x, t)" by (auto simp: psi_def) also { have "(x, t) \ X0 \ C" using t x by auto also note \\ \ W\ finally have "(x, t) \ W" . with t x have "(x, t) \ W \ U \ C" by blast also note \W \ U \ C = W0 \ U \ C\ finally have "psi (x, t) < e" by (auto simp: W0_def) } finally show "dist (fx (x, t)) (fx (x0, t)) \ e" by simp qed from X0(1,2) this show ?thesis .. qed subsection \Continuity\ lemma continuous_at_imp_continuous_within: "continuous (at x) f \ continuous (at x within s) f" unfolding continuous_within continuous_at using Lim_at_imp_Lim_at_within by auto lemma Lim_trivial_limit: "trivial_limit net \ (f \ l) net" by simp lemmas continuous_on = continuous_on_def \ \legacy theorem name\ lemma continuous_within_subset: "continuous (at x within s) f \ t \ s \ continuous (at x within t) f" unfolding continuous_within by(metis tendsto_within_subset) lemma continuous_on_interior: "continuous_on s f \ x \ interior s \ continuous (at x) f" by (metis continuous_on_eq_continuous_at continuous_on_subset interiorE) lemma continuous_on_eq: "\continuous_on s f; \x. x \ s \ f x = g x\ \ continuous_on s g" unfolding continuous_on_def tendsto_def eventually_at_topological by simp text \Characterization of various kinds of continuity in terms of sequences.\ lemma continuous_within_sequentiallyI: fixes f :: "'a::{first_countable_topology, t2_space} \ 'b::topological_space" assumes "\u::nat \ 'a. u \ a \ (\n. u n \ s) \ (\n. f (u n)) \ f a" shows "continuous (at a within s) f" using assms unfolding continuous_within tendsto_def[where l = "f a"] by (auto intro!: sequentially_imp_eventually_within) lemma continuous_within_tendsto_compose: fixes f::"'a::t2_space \ 'b::topological_space" assumes "continuous (at a within s) f" "eventually (\n. x n \ s) F" "(x \ a) F " shows "((\n. f (x n)) \ f a) F" proof - have *: "filterlim x (inf (nhds a) (principal s)) F" using assms(2) assms(3) unfolding at_within_def filterlim_inf by (auto simp: filterlim_principal eventually_mono) show ?thesis by (auto simp: assms(1) continuous_within[symmetric] tendsto_at_within_iff_tendsto_nhds[symmetric] intro!: filterlim_compose[OF _ *]) qed lemma continuous_within_tendsto_compose': fixes f::"'a::t2_space \ 'b::topological_space" assumes "continuous (at a within s) f" "\n. x n \ s" "(x \ a) F " shows "((\n. f (x n)) \ f a) F" by (auto intro!: continuous_within_tendsto_compose[OF assms(1)] simp add: assms) lemma continuous_within_sequentially: fixes f :: "'a::{first_countable_topology, t2_space} \ 'b::topological_space" shows "continuous (at a within s) f \ (\x. (\n::nat. x n \ s) \ (x \ a) sequentially \ ((f \ x) \ f a) sequentially)" using continuous_within_tendsto_compose'[of a s f _ sequentially] continuous_within_sequentiallyI[of a s f] by (auto simp: o_def) lemma continuous_at_sequentiallyI: fixes f :: "'a::{first_countable_topology, t2_space} \ 'b::topological_space" assumes "\u. u \ a \ (\n. f (u n)) \ f a" shows "continuous (at a) f" using continuous_within_sequentiallyI[of a UNIV f] assms by auto lemma continuous_at_sequentially: fixes f :: "'a::metric_space \ 'b::topological_space" shows "continuous (at a) f \ (\x. (x \ a) sequentially --> ((f \ x) \ f a) sequentially)" using continuous_within_sequentially[of a UNIV f] by simp lemma continuous_on_sequentiallyI: fixes f :: "'a::{first_countable_topology, t2_space} \ 'b::topological_space" assumes "\u a. (\n. u n \ s) \ a \ s \ u \ a \ (\n. f (u n)) \ f a" shows "continuous_on s f" using assms unfolding continuous_on_eq_continuous_within using continuous_within_sequentiallyI[of _ s f] by auto lemma continuous_on_sequentially: fixes f :: "'a::{first_countable_topology, t2_space} \ 'b::topological_space" shows "continuous_on s f \ (\x. \a \ s. (\n. x(n) \ s) \ (x \ a) sequentially --> ((f \ x) \ f a) sequentially)" (is "?lhs = ?rhs") proof assume ?rhs then show ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto next assume ?lhs then show ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto qed text \Continuity in terms of open preimages.\ lemma continuous_at_open: "continuous (at x) f \ (\t. open t \ f x \ t --> (\s. open s \ x \ s \ (\x' \ s. (f x') \ t)))" unfolding continuous_within_topological [of x UNIV f] unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto lemma continuous_imp_tendsto: assumes "continuous (at x0) f" and "x \ x0" shows "(f \ x) \ (f x0)" proof (rule topological_tendstoI) fix S assume "open S" "f x0 \ S" then obtain T where T_def: "open T" "x0 \ T" "\x\T. f x \ S" using assms continuous_at_open by metis then have "eventually (\n. x n \ T) sequentially" using assms T_def by (auto simp: tendsto_def) then show "eventually (\n. (f \ x) n \ S) sequentially" using T_def by (auto elim!: eventually_mono) qed subsection \Homeomorphisms\ definition\<^marker>\tag important\ "homeomorphism s t f g \ (\x\s. (g(f x) = x)) \ (f ` s = t) \ continuous_on s f \ (\y\t. (f(g y) = y)) \ (g ` t = s) \ continuous_on t g" lemma homeomorphismI [intro?]: assumes "continuous_on S f" "continuous_on T g" "f ` S \ T" "g ` T \ S" "\x. x \ S \ g(f x) = x" "\y. y \ T \ f(g y) = y" shows "homeomorphism S T f g" using assms by (force simp: homeomorphism_def) lemma homeomorphism_translation: fixes a :: "'a :: real_normed_vector" shows "homeomorphism ((+) a ` S) S ((+) (- a)) ((+) a)" unfolding homeomorphism_def by (auto simp: algebra_simps continuous_intros) lemma homeomorphism_ident: "homeomorphism T T (\a. a) (\a. a)" by (rule homeomorphismI) auto lemma homeomorphism_compose: assumes "homeomorphism S T f g" "homeomorphism T U h k" shows "homeomorphism S U (h o f) (g o k)" using assms unfolding homeomorphism_def by (intro conjI ballI continuous_on_compose) (auto simp: image_iff) lemma homeomorphism_cong: "homeomorphism X' Y' f' g'" if "homeomorphism X Y f g" "X' = X" "Y' = Y" "\x. x \ X \ f' x = f x" "\y. y \ Y \ g' y = g y" using that by (auto simp add: homeomorphism_def) lemma homeomorphism_empty [simp]: "homeomorphism {} {} f g" unfolding homeomorphism_def by auto lemma homeomorphism_symD: "homeomorphism S t f g \ homeomorphism t S g f" by (simp add: homeomorphism_def) lemma homeomorphism_sym: "homeomorphism S t f g = homeomorphism t S g f" by (force simp: homeomorphism_def) definition\<^marker>\tag important\ homeomorphic :: "'a::topological_space set \ 'b::topological_space set \ bool" (infixr "homeomorphic" 60) where "s homeomorphic t \ (\f g. homeomorphism s t f g)" lemma homeomorphic_empty [iff]: "S homeomorphic {} \ S = {}" "{} homeomorphic S \ S = {}" by (auto simp: homeomorphic_def homeomorphism_def) lemma homeomorphic_refl: "s homeomorphic s" unfolding homeomorphic_def homeomorphism_def using continuous_on_id apply (rule_tac x = "(\x. x)" in exI) apply (rule_tac x = "(\x. x)" in exI) apply blast done lemma homeomorphic_sym: "s homeomorphic t \ t homeomorphic s" unfolding homeomorphic_def homeomorphism_def by blast lemma homeomorphic_trans [trans]: assumes "S homeomorphic T" and "T homeomorphic U" shows "S homeomorphic U" using assms unfolding homeomorphic_def by (metis homeomorphism_compose) lemma homeomorphic_minimal: "s homeomorphic t \ (\f g. (\x\s. f(x) \ t \ (g(f(x)) = x)) \ (\y\t. g(y) \ s \ (f(g(y)) = y)) \ continuous_on s f \ continuous_on t g)" (is "?lhs = ?rhs") proof assume ?lhs then show ?rhs by (fastforce simp: homeomorphic_def homeomorphism_def) next assume ?rhs then show ?lhs apply clarify unfolding homeomorphic_def homeomorphism_def by (metis equalityI image_subset_iff subsetI) qed lemma homeomorphicI [intro?]: "\f ` S = T; g ` T = S; continuous_on S f; continuous_on T g; \x. x \ S \ g(f(x)) = x; \y. y \ T \ f(g(y)) = y\ \ S homeomorphic T" unfolding homeomorphic_def homeomorphism_def by metis lemma homeomorphism_of_subsets: "\homeomorphism S T f g; S' \ S; T'' \ T; f ` S' = T'\ \ homeomorphism S' T' f g" apply (auto simp: homeomorphism_def elim!: continuous_on_subset) by (metis subsetD imageI) lemma homeomorphism_apply1: "\homeomorphism S T f g; x \ S\ \ g(f x) = x" by (simp add: homeomorphism_def) lemma homeomorphism_apply2: "\homeomorphism S T f g; x \ T\ \ f(g x) = x" by (simp add: homeomorphism_def) lemma homeomorphism_image1: "homeomorphism S T f g \ f ` S = T" by (simp add: homeomorphism_def) lemma homeomorphism_image2: "homeomorphism S T f g \ g ` T = S" by (simp add: homeomorphism_def) lemma homeomorphism_cont1: "homeomorphism S T f g \ continuous_on S f" by (simp add: homeomorphism_def) lemma homeomorphism_cont2: "homeomorphism S T f g \ continuous_on T g" by (simp add: homeomorphism_def) lemma continuous_on_no_limpt: "(\x. \ x islimpt S) \ continuous_on S f" unfolding continuous_on_def by (metis UNIV_I empty_iff eventually_at_topological islimptE open_UNIV tendsto_def trivial_limit_within) lemma continuous_on_finite: fixes S :: "'a::t1_space set" shows "finite S \ continuous_on S f" by (metis continuous_on_no_limpt islimpt_finite) lemma homeomorphic_finite: fixes S :: "'a::t1_space set" and T :: "'b::t1_space set" assumes "finite T" shows "S homeomorphic T \ finite S \ finite T \ card S = card T" (is "?lhs = ?rhs") proof assume "S homeomorphic T" with assms show ?rhs apply (auto simp: homeomorphic_def homeomorphism_def) apply (metis finite_imageI) by (metis card_image_le finite_imageI le_antisym) next assume R: ?rhs with finite_same_card_bij obtain h where "bij_betw h S T" by auto with R show ?lhs apply (auto simp: homeomorphic_def homeomorphism_def continuous_on_finite) apply (rule_tac x=h in exI) apply (rule_tac x="inv_into S h" in exI) apply (auto simp: bij_betw_inv_into_left bij_betw_inv_into_right bij_betw_imp_surj_on inv_into_into bij_betwE) apply (metis bij_betw_def bij_betw_inv_into) done qed text \Relatively weak hypotheses if a set is compact.\ lemma homeomorphism_compact: fixes f :: "'a::topological_space \ 'b::t2_space" assumes "compact s" "continuous_on s f" "f ` s = t" "inj_on f s" shows "\g. homeomorphism s t f g" proof - define g where "g x = (SOME y. y\s \ f y = x)" for x have g: "\x\s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto { fix y assume "y \ t" then obtain x where x:"f x = y" "x\s" using assms(3) by auto then have "g (f x) = x" using g by auto then have "f (g y) = y" unfolding x(1)[symmetric] by auto } then have g':"\x\t. f (g x) = x" by auto moreover { fix x have "x\s \ x \ g ` t" using g[THEN bspec[where x=x]] unfolding image_iff using assms(3) by (auto intro!: bexI[where x="f x"]) moreover { assume "x\g ` t" then obtain y where y:"y\t" "g y = x" by auto then obtain x' where x':"x'\s" "f x' = y" using assms(3) by auto then have "x \ s" unfolding g_def using someI2[of "\b. b\s \ f b = y" x' "\x. x\s"] unfolding y(2)[symmetric] and g_def by auto } ultimately have "x\s \ x \ g ` t" .. } then have "g ` t = s" by auto ultimately show ?thesis unfolding homeomorphism_def homeomorphic_def apply (rule_tac x=g in exI) using g and assms(3) and continuous_on_inv[OF assms(2,1), of g, unfolded assms(3)] and assms(2) apply auto done qed lemma homeomorphic_compact: fixes f :: "'a::topological_space \ 'b::t2_space" shows "compact s \ continuous_on s f \ (f ` s = t) \ inj_on f s \ s homeomorphic t" unfolding homeomorphic_def by (metis homeomorphism_compact) text\Preservation of topological properties.\ lemma homeomorphic_compactness: "s homeomorphic t \ (compact s \ compact t)" unfolding homeomorphic_def homeomorphism_def by (metis compact_continuous_image) subsection\<^marker>\tag unimportant\ \On Linorder Topologies\ lemma islimpt_greaterThanLessThan1: fixes a b::"'a::{linorder_topology, dense_order}" assumes "a < b" shows "a islimpt {a<.. T" from open_right[OF this \a < b\] obtain c where c: "a < c" "{a.. T" by auto with assms dense[of a "min c b"] show "\y\{a<.. T \ y \ a" by (metis atLeastLessThan_iff greaterThanLessThan_iff min_less_iff_conj not_le order.strict_implies_order subset_eq) qed lemma islimpt_greaterThanLessThan2: fixes a b::"'a::{linorder_topology, dense_order}" assumes "a < b" shows "b islimpt {a<.. T" from open_left[OF this \a < b\] obtain c where c: "c < b" "{c<..b} \ T" by auto with assms dense[of "max a c" b] show "\y\{a<.. T \ y \ b" by (metis greaterThanAtMost_iff greaterThanLessThan_iff max_less_iff_conj not_le order.strict_implies_order subset_eq) qed lemma closure_greaterThanLessThan[simp]: fixes a b::"'a::{linorder_topology, dense_order}" shows "a < b \ closure {a <..< b} = {a .. b}" (is "_ \ ?l = ?r") proof have "?l \ closure ?r" by (rule closure_mono) auto thus "closure {a<.. {a..b}" by simp qed (auto simp: closure_def order.order_iff_strict islimpt_greaterThanLessThan1 islimpt_greaterThanLessThan2) lemma closure_greaterThan[simp]: fixes a b::"'a::{no_top, linorder_topology, dense_order}" shows "closure {a<..} = {a..}" proof - from gt_ex obtain b where "a < b" by auto hence "{a<..} = {a<.. {b..}" by auto also have "closure \ = {a..}" using \a < b\ unfolding closure_Un by auto finally show ?thesis . qed lemma closure_lessThan[simp]: fixes b::"'a::{no_bot, linorder_topology, dense_order}" shows "closure {.. {..a}" by auto also have "closure \ = {..b}" using \a < b\ unfolding closure_Un by auto finally show ?thesis . qed lemma closure_atLeastLessThan[simp]: fixes a b::"'a::{linorder_topology, dense_order}" assumes "a < b" shows "closure {a ..< b} = {a .. b}" proof - from assms have "{a ..< b} = {a} \ {a <..< b}" by auto also have "closure \ = {a .. b}" unfolding closure_Un by (auto simp: assms less_imp_le) finally show ?thesis . qed lemma closure_greaterThanAtMost[simp]: fixes a b::"'a::{linorder_topology, dense_order}" assumes "a < b" shows "closure {a <.. b} = {a .. b}" proof - from assms have "{a <.. b} = {b} \ {a <..< b}" by auto also have "closure \ = {a .. b}" unfolding closure_Un by (auto simp: assms less_imp_le) finally show ?thesis . qed end \ No newline at end of file diff --git a/src/HOL/Analysis/Finite_Cartesian_Product.thy b/src/HOL/Analysis/Finite_Cartesian_Product.thy --- a/src/HOL/Analysis/Finite_Cartesian_Product.thy +++ b/src/HOL/Analysis/Finite_Cartesian_Product.thy @@ -1,1286 +1,1285 @@ (* Title: HOL/Analysis/Finite_Cartesian_Product.thy Author: Amine Chaieb, University of Cambridge *) section \Definition of Finite Cartesian Product Type\ theory Finite_Cartesian_Product imports Euclidean_Space L2_Norm "HOL-Library.Numeral_Type" "HOL-Library.Countable_Set" "HOL-Library.FuncSet" begin subsection\<^marker>\tag unimportant\ \Finite Cartesian products, with indexing and lambdas\ typedef ('a, 'b) vec = "UNIV :: ('b::finite \ 'a) set" morphisms vec_nth vec_lambda .. declare vec_lambda_inject [simplified, simp] bundle vec_syntax begin notation vec_nth (infixl "$" 90) and vec_lambda (binder "\" 10) end bundle no_vec_syntax begin no_notation vec_nth (infixl "$" 90) and vec_lambda (binder "\" 10) end unbundle vec_syntax text \ Concrete syntax for \('a, 'b) vec\: \<^item> \'a^'b\ becomes \('a, 'b::finite) vec\ \<^item> \'a^'b::_\ becomes \('a, 'b) vec\ without extra sort-constraint \ syntax "_vec_type" :: "type \ type \ type" (infixl "^" 15) parse_translation \ let fun vec t u = Syntax.const \<^type_syntax>\vec\ $ t $ u; fun finite_vec_tr [t, u] = (case Term_Position.strip_positions u of v as Free (x, _) => if Lexicon.is_tid x then vec t (Syntax.const \<^syntax_const>\_ofsort\ $ v $ Syntax.const \<^class_syntax>\finite\) else vec t u | _ => vec t u) in [(\<^syntax_const>\_vec_type\, K finite_vec_tr)] end \ lemma vec_eq_iff: "(x = y) \ (\i. x$i = y$i)" by (simp add: vec_nth_inject [symmetric] fun_eq_iff) lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i" by (simp add: vec_lambda_inverse) lemma vec_lambda_unique: "(\i. f$i = g i) \ vec_lambda g = f" by (auto simp add: vec_eq_iff) lemma vec_lambda_eta [simp]: "(\ i. (g$i)) = g" by (simp add: vec_eq_iff) subsection \Cardinality of vectors\ instance vec :: (finite, finite) finite proof show "finite (UNIV :: ('a, 'b) vec set)" proof (subst bij_betw_finite) show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff) have "finite (PiE (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro finite_PiE) auto also have "(PiE (UNIV :: 'b set) (\_. UNIV :: 'a set)) = Pi UNIV (\_. UNIV)" by auto finally show "finite \" . qed qed lemma countable_PiE: "finite I \ (\i. i \ I \ countable (F i)) \ countable (Pi\<^sub>E I F)" by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq) instance vec :: (countable, finite) countable proof have "countable (UNIV :: ('a, 'b) vec set)" proof (rule countableI_bij2) show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff) have "countable (PiE (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro countable_PiE) auto also have "(PiE (UNIV :: 'b set) (\_. UNIV :: 'a set)) = Pi UNIV (\_. UNIV)" by auto finally show "countable \" . qed thus "\t::('a, 'b) vec \ nat. inj t" by (auto elim!: countableE) qed lemma infinite_UNIV_vec: assumes "infinite (UNIV :: 'a set)" shows "infinite (UNIV :: ('a^'b) set)" proof (subst bij_betw_finite) show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff) have "infinite (PiE (UNIV :: 'b set) (\_. UNIV :: 'a set))" (is "infinite ?A") proof assume "finite ?A" hence "finite ((\f. f undefined) ` ?A)" by (rule finite_imageI) also have "(\f. f undefined) ` ?A = UNIV" by auto finally show False using \infinite (UNIV :: 'a set)\ by contradiction qed also have "?A = Pi UNIV (\_. UNIV)" by auto finally show "infinite (Pi (UNIV :: 'b set) (\_. UNIV :: 'a set))" . qed proposition CARD_vec [simp]: "CARD('a^'b) = CARD('a) ^ CARD('b)" proof (cases "finite (UNIV :: 'a set)") case True show ?thesis proof (subst bij_betw_same_card) show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\_. UNIV :: 'a set))" by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff) have "CARD('a) ^ CARD('b) = card (PiE (UNIV :: 'b set) (\_. UNIV :: 'a set))" (is "_ = card ?A") - by (subst card_PiE) (auto simp: prod_constant) - + by (subst card_PiE) (auto) also have "?A = Pi UNIV (\_. UNIV)" by auto finally show "card \ = CARD('a) ^ CARD('b)" .. qed qed (simp_all add: infinite_UNIV_vec) lemma countable_vector: fixes B:: "'n::finite \ 'a set" assumes "\i. countable (B i)" shows "countable {V. \i::'n::finite. V $ i \ B i}" proof - have "f \ ($) ` {V. \i. V $ i \ B i}" if "f \ Pi\<^sub>E UNIV B" for f proof - have "\W. (\i. W $ i \ B i) \ ($) W = f" by (metis that PiE_iff UNIV_I vec_lambda_inverse) then show "f \ ($) ` {v. \i. v $ i \ B i}" by blast qed then have "Pi\<^sub>E UNIV B = vec_nth ` {V. \i::'n. V $ i \ B i}" by blast then have "countable (vec_nth ` {V. \i. V $ i \ B i})" by (metis finite_class.finite_UNIV countable_PiE assms) then have "countable (vec_lambda ` vec_nth ` {V. \i. V $ i \ B i})" by auto then show ?thesis by (simp add: image_comp o_def vec_nth_inverse) qed subsection\<^marker>\tag unimportant\ \Group operations and class instances\ instantiation vec :: (zero, finite) zero begin definition "0 \ (\ i. 0)" instance .. end instantiation vec :: (plus, finite) plus begin definition "(+) \ (\ x y. (\ i. x$i + y$i))" instance .. end instantiation vec :: (minus, finite) minus begin definition "(-) \ (\ x y. (\ i. x$i - y$i))" instance .. end instantiation vec :: (uminus, finite) uminus begin definition "uminus \ (\ x. (\ i. - (x$i)))" instance .. end lemma zero_index [simp]: "0 $ i = 0" unfolding zero_vec_def by simp lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i" unfolding plus_vec_def by simp lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i" unfolding minus_vec_def by simp lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)" unfolding uminus_vec_def by simp instance vec :: (semigroup_add, finite) semigroup_add by standard (simp add: vec_eq_iff add.assoc) instance vec :: (ab_semigroup_add, finite) ab_semigroup_add by standard (simp add: vec_eq_iff add.commute) instance vec :: (monoid_add, finite) monoid_add by standard (simp_all add: vec_eq_iff) instance vec :: (comm_monoid_add, finite) comm_monoid_add by standard (simp add: vec_eq_iff) instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add by standard (simp_all add: vec_eq_iff) instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add by standard (simp_all add: vec_eq_iff diff_diff_eq) instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add .. instance vec :: (group_add, finite) group_add by standard (simp_all add: vec_eq_iff) instance vec :: (ab_group_add, finite) ab_group_add by standard (simp_all add: vec_eq_iff) subsection\<^marker>\tag unimportant\\Basic componentwise operations on vectors\ instantiation vec :: (times, finite) times begin definition "(*) \ (\ x y. (\ i. (x$i) * (y$i)))" instance .. end instantiation vec :: (one, finite) one begin definition "1 \ (\ i. 1)" instance .. end instantiation vec :: (ord, finite) ord begin definition "x \ y \ (\i. x$i \ y$i)" definition "x < (y::'a^'b) \ x \ y \ \ y \ x" instance .. end text\The ordering on one-dimensional vectors is linear.\ instance vec:: (order, finite) order by standard (auto simp: less_eq_vec_def less_vec_def vec_eq_iff intro: order.trans order.antisym order.strict_implies_order) instance vec :: (linorder, CARD_1) linorder proof obtain a :: 'b where all: "\P. (\i. P i) \ P a" proof - have "CARD ('b) = 1" by (rule CARD_1) then obtain b :: 'b where "UNIV = {b}" by (auto iff: card_Suc_eq) then have "\P. (\i\UNIV. P i) \ P b" by auto then show thesis by (auto intro: that) qed fix x y :: "'a^'b::CARD_1" note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps show "x \ y \ y \ x" by auto qed text\Constant Vectors\ definition "vec x = (\ i. x)" text\Also the scalar-vector multiplication.\ definition vector_scalar_mult:: "'a::times \ 'a ^ 'n \ 'a ^ 'n" (infixl "*s" 70) where "c *s x = (\ i. c * (x$i))" text \scalar product\ definition scalar_product :: "'a :: semiring_1 ^ 'n \ 'a ^ 'n \ 'a" where "scalar_product v w = (\ i \ UNIV. v $ i * w $ i)" subsection \Real vector space\ instantiation\<^marker>\tag unimportant\ vec :: (real_vector, finite) real_vector begin definition\<^marker>\tag important\ "scaleR \ (\ r x. (\ i. scaleR r (x$i)))" lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)" unfolding scaleR_vec_def by simp instance\<^marker>\tag unimportant\ by standard (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib) end subsection \Topological space\ instantiation\<^marker>\tag unimportant\ vec :: (topological_space, finite) topological_space begin definition\<^marker>\tag important\ [code del]: "open (S :: ('a ^ 'b) set) \ (\x\S. \A. (\i. open (A i) \ x$i \ A i) \ (\y. (\i. y$i \ A i) \ y \ S))" instance\<^marker>\tag unimportant\ proof show "open (UNIV :: ('a ^ 'b) set)" unfolding open_vec_def by auto next fix S T :: "('a ^ 'b) set" assume "open S" "open T" thus "open (S \ T)" unfolding open_vec_def apply clarify apply (drule (1) bspec)+ apply (clarify, rename_tac Sa Ta) apply (rule_tac x="\i. Sa i \ Ta i" in exI) apply (simp add: open_Int) done next fix K :: "('a ^ 'b) set set" assume "\S\K. open S" thus "open (\K)" unfolding open_vec_def apply clarify apply (drule (1) bspec) apply (drule (1) bspec) apply clarify apply (rule_tac x=A in exI) apply fast done qed end lemma open_vector_box: "\i. open (S i) \ open {x. \i. x $ i \ S i}" unfolding open_vec_def by auto lemma open_vimage_vec_nth: "open S \ open ((\x. x $ i) -` S)" unfolding open_vec_def apply clarify apply (rule_tac x="\k. if k = i then S else UNIV" in exI, simp) done lemma closed_vimage_vec_nth: "closed S \ closed ((\x. x $ i) -` S)" unfolding closed_open vimage_Compl [symmetric] by (rule open_vimage_vec_nth) lemma closed_vector_box: "\i. closed (S i) \ closed {x. \i. x $ i \ S i}" proof - have "{x. \i. x $ i \ S i} = (\i. (\x. x $ i) -` S i)" by auto thus "\i. closed (S i) \ closed {x. \i. x $ i \ S i}" by (simp add: closed_INT closed_vimage_vec_nth) qed lemma tendsto_vec_nth [tendsto_intros]: assumes "((\x. f x) \ a) net" shows "((\x. f x $ i) \ a $ i) net" proof (rule topological_tendstoI) fix S assume "open S" "a $ i \ S" then have "open ((\y. y $ i) -` S)" "a \ ((\y. y $ i) -` S)" by (simp_all add: open_vimage_vec_nth) with assms have "eventually (\x. f x \ (\y. y $ i) -` S) net" by (rule topological_tendstoD) then show "eventually (\x. f x $ i \ S) net" by simp qed lemma isCont_vec_nth [simp]: "isCont f a \ isCont (\x. f x $ i) a" unfolding isCont_def by (rule tendsto_vec_nth) lemma vec_tendstoI: assumes "\i. ((\x. f x $ i) \ a $ i) net" shows "((\x. f x) \ a) net" proof (rule topological_tendstoI) fix S assume "open S" and "a \ S" then obtain A where A: "\i. open (A i)" "\i. a $ i \ A i" and S: "\y. \i. y $ i \ A i \ y \ S" unfolding open_vec_def by metis have "\i. eventually (\x. f x $ i \ A i) net" using assms A by (rule topological_tendstoD) hence "eventually (\x. \i. f x $ i \ A i) net" by (rule eventually_all_finite) thus "eventually (\x. f x \ S) net" by (rule eventually_mono, simp add: S) qed lemma tendsto_vec_lambda [tendsto_intros]: assumes "\i. ((\x. f x i) \ a i) net" shows "((\x. \ i. f x i) \ (\ i. a i)) net" using assms by (simp add: vec_tendstoI) lemma open_image_vec_nth: assumes "open S" shows "open ((\x. x $ i) ` S)" proof (rule openI) fix a assume "a \ (\x. x $ i) ` S" then obtain z where "a = z $ i" and "z \ S" .. then obtain A where A: "\i. open (A i) \ z $ i \ A i" and S: "\y. (\i. y $ i \ A i) \ y \ S" using \open S\ unfolding open_vec_def by auto hence "A i \ (\x. x $ i) ` S" by (clarsimp, rule_tac x="\ j. if j = i then x else z $ j" in image_eqI, simp_all) hence "open (A i) \ a \ A i \ A i \ (\x. x $ i) ` S" using A \a = z $ i\ by simp then show "\T. open T \ a \ T \ T \ (\x. x $ i) ` S" by - (rule exI) qed instance\<^marker>\tag unimportant\ vec :: (perfect_space, finite) perfect_space proof fix x :: "'a ^ 'b" show "\ open {x}" proof assume "open {x}" hence "\i. open ((\x. x $ i) ` {x})" by (fast intro: open_image_vec_nth) hence "\i. open {x $ i}" by simp thus "False" by (simp add: not_open_singleton) qed qed subsection \Metric space\ (* TODO: Product of uniform spaces and compatibility with metric_spaces! *) instantiation\<^marker>\tag unimportant\ vec :: (metric_space, finite) dist begin definition\<^marker>\tag important\ "dist x y = L2_set (\i. dist (x$i) (y$i)) UNIV" instance .. end instantiation\<^marker>\tag unimportant\ vec :: (metric_space, finite) uniformity_dist begin definition\<^marker>\tag important\ [code del]: "(uniformity :: (('a^'b::_) \ ('a^'b::_)) filter) = (INF e\{0 <..}. principal {(x, y). dist x y < e})" instance\<^marker>\tag unimportant\ by standard (rule uniformity_vec_def) end declare uniformity_Abort[where 'a="'a :: metric_space ^ 'b :: finite", code] instantiation\<^marker>\tag unimportant\ vec :: (metric_space, finite) metric_space begin proposition dist_vec_nth_le: "dist (x $ i) (y $ i) \ dist x y" unfolding dist_vec_def by (rule member_le_L2_set) simp_all instance proof fix x y :: "'a ^ 'b" show "dist x y = 0 \ x = y" unfolding dist_vec_def by (simp add: L2_set_eq_0_iff vec_eq_iff) next fix x y z :: "'a ^ 'b" show "dist x y \ dist x z + dist y z" unfolding dist_vec_def apply (rule order_trans [OF _ L2_set_triangle_ineq]) apply (simp add: L2_set_mono dist_triangle2) done next fix S :: "('a ^ 'b) set" have *: "open S \ (\x\S. \e>0. \y. dist y x < e \ y \ S)" proof assume "open S" show "\x\S. \e>0. \y. dist y x < e \ y \ S" proof fix x assume "x \ S" obtain A where A: "\i. open (A i)" "\i. x $ i \ A i" and S: "\y. (\i. y $ i \ A i) \ y \ S" using \open S\ and \x \ S\ unfolding open_vec_def by metis have "\i\UNIV. \r>0. \y. dist y (x $ i) < r \ y \ A i" using A unfolding open_dist by simp hence "\r. \i\UNIV. 0 < r i \ (\y. dist y (x $ i) < r i \ y \ A i)" by (rule finite_set_choice [OF finite]) then obtain r where r1: "\i. 0 < r i" and r2: "\i y. dist y (x $ i) < r i \ y \ A i" by fast have "0 < Min (range r) \ (\y. dist y x < Min (range r) \ y \ S)" by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le]) thus "\e>0. \y. dist y x < e \ y \ S" .. qed next assume *: "\x\S. \e>0. \y. dist y x < e \ y \ S" show "open S" proof (unfold open_vec_def, rule) fix x assume "x \ S" then obtain e where "0 < e" and S: "\y. dist y x < e \ y \ S" using * by fast define r where [abs_def]: "r i = e / sqrt (of_nat CARD('b))" for i :: 'b from \0 < e\ have r: "\i. 0 < r i" unfolding r_def by simp_all from \0 < e\ have e: "e = L2_set r UNIV" unfolding r_def by (simp add: L2_set_constant) define A where "A i = {y. dist (x $ i) y < r i}" for i have "\i. open (A i) \ x $ i \ A i" unfolding A_def by (simp add: open_ball r) moreover have "\y. (\i. y $ i \ A i) \ y \ S" by (simp add: A_def S dist_vec_def e L2_set_strict_mono dist_commute) ultimately show "\A. (\i. open (A i) \ x $ i \ A i) \ (\y. (\i. y $ i \ A i) \ y \ S)" by metis qed qed show "open S = (\x\S. \\<^sub>F (x', y) in uniformity. x' = x \ y \ S)" unfolding * eventually_uniformity_metric by (simp del: split_paired_All add: dist_vec_def dist_commute) qed end lemma Cauchy_vec_nth: "Cauchy (\n. X n) \ Cauchy (\n. X n $ i)" unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le]) lemma vec_CauchyI: fixes X :: "nat \ 'a::metric_space ^ 'n" assumes X: "\i. Cauchy (\n. X n $ i)" shows "Cauchy (\n. X n)" proof (rule metric_CauchyI) fix r :: real assume "0 < r" hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp define N where "N i = (LEAST N. \m\N. \n\N. dist (X m $ i) (X n $ i) < ?s)" for i define M where "M = Max (range N)" have "\i. \N. \m\N. \n\N. dist (X m $ i) (X n $ i) < ?s" using X \0 < ?s\ by (rule metric_CauchyD) hence "\i. \m\N i. \n\N i. dist (X m $ i) (X n $ i) < ?s" unfolding N_def by (rule LeastI_ex) hence M: "\i. \m\M. \n\M. dist (X m $ i) (X n $ i) < ?s" unfolding M_def by simp { fix m n :: nat assume "M \ m" "M \ n" have "dist (X m) (X n) = L2_set (\i. dist (X m $ i) (X n $ i)) UNIV" unfolding dist_vec_def .. also have "\ \ sum (\i. dist (X m $ i) (X n $ i)) UNIV" by (rule L2_set_le_sum [OF zero_le_dist]) also have "\ < sum (\i::'n. ?s) UNIV" by (rule sum_strict_mono, simp_all add: M \M \ m\ \M \ n\) also have "\ = r" by simp finally have "dist (X m) (X n) < r" . } hence "\m\M. \n\M. dist (X m) (X n) < r" by simp then show "\M. \m\M. \n\M. dist (X m) (X n) < r" .. qed instance\<^marker>\tag unimportant\ vec :: (complete_space, finite) complete_space proof fix X :: "nat \ 'a ^ 'b" assume "Cauchy X" have "\i. (\n. X n $ i) \ lim (\n. X n $ i)" using Cauchy_vec_nth [OF \Cauchy X\] by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff) hence "X \ vec_lambda (\i. lim (\n. X n $ i))" by (simp add: vec_tendstoI) then show "convergent X" by (rule convergentI) qed subsection \Normed vector space\ instantiation\<^marker>\tag unimportant\ vec :: (real_normed_vector, finite) real_normed_vector begin definition\<^marker>\tag important\ "norm x = L2_set (\i. norm (x$i)) UNIV" definition\<^marker>\tag important\ "sgn (x::'a^'b) = scaleR (inverse (norm x)) x" instance\<^marker>\tag unimportant\ proof fix a :: real and x y :: "'a ^ 'b" show "norm x = 0 \ x = 0" unfolding norm_vec_def by (simp add: L2_set_eq_0_iff vec_eq_iff) show "norm (x + y) \ norm x + norm y" unfolding norm_vec_def apply (rule order_trans [OF _ L2_set_triangle_ineq]) apply (simp add: L2_set_mono norm_triangle_ineq) done show "norm (scaleR a x) = \a\ * norm x" unfolding norm_vec_def by (simp add: L2_set_right_distrib) show "sgn x = scaleR (inverse (norm x)) x" by (rule sgn_vec_def) show "dist x y = norm (x - y)" unfolding dist_vec_def norm_vec_def by (simp add: dist_norm) qed end lemma norm_nth_le: "norm (x $ i) \ norm x" unfolding norm_vec_def by (rule member_le_L2_set) simp_all lemma norm_le_componentwise_cart: fixes x :: "'a::real_normed_vector^'n" assumes "\i. norm(x$i) \ norm(y$i)" shows "norm x \ norm y" unfolding norm_vec_def by (rule L2_set_mono) (auto simp: assms) lemma component_le_norm_cart: "\x$i\ \ norm x" apply (simp add: norm_vec_def) apply (rule member_le_L2_set, simp_all) done lemma norm_bound_component_le_cart: "norm x \ e ==> \x$i\ \ e" by (metis component_le_norm_cart order_trans) lemma norm_bound_component_lt_cart: "norm x < e ==> \x$i\ < e" by (metis component_le_norm_cart le_less_trans) lemma norm_le_l1_cart: "norm x \ sum(\i. \x$i\) UNIV" by (simp add: norm_vec_def L2_set_le_sum) lemma bounded_linear_vec_nth[intro]: "bounded_linear (\x. x $ i)" apply standard apply (rule vector_add_component) apply (rule vector_scaleR_component) apply (rule_tac x="1" in exI, simp add: norm_nth_le) done instance vec :: (banach, finite) banach .. subsection \Inner product space\ instantiation\<^marker>\tag unimportant\ vec :: (real_inner, finite) real_inner begin definition\<^marker>\tag important\ "inner x y = sum (\i. inner (x$i) (y$i)) UNIV" instance\<^marker>\tag unimportant\ proof fix r :: real and x y z :: "'a ^ 'b" show "inner x y = inner y x" unfolding inner_vec_def by (simp add: inner_commute) show "inner (x + y) z = inner x z + inner y z" unfolding inner_vec_def by (simp add: inner_add_left sum.distrib) show "inner (scaleR r x) y = r * inner x y" unfolding inner_vec_def by (simp add: sum_distrib_left) show "0 \ inner x x" unfolding inner_vec_def by (simp add: sum_nonneg) show "inner x x = 0 \ x = 0" unfolding inner_vec_def by (simp add: vec_eq_iff sum_nonneg_eq_0_iff) show "norm x = sqrt (inner x x)" unfolding inner_vec_def norm_vec_def L2_set_def by (simp add: power2_norm_eq_inner) qed end subsection \Euclidean space\ text \Vectors pointing along a single axis.\ definition\<^marker>\tag important\ "axis k x = (\ i. if i = k then x else 0)" lemma axis_nth [simp]: "axis i x $ i = x" unfolding axis_def by simp lemma axis_eq_axis: "axis i x = axis j y \ x = y \ i = j \ x = 0 \ y = 0" unfolding axis_def vec_eq_iff by auto lemma inner_axis_axis: "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)" unfolding inner_vec_def apply (cases "i = j") apply clarsimp apply (subst sum.remove [of _ j], simp_all) apply (rule sum.neutral, simp add: axis_def) apply (rule sum.neutral, simp add: axis_def) done lemma inner_axis: "inner x (axis i y) = inner (x $ i) y" by (simp add: inner_vec_def axis_def sum.remove [where x=i]) lemma inner_axis': "inner(axis i y) x = inner y (x $ i)" by (simp add: inner_axis inner_commute) instantiation\<^marker>\tag unimportant\ vec :: (euclidean_space, finite) euclidean_space begin definition\<^marker>\tag important\ "Basis = (\i. \u\Basis. {axis i u})" instance\<^marker>\tag unimportant\ proof show "(Basis :: ('a ^ 'b) set) \ {}" unfolding Basis_vec_def by simp next show "finite (Basis :: ('a ^ 'b) set)" unfolding Basis_vec_def by simp next fix u v :: "'a ^ 'b" assume "u \ Basis" and "v \ Basis" thus "inner u v = (if u = v then 1 else 0)" unfolding Basis_vec_def by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis) next fix x :: "'a ^ 'b" show "(\u\Basis. inner x u = 0) \ x = 0" unfolding Basis_vec_def by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff) qed proposition DIM_cart [simp]: "DIM('a^'b) = CARD('b) * DIM('a)" proof - have "card (\i::'b. \u::'a\Basis. {axis i u}) = (\i::'b\UNIV. card (\u::'a\Basis. {axis i u}))" by (rule card_UN_disjoint) (auto simp: axis_eq_axis) also have "... = CARD('b) * DIM('a)" by (subst card_UN_disjoint) (auto simp: axis_eq_axis) finally show ?thesis by (simp add: Basis_vec_def) qed end lemma norm_axis_1 [simp]: "norm (axis m (1::real)) = 1" by (simp add: inner_axis' norm_eq_1) lemma sum_norm_allsubsets_bound_cart: fixes f:: "'a \ real ^'n" assumes fP: "finite P" and fPs: "\Q. Q \ P \ norm (sum f Q) \ e" shows "sum (\x. norm (f x)) P \ 2 * real CARD('n) * e" using sum_norm_allsubsets_bound[OF assms] by simp lemma cart_eq_inner_axis: "a $ i = inner a (axis i 1)" by (simp add: inner_axis) lemma axis_eq_0_iff [simp]: shows "axis m x = 0 \ x = 0" by (simp add: axis_def vec_eq_iff) lemma axis_in_Basis_iff [simp]: "axis i a \ Basis \ a \ Basis" by (auto simp: Basis_vec_def axis_eq_axis) text\Mapping each basis element to the corresponding finite index\ definition axis_index :: "('a::comm_ring_1)^'n \ 'n" where "axis_index v \ SOME i. v = axis i 1" lemma axis_inverse: fixes v :: "real^'n" assumes "v \ Basis" shows "\i. v = axis i 1" proof - have "v \ (\n. \r\Basis. {axis n r})" using assms Basis_vec_def by blast then show ?thesis by (force simp add: vec_eq_iff) qed lemma axis_index: fixes v :: "real^'n" assumes "v \ Basis" shows "v = axis (axis_index v) 1" by (metis (mono_tags) assms axis_inverse axis_index_def someI_ex) lemma axis_index_axis [simp]: fixes UU :: "real^'n" shows "(axis_index (axis u 1 :: real^'n)) = (u::'n)" by (simp add: axis_eq_axis axis_index_def) subsection\<^marker>\tag unimportant\ \A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space\ lemma sum_cong_aux: "(\x. x \ A \ f x = g x) \ sum f A = sum g A" by (auto intro: sum.cong) hide_fact (open) sum_cong_aux method_setup vector = \ let val ss1 = simpset_of (put_simpset HOL_basic_ss \<^context> addsimps [@{thm sum.distrib} RS sym, @{thm sum_subtractf} RS sym, @{thm sum_distrib_left}, @{thm sum_distrib_right}, @{thm sum_negf} RS sym]) val ss2 = simpset_of (\<^context> addsimps [@{thm plus_vec_def}, @{thm times_vec_def}, @{thm minus_vec_def}, @{thm uminus_vec_def}, @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def}, @{thm scaleR_vec_def}, @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}]) fun vector_arith_tac ctxt ths = simp_tac (put_simpset ss1 ctxt) THEN' (fn i => resolve_tac ctxt @{thms Finite_Cartesian_Product.sum_cong_aux} i ORELSE resolve_tac ctxt @{thms sum.neutral} i ORELSE simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm vec_eq_iff}]) i) (* THEN' TRY o clarify_tac HOL_cs THEN' (TRY o rtac @{thm iffI}) *) THEN' asm_full_simp_tac (put_simpset ss2 ctxt addsimps ths) in Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (vector_arith_tac ctxt ths)) end \ "lift trivial vector statements to real arith statements" lemma vec_0[simp]: "vec 0 = 0" by vector lemma vec_1[simp]: "vec 1 = 1" by vector lemma vec_inj[simp]: "vec x = vec y \ x = y" by vector lemma vec_in_image_vec: "vec x \ (vec ` S) \ x \ S" by auto lemma vec_add: "vec(x + y) = vec x + vec y" by vector lemma vec_sub: "vec(x - y) = vec x - vec y" by vector lemma vec_cmul: "vec(c * x) = c *s vec x " by vector lemma vec_neg: "vec(- x) = - vec x " by vector lemma vec_scaleR: "vec(c * x) = c *\<^sub>R vec x" by vector lemma vec_sum: assumes "finite S" shows "vec(sum f S) = sum (vec \ f) S" using assms proof induct case empty then show ?case by simp next case insert then show ?case by (auto simp add: vec_add) qed text\Obvious "component-pushing".\ lemma vec_component [simp]: "vec x $ i = x" by vector lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i" by vector lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)" by vector lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector lemmas\<^marker>\tag unimportant\ vector_component = vec_component vector_add_component vector_mult_component vector_smult_component vector_minus_component vector_uminus_component vector_scaleR_component cond_component subsection\<^marker>\tag unimportant\ \Some frequently useful arithmetic lemmas over vectors\ instance vec :: (semigroup_mult, finite) semigroup_mult by standard (vector mult.assoc) instance vec :: (monoid_mult, finite) monoid_mult by standard vector+ instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult by standard (vector mult.commute) instance vec :: (comm_monoid_mult, finite) comm_monoid_mult by standard vector instance vec :: (semiring, finite) semiring by standard (vector field_simps)+ instance vec :: (semiring_0, finite) semiring_0 by standard (vector field_simps)+ instance vec :: (semiring_1, finite) semiring_1 by standard vector instance vec :: (comm_semiring, finite) comm_semiring by standard (vector field_simps)+ instance vec :: (comm_semiring_0, finite) comm_semiring_0 .. instance vec :: (semiring_0_cancel, finite) semiring_0_cancel .. instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel .. instance vec :: (ring, finite) ring .. instance vec :: (semiring_1_cancel, finite) semiring_1_cancel .. instance vec :: (comm_semiring_1, finite) comm_semiring_1 .. instance vec :: (ring_1, finite) ring_1 .. instance vec :: (real_algebra, finite) real_algebra by standard (simp_all add: vec_eq_iff) instance vec :: (real_algebra_1, finite) real_algebra_1 .. lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n" proof (induct n) case 0 then show ?case by vector next case Suc then show ?case by vector qed lemma one_index [simp]: "(1 :: 'a :: one ^ 'n) $ i = 1" by vector lemma neg_one_index [simp]: "(- 1 :: 'a :: {one, uminus} ^ 'n) $ i = - 1" by vector instance vec :: (semiring_char_0, finite) semiring_char_0 proof fix m n :: nat show "inj (of_nat :: nat \ 'a ^ 'b)" by (auto intro!: injI simp add: vec_eq_iff of_nat_index) qed instance vec :: (numeral, finite) numeral .. instance vec :: (semiring_numeral, finite) semiring_numeral .. lemma numeral_index [simp]: "numeral w $ i = numeral w" by (induct w) (simp_all only: numeral.simps vector_add_component one_index) lemma neg_numeral_index [simp]: "- numeral w $ i = - numeral w" by (simp only: vector_uminus_component numeral_index) instance vec :: (comm_ring_1, finite) comm_ring_1 .. instance vec :: (ring_char_0, finite) ring_char_0 .. lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x" by (vector mult.assoc) lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x" by (vector field_simps) lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y" by (vector field_simps) lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y" by (vector field_simps) lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x" by (vector field_simps) lemma vec_eq[simp]: "(vec m = vec n) \ (m = n)" by (simp add: vec_eq_iff) lemma Vector_Spaces_linear_vec [simp]: "Vector_Spaces.linear (*) vector_scalar_mult vec" by unfold_locales (vector algebra_simps)+ lemma vector_mul_eq_0[simp]: "(a *s x = 0) \ a = (0::'a::idom) \ x = 0" by vector lemma vector_mul_lcancel[simp]: "a *s x = a *s y \ a = (0::'a::field) \ x = y" by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib) lemma vector_mul_rcancel[simp]: "a *s x = b *s x \ (a::'a::field) = b \ x = 0" by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib) lemma scalar_mult_eq_scaleR [abs_def]: "c *s x = c *\<^sub>R x" unfolding scaleR_vec_def vector_scalar_mult_def by simp lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \c\ * dist x y" unfolding dist_norm scalar_mult_eq_scaleR unfolding scaleR_right_diff_distrib[symmetric] by simp lemma sum_component [simp]: fixes f:: " 'a \ ('b::comm_monoid_add) ^'n" shows "(sum f S)$i = sum (\x. (f x)$i) S" proof (cases "finite S") case True then show ?thesis by induct simp_all next case False then show ?thesis by simp qed lemma sum_eq: "sum f S = (\ i. sum (\x. (f x)$i ) S)" by (simp add: vec_eq_iff) lemma sum_cmul: fixes f:: "'c \ ('a::semiring_1)^'n" shows "sum (\x. c *s f x) S = c *s sum f S" by (simp add: vec_eq_iff sum_distrib_left) lemma linear_vec [simp]: "linear vec" using Vector_Spaces_linear_vec apply (auto ) by unfold_locales (vector algebra_simps)+ subsection \Matrix operations\ text\Matrix notation. NB: an MxN matrix is of type \<^typ>\'a^'n^'m\, not \<^typ>\'a^'m^'n\\ definition\<^marker>\tag important\ map_matrix::"('a \ 'b) \ (('a, 'i::finite)vec, 'j::finite) vec \ (('b, 'i)vec, 'j) vec" where "map_matrix f x = (\ i j. f (x $ i $ j))" lemma nth_map_matrix[simp]: "map_matrix f x $ i $ j = f (x $ i $ j)" by (simp add: map_matrix_def) definition\<^marker>\tag important\ matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \ 'a ^'p^'n \ 'a ^ 'p ^'m" (infixl "**" 70) where "m ** m' == (\ i j. sum (\k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m" definition\<^marker>\tag important\ matrix_vector_mult :: "('a::semiring_1) ^'n^'m \ 'a ^'n \ 'a ^ 'm" (infixl "*v" 70) where "m *v x \ (\ i. sum (\j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m" definition\<^marker>\tag important\ vector_matrix_mult :: "'a ^ 'm \ ('a::semiring_1) ^'n^'m \ 'a ^'n " (infixl "v*" 70) where "v v* m == (\ j. sum (\i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n" definition\<^marker>\tag unimportant\ "(mat::'a::zero => 'a ^'n^'n) k = (\ i j. if i = j then k else 0)" definition\<^marker>\tag unimportant\ transpose where "(transpose::'a^'n^'m \ 'a^'m^'n) A = (\ i j. ((A$j)$i))" definition\<^marker>\tag unimportant\ "(row::'m => 'a ^'n^'m \ 'a ^'n) i A = (\ j. ((A$i)$j))" definition\<^marker>\tag unimportant\ "(column::'n =>'a^'n^'m =>'a^'m) j A = (\ i. ((A$i)$j))" definition\<^marker>\tag unimportant\ "rows(A::'a^'n^'m) = { row i A | i. i \ (UNIV :: 'm set)}" definition\<^marker>\tag unimportant\ "columns(A::'a^'n^'m) = { column i A | i. i \ (UNIV :: 'n set)}" lemma times0_left [simp]: "(0::'a::semiring_1^'n^'m) ** (A::'a ^'p^'n) = 0" by (simp add: matrix_matrix_mult_def zero_vec_def) lemma times0_right [simp]: "(A::'a::semiring_1^'n^'m) ** (0::'a ^'p^'n) = 0" by (simp add: matrix_matrix_mult_def zero_vec_def) lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def) lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)" by (vector matrix_matrix_mult_def sum.distrib[symmetric] field_simps) lemma matrix_mul_lid [simp]: fixes A :: "'a::semiring_1 ^ 'm ^ 'n" shows "mat 1 ** A = A" apply (simp add: matrix_matrix_mult_def mat_def) apply vector apply (auto simp only: if_distrib if_distribR sum.delta'[OF finite] mult_1_left mult_zero_left if_True UNIV_I) done lemma matrix_mul_rid [simp]: fixes A :: "'a::semiring_1 ^ 'm ^ 'n" shows "A ** mat 1 = A" apply (simp add: matrix_matrix_mult_def mat_def) apply vector apply (auto simp only: if_distrib if_distribR sum.delta[OF finite] mult_1_right mult_zero_right if_True UNIV_I cong: if_cong) done proposition matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C" apply (vector matrix_matrix_mult_def sum_distrib_left sum_distrib_right mult.assoc) apply (subst sum.swap) apply simp done proposition matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x" apply (vector matrix_matrix_mult_def matrix_vector_mult_def sum_distrib_left sum_distrib_right mult.assoc) apply (subst sum.swap) apply simp done proposition scalar_matrix_assoc: fixes A :: "('a::real_algebra_1)^'m^'n" shows "k *\<^sub>R (A ** B) = (k *\<^sub>R A) ** B" by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff scaleR_sum_right) proposition matrix_scalar_ac: fixes A :: "('a::real_algebra_1)^'m^'n" shows "A ** (k *\<^sub>R B) = k *\<^sub>R A ** B" by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff) lemma matrix_vector_mul_lid [simp]: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)" apply (vector matrix_vector_mult_def mat_def) - apply (simp add: if_distrib if_distribR sum.delta' cong del: if_weak_cong) + apply (simp add: if_distrib if_distribR cong del: if_weak_cong) done lemma matrix_transpose_mul: "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)" by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult.commute) lemma matrix_mult_transpose_dot_column: shows "transpose A ** A = (\ i j. inner (column i A) (column j A))" by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def column_def inner_vec_def) lemma matrix_mult_transpose_dot_row: shows "A ** transpose A = (\ i j. inner (row i A) (row j A))" by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def row_def inner_vec_def) lemma matrix_eq: fixes A B :: "'a::semiring_1 ^ 'n ^ 'm" shows "A = B \ (\x. A *v x = B *v x)" (is "?lhs \ ?rhs") apply auto apply (subst vec_eq_iff) apply clarify apply (clarsimp simp add: matrix_vector_mult_def if_distrib if_distribR vec_eq_iff cong del: if_weak_cong) apply (erule_tac x="axis ia 1" in allE) apply (erule_tac x="i" in allE) apply (auto simp add: if_distrib if_distribR axis_def sum.delta[OF finite] cong del: if_weak_cong) done lemma matrix_vector_mul_component: "(A *v x)$k = inner (A$k) x" by (simp add: matrix_vector_mult_def inner_vec_def) lemma dot_lmul_matrix: "inner ((x::real ^_) v* A) y = inner x (A *v y)" apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def sum_distrib_right sum_distrib_left ac_simps) apply (subst sum.swap) apply simp done lemma transpose_mat [simp]: "transpose (mat n) = mat n" by (vector transpose_def mat_def) lemma transpose_transpose [simp]: "transpose(transpose A) = A" by (vector transpose_def) lemma row_transpose [simp]: "row i (transpose A) = column i A" by (simp add: row_def column_def transpose_def vec_eq_iff) lemma column_transpose [simp]: "column i (transpose A) = row i A" by (simp add: row_def column_def transpose_def vec_eq_iff) lemma rows_transpose [simp]: "rows(transpose A) = columns A" by (auto simp add: rows_def columns_def intro: set_eqI) lemma columns_transpose [simp]: "columns(transpose A) = rows A" by (metis transpose_transpose rows_transpose) lemma transpose_scalar: "transpose (k *\<^sub>R A) = k *\<^sub>R transpose A" unfolding transpose_def by (simp add: vec_eq_iff) lemma transpose_iff [iff]: "transpose A = transpose B \ A = B" by (metis transpose_transpose) lemma matrix_mult_sum: "(A::'a::comm_semiring_1^'n^'m) *v x = sum (\i. (x$i) *s column i A) (UNIV:: 'n set)" by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult.commute) lemma vector_componentwise: "(x::'a::ring_1^'n) = (\ j. \i\UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)" by (simp add: axis_def if_distrib sum.If_cases vec_eq_iff) lemma basis_expansion: "sum (\i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)" by (auto simp add: axis_def vec_eq_iff if_distrib sum.If_cases cong del: if_weak_cong) text\Correspondence between matrices and linear operators.\ definition\<^marker>\tag important\ matrix :: "('a::{plus,times, one, zero}^'m \ 'a ^ 'n) \ 'a^'m^'n" where "matrix f = (\ i j. (f(axis j 1))$i)" lemma matrix_id_mat_1: "matrix id = mat 1" by (simp add: mat_def matrix_def axis_def) lemma matrix_scaleR: "(matrix ((*\<^sub>R) r)) = mat r" by (simp add: mat_def matrix_def axis_def if_distrib cong: if_cong) lemma matrix_vector_mul_linear[intro, simp]: "linear (\x. A *v (x::'a::real_algebra_1 ^ _))" by (simp add: linear_iff matrix_vector_mult_def vec_eq_iff field_simps sum_distrib_left sum.distrib scaleR_right.sum) lemma vector_matrix_left_distrib [algebra_simps]: shows "(x + y) v* A = x v* A + y v* A" unfolding vector_matrix_mult_def by (simp add: algebra_simps sum.distrib vec_eq_iff) lemma matrix_vector_right_distrib [algebra_simps]: "A *v (x + y) = A *v x + A *v y" by (vector matrix_vector_mult_def sum.distrib distrib_left) lemma matrix_vector_mult_diff_distrib [algebra_simps]: fixes A :: "'a::ring_1^'n^'m" shows "A *v (x - y) = A *v x - A *v y" by (vector matrix_vector_mult_def sum_subtractf right_diff_distrib) lemma matrix_vector_mult_scaleR[algebra_simps]: fixes A :: "real^'n^'m" shows "A *v (c *\<^sub>R x) = c *\<^sub>R (A *v x)" using linear_iff matrix_vector_mul_linear by blast lemma matrix_vector_mult_0_right [simp]: "A *v 0 = 0" by (simp add: matrix_vector_mult_def vec_eq_iff) lemma matrix_vector_mult_0 [simp]: "0 *v w = 0" by (simp add: matrix_vector_mult_def vec_eq_iff) lemma matrix_vector_mult_add_rdistrib [algebra_simps]: "(A + B) *v x = (A *v x) + (B *v x)" by (vector matrix_vector_mult_def sum.distrib distrib_right) lemma matrix_vector_mult_diff_rdistrib [algebra_simps]: fixes A :: "'a :: ring_1^'n^'m" shows "(A - B) *v x = (A *v x) - (B *v x)" by (vector matrix_vector_mult_def sum_subtractf left_diff_distrib) lemma matrix_vector_column: "(A::'a::comm_semiring_1^'n^_) *v x = sum (\i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)" by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult.commute) subsection\Inverse matrices (not necessarily square)\ definition\<^marker>\tag important\ "invertible(A::'a::semiring_1^'n^'m) \ (\A'::'a^'m^'n. A ** A' = mat 1 \ A' ** A = mat 1)" definition\<^marker>\tag important\ "matrix_inv(A:: 'a::semiring_1^'n^'m) = (SOME A'::'a^'m^'n. A ** A' = mat 1 \ A' ** A = mat 1)" lemma inj_matrix_vector_mult: fixes A::"'a::field^'n^'m" assumes "invertible A" shows "inj ((*v) A)" by (metis assms inj_on_inverseI invertible_def matrix_vector_mul_assoc matrix_vector_mul_lid) lemma scalar_invertible: fixes A :: "('a::real_algebra_1)^'m^'n" assumes "k \ 0" and "invertible A" shows "invertible (k *\<^sub>R A)" proof - obtain A' where "A ** A' = mat 1" and "A' ** A = mat 1" using assms unfolding invertible_def by auto with \k \ 0\ have "(k *\<^sub>R A) ** ((1/k) *\<^sub>R A') = mat 1" "((1/k) *\<^sub>R A') ** (k *\<^sub>R A) = mat 1" by (simp_all add: assms matrix_scalar_ac) thus "invertible (k *\<^sub>R A)" unfolding invertible_def by auto qed proposition scalar_invertible_iff: fixes A :: "('a::real_algebra_1)^'m^'n" assumes "k \ 0" and "invertible A" shows "invertible (k *\<^sub>R A) \ k \ 0 \ invertible A" by (simp add: assms scalar_invertible) lemma vector_transpose_matrix [simp]: "x v* transpose A = A *v x" unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def by simp lemma transpose_matrix_vector [simp]: "transpose A *v x = x v* A" unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def by simp lemma vector_scalar_commute: fixes A :: "'a::{field}^'m^'n" shows "A *v (c *s x) = c *s (A *v x)" by (simp add: vector_scalar_mult_def matrix_vector_mult_def mult_ac sum_distrib_left) lemma scalar_vector_matrix_assoc: fixes k :: "'a::{field}" and x :: "'a::{field}^'n" and A :: "'a^'m^'n" shows "(k *s x) v* A = k *s (x v* A)" by (metis transpose_matrix_vector vector_scalar_commute) lemma vector_matrix_mult_0 [simp]: "0 v* A = 0" unfolding vector_matrix_mult_def by (simp add: zero_vec_def) lemma vector_matrix_mult_0_right [simp]: "x v* 0 = 0" unfolding vector_matrix_mult_def by (simp add: zero_vec_def) lemma vector_matrix_mul_rid [simp]: fixes v :: "('a::semiring_1)^'n" shows "v v* mat 1 = v" by (metis matrix_vector_mul_lid transpose_mat vector_transpose_matrix) lemma scaleR_vector_matrix_assoc: fixes k :: real and x :: "real^'n" and A :: "real^'m^'n" shows "(k *\<^sub>R x) v* A = k *\<^sub>R (x v* A)" by (metis matrix_vector_mult_scaleR transpose_matrix_vector) proposition vector_scaleR_matrix_ac: fixes k :: real and x :: "real^'n" and A :: "real^'m^'n" shows "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)" proof - have "x v* (k *\<^sub>R A) = (k *\<^sub>R x) v* A" unfolding vector_matrix_mult_def by (simp add: algebra_simps) with scaleR_vector_matrix_assoc show "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)" by auto qed end diff --git a/src/HOL/Analysis/Linear_Algebra.thy b/src/HOL/Analysis/Linear_Algebra.thy --- a/src/HOL/Analysis/Linear_Algebra.thy +++ b/src/HOL/Analysis/Linear_Algebra.thy @@ -1,1972 +1,1879 @@ (* Title: HOL/Analysis/Linear_Algebra.thy Author: Amine Chaieb, University of Cambridge *) section \Elementary Linear Algebra on Euclidean Spaces\ theory Linear_Algebra imports Euclidean_Space "HOL-Library.Infinite_Set" begin lemma linear_simps: assumes "bounded_linear f" shows "f (a + b) = f a + f b" "f (a - b) = f a - f b" "f 0 = 0" "f (- a) = - f a" "f (s *\<^sub>R v) = s *\<^sub>R (f v)" proof - interpret f: bounded_linear f by fact show "f (a + b) = f a + f b" by (rule f.add) show "f (a - b) = f a - f b" by (rule f.diff) show "f 0 = 0" by (rule f.zero) show "f (- a) = - f a" by (rule f.neg) show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scale) qed lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x \ (UNIV::'a::finite set)}" using finite finite_image_set by blast lemma substdbasis_expansion_unique: includes inner_syntax assumes d: "d \ Basis" shows "(\i\d. f i *\<^sub>R i) = (x::'a::euclidean_space) \ (\i\Basis. (i \ d \ f i = x \ i) \ (i \ d \ x \ i = 0))" proof - have *: "\x a b P. x * (if P then a else b) = (if P then x * a else x * b)" by auto have **: "finite d" by (auto intro: finite_subset[OF assms]) have ***: "\i. i \ Basis \ (\i\d. f i *\<^sub>R i) \ i = (\x\d. if x = i then f x else 0)" using d by (auto intro!: sum.cong simp: inner_Basis inner_sum_left) show ?thesis unfolding euclidean_eq_iff[where 'a='a] by (auto simp: sum.delta[OF **] ***) qed lemma independent_substdbasis: "d \ Basis \ independent d" by (rule independent_mono[OF independent_Basis]) lemma subset_translation_eq [simp]: fixes a :: "'a::real_vector" shows "(+) a ` s \ (+) a ` t \ s \ t" by auto lemma translate_inj_on: fixes A :: "'a::ab_group_add set" shows "inj_on (\x. a + x) A" unfolding inj_on_def by auto lemma translation_assoc: fixes a b :: "'a::ab_group_add" shows "(\x. b + x) ` ((\x. a + x) ` S) = (\x. (a + b) + x) ` S" by auto lemma translation_invert: fixes a :: "'a::ab_group_add" assumes "(\x. a + x) ` A = (\x. a + x) ` B" shows "A = B" proof - have "(\x. -a + x) ` ((\x. a + x) ` A) = (\x. - a + x) ` ((\x. a + x) ` B)" using assms by auto then show ?thesis using translation_assoc[of "-a" a A] translation_assoc[of "-a" a B] by auto qed lemma translation_galois: fixes a :: "'a::ab_group_add" shows "T = ((\x. a + x) ` S) \ S = ((\x. (- a) + x) ` T)" using translation_assoc[of "-a" a S] apply auto using translation_assoc[of a "-a" T] apply auto done lemma translation_inverse_subset: assumes "((\x. - a + x) ` V) \ (S :: 'n::ab_group_add set)" shows "V \ ((\x. a + x) ` S)" proof - { fix x assume "x \ V" then have "x-a \ S" using assms by auto then have "x \ {a + v |v. v \ S}" apply auto apply (rule exI[of _ "x-a"], simp) done then have "x \ ((\x. a+x) ` S)" by auto } then show ?thesis by auto qed subsection\<^marker>\tag unimportant\ \More interesting properties of the norm\ unbundle inner_syntax text\Equality of vectors in terms of \<^term>\(\)\ products.\ lemma linear_componentwise: fixes f:: "'a::euclidean_space \ 'b::real_inner" assumes lf: "linear f" shows "(f x) \ j = (\i\Basis. (x\i) * (f i\j))" (is "?lhs = ?rhs") proof - interpret linear f by fact have "?rhs = (\i\Basis. (x\i) *\<^sub>R (f i))\j" by (simp add: inner_sum_left) then show ?thesis by (simp add: euclidean_representation sum[symmetric] scale[symmetric]) qed lemma vector_eq: "x = y \ x \ x = x \ y \ y \ y = x \ x" (is "?lhs \ ?rhs") proof assume ?lhs then show ?rhs by simp next assume ?rhs then have "x \ x - x \ y = 0 \ x \ y - y \ y = 0" by simp then have "x \ (x - y) = 0 \ y \ (x - y) = 0" by (simp add: inner_diff inner_commute) then have "(x - y) \ (x - y) = 0" by (simp add: field_simps inner_diff inner_commute) then show "x = y" by simp qed lemma norm_triangle_half_r: "norm (y - x1) < e / 2 \ norm (y - x2) < e / 2 \ norm (x1 - x2) < e" using dist_triangle_half_r unfolding dist_norm[symmetric] by auto lemma norm_triangle_half_l: assumes "norm (x - y) < e / 2" and "norm (x' - y) < e / 2" shows "norm (x - x') < e" using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]] unfolding dist_norm[symmetric] . lemma abs_triangle_half_r: fixes y :: "'a::linordered_field" shows "abs (y - x1) < e / 2 \ abs (y - x2) < e / 2 \ abs (x1 - x2) < e" by linarith lemma abs_triangle_half_l: fixes y :: "'a::linordered_field" assumes "abs (x - y) < e / 2" and "abs (x' - y) < e / 2" shows "abs (x - x') < e" using assms by linarith lemma sum_clauses: shows "sum f {} = 0" and "finite S \ sum f (insert x S) = (if x \ S then sum f S else f x + sum f S)" by (auto simp add: insert_absorb) lemma vector_eq_ldot: "(\x. x \ y = x \ z) \ y = z" proof assume "\x. x \ y = x \ z" then have "\x. x \ (y - z) = 0" by (simp add: inner_diff) then have "(y - z) \ (y - z) = 0" .. then show "y = z" by simp qed simp lemma vector_eq_rdot: "(\z. x \ z = y \ z) \ x = y" proof assume "\z. x \ z = y \ z" then have "\z. (x - y) \ z = 0" by (simp add: inner_diff) then have "(x - y) \ (x - y) = 0" .. then show "x = y" by simp qed simp subsection \Substandard Basis\ lemma ex_card: assumes "n \ card A" shows "\S\A. card S = n" proof (cases "finite A") case True from ex_bij_betw_nat_finite[OF this] obtain f where f: "bij_betw f {0..n \ card A\ have "{..< n} \ {..< card A}" "inj_on f {..< n}" by (auto simp: bij_betw_def intro: subset_inj_on) ultimately have "f ` {..< n} \ A" "card (f ` {..< n}) = n" by (auto simp: bij_betw_def card_image) then show ?thesis by blast next case False with \n \ card A\ show ?thesis by force qed lemma subspace_substandard: "subspace {x::'a::euclidean_space. (\i\Basis. P i \ x\i = 0)}" by (auto simp: subspace_def inner_add_left) lemma dim_substandard: assumes d: "d \ Basis" shows "dim {x::'a::euclidean_space. \i\Basis. i \ d \ x\i = 0} = card d" (is "dim ?A = _") proof (rule dim_unique) from d show "d \ ?A" by (auto simp: inner_Basis) from d show "independent d" by (rule independent_mono [OF independent_Basis]) have "x \ span d" if "\i\Basis. i \ d \ x \ i = 0" for x proof - have "finite d" by (rule finite_subset [OF d finite_Basis]) then have "(\i\d. (x \ i) *\<^sub>R i) \ span d" by (simp add: span_sum span_clauses) also have "(\i\d. (x \ i) *\<^sub>R i) = (\i\Basis. (x \ i) *\<^sub>R i)" by (rule sum.mono_neutral_cong_left [OF finite_Basis d]) (auto simp: that) finally show "x \ span d" by (simp only: euclidean_representation) qed then show "?A \ span d" by auto qed simp subsection \Orthogonality\ definition\<^marker>\tag important\ (in real_inner) "orthogonal x y \ x \ y = 0" context real_inner begin lemma orthogonal_self: "orthogonal x x \ x = 0" by (simp add: orthogonal_def) lemma orthogonal_clauses: "orthogonal a 0" "orthogonal a x \ orthogonal a (c *\<^sub>R x)" "orthogonal a x \ orthogonal a (- x)" "orthogonal a x \ orthogonal a y \ orthogonal a (x + y)" "orthogonal a x \ orthogonal a y \ orthogonal a (x - y)" "orthogonal 0 a" "orthogonal x a \ orthogonal (c *\<^sub>R x) a" "orthogonal x a \ orthogonal (- x) a" "orthogonal x a \ orthogonal y a \ orthogonal (x + y) a" "orthogonal x a \ orthogonal y a \ orthogonal (x - y) a" unfolding orthogonal_def inner_add inner_diff by auto end lemma orthogonal_commute: "orthogonal x y \ orthogonal y x" by (simp add: orthogonal_def inner_commute) lemma orthogonal_scaleR [simp]: "c \ 0 \ orthogonal (c *\<^sub>R x) = orthogonal x" by (rule ext) (simp add: orthogonal_def) lemma pairwise_ortho_scaleR: "pairwise (\i j. orthogonal (f i) (g j)) B \ pairwise (\i j. orthogonal (a i *\<^sub>R f i) (a j *\<^sub>R g j)) B" by (auto simp: pairwise_def orthogonal_clauses) lemma orthogonal_rvsum: "\finite s; \y. y \ s \ orthogonal x (f y)\ \ orthogonal x (sum f s)" by (induction s rule: finite_induct) (auto simp: orthogonal_clauses) lemma orthogonal_lvsum: "\finite s; \x. x \ s \ orthogonal (f x) y\ \ orthogonal (sum f s) y" by (induction s rule: finite_induct) (auto simp: orthogonal_clauses) lemma norm_add_Pythagorean: assumes "orthogonal a b" shows "norm(a + b) ^ 2 = norm a ^ 2 + norm b ^ 2" proof - from assms have "(a - (0 - b)) \ (a - (0 - b)) = a \ a - (0 - b \ b)" by (simp add: algebra_simps orthogonal_def inner_commute) then show ?thesis by (simp add: power2_norm_eq_inner) qed lemma norm_sum_Pythagorean: assumes "finite I" "pairwise (\i j. orthogonal (f i) (f j)) I" shows "(norm (sum f I))\<^sup>2 = (\i\I. (norm (f i))\<^sup>2)" using assms proof (induction I rule: finite_induct) case empty then show ?case by simp next case (insert x I) then have "orthogonal (f x) (sum f I)" by (metis pairwise_insert orthogonal_rvsum) with insert show ?case by (simp add: pairwise_insert norm_add_Pythagorean) qed subsection \Orthogonality of a transformation\ definition\<^marker>\tag important\ "orthogonal_transformation f \ linear f \ (\v w. f v \ f w = v \ w)" lemma\<^marker>\tag unimportant\ orthogonal_transformation: "orthogonal_transformation f \ linear f \ (\v. norm (f v) = norm v)" unfolding orthogonal_transformation_def apply auto apply (erule_tac x=v in allE)+ apply (simp add: norm_eq_sqrt_inner) apply (simp add: dot_norm linear_add[symmetric]) done lemma\<^marker>\tag unimportant\ orthogonal_transformation_id [simp]: "orthogonal_transformation (\x. x)" by (simp add: linear_iff orthogonal_transformation_def) lemma\<^marker>\tag unimportant\ orthogonal_orthogonal_transformation: "orthogonal_transformation f \ orthogonal (f x) (f y) \ orthogonal x y" by (simp add: orthogonal_def orthogonal_transformation_def) lemma\<^marker>\tag unimportant\ orthogonal_transformation_compose: "\orthogonal_transformation f; orthogonal_transformation g\ \ orthogonal_transformation(f \ g)" by (auto simp: orthogonal_transformation_def linear_compose) lemma\<^marker>\tag unimportant\ orthogonal_transformation_neg: "orthogonal_transformation(\x. -(f x)) \ orthogonal_transformation f" by (auto simp: orthogonal_transformation_def dest: linear_compose_neg) lemma\<^marker>\tag unimportant\ orthogonal_transformation_scaleR: "orthogonal_transformation f \ f (c *\<^sub>R v) = c *\<^sub>R f v" by (simp add: linear_iff orthogonal_transformation_def) lemma\<^marker>\tag unimportant\ orthogonal_transformation_linear: "orthogonal_transformation f \ linear f" by (simp add: orthogonal_transformation_def) lemma\<^marker>\tag unimportant\ orthogonal_transformation_inj: "orthogonal_transformation f \ inj f" unfolding orthogonal_transformation_def inj_on_def by (metis vector_eq) lemma\<^marker>\tag unimportant\ orthogonal_transformation_surj: "orthogonal_transformation f \ surj f" for f :: "'a::euclidean_space \ 'a::euclidean_space" by (simp add: linear_injective_imp_surjective orthogonal_transformation_inj orthogonal_transformation_linear) lemma\<^marker>\tag unimportant\ orthogonal_transformation_bij: "orthogonal_transformation f \ bij f" for f :: "'a::euclidean_space \ 'a::euclidean_space" by (simp add: bij_def orthogonal_transformation_inj orthogonal_transformation_surj) lemma\<^marker>\tag unimportant\ orthogonal_transformation_inv: "orthogonal_transformation f \ orthogonal_transformation (inv f)" for f :: "'a::euclidean_space \ 'a::euclidean_space" by (metis (no_types, hide_lams) bijection.inv_right bijection_def inj_linear_imp_inv_linear orthogonal_transformation orthogonal_transformation_bij orthogonal_transformation_inj) lemma\<^marker>\tag unimportant\ orthogonal_transformation_norm: "orthogonal_transformation f \ norm (f x) = norm x" by (metis orthogonal_transformation) subsection \Bilinear functions\ definition\<^marker>\tag important\ bilinear :: "('a::real_vector \ 'b::real_vector \ 'c::real_vector) \ bool" where "bilinear f \ (\x. linear (\y. f x y)) \ (\y. linear (\x. f x y))" lemma bilinear_ladd: "bilinear h \ h (x + y) z = h x z + h y z" by (simp add: bilinear_def linear_iff) lemma bilinear_radd: "bilinear h \ h x (y + z) = h x y + h x z" by (simp add: bilinear_def linear_iff) lemma bilinear_times: fixes c::"'a::real_algebra" shows "bilinear (\x y::'a. x*y)" by (auto simp: bilinear_def distrib_left distrib_right intro!: linearI) lemma bilinear_lmul: "bilinear h \ h (c *\<^sub>R x) y = c *\<^sub>R h x y" by (simp add: bilinear_def linear_iff) lemma bilinear_rmul: "bilinear h \ h x (c *\<^sub>R y) = c *\<^sub>R h x y" by (simp add: bilinear_def linear_iff) lemma bilinear_lneg: "bilinear h \ h (- x) y = - h x y" by (drule bilinear_lmul [of _ "- 1"]) simp lemma bilinear_rneg: "bilinear h \ h x (- y) = - h x y" by (drule bilinear_rmul [of _ _ "- 1"]) simp lemma (in ab_group_add) eq_add_iff: "x = x + y \ y = 0" using add_left_imp_eq[of x y 0] by auto lemma bilinear_lzero: assumes "bilinear h" shows "h 0 x = 0" using bilinear_ladd [OF assms, of 0 0 x] by (simp add: eq_add_iff field_simps) lemma bilinear_rzero: assumes "bilinear h" shows "h x 0 = 0" using bilinear_radd [OF assms, of x 0 0 ] by (simp add: eq_add_iff field_simps) lemma bilinear_lsub: "bilinear h \ h (x - y) z = h x z - h y z" using bilinear_ladd [of h x "- y"] by (simp add: bilinear_lneg) lemma bilinear_rsub: "bilinear h \ h z (x - y) = h z x - h z y" using bilinear_radd [of h _ x "- y"] by (simp add: bilinear_rneg) lemma bilinear_sum: assumes "bilinear h" shows "h (sum f S) (sum g T) = sum (\(i,j). h (f i) (g j)) (S \ T) " proof - interpret l: linear "\x. h x y" for y using assms by (simp add: bilinear_def) interpret r: linear "\y. h x y" for x using assms by (simp add: bilinear_def) have "h (sum f S) (sum g T) = sum (\x. h (f x) (sum g T)) S" by (simp add: l.sum) also have "\ = sum (\x. sum (\y. h (f x) (g y)) T) S" by (rule sum.cong) (simp_all add: r.sum) finally show ?thesis unfolding sum.cartesian_product . qed subsection \Adjoints\ definition\<^marker>\tag important\ adjoint :: "(('a::real_inner) \ ('b::real_inner)) \ 'b \ 'a" where "adjoint f = (SOME f'. \x y. f x \ y = x \ f' y)" lemma adjoint_unique: assumes "\x y. inner (f x) y = inner x (g y)" shows "adjoint f = g" unfolding adjoint_def proof (rule some_equality) show "\x y. inner (f x) y = inner x (g y)" by (rule assms) next fix h assume "\x y. inner (f x) y = inner x (h y)" then have "\x y. inner x (g y) = inner x (h y)" using assms by simp then have "\x y. inner x (g y - h y) = 0" by (simp add: inner_diff_right) then have "\y. inner (g y - h y) (g y - h y) = 0" by simp then have "\y. h y = g y" by simp then show "h = g" by (simp add: ext) qed text \TODO: The following lemmas about adjoints should hold for any Hilbert space (i.e. complete inner product space). (see \<^url>\https://en.wikipedia.org/wiki/Hermitian_adjoint\) \ lemma adjoint_works: fixes f :: "'n::euclidean_space \ 'm::euclidean_space" assumes lf: "linear f" shows "x \ adjoint f y = f x \ y" proof - interpret linear f by fact have "\y. \w. \x. f x \ y = x \ w" proof (intro allI exI) fix y :: "'m" and x let ?w = "(\i\Basis. (f i \ y) *\<^sub>R i) :: 'n" have "f x \ y = f (\i\Basis. (x \ i) *\<^sub>R i) \ y" by (simp add: euclidean_representation) also have "\ = (\i\Basis. (x \ i) *\<^sub>R f i) \ y" by (simp add: sum scale) finally show "f x \ y = x \ ?w" by (simp add: inner_sum_left inner_sum_right mult.commute) qed then show ?thesis unfolding adjoint_def choice_iff by (intro someI2_ex[where Q="\f'. x \ f' y = f x \ y"]) auto qed lemma adjoint_clauses: fixes f :: "'n::euclidean_space \ 'm::euclidean_space" assumes lf: "linear f" shows "x \ adjoint f y = f x \ y" and "adjoint f y \ x = y \ f x" by (simp_all add: adjoint_works[OF lf] inner_commute) lemma adjoint_linear: fixes f :: "'n::euclidean_space \ 'm::euclidean_space" assumes lf: "linear f" shows "linear (adjoint f)" by (simp add: lf linear_iff euclidean_eq_iff[where 'a='n] euclidean_eq_iff[where 'a='m] adjoint_clauses[OF lf] inner_distrib) lemma adjoint_adjoint: fixes f :: "'n::euclidean_space \ 'm::euclidean_space" assumes lf: "linear f" shows "adjoint (adjoint f) = f" by (rule adjoint_unique, simp add: adjoint_clauses [OF lf]) -subsection \Archimedean properties and useful consequences\ - -text\Bernoulli's inequality\ -proposition Bernoulli_inequality: - fixes x :: real - assumes "-1 \ x" - shows "1 + n * x \ (1 + x) ^ n" -proof (induct n) - case 0 - then show ?case by simp -next - case (Suc n) - have "1 + Suc n * x \ 1 + (Suc n)*x + n * x^2" - by (simp add: algebra_simps) - also have "... = (1 + x) * (1 + n*x)" - by (auto simp: power2_eq_square algebra_simps of_nat_Suc) - also have "... \ (1 + x) ^ Suc n" - using Suc.hyps assms mult_left_mono by fastforce - finally show ?case . -qed - -corollary Bernoulli_inequality_even: - fixes x :: real - assumes "even n" - shows "1 + n * x \ (1 + x) ^ n" -proof (cases "-1 \ x \ n=0") - case True - then show ?thesis - by (auto simp: Bernoulli_inequality) -next - case False - then have "real n \ 1" - by simp - with False have "n * x \ -1" - by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one) - then have "1 + n * x \ 0" - by auto - also have "... \ (1 + x) ^ n" - using assms - using zero_le_even_power by blast - finally show ?thesis . -qed - -corollary real_arch_pow: - fixes x :: real - assumes x: "1 < x" - shows "\n. y < x^n" -proof - - from x have x0: "x - 1 > 0" - by arith - from reals_Archimedean3[OF x0, rule_format, of y] - obtain n :: nat where n: "y < real n * (x - 1)" by metis - from x0 have x00: "x- 1 \ -1" by arith - from Bernoulli_inequality[OF x00, of n] n - have "y < x^n" by auto - then show ?thesis by metis -qed - -corollary real_arch_pow_inv: - fixes x y :: real - assumes y: "y > 0" - and x1: "x < 1" - shows "\n. x^n < y" -proof (cases "x > 0") - case True - with x1 have ix: "1 < 1/x" by (simp add: field_simps) - from real_arch_pow[OF ix, of "1/y"] - obtain n where n: "1/y < (1/x)^n" by blast - then show ?thesis using y \x > 0\ - by (auto simp add: field_simps) -next - case False - with y x1 show ?thesis - by (metis less_le_trans not_less power_one_right) -qed - -lemma forall_pos_mono: - "(\d e::real. d < e \ P d \ P e) \ - (\n::nat. n \ 0 \ P (inverse (real n))) \ (\e. 0 < e \ P e)" - by (metis real_arch_inverse) - -lemma forall_pos_mono_1: - "(\d e::real. d < e \ P d \ P e) \ - (\n. P (inverse (real (Suc n)))) \ 0 < e \ P e" - apply (rule forall_pos_mono) - apply auto - apply (metis Suc_pred of_nat_Suc) - done - - subsection\<^marker>\tag unimportant\ \Euclidean Spaces as Typeclass\ lemma independent_Basis: "independent Basis" by (rule independent_Basis) lemma span_Basis [simp]: "span Basis = UNIV" by (rule span_Basis) lemma in_span_Basis: "x \ span Basis" unfolding span_Basis .. subsection\<^marker>\tag unimportant\ \Linearity and Bilinearity continued\ lemma linear_bounded: fixes f :: "'a::euclidean_space \ 'b::real_normed_vector" assumes lf: "linear f" shows "\B. \x. norm (f x) \ B * norm x" proof interpret linear f by fact let ?B = "\b\Basis. norm (f b)" show "\x. norm (f x) \ ?B * norm x" proof fix x :: 'a let ?g = "\b. (x \ b) *\<^sub>R f b" have "norm (f x) = norm (f (\b\Basis. (x \ b) *\<^sub>R b))" unfolding euclidean_representation .. also have "\ = norm (sum ?g Basis)" by (simp add: sum scale) finally have th0: "norm (f x) = norm (sum ?g Basis)" . have th: "norm (?g i) \ norm (f i) * norm x" if "i \ Basis" for i proof - from Basis_le_norm[OF that, of x] show "norm (?g i) \ norm (f i) * norm x" unfolding norm_scaleR by (metis mult.commute mult_left_mono norm_ge_zero) qed from sum_norm_le[of _ ?g, OF th] show "norm (f x) \ ?B * norm x" unfolding th0 sum_distrib_right by metis qed qed lemma linear_conv_bounded_linear: fixes f :: "'a::euclidean_space \ 'b::real_normed_vector" shows "linear f \ bounded_linear f" proof assume "linear f" then interpret f: linear f . show "bounded_linear f" proof have "\B. \x. norm (f x) \ B * norm x" using \linear f\ by (rule linear_bounded) then show "\K. \x. norm (f x) \ norm x * K" by (simp add: mult.commute) qed next assume "bounded_linear f" then interpret f: bounded_linear f . show "linear f" .. qed lemmas linear_linear = linear_conv_bounded_linear[symmetric] lemma inj_linear_imp_inv_bounded_linear: fixes f::"'a::euclidean_space \ 'a" shows "\bounded_linear f; inj f\ \ bounded_linear (inv f)" by (simp add: inj_linear_imp_inv_linear linear_linear) lemma linear_bounded_pos: fixes f :: "'a::euclidean_space \ 'b::real_normed_vector" assumes lf: "linear f" obtains B where "B > 0" "\x. norm (f x) \ B * norm x" proof - have "\B > 0. \x. norm (f x) \ norm x * B" using lf unfolding linear_conv_bounded_linear by (rule bounded_linear.pos_bounded) with that show ?thesis by (auto simp: mult.commute) qed lemma linear_invertible_bounded_below_pos: fixes f :: "'a::real_normed_vector \ 'b::euclidean_space" assumes "linear f" "linear g" "g \ f = id" obtains B where "B > 0" "\x. B * norm x \ norm(f x)" proof - obtain B where "B > 0" and B: "\x. norm (g x) \ B * norm x" using linear_bounded_pos [OF \linear g\] by blast show thesis proof show "0 < 1/B" by (simp add: \B > 0\) show "1/B * norm x \ norm (f x)" for x proof - have "1/B * norm x = 1/B * norm (g (f x))" using assms by (simp add: pointfree_idE) also have "\ \ norm (f x)" using B [of "f x"] by (simp add: \B > 0\ mult.commute pos_divide_le_eq) finally show ?thesis . qed qed qed lemma linear_inj_bounded_below_pos: fixes f :: "'a::real_normed_vector \ 'b::euclidean_space" assumes "linear f" "inj f" obtains B where "B > 0" "\x. B * norm x \ norm(f x)" using linear_injective_left_inverse [OF assms] linear_invertible_bounded_below_pos assms by blast lemma bounded_linearI': fixes f ::"'a::euclidean_space \ 'b::real_normed_vector" assumes "\x y. f (x + y) = f x + f y" and "\c x. f (c *\<^sub>R x) = c *\<^sub>R f x" shows "bounded_linear f" using assms linearI linear_conv_bounded_linear by blast lemma bilinear_bounded: fixes h :: "'m::euclidean_space \ 'n::euclidean_space \ 'k::real_normed_vector" assumes bh: "bilinear h" shows "\B. \x y. norm (h x y) \ B * norm x * norm y" proof (clarify intro!: exI[of _ "\i\Basis. \j\Basis. norm (h i j)"]) fix x :: 'm fix y :: 'n have "norm (h x y) = norm (h (sum (\i. (x \ i) *\<^sub>R i) Basis) (sum (\i. (y \ i) *\<^sub>R i) Basis))" by (simp add: euclidean_representation) also have "\ = norm (sum (\ (i,j). h ((x \ i) *\<^sub>R i) ((y \ j) *\<^sub>R j)) (Basis \ Basis))" unfolding bilinear_sum[OF bh] .. finally have th: "norm (h x y) = \" . have "\i j. \i \ Basis; j \ Basis\ \ \x \ i\ * (\y \ j\ * norm (h i j)) \ norm x * (norm y * norm (h i j))" by (auto simp add: zero_le_mult_iff Basis_le_norm mult_mono) then show "norm (h x y) \ (\i\Basis. \j\Basis. norm (h i j)) * norm x * norm y" unfolding sum_distrib_right th sum.cartesian_product by (clarsimp simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] field_simps simp del: scaleR_scaleR intro!: sum_norm_le) qed lemma bilinear_conv_bounded_bilinear: fixes h :: "'a::euclidean_space \ 'b::euclidean_space \ 'c::real_normed_vector" shows "bilinear h \ bounded_bilinear h" proof assume "bilinear h" show "bounded_bilinear h" proof fix x y z show "h (x + y) z = h x z + h y z" using \bilinear h\ unfolding bilinear_def linear_iff by simp next fix x y z show "h x (y + z) = h x y + h x z" using \bilinear h\ unfolding bilinear_def linear_iff by simp next 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 using \bilinear h\ unfolding bilinear_def linear_iff by simp_all next have "\B. \x y. norm (h x y) \ B * norm x * norm y" using \bilinear h\ by (rule bilinear_bounded) then show "\K. \x y. norm (h x y) \ norm x * norm y * K" by (simp add: ac_simps) qed next assume "bounded_bilinear h" then interpret h: bounded_bilinear h . show "bilinear h" unfolding bilinear_def linear_conv_bounded_linear using h.bounded_linear_left h.bounded_linear_right by simp qed lemma bilinear_bounded_pos: fixes h :: "'a::euclidean_space \ 'b::euclidean_space \ 'c::real_normed_vector" assumes bh: "bilinear h" shows "\B > 0. \x y. norm (h x y) \ B * norm x * norm y" proof - have "\B > 0. \x y. norm (h x y) \ norm x * norm y * B" using bh [unfolded bilinear_conv_bounded_bilinear] by (rule bounded_bilinear.pos_bounded) then show ?thesis by (simp only: ac_simps) qed lemma bounded_linear_imp_has_derivative: "bounded_linear f \ (f has_derivative f) net" by (auto simp add: has_derivative_def linear_diff linear_linear linear_def dest: bounded_linear.linear) lemma linear_imp_has_derivative: fixes f :: "'a::euclidean_space \ 'b::real_normed_vector" shows "linear f \ (f has_derivative f) net" by (simp add: bounded_linear_imp_has_derivative linear_conv_bounded_linear) lemma bounded_linear_imp_differentiable: "bounded_linear f \ f differentiable net" using bounded_linear_imp_has_derivative differentiable_def by blast lemma linear_imp_differentiable: fixes f :: "'a::euclidean_space \ 'b::real_normed_vector" shows "linear f \ f differentiable net" by (metis linear_imp_has_derivative differentiable_def) subsection\<^marker>\tag unimportant\ \We continue\ lemma independent_bound: fixes S :: "'a::euclidean_space set" shows "independent S \ finite S \ card S \ DIM('a)" by (metis dim_subset_UNIV finiteI_independent dim_span_eq_card_independent) lemmas independent_imp_finite = finiteI_independent corollary fixes S :: "'a::euclidean_space set" assumes "independent S" shows independent_card_le:"card S \ DIM('a)" using assms independent_bound by auto lemma dependent_biggerset: fixes S :: "'a::euclidean_space set" shows "(finite S \ card S > DIM('a)) \ dependent S" by (metis independent_bound not_less) text \Picking an orthogonal replacement for a spanning set.\ lemma vector_sub_project_orthogonal: fixes b x :: "'a::euclidean_space" shows "b \ (x - ((b \ x) / (b \ b)) *\<^sub>R b) = 0" unfolding inner_simps by auto lemma pairwise_orthogonal_insert: assumes "pairwise orthogonal S" and "\y. y \ S \ orthogonal x y" shows "pairwise orthogonal (insert x S)" using assms unfolding pairwise_def by (auto simp add: orthogonal_commute) lemma basis_orthogonal: fixes B :: "'a::real_inner set" assumes fB: "finite B" shows "\C. finite C \ card C \ card B \ span C = span B \ pairwise orthogonal C" (is " \C. ?P B C") using fB proof (induct rule: finite_induct) case empty then show ?case apply (rule exI[where x="{}"]) apply (auto simp add: pairwise_def) done next case (insert a B) note fB = \finite B\ and aB = \a \ B\ from \\C. finite C \ card C \ card B \ span C = span B \ pairwise orthogonal C\ obtain C where C: "finite C" "card C \ card B" "span C = span B" "pairwise orthogonal C" by blast let ?a = "a - sum (\x. (x \ a / (x \ x)) *\<^sub>R x) C" let ?C = "insert ?a C" from C(1) have fC: "finite ?C" by simp from fB aB C(1,2) have cC: "card ?C \ card (insert a B)" by (simp add: card_insert_if) { fix x k have th0: "\(a::'a) b c. a - (b - c) = c + (a - b)" by (simp add: field_simps) have "x - k *\<^sub>R (a - (\x\C. (x \ a / (x \ x)) *\<^sub>R x)) \ span C \ x - k *\<^sub>R a \ span C" apply (simp only: scaleR_right_diff_distrib th0) apply (rule span_add_eq) apply (rule span_scale) apply (rule span_sum) apply (rule span_scale) apply (rule span_base) apply assumption done } then have SC: "span ?C = span (insert a B)" unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto { fix y assume yC: "y \ C" then have Cy: "C = insert y (C - {y})" by blast have fth: "finite (C - {y})" using C by simp have "orthogonal ?a y" unfolding orthogonal_def unfolding inner_diff inner_sum_left right_minus_eq unfolding sum.remove [OF \finite C\ \y \ C\] apply (clarsimp simp add: inner_commute[of y a]) apply (rule sum.neutral) apply clarsimp apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format]) using \y \ C\ by auto } with \pairwise orthogonal C\ have CPO: "pairwise orthogonal ?C" by (rule pairwise_orthogonal_insert) from fC cC SC CPO have "?P (insert a B) ?C" by blast then show ?case by blast qed lemma orthogonal_basis_exists: fixes V :: "('a::euclidean_space) set" shows "\B. independent B \ B \ span V \ V \ span B \ (card B = dim V) \ pairwise orthogonal B" proof - from basis_exists[of V] obtain B where B: "B \ V" "independent B" "V \ span B" "card B = dim V" by force from B have fB: "finite B" "card B = dim V" using independent_bound by auto from basis_orthogonal[OF fB(1)] obtain C where C: "finite C" "card C \ card B" "span C = span B" "pairwise orthogonal C" by blast from C B have CSV: "C \ span V" by (metis span_superset span_mono subset_trans) from span_mono[OF B(3)] C have SVC: "span V \ span C" by (simp add: span_span) from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB have iC: "independent C" - by (simp add: dim_span) + by (simp) from C fB have "card C \ dim V" by simp moreover have "dim V \ card C" using span_card_ge_dim[OF CSV SVC C(1)] by simp ultimately have CdV: "card C = dim V" using C(1) by simp from C B CSV CdV iC show ?thesis by auto qed text \Low-dimensional subset is in a hyperplane (weak orthogonal complement).\ lemma span_not_univ_orthogonal: fixes S :: "'a::euclidean_space set" assumes sU: "span S \ UNIV" shows "\a::'a. a \ 0 \ (\x \ span S. a \ x = 0)" proof - from sU obtain a where a: "a \ span S" by blast from orthogonal_basis_exists obtain B where B: "independent B" "B \ span S" "S \ span B" "card B = dim S" "pairwise orthogonal B" by blast from B have fB: "finite B" "card B = dim S" using independent_bound by auto from span_mono[OF B(2)] span_mono[OF B(3)] have sSB: "span S = span B" by (simp add: span_span) let ?a = "a - sum (\b. (a \ b / (b \ b)) *\<^sub>R b) B" have "sum (\b. (a \ b / (b \ b)) *\<^sub>R b) B \ span S" unfolding sSB apply (rule span_sum) apply (rule span_scale) apply (rule span_base) apply assumption done with a have a0:"?a \ 0" by auto have "?a \ x = 0" if "x\span B" for x proof (rule span_induct [OF that]) show "subspace {x. ?a \ x = 0}" by (auto simp add: subspace_def inner_add) next { fix x assume x: "x \ B" from x have B': "B = insert x (B - {x})" by blast have fth: "finite (B - {x})" using fB by simp have "?a \ x = 0" apply (subst B') using fB fth unfolding sum_clauses(2)[OF fth] apply simp unfolding inner_simps apply (clarsimp simp add: inner_add inner_sum_left) apply (rule sum.neutral, rule ballI) apply (simp only: inner_commute) apply (auto simp add: x field_simps intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format]) done } then show "?a \ x = 0" if "x \ B" for x using that by blast qed with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"]) qed lemma span_not_univ_subset_hyperplane: fixes S :: "'a::euclidean_space set" assumes SU: "span S \ UNIV" shows "\ a. a \0 \ span S \ {x. a \ x = 0}" using span_not_univ_orthogonal[OF SU] by auto lemma lowdim_subset_hyperplane: fixes S :: "'a::euclidean_space set" assumes d: "dim S < DIM('a)" shows "\a::'a. a \ 0 \ span S \ {x. a \ x = 0}" proof - { assume "span S = UNIV" then have "dim (span S) = dim (UNIV :: ('a) set)" by simp then have "dim S = DIM('a)" by (metis Euclidean_Space.dim_UNIV dim_span) with d have False by arith } then have th: "span S \ UNIV" by blast from span_not_univ_subset_hyperplane[OF th] show ?thesis . qed lemma linear_eq_stdbasis: fixes f :: "'a::euclidean_space \ _" assumes lf: "linear f" and lg: "linear g" and fg: "\b. b \ Basis \ f b = g b" shows "f = g" using linear_eq_on_span[OF lf lg, of Basis] fg by auto text \Similar results for bilinear functions.\ lemma bilinear_eq: assumes bf: "bilinear f" and bg: "bilinear g" and SB: "S \ span B" and TC: "T \ span C" and "x\S" "y\T" and fg: "\x y. \x \ B; y\ C\ \ f x y = g x y" shows "f x y = g x y" proof - let ?P = "{x. \y\ span C. f x y = g x y}" from bf bg have sp: "subspace ?P" unfolding bilinear_def linear_iff subspace_def bf bg by (auto simp add: span_zero bilinear_lzero[OF bf] bilinear_lzero[OF bg] span_add Ball_def intro: bilinear_ladd[OF bf]) have sfg: "\x. x \ B \ subspace {a. f x a = g x a}" apply (auto simp add: subspace_def) using bf bg unfolding bilinear_def linear_iff apply (auto simp add: span_zero bilinear_rzero[OF bf] bilinear_rzero[OF bg] span_add Ball_def intro: bilinear_ladd[OF bf]) done have "\y\ span C. f x y = g x y" if "x \ span B" for x apply (rule span_induct [OF that sp]) using fg sfg span_induct by blast then show ?thesis using SB TC assms by auto qed lemma bilinear_eq_stdbasis: fixes f :: "'a::euclidean_space \ 'b::euclidean_space \ _" assumes bf: "bilinear f" and bg: "bilinear g" and fg: "\i j. i \ Basis \ j \ Basis \ f i j = g i j" shows "f = g" using bilinear_eq[OF bf bg equalityD2[OF span_Basis] equalityD2[OF span_Basis]] fg by blast subsection \Infinity norm\ definition\<^marker>\tag important\ "infnorm (x::'a::euclidean_space) = Sup {\x \ b\ |b. b \ Basis}" lemma infnorm_set_image: fixes x :: "'a::euclidean_space" shows "{\x \ i\ |i. i \ Basis} = (\i. \x \ i\) ` Basis" by blast lemma infnorm_Max: fixes x :: "'a::euclidean_space" shows "infnorm x = Max ((\i. \x \ i\) ` Basis)" by (simp add: infnorm_def infnorm_set_image cSup_eq_Max) lemma infnorm_set_lemma: fixes x :: "'a::euclidean_space" shows "finite {\x \ i\ |i. i \ Basis}" and "{\x \ i\ |i. i \ Basis} \ {}" unfolding infnorm_set_image by auto lemma infnorm_pos_le: fixes x :: "'a::euclidean_space" shows "0 \ infnorm x" by (simp add: infnorm_Max Max_ge_iff ex_in_conv) lemma infnorm_triangle: fixes x :: "'a::euclidean_space" shows "infnorm (x + y) \ infnorm x + infnorm y" proof - have *: "\a b c d :: real. \a\ \ c \ \b\ \ d \ \a + b\ \ c + d" by simp show ?thesis by (auto simp: infnorm_Max inner_add_left intro!: *) qed lemma infnorm_eq_0: fixes x :: "'a::euclidean_space" shows "infnorm x = 0 \ x = 0" proof - have "infnorm x \ 0 \ x = 0" unfolding infnorm_Max by (simp add: euclidean_all_zero_iff) then show ?thesis using infnorm_pos_le[of x] by simp qed lemma infnorm_0: "infnorm 0 = 0" by (simp add: infnorm_eq_0) lemma infnorm_neg: "infnorm (- x) = infnorm x" unfolding infnorm_def by simp lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)" by (metis infnorm_neg minus_diff_eq) lemma absdiff_infnorm: "\infnorm x - infnorm y\ \ infnorm (x - y)" proof - have *: "\(nx::real) n ny. nx \ n + ny \ ny \ n + nx \ \nx - ny\ \ n" by arith show ?thesis proof (rule *) from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"] show "infnorm x \ infnorm (x - y) + infnorm y" "infnorm y \ infnorm (x - y) + infnorm x" by (simp_all add: field_simps infnorm_neg) qed qed lemma real_abs_infnorm: "\infnorm x\ = infnorm x" using infnorm_pos_le[of x] by arith lemma Basis_le_infnorm: fixes x :: "'a::euclidean_space" shows "b \ Basis \ \x \ b\ \ infnorm x" by (simp add: infnorm_Max) lemma infnorm_mul: "infnorm (a *\<^sub>R x) = \a\ * infnorm x" unfolding infnorm_Max proof (safe intro!: Max_eqI) let ?B = "(\i. \x \ i\) ` Basis" { fix b :: 'a assume "b \ Basis" then show "\a *\<^sub>R x \ b\ \ \a\ * Max ?B" by (simp add: abs_mult mult_left_mono) next from Max_in[of ?B] obtain b where "b \ Basis" "Max ?B = \x \ b\" by (auto simp del: Max_in) then show "\a\ * Max ((\i. \x \ i\) ` Basis) \ (\i. \a *\<^sub>R x \ i\) ` Basis" by (intro image_eqI[where x=b]) (auto simp: abs_mult) } qed simp lemma infnorm_mul_lemma: "infnorm (a *\<^sub>R x) \ \a\ * infnorm x" unfolding infnorm_mul .. lemma infnorm_pos_lt: "infnorm x > 0 \ x \ 0" using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith text \Prove that it differs only up to a bound from Euclidean norm.\ lemma infnorm_le_norm: "infnorm x \ norm x" by (simp add: Basis_le_norm infnorm_Max) lemma norm_le_infnorm: fixes x :: "'a::euclidean_space" shows "norm x \ sqrt DIM('a) * infnorm x" unfolding norm_eq_sqrt_inner id_def proof (rule real_le_lsqrt[OF inner_ge_zero]) show "sqrt DIM('a) * infnorm x \ 0" by (simp add: zero_le_mult_iff infnorm_pos_le) have "x \ x \ (\b\Basis. x \ b * (x \ b))" by (metis euclidean_inner order_refl) also have "... \ DIM('a) * \infnorm x\\<^sup>2" by (rule sum_bounded_above) (metis Basis_le_infnorm abs_le_square_iff power2_eq_square real_abs_infnorm) also have "... \ (sqrt DIM('a) * infnorm x)\<^sup>2" by (simp add: power_mult_distrib) finally show "x \ x \ (sqrt DIM('a) * infnorm x)\<^sup>2" . qed lemma tendsto_infnorm [tendsto_intros]: assumes "(f \ a) F" shows "((\x. infnorm (f x)) \ infnorm a) F" proof (rule tendsto_compose [OF LIM_I assms]) fix r :: real assume "r > 0" then show "\s>0. \x. x \ a \ norm (x - a) < s \ norm (infnorm x - infnorm a) < r" by (metis real_norm_def le_less_trans absdiff_infnorm infnorm_le_norm) qed text \Equality in Cauchy-Schwarz and triangle inequalities.\ lemma norm_cauchy_schwarz_eq: "x \ y = norm x * norm y \ norm x *\<^sub>R y = norm y *\<^sub>R x" (is "?lhs \ ?rhs") proof (cases "x=0") case True then show ?thesis by auto next case False from inner_eq_zero_iff[of "norm y *\<^sub>R x - norm x *\<^sub>R y"] have "?rhs \ (norm y * (norm y * norm x * norm x - norm x * (x \ y)) - norm x * (norm y * (y \ x) - norm x * norm y * norm y) = 0)" using False unfolding inner_simps by (auto simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps) also have "\ \ (2 * norm x * norm y * (norm x * norm y - x \ y) = 0)" using False by (simp add: field_simps inner_commute) also have "\ \ ?lhs" using False by auto finally show ?thesis by metis qed lemma norm_cauchy_schwarz_abs_eq: "\x \ y\ = norm x * norm y \ norm x *\<^sub>R y = norm y *\<^sub>R x \ norm x *\<^sub>R y = - norm y *\<^sub>R x" (is "?lhs \ ?rhs") proof - have th: "\(x::real) a. a \ 0 \ \x\ = a \ x = a \ x = - a" by arith have "?rhs \ norm x *\<^sub>R y = norm y *\<^sub>R x \ norm (- x) *\<^sub>R y = norm y *\<^sub>R (- x)" by simp also have "\ \ (x \ y = norm x * norm y \ (- x) \ y = norm x * norm y)" unfolding norm_cauchy_schwarz_eq[symmetric] unfolding norm_minus_cancel norm_scaleR .. also have "\ \ ?lhs" unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps by auto finally show ?thesis .. qed lemma norm_triangle_eq: fixes x y :: "'a::real_inner" shows "norm (x + y) = norm x + norm y \ norm x *\<^sub>R y = norm y *\<^sub>R x" proof (cases "x = 0 \ y = 0") case True then show ?thesis by force next case False then have n: "norm x > 0" "norm y > 0" by auto have "norm (x + y) = norm x + norm y \ (norm (x + y))\<^sup>2 = (norm x + norm y)\<^sup>2" by simp also have "\ \ norm x *\<^sub>R y = norm y *\<^sub>R x" unfolding norm_cauchy_schwarz_eq[symmetric] unfolding power2_norm_eq_inner inner_simps by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps) finally show ?thesis . qed subsection \Collinearity\ definition\<^marker>\tag important\ collinear :: "'a::real_vector set \ bool" where "collinear S \ (\u. \x \ S. \ y \ S. \c. x - y = c *\<^sub>R u)" lemma collinear_alt: "collinear S \ (\u v. \x \ S. \c. x = u + c *\<^sub>R v)" (is "?lhs = ?rhs") proof assume ?lhs then show ?rhs unfolding collinear_def by (metis Groups.add_ac(2) diff_add_cancel) next assume ?rhs then obtain u v where *: "\x. x \ S \ \c. x = u + c *\<^sub>R v" by (auto simp: ) have "\c. x - y = c *\<^sub>R v" if "x \ S" "y \ S" for x y by (metis *[OF \x \ S\] *[OF \y \ S\] scaleR_left.diff add_diff_cancel_left) then show ?lhs using collinear_def by blast qed lemma collinear: fixes S :: "'a::{perfect_space,real_vector} set" shows "collinear S \ (\u. u \ 0 \ (\x \ S. \ y \ S. \c. x - y = c *\<^sub>R u))" proof - have "\v. v \ 0 \ (\x\S. \y\S. \c. x - y = c *\<^sub>R v)" if "\x\S. \y\S. \c. x - y = c *\<^sub>R u" "u=0" for u proof - have "\x\S. \y\S. x = y" using that by auto moreover obtain v::'a where "v \ 0" using UNIV_not_singleton [of 0] by auto ultimately have "\x\S. \y\S. \c. x - y = c *\<^sub>R v" by auto then show ?thesis using \v \ 0\ by blast qed then show ?thesis apply (clarsimp simp: collinear_def) by (metis scaleR_zero_right vector_fraction_eq_iff) qed lemma collinear_subset: "\collinear T; S \ T\ \ collinear S" by (meson collinear_def subsetCE) lemma collinear_empty [iff]: "collinear {}" by (simp add: collinear_def) lemma collinear_sing [iff]: "collinear {x}" by (simp add: collinear_def) lemma collinear_2 [iff]: "collinear {x, y}" apply (simp add: collinear_def) apply (rule exI[where x="x - y"]) by (metis minus_diff_eq scaleR_left.minus scaleR_one) lemma collinear_lemma: "collinear {0, x, y} \ x = 0 \ y = 0 \ (\c. y = c *\<^sub>R x)" (is "?lhs \ ?rhs") proof (cases "x = 0 \ y = 0") case True then show ?thesis by (auto simp: insert_commute) next case False show ?thesis proof assume h: "?lhs" then obtain u where u: "\ x\ {0,x,y}. \y\ {0,x,y}. \c. x - y = c *\<^sub>R u" unfolding collinear_def by blast from u[rule_format, of x 0] u[rule_format, of y 0] obtain cx and cy where cx: "x = cx *\<^sub>R u" and cy: "y = cy *\<^sub>R u" by auto from cx cy False have cx0: "cx \ 0" and cy0: "cy \ 0" by auto let ?d = "cy / cx" from cx cy cx0 have "y = ?d *\<^sub>R x" by simp then show ?rhs using False by blast next assume h: "?rhs" then obtain c where c: "y = c *\<^sub>R x" using False by blast show ?lhs unfolding collinear_def c apply (rule exI[where x=x]) apply auto apply (rule exI[where x="- 1"], simp) apply (rule exI[where x= "-c"], simp) apply (rule exI[where x=1], simp) apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib) apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib) done qed qed lemma norm_cauchy_schwarz_equal: "\x \ y\ = norm x * norm y \ collinear {0, x, y}" proof (cases "x=0") case True then show ?thesis by (auto simp: insert_commute) next case False then have nnz: "norm x \ 0" by auto show ?thesis proof assume "\x \ y\ = norm x * norm y" then show "collinear {0, x, y}" unfolding norm_cauchy_schwarz_abs_eq collinear_lemma by (meson eq_vector_fraction_iff nnz) next assume "collinear {0, x, y}" with False show "\x \ y\ = norm x * norm y" unfolding norm_cauchy_schwarz_abs_eq collinear_lemma by (auto simp: abs_if) qed qed subsection\Properties of special hyperplanes\ lemma subspace_hyperplane: "subspace {x. a \ x = 0}" by (simp add: subspace_def inner_right_distrib) lemma subspace_hyperplane2: "subspace {x. x \ a = 0}" by (simp add: inner_commute inner_right_distrib subspace_def) lemma special_hyperplane_span: fixes S :: "'n::euclidean_space set" assumes "k \ Basis" shows "{x. k \ x = 0} = span (Basis - {k})" proof - have *: "x \ span (Basis - {k})" if "k \ x = 0" for x proof - have "x = (\b\Basis. (x \ b) *\<^sub>R b)" by (simp add: euclidean_representation) also have "... = (\b \ Basis - {k}. (x \ b) *\<^sub>R b)" by (auto simp: sum.remove [of _ k] inner_commute assms that) finally have "x = (\b\Basis - {k}. (x \ b) *\<^sub>R b)" . then show ?thesis by (simp add: span_finite) qed show ?thesis apply (rule span_subspace [symmetric]) using assms apply (auto simp: inner_not_same_Basis intro: * subspace_hyperplane) done qed lemma dim_special_hyperplane: fixes k :: "'n::euclidean_space" shows "k \ Basis \ dim {x. k \ x = 0} = DIM('n) - 1" apply (simp add: special_hyperplane_span) apply (rule dim_unique [OF subset_refl]) apply (auto simp: independent_substdbasis) apply (metis member_remove remove_def span_base) done proposition dim_hyperplane: fixes a :: "'a::euclidean_space" assumes "a \ 0" shows "dim {x. a \ x = 0} = DIM('a) - 1" proof - have span0: "span {x. a \ x = 0} = {x. a \ x = 0}" by (rule span_unique) (auto simp: subspace_hyperplane) then obtain B where "independent B" and Bsub: "B \ {x. a \ x = 0}" and subspB: "{x. a \ x = 0} \ span B" and card0: "(card B = dim {x. a \ x = 0})" and ortho: "pairwise orthogonal B" using orthogonal_basis_exists by metis with assms have "a \ span B" by (metis (mono_tags, lifting) span_eq inner_eq_zero_iff mem_Collect_eq span0) then have ind: "independent (insert a B)" by (simp add: \independent B\ independent_insert) have "finite B" using \independent B\ independent_bound by blast have "UNIV \ span (insert a B)" proof fix y::'a obtain r z where z: "y = r *\<^sub>R a + z" "a \ z = 0" apply (rule_tac r="(a \ y) / (a \ a)" and z = "y - ((a \ y) / (a \ a)) *\<^sub>R a" in that) using assms by (auto simp: algebra_simps) show "y \ span (insert a B)" by (metis (mono_tags, lifting) z Bsub span_eq_iff add_diff_cancel_left' mem_Collect_eq span0 span_breakdown_eq span_subspace subspB) qed then have dima: "DIM('a) = dim(insert a B)" by (metis independent_Basis span_Basis dim_eq_card top.extremum_uniqueI) then show ?thesis by (metis (mono_tags, lifting) Bsub Diff_insert_absorb \a \ span B\ ind card0 card_Diff_singleton dim_span indep_card_eq_dim_span insertI1 subsetCE subspB) qed lemma lowdim_eq_hyperplane: fixes S :: "'a::euclidean_space set" assumes "dim S = DIM('a) - 1" obtains a where "a \ 0" and "span S = {x. a \ x = 0}" proof - have dimS: "dim S < DIM('a)" by (simp add: assms) then obtain b where b: "b \ 0" "span S \ {a. b \ a = 0}" using lowdim_subset_hyperplane [of S] by fastforce show ?thesis apply (rule that[OF b(1)]) apply (rule subspace_dim_equal) - by (auto simp: assms b dim_hyperplane dim_span subspace_hyperplane - subspace_span) + by (auto simp: assms b dim_hyperplane subspace_hyperplane) qed lemma dim_eq_hyperplane: fixes S :: "'n::euclidean_space set" shows "dim S = DIM('n) - 1 \ (\a. a \ 0 \ span S = {x. a \ x = 0})" by (metis One_nat_def dim_hyperplane dim_span lowdim_eq_hyperplane) -subsection\ Orthogonal bases, Gram-Schmidt process, and related theorems\ +subsection\ Orthogonal bases and Gram-Schmidt process\ lemma pairwise_orthogonal_independent: assumes "pairwise orthogonal S" and "0 \ S" shows "independent S" proof - have 0: "\x y. \x \ y; x \ S; y \ S\ \ x \ y = 0" using assms by (simp add: pairwise_def orthogonal_def) have "False" if "a \ S" and a: "a \ span (S - {a})" for a proof - obtain T U where "T \ S - {a}" "a = (\v\T. U v *\<^sub>R v)" using a by (force simp: span_explicit) then have "a \ a = a \ (\v\T. U v *\<^sub>R v)" by simp also have "... = 0" apply (simp add: inner_sum_right) apply (rule comm_monoid_add_class.sum.neutral) by (metis "0" DiffE \T \ S - {a}\ mult_not_zero singletonI subsetCE \a \ S\) finally show ?thesis using \0 \ S\ \a \ S\ by auto qed then show ?thesis by (force simp: dependent_def) qed lemma pairwise_orthogonal_imp_finite: fixes S :: "'a::euclidean_space set" assumes "pairwise orthogonal S" shows "finite S" proof - have "independent (S - {0})" apply (rule pairwise_orthogonal_independent) apply (metis Diff_iff assms pairwise_def) by blast then show ?thesis by (meson independent_imp_finite infinite_remove) qed lemma subspace_orthogonal_to_vector: "subspace {y. orthogonal x y}" by (simp add: subspace_def orthogonal_clauses) lemma subspace_orthogonal_to_vectors: "subspace {y. \x \ S. orthogonal x y}" by (simp add: subspace_def orthogonal_clauses) lemma orthogonal_to_span: assumes a: "a \ span S" and x: "\y. y \ S \ orthogonal x y" shows "orthogonal x a" by (metis a orthogonal_clauses(1,2,4) span_induct_alt x) proposition Gram_Schmidt_step: fixes S :: "'a::euclidean_space set" assumes S: "pairwise orthogonal S" and x: "x \ span S" shows "orthogonal x (a - (\b\S. (b \ a / (b \ b)) *\<^sub>R b))" proof - have "finite S" by (simp add: S pairwise_orthogonal_imp_finite) have "orthogonal (a - (\b\S. (b \ a / (b \ b)) *\<^sub>R b)) x" if "x \ S" for x proof - have "a \ x = (\y\S. if y = x then y \ a else 0)" - by (simp add: \finite S\ inner_commute sum.delta that) + by (simp add: \finite S\ inner_commute that) also have "... = (\b\S. b \ a * (b \ x) / (b \ b))" apply (rule sum.cong [OF refl], simp) by (meson S orthogonal_def pairwise_def that) finally show ?thesis by (simp add: orthogonal_def algebra_simps inner_sum_left) qed then show ?thesis using orthogonal_to_span orthogonal_commute x by blast qed lemma orthogonal_extension_aux: fixes S :: "'a::euclidean_space set" assumes "finite T" "finite S" "pairwise orthogonal S" shows "\U. pairwise orthogonal (S \ U) \ span (S \ U) = span (S \ T)" using assms proof (induction arbitrary: S) case empty then show ?case by simp (metis sup_bot_right) next case (insert a T) have 0: "\x y. \x \ y; x \ S; y \ S\ \ x \ y = 0" using insert by (simp add: pairwise_def orthogonal_def) define a' where "a' = a - (\b\S. (b \ a / (b \ b)) *\<^sub>R b)" obtain U where orthU: "pairwise orthogonal (S \ insert a' U)" and spanU: "span (insert a' S \ U) = span (insert a' S \ T)" by (rule exE [OF insert.IH [of "insert a' S"]]) (auto simp: Gram_Schmidt_step a'_def insert.prems orthogonal_commute pairwise_orthogonal_insert span_clauses) have orthS: "\x. x \ S \ a' \ x = 0" apply (simp add: a'_def) using Gram_Schmidt_step [OF \pairwise orthogonal S\] apply (force simp: orthogonal_def inner_commute span_superset [THEN subsetD]) done have "span (S \ insert a' U) = span (insert a' (S \ T))" using spanU by simp also have "... = span (insert a (S \ T))" apply (rule eq_span_insert_eq) apply (simp add: a'_def span_neg span_sum span_base span_mul) done also have "... = span (S \ insert a T)" by simp finally show ?case by (rule_tac x="insert a' U" in exI) (use orthU in auto) qed proposition orthogonal_extension: fixes S :: "'a::euclidean_space set" assumes S: "pairwise orthogonal S" obtains U where "pairwise orthogonal (S \ U)" "span (S \ U) = span (S \ T)" proof - obtain B where "finite B" "span B = span T" using basis_subspace_exists [of "span T"] subspace_span by metis with orthogonal_extension_aux [of B S] obtain U where "pairwise orthogonal (S \ U)" "span (S \ U) = span (S \ B)" using assms pairwise_orthogonal_imp_finite by auto with \span B = span T\ show ?thesis by (rule_tac U=U in that) (auto simp: span_Un) qed corollary\<^marker>\tag unimportant\ orthogonal_extension_strong: fixes S :: "'a::euclidean_space set" assumes S: "pairwise orthogonal S" obtains U where "U \ (insert 0 S) = {}" "pairwise orthogonal (S \ U)" "span (S \ U) = span (S \ T)" proof - obtain U where "pairwise orthogonal (S \ U)" "span (S \ U) = span (S \ T)" using orthogonal_extension assms by blast then show ?thesis apply (rule_tac U = "U - (insert 0 S)" in that) apply blast apply (force simp: pairwise_def) apply (metis Un_Diff_cancel Un_insert_left span_redundant span_zero) done qed subsection\Decomposing a vector into parts in orthogonal subspaces\ text\existence of orthonormal basis for a subspace.\ lemma orthogonal_spanningset_subspace: fixes S :: "'a :: euclidean_space set" assumes "subspace S" obtains B where "B \ S" "pairwise orthogonal B" "span B = S" proof - obtain B where "B \ S" "independent B" "S \ span B" "card B = dim S" using basis_exists by blast with orthogonal_extension [of "{}" B] show ?thesis by (metis Un_empty_left assms pairwise_empty span_superset span_subspace that) qed lemma orthogonal_basis_subspace: fixes S :: "'a :: euclidean_space set" assumes "subspace S" obtains B where "0 \ B" "B \ S" "pairwise orthogonal B" "independent B" "card B = dim S" "span B = S" proof - obtain B where "B \ S" "pairwise orthogonal B" "span B = S" using assms orthogonal_spanningset_subspace by blast then show ?thesis apply (rule_tac B = "B - {0}" in that) apply (auto simp: indep_card_eq_dim_span pairwise_subset pairwise_orthogonal_independent elim: pairwise_subset) done qed proposition orthonormal_basis_subspace: fixes S :: "'a :: euclidean_space set" assumes "subspace S" obtains B where "B \ S" "pairwise orthogonal B" and "\x. x \ B \ norm x = 1" and "independent B" "card B = dim S" "span B = S" proof - obtain B where "0 \ B" "B \ S" and orth: "pairwise orthogonal B" and "independent B" "card B = dim S" "span B = S" by (blast intro: orthogonal_basis_subspace [OF assms]) have 1: "(\x. x /\<^sub>R norm x) ` B \ S" using \span B = S\ span_superset span_mul by fastforce have 2: "pairwise orthogonal ((\x. x /\<^sub>R norm x) ` B)" using orth by (force simp: pairwise_def orthogonal_clauses) have 3: "\x. x \ (\x. x /\<^sub>R norm x) ` B \ norm x = 1" by (metis (no_types, lifting) \0 \ B\ image_iff norm_sgn sgn_div_norm) have 4: "independent ((\x. x /\<^sub>R norm x) ` B)" by (metis "2" "3" norm_zero pairwise_orthogonal_independent zero_neq_one) have "inj_on (\x. x /\<^sub>R norm x) B" proof fix x y assume "x \ B" "y \ B" "x /\<^sub>R norm x = y /\<^sub>R norm y" moreover have "\i. i \ B \ norm (i /\<^sub>R norm i) = 1" using 3 by blast ultimately show "x = y" by (metis norm_eq_1 orth orthogonal_clauses(7) orthogonal_commute orthogonal_def pairwise_def zero_neq_one) qed then have 5: "card ((\x. x /\<^sub>R norm x) ` B) = dim S" by (metis \card B = dim S\ card_image) have 6: "span ((\x. x /\<^sub>R norm x) ` B) = S" by (metis "1" "4" "5" assms card_eq_dim independent_imp_finite span_subspace) show ?thesis by (rule that [OF 1 2 3 4 5 6]) qed proposition\<^marker>\tag unimportant\ orthogonal_to_subspace_exists_gen: fixes S :: "'a :: euclidean_space set" assumes "span S \ span T" obtains x where "x \ 0" "x \ span T" "\y. y \ span S \ orthogonal x y" proof - obtain B where "B \ span S" and orthB: "pairwise orthogonal B" and "\x. x \ B \ norm x = 1" and "independent B" "card B = dim S" "span B = span S" - by (rule orthonormal_basis_subspace [of "span S", OF subspace_span]) - (auto simp: dim_span) + by (rule orthonormal_basis_subspace [of "span S", OF subspace_span]) (auto) with assms obtain u where spanBT: "span B \ span T" and "u \ span B" "u \ span T" by auto obtain C where orthBC: "pairwise orthogonal (B \ C)" and spanBC: "span (B \ C) = span (B \ {u})" by (blast intro: orthogonal_extension [OF orthB]) show thesis proof (cases "C \ insert 0 B") case True then have "C \ span B" using span_eq by (metis span_insert_0 subset_trans) moreover have "u \ span (B \ C)" using \span (B \ C) = span (B \ {u})\ span_superset by force ultimately show ?thesis using True \u \ span B\ by (metis Un_insert_left span_insert_0 sup.orderE) next case False then obtain x where "x \ C" "x \ 0" "x \ B" by blast then have "x \ span T" by (metis (no_types, lifting) Un_insert_right Un_upper2 \u \ span T\ spanBT spanBC \u \ span T\ insert_subset span_superset span_mono span_span subsetCE subset_trans sup_bot.comm_neutral) moreover have "orthogonal x y" if "y \ span B" for y using that proof (rule span_induct) show "subspace {a. orthogonal x a}" by (simp add: subspace_orthogonal_to_vector) show "\b. b \ B \ orthogonal x b" by (metis Un_iff \x \ C\ \x \ B\ orthBC pairwise_def) qed ultimately show ?thesis using \x \ 0\ that \span B = span S\ by auto qed qed corollary\<^marker>\tag unimportant\ orthogonal_to_subspace_exists: fixes S :: "'a :: euclidean_space set" assumes "dim S < DIM('a)" obtains x where "x \ 0" "\y. y \ span S \ orthogonal x y" proof - -have "span S \ UNIV" + have "span S \ UNIV" by (metis (mono_tags) UNIV_I assms inner_eq_zero_iff less_le lowdim_subset_hyperplane mem_Collect_eq top.extremum_strict top.not_eq_extremum) with orthogonal_to_subspace_exists_gen [of S UNIV] that show ?thesis - by (auto simp: span_UNIV) + by (auto) qed corollary\<^marker>\tag unimportant\ orthogonal_to_vector_exists: fixes x :: "'a :: euclidean_space" assumes "2 \ DIM('a)" obtains y where "y \ 0" "orthogonal x y" proof - have "dim {x} < DIM('a)" using assms by auto then show thesis by (rule orthogonal_to_subspace_exists) (simp add: orthogonal_commute span_base that) qed proposition\<^marker>\tag unimportant\ orthogonal_subspace_decomp_exists: fixes S :: "'a :: euclidean_space set" obtains y z where "y \ span S" and "\w. w \ span S \ orthogonal z w" and "x = y + z" proof - obtain T where "0 \ T" "T \ span S" "pairwise orthogonal T" "independent T" "card T = dim (span S)" "span T = span S" using orthogonal_basis_subspace subspace_span by blast let ?a = "\b\T. (b \ x / (b \ b)) *\<^sub>R b" have orth: "orthogonal (x - ?a) w" if "w \ span S" for w by (simp add: Gram_Schmidt_step \pairwise orthogonal T\ \span T = span S\ orthogonal_commute that) show ?thesis apply (rule_tac y = "?a" and z = "x - ?a" in that) apply (meson \T \ span S\ span_scale span_sum subsetCE) apply (fact orth, simp) done qed lemma orthogonal_subspace_decomp_unique: fixes S :: "'a :: euclidean_space set" assumes "x + y = x' + y'" and ST: "x \ span S" "x' \ span S" "y \ span T" "y' \ span T" and orth: "\a b. \a \ S; b \ T\ \ orthogonal a b" shows "x = x' \ y = y'" proof - have "x + y - y' = x'" by (simp add: assms) moreover have "\a b. \a \ span S; b \ span T\ \ orthogonal a b" by (meson orth orthogonal_commute orthogonal_to_span) ultimately have "0 = x' - x" by (metis (full_types) add_diff_cancel_left' ST diff_right_commute orthogonal_clauses(10) orthogonal_clauses(5) orthogonal_self) with assms show ?thesis by auto qed lemma vector_in_orthogonal_spanningset: fixes a :: "'a::euclidean_space" obtains S where "a \ S" "pairwise orthogonal S" "span S = UNIV" by (metis UNIV_I Un_iff empty_iff insert_subset orthogonal_extension pairwise_def pairwise_orthogonal_insert span_UNIV subsetI subset_antisym) lemma vector_in_orthogonal_basis: fixes a :: "'a::euclidean_space" assumes "a \ 0" obtains S where "a \ S" "0 \ S" "pairwise orthogonal S" "independent S" "finite S" "span S = UNIV" "card S = DIM('a)" proof - obtain S where S: "a \ S" "pairwise orthogonal S" "span S = UNIV" using vector_in_orthogonal_spanningset . show thesis proof show "pairwise orthogonal (S - {0})" using pairwise_mono S(2) by blast show "independent (S - {0})" by (simp add: \pairwise orthogonal (S - {0})\ pairwise_orthogonal_independent) show "finite (S - {0})" using \independent (S - {0})\ independent_imp_finite by blast show "card (S - {0}) = DIM('a)" using span_delete_0 [of S] S - by (simp add: \independent (S - {0})\ indep_card_eq_dim_span dim_UNIV) + by (simp add: \independent (S - {0})\ indep_card_eq_dim_span) qed (use S \a \ 0\ in auto) qed lemma vector_in_orthonormal_basis: fixes a :: "'a::euclidean_space" assumes "norm a = 1" obtains S where "a \ S" "pairwise orthogonal S" "\x. x \ S \ norm x = 1" "independent S" "card S = DIM('a)" "span S = UNIV" proof - have "a \ 0" using assms by auto then obtain S where "a \ S" "0 \ S" "finite S" and S: "pairwise orthogonal S" "independent S" "span S = UNIV" "card S = DIM('a)" by (metis vector_in_orthogonal_basis) let ?S = "(\x. x /\<^sub>R norm x) ` S" show thesis proof show "a \ ?S" using \a \ S\ assms image_iff by fastforce next show "pairwise orthogonal ?S" using \pairwise orthogonal S\ by (auto simp: pairwise_def orthogonal_def) show "\x. x \ (\x. x /\<^sub>R norm x) ` S \ norm x = 1" using \0 \ S\ by (auto simp: field_split_simps) then show "independent ?S" by (metis \pairwise orthogonal ((\x. x /\<^sub>R norm x) ` S)\ norm_zero pairwise_orthogonal_independent zero_neq_one) have "inj_on (\x. x /\<^sub>R norm x) S" unfolding inj_on_def by (metis (full_types) S(1) \0 \ S\ inverse_nonzero_iff_nonzero norm_eq_zero orthogonal_scaleR orthogonal_self pairwise_def) then show "card ?S = DIM('a)" by (simp add: card_image S) show "span ?S = UNIV" by (metis (no_types) \0 \ S\ \finite S\ \span S = UNIV\ field_class.field_inverse_zero inverse_inverse_eq less_irrefl span_image_scale zero_less_norm_iff) qed qed proposition dim_orthogonal_sum: fixes A :: "'a::euclidean_space set" assumes "\x y. \x \ A; y \ B\ \ x \ y = 0" shows "dim(A \ B) = dim A + dim B" proof - have 1: "\x y. \x \ span A; y \ B\ \ x \ y = 0" by (erule span_induct [OF _ subspace_hyperplane2]; simp add: assms) have "\x y. \x \ span A; y \ span B\ \ x \ y = 0" using 1 by (simp add: span_induct [OF _ subspace_hyperplane]) then have 0: "\x y. \x \ span A; y \ span B\ \ x \ y = 0" by simp have "dim(A \ B) = dim (span (A \ B))" - by (simp add: dim_span) + by (simp) also have "span (A \ B) = ((\(a, b). a + b) ` (span A \ span B))" by (auto simp add: span_Un image_def) also have "dim \ = dim {x + y |x y. x \ span A \ y \ span B}" by (auto intro!: arg_cong [where f=dim]) also have "... = dim {x + y |x y. x \ span A \ y \ span B} + dim(span A \ span B)" by (auto simp: dest: 0) also have "... = dim (span A) + dim (span B)" - by (rule dim_sums_Int) (auto simp: subspace_span) + by (rule dim_sums_Int) (auto) also have "... = dim A + dim B" - by (simp add: dim_span) + by (simp) finally show ?thesis . qed lemma dim_subspace_orthogonal_to_vectors: fixes A :: "'a::euclidean_space set" assumes "subspace A" "subspace B" "A \ B" shows "dim {y \ B. \x \ A. orthogonal x y} + dim A = dim B" proof - have "dim (span ({y \ B. \x\A. orthogonal x y} \ A)) = dim (span B)" proof (rule arg_cong [where f=dim, OF subset_antisym]) show "span ({y \ B. \x\A. orthogonal x y} \ A) \ span B" by (simp add: \A \ B\ Collect_restrict span_mono) next have *: "x \ span ({y \ B. \x\A. orthogonal x y} \ A)" if "x \ B" for x proof - obtain y z where "x = y + z" "y \ span A" and orth: "\w. w \ span A \ orthogonal z w" using orthogonal_subspace_decomp_exists [of A x] that by auto have "y \ span B" using \y \ span A\ assms(3) span_mono by blast then have "z \ {a \ B. \x. x \ A \ orthogonal x a}" apply simp using \x = y + z\ assms(1) assms(2) orth orthogonal_commute span_add_eq span_eq_iff that by blast then have z: "z \ span {y \ B. \x\A. orthogonal x y}" by (meson span_superset subset_iff) then show ?thesis apply (auto simp: span_Un image_def \x = y + z\ \y \ span A\) using \y \ span A\ add.commute by blast qed show "span B \ span ({y \ B. \x\A. orthogonal x y} \ A)" - by (rule span_minimal) - (auto intro: * span_minimal simp: subspace_span) + by (rule span_minimal) (auto intro: * span_minimal) qed then show ?thesis by (metis (no_types, lifting) dim_orthogonal_sum dim_span mem_Collect_eq orthogonal_commute orthogonal_def) qed subsection\Linear functions are (uniformly) continuous on any set\ subsection\<^marker>\tag unimportant\ \Topological properties of linear functions\ lemma linear_lim_0: assumes "bounded_linear f" shows "(f \ 0) (at (0))" proof - interpret f: bounded_linear f by fact have "(f \ f 0) (at 0)" using tendsto_ident_at by (rule f.tendsto) then show ?thesis unfolding f.zero . qed lemma linear_continuous_at: assumes "bounded_linear f" shows "continuous (at a) f" unfolding continuous_at using assms apply (rule bounded_linear.tendsto) apply (rule tendsto_ident_at) done lemma linear_continuous_within: "bounded_linear f \ continuous (at x within s) f" using continuous_at_imp_continuous_at_within linear_continuous_at by blast lemma linear_continuous_on: "bounded_linear f \ continuous_on s f" using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto lemma Lim_linear: fixes f :: "'a::euclidean_space \ 'b::euclidean_space" and h :: "'b \ 'c::real_normed_vector" assumes "(f \ l) F" "linear h" shows "((\x. h(f x)) \ h l) F" proof - obtain B where B: "B > 0" "\x. norm (h x) \ B * norm x" using linear_bounded_pos [OF \linear h\] by blast show ?thesis unfolding tendsto_iff proof (intro allI impI) show "\\<^sub>F x in F. dist (h (f x)) (h l) < e" if "e > 0" for e proof - have "\\<^sub>F x in F. dist (f x) l < e/B" by (simp add: \0 < B\ assms(1) tendstoD that) then show ?thesis unfolding dist_norm proof (rule eventually_mono) show "norm (h (f x) - h l) < e" if "norm (f x - l) < e / B" for x using that B apply (simp add: field_split_simps) - by (metis \linear h\ le_less_trans linear_diff mult.commute) + by (metis \linear h\ le_less_trans linear_diff) qed qed qed qed lemma linear_continuous_compose: fixes f :: "'a::euclidean_space \ 'b::euclidean_space" and g :: "'b \ 'c::real_normed_vector" assumes "continuous F f" "linear g" shows "continuous F (\x. g(f x))" using assms unfolding continuous_def by (rule Lim_linear) lemma linear_continuous_on_compose: fixes f :: "'a::euclidean_space \ 'b::euclidean_space" and g :: "'b \ 'c::real_normed_vector" assumes "continuous_on S f" "linear g" shows "continuous_on S (\x. g(f x))" using assms by (simp add: continuous_on_eq_continuous_within linear_continuous_compose) text\Also bilinear functions, in composition form\ lemma bilinear_continuous_compose: fixes h :: "'a::euclidean_space \ 'b::euclidean_space \ 'c::real_normed_vector" assumes "continuous F f" "continuous F g" "bilinear h" shows "continuous F (\x. h (f x) (g x))" using assms bilinear_conv_bounded_bilinear bounded_bilinear.continuous by blast lemma bilinear_continuous_on_compose: fixes h :: "'a::euclidean_space \ 'b::euclidean_space \ 'c::real_normed_vector" and f :: "'d::t2_space \ 'a" assumes "continuous_on S f" "continuous_on S g" "bilinear h" shows "continuous_on S (\x. h (f x) (g x))" using assms by (simp add: continuous_on_eq_continuous_within bilinear_continuous_compose) end diff --git a/src/HOL/Real.thy b/src/HOL/Real.thy --- a/src/HOL/Real.thy +++ b/src/HOL/Real.thy @@ -1,1656 +1,1746 @@ (* Title: HOL/Real.thy Author: Jacques D. Fleuriot, University of Edinburgh, 1998 Author: Larry Paulson, University of Cambridge Author: Jeremy Avigad, Carnegie Mellon University Author: Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4 Construction of Cauchy Reals by Brian Huffman, 2010 *) section \Development of the Reals using Cauchy Sequences\ theory Real imports Rat begin text \ This theory contains a formalization of the real numbers as equivalence classes of Cauchy sequences of rationals. See \<^file>\~~/src/HOL/ex/Dedekind_Real.thy\ for an alternative construction using Dedekind cuts. \ subsection \Preliminary lemmas\ text\Useful in convergence arguments\ lemma inverse_of_nat_le: fixes n::nat shows "\n \ m; n\0\ \ 1 / of_nat m \ (1::'a::linordered_field) / of_nat n" by (simp add: frac_le) lemma add_diff_add: "(a + c) - (b + d) = (a - b) + (c - d)" for a b c d :: "'a::ab_group_add" by simp lemma minus_diff_minus: "- a - - b = - (a - b)" for a b :: "'a::ab_group_add" by simp lemma mult_diff_mult: "(x * y - a * b) = x * (y - b) + (x - a) * b" for x y a b :: "'a::ring" by (simp add: algebra_simps) lemma inverse_diff_inverse: fixes a b :: "'a::division_ring" assumes "a \ 0" and "b \ 0" shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)" using assms by (simp add: algebra_simps) lemma obtain_pos_sum: fixes r :: rat assumes r: "0 < r" obtains s t where "0 < s" and "0 < t" and "r = s + t" proof from r show "0 < r/2" by simp from r show "0 < r/2" by simp show "r = r/2 + r/2" by simp qed subsection \Sequences that converge to zero\ definition vanishes :: "(nat \ rat) \ bool" where "vanishes X \ (\r>0. \k. \n\k. \X n\ < r)" lemma vanishesI: "(\r. 0 < r \ \k. \n\k. \X n\ < r) \ vanishes X" unfolding vanishes_def by simp lemma vanishesD: "vanishes X \ 0 < r \ \k. \n\k. \X n\ < r" unfolding vanishes_def by simp lemma vanishes_const [simp]: "vanishes (\n. c) \ c = 0" proof (cases "c = 0") case True then show ?thesis by (simp add: vanishesI) next case False then show ?thesis unfolding vanishes_def using zero_less_abs_iff by blast qed lemma vanishes_minus: "vanishes X \ vanishes (\n. - X n)" unfolding vanishes_def by simp lemma vanishes_add: assumes X: "vanishes X" and Y: "vanishes Y" shows "vanishes (\n. X n + Y n)" proof (rule vanishesI) fix r :: rat assume "0 < r" then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" by (rule obtain_pos_sum) obtain i where i: "\n\i. \X n\ < s" using vanishesD [OF X s] .. obtain j where j: "\n\j. \Y n\ < t" using vanishesD [OF Y t] .. have "\n\max i j. \X n + Y n\ < r" proof clarsimp fix n assume n: "i \ n" "j \ n" have "\X n + Y n\ \ \X n\ + \Y n\" by (rule abs_triangle_ineq) also have "\ < s + t" by (simp add: add_strict_mono i j n) finally show "\X n + Y n\ < r" by (simp only: r) qed then show "\k. \n\k. \X n + Y n\ < r" .. qed lemma vanishes_diff: assumes "vanishes X" "vanishes Y" shows "vanishes (\n. X n - Y n)" unfolding diff_conv_add_uminus by (intro vanishes_add vanishes_minus assms) lemma vanishes_mult_bounded: assumes X: "\a>0. \n. \X n\ < a" assumes Y: "vanishes (\n. Y n)" shows "vanishes (\n. X n * Y n)" proof (rule vanishesI) fix r :: rat assume r: "0 < r" obtain a where a: "0 < a" "\n. \X n\ < a" using X by blast obtain b where b: "0 < b" "r = a * b" proof show "0 < r / a" using r a by simp show "r = a * (r / a)" using a by simp qed obtain k where k: "\n\k. \Y n\ < b" using vanishesD [OF Y b(1)] .. have "\n\k. \X n * Y n\ < r" by (simp add: b(2) abs_mult mult_strict_mono' a k) then show "\k. \n\k. \X n * Y n\ < r" .. qed subsection \Cauchy sequences\ definition cauchy :: "(nat \ rat) \ bool" where "cauchy X \ (\r>0. \k. \m\k. \n\k. \X m - X n\ < r)" lemma cauchyI: "(\r. 0 < r \ \k. \m\k. \n\k. \X m - X n\ < r) \ cauchy X" unfolding cauchy_def by simp lemma cauchyD: "cauchy X \ 0 < r \ \k. \m\k. \n\k. \X m - X n\ < r" unfolding cauchy_def by simp lemma cauchy_const [simp]: "cauchy (\n. x)" unfolding cauchy_def by simp lemma cauchy_add [simp]: assumes X: "cauchy X" and Y: "cauchy Y" shows "cauchy (\n. X n + Y n)" proof (rule cauchyI) fix r :: rat assume "0 < r" then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" by (rule obtain_pos_sum) obtain i where i: "\m\i. \n\i. \X m - X n\ < s" using cauchyD [OF X s] .. obtain j where j: "\m\j. \n\j. \Y m - Y n\ < t" using cauchyD [OF Y t] .. have "\m\max i j. \n\max i j. \(X m + Y m) - (X n + Y n)\ < r" proof clarsimp fix m n assume *: "i \ m" "j \ m" "i \ n" "j \ n" have "\(X m + Y m) - (X n + Y n)\ \ \X m - X n\ + \Y m - Y n\" unfolding add_diff_add by (rule abs_triangle_ineq) also have "\ < s + t" by (rule add_strict_mono) (simp_all add: i j *) finally show "\(X m + Y m) - (X n + Y n)\ < r" by (simp only: r) qed then show "\k. \m\k. \n\k. \(X m + Y m) - (X n + Y n)\ < r" .. qed lemma cauchy_minus [simp]: assumes X: "cauchy X" shows "cauchy (\n. - X n)" using assms unfolding cauchy_def unfolding minus_diff_minus abs_minus_cancel . lemma cauchy_diff [simp]: assumes "cauchy X" "cauchy Y" shows "cauchy (\n. X n - Y n)" using assms unfolding diff_conv_add_uminus by (simp del: add_uminus_conv_diff) lemma cauchy_imp_bounded: assumes "cauchy X" shows "\b>0. \n. \X n\ < b" proof - obtain k where k: "\m\k. \n\k. \X m - X n\ < 1" using cauchyD [OF assms zero_less_one] .. show "\b>0. \n. \X n\ < b" proof (intro exI conjI allI) have "0 \ \X 0\" by simp also have "\X 0\ \ Max (abs ` X ` {..k})" by simp finally have "0 \ Max (abs ` X ` {..k})" . then show "0 < Max (abs ` X ` {..k}) + 1" by simp next fix n :: nat show "\X n\ < Max (abs ` X ` {..k}) + 1" proof (rule linorder_le_cases) assume "n \ k" then have "\X n\ \ Max (abs ` X ` {..k})" by simp then show "\X n\ < Max (abs ` X ` {..k}) + 1" by simp next assume "k \ n" have "\X n\ = \X k + (X n - X k)\" by simp also have "\X k + (X n - X k)\ \ \X k\ + \X n - X k\" by (rule abs_triangle_ineq) also have "\ < Max (abs ` X ` {..k}) + 1" by (rule add_le_less_mono) (simp_all add: k \k \ n\) finally show "\X n\ < Max (abs ` X ` {..k}) + 1" . qed qed qed lemma cauchy_mult [simp]: assumes X: "cauchy X" and Y: "cauchy Y" shows "cauchy (\n. X n * Y n)" proof (rule cauchyI) fix r :: rat assume "0 < r" then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v" by (rule obtain_pos_sum) obtain a where a: "0 < a" "\n. \X n\ < a" using cauchy_imp_bounded [OF X] by blast obtain b where b: "0 < b" "\n. \Y n\ < b" using cauchy_imp_bounded [OF Y] by blast obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b" proof show "0 < v/b" using v b(1) by simp show "0 < u/a" using u a(1) by simp show "r = a * (u/a) + (v/b) * b" using a(1) b(1) \r = u + v\ by simp qed obtain i where i: "\m\i. \n\i. \X m - X n\ < s" using cauchyD [OF X s] .. obtain j where j: "\m\j. \n\j. \Y m - Y n\ < t" using cauchyD [OF Y t] .. have "\m\max i j. \n\max i j. \X m * Y m - X n * Y n\ < r" proof clarsimp fix m n assume *: "i \ m" "j \ m" "i \ n" "j \ n" have "\X m * Y m - X n * Y n\ = \X m * (Y m - Y n) + (X m - X n) * Y n\" unfolding mult_diff_mult .. also have "\ \ \X m * (Y m - Y n)\ + \(X m - X n) * Y n\" by (rule abs_triangle_ineq) also have "\ = \X m\ * \Y m - Y n\ + \X m - X n\ * \Y n\" unfolding abs_mult .. also have "\ < a * t + s * b" by (simp_all add: add_strict_mono mult_strict_mono' a b i j *) finally show "\X m * Y m - X n * Y n\ < r" by (simp only: r) qed then show "\k. \m\k. \n\k. \X m * Y m - X n * Y n\ < r" .. qed lemma cauchy_not_vanishes_cases: assumes X: "cauchy X" assumes nz: "\ vanishes X" shows "\b>0. \k. (\n\k. b < - X n) \ (\n\k. b < X n)" proof - obtain r where "0 < r" and r: "\k. \n\k. r \ \X n\" using nz unfolding vanishes_def by (auto simp add: not_less) obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t" using \0 < r\ by (rule obtain_pos_sum) obtain i where i: "\m\i. \n\i. \X m - X n\ < s" using cauchyD [OF X s] .. obtain k where "i \ k" and "r \ \X k\" using r by blast have k: "\n\k. \X n - X k\ < s" using i \i \ k\ by auto have "X k \ - r \ r \ X k" using \r \ \X k\\ by auto then have "(\n\k. t < - X n) \ (\n\k. t < X n)" unfolding \r = s + t\ using k by auto then have "\k. (\n\k. t < - X n) \ (\n\k. t < X n)" .. then show "\t>0. \k. (\n\k. t < - X n) \ (\n\k. t < X n)" using t by auto qed lemma cauchy_not_vanishes: assumes X: "cauchy X" and nz: "\ vanishes X" shows "\b>0. \k. \n\k. b < \X n\" using cauchy_not_vanishes_cases [OF assms] by (elim ex_forward conj_forward asm_rl) auto lemma cauchy_inverse [simp]: assumes X: "cauchy X" and nz: "\ vanishes X" shows "cauchy (\n. inverse (X n))" proof (rule cauchyI) fix r :: rat assume "0 < r" obtain b i where b: "0 < b" and i: "\n\i. b < \X n\" using cauchy_not_vanishes [OF X nz] by blast from b i have nz: "\n\i. X n \ 0" by auto obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b" proof show "0 < b * r * b" by (simp add: \0 < r\ b) show "r = inverse b * (b * r * b) * inverse b" using b by simp qed obtain j where j: "\m\j. \n\j. \X m - X n\ < s" using cauchyD [OF X s] .. have "\m\max i j. \n\max i j. \inverse (X m) - inverse (X n)\ < r" proof clarsimp fix m n assume *: "i \ m" "j \ m" "i \ n" "j \ n" have "\inverse (X m) - inverse (X n)\ = inverse \X m\ * \X m - X n\ * inverse \X n\" by (simp add: inverse_diff_inverse nz * abs_mult) also have "\ < inverse b * s * inverse b" by (simp add: mult_strict_mono less_imp_inverse_less i j b * s) finally show "\inverse (X m) - inverse (X n)\ < r" by (simp only: r) qed then show "\k. \m\k. \n\k. \inverse (X m) - inverse (X n)\ < r" .. qed lemma vanishes_diff_inverse: assumes X: "cauchy X" "\ vanishes X" and Y: "cauchy Y" "\ vanishes Y" and XY: "vanishes (\n. X n - Y n)" shows "vanishes (\n. inverse (X n) - inverse (Y n))" proof (rule vanishesI) fix r :: rat assume r: "0 < r" obtain a i where a: "0 < a" and i: "\n\i. a < \X n\" using cauchy_not_vanishes [OF X] by blast obtain b j where b: "0 < b" and j: "\n\j. b < \Y n\" using cauchy_not_vanishes [OF Y] by blast obtain s where s: "0 < s" and "inverse a * s * inverse b = r" proof show "0 < a * r * b" using a r b by simp show "inverse a * (a * r * b) * inverse b = r" using a r b by simp qed obtain k where k: "\n\k. \X n - Y n\ < s" using vanishesD [OF XY s] .. have "\n\max (max i j) k. \inverse (X n) - inverse (Y n)\ < r" proof clarsimp fix n assume n: "i \ n" "j \ n" "k \ n" with i j a b have "X n \ 0" and "Y n \ 0" by auto then have "\inverse (X n) - inverse (Y n)\ = inverse \X n\ * \X n - Y n\ * inverse \Y n\" by (simp add: inverse_diff_inverse abs_mult) also have "\ < inverse a * s * inverse b" by (intro mult_strict_mono' less_imp_inverse_less) (simp_all add: a b i j k n) also note \inverse a * s * inverse b = r\ finally show "\inverse (X n) - inverse (Y n)\ < r" . qed then show "\k. \n\k. \inverse (X n) - inverse (Y n)\ < r" .. qed subsection \Equivalence relation on Cauchy sequences\ definition realrel :: "(nat \ rat) \ (nat \ rat) \ bool" where "realrel = (\X Y. cauchy X \ cauchy Y \ vanishes (\n. X n - Y n))" lemma realrelI [intro?]: "cauchy X \ cauchy Y \ vanishes (\n. X n - Y n) \ realrel X Y" by (simp add: realrel_def) lemma realrel_refl: "cauchy X \ realrel X X" by (simp add: realrel_def) lemma symp_realrel: "symp realrel" by (simp add: abs_minus_commute realrel_def symp_def vanishes_def) lemma transp_realrel: "transp realrel" unfolding realrel_def by (rule transpI) (force simp add: dest: vanishes_add) lemma part_equivp_realrel: "part_equivp realrel" by (blast intro: part_equivpI symp_realrel transp_realrel realrel_refl cauchy_const) subsection \The field of real numbers\ quotient_type real = "nat \ rat" / partial: realrel morphisms rep_real Real by (rule part_equivp_realrel) lemma cr_real_eq: "pcr_real = (\x y. cauchy x \ Real x = y)" unfolding real.pcr_cr_eq cr_real_def realrel_def by auto lemma Real_induct [induct type: real]: (* TODO: generate automatically *) assumes "\X. cauchy X \ P (Real X)" shows "P x" proof (induct x) case (1 X) then have "cauchy X" by (simp add: realrel_def) then show "P (Real X)" by (rule assms) qed lemma eq_Real: "cauchy X \ cauchy Y \ Real X = Real Y \ vanishes (\n. X n - Y n)" using real.rel_eq_transfer unfolding real.pcr_cr_eq cr_real_def rel_fun_def realrel_def by simp lemma Domainp_pcr_real [transfer_domain_rule]: "Domainp pcr_real = cauchy" by (simp add: real.domain_eq realrel_def) instantiation real :: field begin lift_definition zero_real :: "real" is "\n. 0" by (simp add: realrel_refl) lift_definition one_real :: "real" is "\n. 1" by (simp add: realrel_refl) lift_definition plus_real :: "real \ real \ real" is "\X Y n. X n + Y n" unfolding realrel_def add_diff_add by (simp only: cauchy_add vanishes_add simp_thms) lift_definition uminus_real :: "real \ real" is "\X n. - X n" unfolding realrel_def minus_diff_minus by (simp only: cauchy_minus vanishes_minus simp_thms) lift_definition times_real :: "real \ real \ real" is "\X Y n. X n * Y n" proof - fix f1 f2 f3 f4 have "\cauchy f1; cauchy f4; vanishes (\n. f1 n - f2 n); vanishes (\n. f3 n - f4 n)\ \ vanishes (\n. f1 n * (f3 n - f4 n) + f4 n * (f1 n - f2 n))" by (simp add: vanishes_add vanishes_mult_bounded cauchy_imp_bounded) then show "\realrel f1 f2; realrel f3 f4\ \ realrel (\n. f1 n * f3 n) (\n. f2 n * f4 n)" by (simp add: mult.commute realrel_def mult_diff_mult) qed lift_definition inverse_real :: "real \ real" is "\X. if vanishes X then (\n. 0) else (\n. inverse (X n))" proof - fix X Y assume "realrel X Y" then have X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\n. X n - Y n)" by (simp_all add: realrel_def) have "vanishes X \ vanishes Y" proof assume "vanishes X" from vanishes_diff [OF this XY] show "vanishes Y" by simp next assume "vanishes Y" from vanishes_add [OF this XY] show "vanishes X" by simp qed then show "?thesis X Y" by (simp add: vanishes_diff_inverse X Y XY realrel_def) qed definition "x - y = x + - y" for x y :: real definition "x div y = x * inverse y" for x y :: real lemma add_Real: "cauchy X \ cauchy Y \ Real X + Real Y = Real (\n. X n + Y n)" using plus_real.transfer by (simp add: cr_real_eq rel_fun_def) lemma minus_Real: "cauchy X \ - Real X = Real (\n. - X n)" using uminus_real.transfer by (simp add: cr_real_eq rel_fun_def) lemma diff_Real: "cauchy X \ cauchy Y \ Real X - Real Y = Real (\n. X n - Y n)" by (simp add: minus_Real add_Real minus_real_def) lemma mult_Real: "cauchy X \ cauchy Y \ Real X * Real Y = Real (\n. X n * Y n)" using times_real.transfer by (simp add: cr_real_eq rel_fun_def) lemma inverse_Real: "cauchy X \ inverse (Real X) = (if vanishes X then 0 else Real (\n. inverse (X n)))" using inverse_real.transfer zero_real.transfer unfolding cr_real_eq rel_fun_def by (simp split: if_split_asm, metis) instance proof fix a b c :: real show "a + b = b + a" by transfer (simp add: ac_simps realrel_def) show "(a + b) + c = a + (b + c)" by transfer (simp add: ac_simps realrel_def) show "0 + a = a" by transfer (simp add: realrel_def) show "- a + a = 0" by transfer (simp add: realrel_def) show "a - b = a + - b" by (rule minus_real_def) show "(a * b) * c = a * (b * c)" by transfer (simp add: ac_simps realrel_def) show "a * b = b * a" by transfer (simp add: ac_simps realrel_def) show "1 * a = a" by transfer (simp add: ac_simps realrel_def) show "(a + b) * c = a * c + b * c" by transfer (simp add: distrib_right realrel_def) show "(0::real) \ (1::real)" by transfer (simp add: realrel_def) have "vanishes (\n. inverse (X n) * X n - 1)" if X: "cauchy X" "\ vanishes X" for X proof (rule vanishesI) fix r::rat assume "0 < r" obtain b k where "b>0" "\n\k. b < \X n\" using X cauchy_not_vanishes by blast then show "\k. \n\k. \inverse (X n) * X n - 1\ < r" using \0 < r\ by force qed then show "a \ 0 \ inverse a * a = 1" by transfer (simp add: realrel_def) show "a div b = a * inverse b" by (rule divide_real_def) show "inverse (0::real) = 0" by transfer (simp add: realrel_def) qed end subsection \Positive reals\ lift_definition positive :: "real \ bool" is "\X. \r>0. \k. \n\k. r < X n" proof - have 1: "\r>0. \k. \n\k. r < Y n" if *: "realrel X Y" and **: "\r>0. \k. \n\k. r < X n" for X Y proof - from * have XY: "vanishes (\n. X n - Y n)" by (simp_all add: realrel_def) from ** obtain r i where "0 < r" and i: "\n\i. r < X n" by blast obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" using \0 < r\ by (rule obtain_pos_sum) obtain j where j: "\n\j. \X n - Y n\ < s" using vanishesD [OF XY s] .. have "\n\max i j. t < Y n" proof clarsimp fix n assume n: "i \ n" "j \ n" have "\X n - Y n\ < s" and "r < X n" using i j n by simp_all then show "t < Y n" by (simp add: r) qed then show ?thesis using t by blast qed fix X Y assume "realrel X Y" then have "realrel X Y" and "realrel Y X" using symp_realrel by (auto simp: symp_def) then show "?thesis X Y" by (safe elim!: 1) qed lemma positive_Real: "cauchy X \ positive (Real X) \ (\r>0. \k. \n\k. r < X n)" using positive.transfer by (simp add: cr_real_eq rel_fun_def) lemma positive_zero: "\ positive 0" by transfer auto lemma positive_add: assumes "positive x" "positive y" shows "positive (x + y)" proof - have *: "\\n\i. a < x n; \n\j. b < y n; 0 < a; 0 < b; n \ max i j\ \ a+b < x n + y n" for x y and a b::rat and i j n::nat by (simp add: add_strict_mono) show ?thesis using assms by transfer (blast intro: * pos_add_strict) qed lemma positive_mult: assumes "positive x" "positive y" shows "positive (x * y)" proof - have *: "\\n\i. a < x n; \n\j. b < y n; 0 < a; 0 < b; n \ max i j\ \ a*b < x n * y n" for x y and a b::rat and i j n::nat by (simp add: mult_strict_mono') show ?thesis using assms by transfer (blast intro: * mult_pos_pos) qed lemma positive_minus: "\ positive x \ x \ 0 \ positive (- x)" apply transfer apply (simp add: realrel_def) apply (blast dest: cauchy_not_vanishes_cases) done instantiation real :: linordered_field begin definition "x < y \ positive (y - x)" definition "x \ y \ x < y \ x = y" for x y :: real definition "\a\ = (if a < 0 then - a else a)" for a :: real definition "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)" for a :: real instance proof fix a b c :: real show "\a\ = (if a < 0 then - a else a)" by (rule abs_real_def) show "a < b \ a \ b \ \ b \ a" "a \ b \ b \ c \ a \ c" "a \ a" "a \ b \ b \ a \ a = b" "a \ b \ c + a \ c + b" unfolding less_eq_real_def less_real_def by (force simp add: positive_zero dest: positive_add)+ show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)" by (rule sgn_real_def) show "a \ b \ b \ a" by (auto dest!: positive_minus simp: less_eq_real_def less_real_def) show "a < b \ 0 < c \ c * a < c * b" unfolding less_real_def by (force simp add: algebra_simps dest: positive_mult) qed end instantiation real :: distrib_lattice begin definition "(inf :: real \ real \ real) = min" definition "(sup :: real \ real \ real) = max" instance by standard (auto simp add: inf_real_def sup_real_def max_min_distrib2) end lemma of_nat_Real: "of_nat x = Real (\n. of_nat x)" by (induct x) (simp_all add: zero_real_def one_real_def add_Real) lemma of_int_Real: "of_int x = Real (\n. of_int x)" by (cases x rule: int_diff_cases) (simp add: of_nat_Real diff_Real) lemma of_rat_Real: "of_rat x = Real (\n. x)" proof (induct x) case (Fract a b) then show ?case apply (simp add: Fract_of_int_quotient of_rat_divide) apply (simp add: of_int_Real divide_inverse inverse_Real mult_Real) done qed instance real :: archimedean_field proof show "\z. x \ of_int z" for x :: real proof (induct x) case (1 X) then obtain b where "0 < b" and b: "\n. \X n\ < b" by (blast dest: cauchy_imp_bounded) then have "Real X < of_int (\b\ + 1)" using 1 apply (simp add: of_int_Real less_real_def diff_Real positive_Real) apply (rule_tac x=1 in exI) apply (simp add: algebra_simps) by (metis abs_ge_self le_less_trans le_of_int_ceiling less_le) then show ?case using less_eq_real_def by blast qed qed instantiation real :: floor_ceiling begin definition [code del]: "\x::real\ = (THE z. of_int z \ x \ x < of_int (z + 1))" instance proof show "of_int \x\ \ x \ x < of_int (\x\ + 1)" for x :: real unfolding floor_real_def using floor_exists1 by (rule theI') qed end subsection \Completeness\ lemma not_positive_Real: assumes "cauchy X" shows "\ positive (Real X) \ (\r>0. \k. \n\k. X n \ r)" (is "?lhs = ?rhs") unfolding positive_Real [OF assms] proof (intro iffI allI notI impI) show "\k. \n\k. X n \ r" if r: "\ (\r>0. \k. \n\k. r < X n)" and "0 < r" for r proof - obtain s t where "s > 0" "t > 0" "r = s+t" using \r > 0\ obtain_pos_sum by blast obtain k where k: "\m n. \m\k; n\k\ \ \X m - X n\ < t" using cauchyD [OF assms \t > 0\] by blast obtain n where "n \ k" "X n \ s" by (meson r \0 < s\ not_less) then have "X l \ r" if "l \ n" for l using k [OF \n \ k\, of l] that \r = s+t\ by linarith then show ?thesis by blast qed qed (meson le_cases not_le) lemma le_Real: assumes "cauchy X" "cauchy Y" shows "Real X \ Real Y = (\r>0. \k. \n\k. X n \ Y n + r)" unfolding not_less [symmetric, where 'a=real] less_real_def apply (simp add: diff_Real not_positive_Real assms) apply (simp add: diff_le_eq ac_simps) done lemma le_RealI: assumes Y: "cauchy Y" shows "\n. x \ of_rat (Y n) \ x \ Real Y" proof (induct x) fix X assume X: "cauchy X" and "\n. Real X \ of_rat (Y n)" then have le: "\m r. 0 < r \ \k. \n\k. X n \ Y m + r" by (simp add: of_rat_Real le_Real) then have "\k. \n\k. X n \ Y n + r" if "0 < r" for r :: rat proof - from that obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" by (rule obtain_pos_sum) obtain i where i: "\m\i. \n\i. \Y m - Y n\ < s" using cauchyD [OF Y s] .. obtain j where j: "\n\j. X n \ Y i + t" using le [OF t] .. have "\n\max i j. X n \ Y n + r" proof clarsimp fix n assume n: "i \ n" "j \ n" have "X n \ Y i + t" using n j by simp moreover have "\Y i - Y n\ < s" using n i by simp ultimately show "X n \ Y n + r" unfolding r by simp qed then show ?thesis .. qed then show "Real X \ Real Y" by (simp add: of_rat_Real le_Real X Y) qed lemma Real_leI: assumes X: "cauchy X" assumes le: "\n. of_rat (X n) \ y" shows "Real X \ y" proof - have "- y \ - Real X" by (simp add: minus_Real X le_RealI of_rat_minus le) then show ?thesis by simp qed lemma less_RealD: assumes "cauchy Y" shows "x < Real Y \ \n. x < of_rat (Y n)" apply (erule contrapos_pp) apply (simp add: not_less) apply (erule Real_leI [OF assms]) done lemma of_nat_less_two_power [simp]: "of_nat n < (2::'a::linordered_idom) ^ n" apply (induct n) apply simp apply (metis add_le_less_mono mult_2 of_nat_Suc one_le_numeral one_le_power power_Suc) done lemma complete_real: fixes S :: "real set" assumes "\x. x \ S" and "\z. \x\S. x \ z" shows "\y. (\x\S. x \ y) \ (\z. (\x\S. x \ z) \ y \ z)" proof - obtain x where x: "x \ S" using assms(1) .. obtain z where z: "\x\S. x \ z" using assms(2) .. define P where "P x \ (\y\S. y \ of_rat x)" for x obtain a where a: "\ P a" proof have "of_int \x - 1\ \ x - 1" by (rule of_int_floor_le) also have "x - 1 < x" by simp finally have "of_int \x - 1\ < x" . then have "\ x \ of_int \x - 1\" by (simp only: not_le) then show "\ P (of_int \x - 1\)" unfolding P_def of_rat_of_int_eq using x by blast qed obtain b where b: "P b" proof show "P (of_int \z\)" unfolding P_def of_rat_of_int_eq proof fix y assume "y \ S" then have "y \ z" using z by simp also have "z \ of_int \z\" by (rule le_of_int_ceiling) finally show "y \ of_int \z\" . qed qed define avg where "avg x y = x/2 + y/2" for x y :: rat define bisect where "bisect = (\(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y))" define A where "A n = fst ((bisect ^^ n) (a, b))" for n define B where "B n = snd ((bisect ^^ n) (a, b))" for n define C where "C n = avg (A n) (B n)" for n have A_0 [simp]: "A 0 = a" unfolding A_def by simp have B_0 [simp]: "B 0 = b" unfolding B_def by simp have A_Suc [simp]: "\n. A (Suc n) = (if P (C n) then A n else C n)" unfolding A_def B_def C_def bisect_def split_def by simp have B_Suc [simp]: "\n. B (Suc n) = (if P (C n) then C n else B n)" unfolding A_def B_def C_def bisect_def split_def by simp have width: "B n - A n = (b - a) / 2^n" for n proof (induct n) case (Suc n) then show ?case by (simp add: C_def eq_divide_eq avg_def algebra_simps) qed simp have twos: "\n. y / 2 ^ n < r" if "0 < r" for y r :: rat proof - obtain n where "y / r < rat_of_nat n" using \0 < r\ reals_Archimedean2 by blast then have "\n. y < r * 2 ^ n" by (metis divide_less_eq less_trans mult.commute of_nat_less_two_power that) then show ?thesis by (simp add: field_split_simps) qed have PA: "\ P (A n)" for n by (induct n) (simp_all add: a) have PB: "P (B n)" for n by (induct n) (simp_all add: b) have ab: "a < b" using a b unfolding P_def by (meson leI less_le_trans of_rat_less) have AB: "A n < B n" for n by (induct n) (simp_all add: ab C_def avg_def) have "A i \ A j \ B j \ B i" if "i < j" for i j using that proof (induction rule: less_Suc_induct) case (1 i) then show ?case apply (clarsimp simp add: C_def avg_def add_divide_distrib [symmetric]) apply (rule AB [THEN less_imp_le]) done qed simp then have A_mono: "A i \ A j" and B_mono: "B j \ B i" if "i \ j" for i j by (metis eq_refl le_neq_implies_less that)+ have cauchy_lemma: "cauchy X" if *: "\n i. i\n \ A n \ X i \ X i \ B n" for X proof (rule cauchyI) fix r::rat assume "0 < r" then obtain k where k: "(b - a) / 2 ^ k < r" using twos by blast have "\X m - X n\ < r" if "m\k" "n\k" for m n proof - have "\X m - X n\ \ B k - A k" by (simp add: * abs_rat_def diff_mono that) also have "... < r" by (simp add: k width) finally show ?thesis . qed then show "\k. \m\k. \n\k. \X m - X n\ < r" by blast qed have "cauchy A" by (rule cauchy_lemma) (meson AB A_mono B_mono dual_order.strict_implies_order less_le_trans) have "cauchy B" by (rule cauchy_lemma) (meson AB A_mono B_mono dual_order.strict_implies_order le_less_trans) have "\x\S. x \ Real B" proof fix x assume "x \ S" then show "x \ Real B" using PB [unfolded P_def] \cauchy B\ by (simp add: le_RealI) qed moreover have "\z. (\x\S. x \ z) \ Real A \ z" by (meson PA Real_leI P_def \cauchy A\ le_cases order.trans) moreover have "vanishes (\n. (b - a) / 2 ^ n)" proof (rule vanishesI) fix r :: rat assume "0 < r" then obtain k where k: "\b - a\ / 2 ^ k < r" using twos by blast have "\n\k. \(b - a) / 2 ^ n\ < r" proof clarify fix n assume n: "k \ n" have "\(b - a) / 2 ^ n\ = \b - a\ / 2 ^ n" by simp also have "\ \ \b - a\ / 2 ^ k" using n by (simp add: divide_left_mono) also note k finally show "\(b - a) / 2 ^ n\ < r" . qed then show "\k. \n\k. \(b - a) / 2 ^ n\ < r" .. qed then have "Real B = Real A" by (simp add: eq_Real \cauchy A\ \cauchy B\ width) ultimately show "\y. (\x\S. x \ y) \ (\z. (\x\S. x \ z) \ y \ z)" by force qed instantiation real :: linear_continuum begin subsection \Supremum of a set of reals\ definition "Sup X = (LEAST z::real. \x\X. x \ z)" definition "Inf X = - Sup (uminus ` X)" for X :: "real set" instance proof show Sup_upper: "x \ Sup X" if "x \ X" "bdd_above X" for x :: real and X :: "real set" proof - from that obtain s where s: "\y\X. y \ s" "\z. \y\X. y \ z \ s \ z" using complete_real[of X] unfolding bdd_above_def by blast then show ?thesis unfolding Sup_real_def by (rule LeastI2_order) (auto simp: that) qed show Sup_least: "Sup X \ z" if "X \ {}" and z: "\x. x \ X \ x \ z" for z :: real and X :: "real set" proof - from that obtain s where s: "\y\X. y \ s" "\z. \y\X. y \ z \ s \ z" using complete_real [of X] by blast then have "Sup X = s" unfolding Sup_real_def by (best intro: Least_equality) also from s z have "\ \ z" by blast finally show ?thesis . qed show "Inf X \ x" if "x \ X" "bdd_below X" for x :: real and X :: "real set" using Sup_upper [of "-x" "uminus ` X"] by (auto simp: Inf_real_def that) show "z \ Inf X" if "X \ {}" "\x. x \ X \ z \ x" for z :: real and X :: "real set" using Sup_least [of "uminus ` X" "- z"] by (force simp: Inf_real_def that) show "\a b::real. a \ b" using zero_neq_one by blast qed end subsection \Hiding implementation details\ hide_const (open) vanishes cauchy positive Real declare Real_induct [induct del] declare Abs_real_induct [induct del] declare Abs_real_cases [cases del] lifting_update real.lifting lifting_forget real.lifting subsection \More Lemmas\ text \BH: These lemmas should not be necessary; they should be covered by existing simp rules and simplification procedures.\ lemma real_mult_less_iff1 [simp]: "0 < z \ x * z < y * z \ x < y" for x y z :: real by simp (* solved by linordered_ring_less_cancel_factor simproc *) lemma real_mult_le_cancel_iff1 [simp]: "0 < z \ x * z \ y * z \ x \ y" for x y z :: real by simp (* solved by linordered_ring_le_cancel_factor simproc *) lemma real_mult_le_cancel_iff2 [simp]: "0 < z \ z * x \ z * y \ x \ y" for x y z :: real by simp (* solved by linordered_ring_le_cancel_factor simproc *) subsection \Embedding numbers into the Reals\ abbreviation real_of_nat :: "nat \ real" where "real_of_nat \ of_nat" abbreviation real :: "nat \ real" where "real \ of_nat" abbreviation real_of_int :: "int \ real" where "real_of_int \ of_int" abbreviation real_of_rat :: "rat \ real" where "real_of_rat \ of_rat" declare [[coercion_enabled]] declare [[coercion "of_nat :: nat \ int"]] declare [[coercion "of_nat :: nat \ real"]] declare [[coercion "of_int :: int \ real"]] (* We do not add rat to the coerced types, this has often unpleasant side effects when writing inverse (Suc n) which sometimes gets two coercions: of_rat (inverse (of_nat (Suc n))) *) declare [[coercion_map map]] declare [[coercion_map "\f g h x. g (h (f x))"]] declare [[coercion_map "\f g (x,y). (f x, g y)"]] declare of_int_eq_0_iff [algebra, presburger] declare of_int_eq_1_iff [algebra, presburger] declare of_int_eq_iff [algebra, presburger] declare of_int_less_0_iff [algebra, presburger] declare of_int_less_1_iff [algebra, presburger] declare of_int_less_iff [algebra, presburger] declare of_int_le_0_iff [algebra, presburger] declare of_int_le_1_iff [algebra, presburger] declare of_int_le_iff [algebra, presburger] declare of_int_0_less_iff [algebra, presburger] declare of_int_0_le_iff [algebra, presburger] declare of_int_1_less_iff [algebra, presburger] declare of_int_1_le_iff [algebra, presburger] lemma int_less_real_le: "n < m \ real_of_int n + 1 \ real_of_int m" proof - have "(0::real) \ 1" by (metis less_eq_real_def zero_less_one) then show ?thesis by (metis floor_of_int less_floor_iff) qed lemma int_le_real_less: "n \ m \ real_of_int n < real_of_int m + 1" by (meson int_less_real_le not_le) lemma real_of_int_div_aux: "(real_of_int x) / (real_of_int d) = real_of_int (x div d) + (real_of_int (x mod d)) / (real_of_int d)" proof - have "x = (x div d) * d + x mod d" by auto then have "real_of_int x = real_of_int (x div d) * real_of_int d + real_of_int(x mod d)" by (metis of_int_add of_int_mult) then have "real_of_int x / real_of_int d = \ / real_of_int d" by simp then show ?thesis by (auto simp add: add_divide_distrib algebra_simps) qed lemma real_of_int_div: "d dvd n \ real_of_int (n div d) = real_of_int n / real_of_int d" for d n :: int by (simp add: real_of_int_div_aux) lemma real_of_int_div2: "0 \ real_of_int n / real_of_int x - real_of_int (n div x)" proof (cases "x = 0") case False then show ?thesis by (metis diff_ge_0_iff_ge floor_divide_of_int_eq of_int_floor_le) qed simp lemma real_of_int_div3: "real_of_int n / real_of_int x - real_of_int (n div x) \ 1" apply (simp add: algebra_simps) by (metis add.commute floor_correct floor_divide_of_int_eq less_eq_real_def of_int_1 of_int_add) lemma real_of_int_div4: "real_of_int (n div x) \ real_of_int n / real_of_int x" using real_of_int_div2 [of n x] by simp subsection \Embedding the Naturals into the Reals\ lemma real_of_card: "real (card A) = sum (\x. 1) A" by simp lemma nat_less_real_le: "n < m \ real n + 1 \ real m" by (metis discrete of_nat_1 of_nat_add of_nat_le_iff) lemma nat_le_real_less: "n \ m \ real n < real m + 1" for m n :: nat by (meson nat_less_real_le not_le) lemma real_of_nat_div_aux: "real x / real d = real (x div d) + real (x mod d) / real d" proof - have "x = (x div d) * d + x mod d" by auto then have "real x = real (x div d) * real d + real(x mod d)" by (metis of_nat_add of_nat_mult) then have "real x / real d = \ / real d" by simp then show ?thesis by (auto simp add: add_divide_distrib algebra_simps) qed lemma real_of_nat_div: "d dvd n \ real(n div d) = real n / real d" by (subst real_of_nat_div_aux) (auto simp add: dvd_eq_mod_eq_0 [symmetric]) lemma real_of_nat_div2: "0 \ real n / real x - real (n div x)" for n x :: nat apply (simp add: algebra_simps) by (metis floor_divide_of_nat_eq of_int_floor_le of_int_of_nat_eq) lemma real_of_nat_div3: "real n / real x - real (n div x) \ 1" for n x :: nat proof (cases "x = 0") case False then show ?thesis by (metis of_int_of_nat_eq real_of_int_div3 zdiv_int) qed auto lemma real_of_nat_div4: "real (n div x) \ real n / real x" for n x :: nat using real_of_nat_div2 [of n x] by simp subsection \The Archimedean Property of the Reals\ lemma real_arch_inverse: "0 < e \ (\n::nat. n \ 0 \ 0 < inverse (real n) \ inverse (real n) < e)" using reals_Archimedean[of e] less_trans[of 0 "1 / real n" e for n::nat] by (auto simp add: field_simps cong: conj_cong simp del: of_nat_Suc) lemma reals_Archimedean3: "0 < x \ \y. \n. y < real n * x" by (auto intro: ex_less_of_nat_mult) lemma real_archimedian_rdiv_eq_0: assumes x0: "x \ 0" and c: "c \ 0" and xc: "\m::nat. m > 0 \ real m * x \ c" shows "x = 0" by (metis reals_Archimedean3 dual_order.order_iff_strict le0 le_less_trans not_le x0 xc) subsection \Rationals\ lemma Rats_abs_iff[simp]: "\(x::real)\ \ \ \ x \ \" by(simp add: abs_real_def split: if_splits) lemma Rats_eq_int_div_int: "\ = {real_of_int i / real_of_int j | i j. j \ 0}" (is "_ = ?S") proof show "\ \ ?S" proof fix x :: real assume "x \ \" then obtain r where "x = of_rat r" unfolding Rats_def .. have "of_rat r \ ?S" by (cases r) (auto simp add: of_rat_rat) then show "x \ ?S" using \x = of_rat r\ by simp qed next show "?S \ \" proof (auto simp: Rats_def) fix i j :: int assume "j \ 0" then have "real_of_int i / real_of_int j = of_rat (Fract i j)" by (simp add: of_rat_rat) then show "real_of_int i / real_of_int j \ range of_rat" by blast qed qed lemma Rats_eq_int_div_nat: "\ = { real_of_int i / real n | i n. n \ 0}" proof (auto simp: Rats_eq_int_div_int) fix i j :: int assume "j \ 0" show "\(i'::int) (n::nat). real_of_int i / real_of_int j = real_of_int i' / real n \ 0 < n" proof (cases "j > 0") case True then have "real_of_int i / real_of_int j = real_of_int i / real (nat j) \ 0 < nat j" by simp then show ?thesis by blast next case False with \j \ 0\ have "real_of_int i / real_of_int j = real_of_int (- i) / real (nat (- j)) \ 0 < nat (- j)" by simp then show ?thesis by blast qed next fix i :: int and n :: nat assume "0 < n" then have "real_of_int i / real n = real_of_int i / real_of_int(int n) \ int n \ 0" by simp then show "\i' j. real_of_int i / real n = real_of_int i' / real_of_int j \ j \ 0" by blast qed lemma Rats_abs_nat_div_natE: assumes "x \ \" obtains m n :: nat where "n \ 0" and "\x\ = real m / real n" and "coprime m n" proof - from \x \ \\ obtain i :: int and n :: nat where "n \ 0" and "x = real_of_int i / real n" by (auto simp add: Rats_eq_int_div_nat) then have "\x\ = real (nat \i\) / real n" by simp then obtain m :: nat where x_rat: "\x\ = real m / real n" by blast let ?gcd = "gcd m n" from \n \ 0\ have gcd: "?gcd \ 0" by simp let ?k = "m div ?gcd" let ?l = "n div ?gcd" let ?gcd' = "gcd ?k ?l" have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m" by (rule dvd_mult_div_cancel) have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n" by (rule dvd_mult_div_cancel) from \n \ 0\ and gcd_l have "?gcd * ?l \ 0" by simp then have "?l \ 0" by (blast dest!: mult_not_zero) moreover have "\x\ = real ?k / real ?l" proof - from gcd have "real ?k / real ?l = real (?gcd * ?k) / real (?gcd * ?l)" by (simp add: real_of_nat_div) also from gcd_k and gcd_l have "\ = real m / real n" by simp also from x_rat have "\ = \x\" .. finally show ?thesis .. qed moreover have "?gcd' = 1" proof - have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)" by (rule gcd_mult_distrib_nat) with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp with gcd show ?thesis by auto qed then have "coprime ?k ?l" by (simp only: coprime_iff_gcd_eq_1) ultimately show ?thesis .. qed subsection \Density of the Rational Reals in the Reals\ text \ This density proof is due to Stefan Richter and was ported by TN. The original source is \<^emph>\Real Analysis\ by H.L. Royden. It employs the Archimedean property of the reals.\ lemma Rats_dense_in_real: fixes x :: real assumes "x < y" shows "\r\\. x < r \ r < y" proof - from \x < y\ have "0 < y - x" by simp with reals_Archimedean obtain q :: nat where q: "inverse (real q) < y - x" and "0 < q" by blast define p where "p = \y * real q\ - 1" define r where "r = of_int p / real q" from q have "x < y - inverse (real q)" by simp also from \0 < q\ have "y - inverse (real q) \ r" by (simp add: r_def p_def le_divide_eq left_diff_distrib) finally have "x < r" . moreover from \0 < q\ have "r < y" by (simp add: r_def p_def divide_less_eq diff_less_eq less_ceiling_iff [symmetric]) moreover have "r \ \" by (simp add: r_def) ultimately show ?thesis by blast qed lemma of_rat_dense: fixes x y :: real assumes "x < y" shows "\q :: rat. x < of_rat q \ of_rat q < y" using Rats_dense_in_real [OF \x < y\] by (auto elim: Rats_cases) subsection \Numerals and Arithmetic\ declaration \ K (Lin_Arith.add_inj_const (\<^const_name>\of_nat\, \<^typ>\nat \ real\) #> Lin_Arith.add_inj_const (\<^const_name>\of_int\, \<^typ>\int \ real\)) \ subsection \Simprules combining \x + y\ and \0\\ (* FIXME ARE THEY NEEDED? *) lemma real_add_minus_iff [simp]: "x + - a = 0 \ x = a" for x a :: real by arith lemma real_add_less_0_iff: "x + y < 0 \ y < - x" for x y :: real by auto lemma real_0_less_add_iff: "0 < x + y \ - x < y" for x y :: real by auto lemma real_add_le_0_iff: "x + y \ 0 \ y \ - x" for x y :: real by auto lemma real_0_le_add_iff: "0 \ x + y \ - x \ y" for x y :: real by auto subsection \Lemmas about powers\ lemma two_realpow_ge_one: "(1::real) \ 2 ^ n" by simp (* FIXME: declare this [simp] for all types, or not at all *) declare sum_squares_eq_zero_iff [simp] sum_power2_eq_zero_iff [simp] lemma real_minus_mult_self_le [simp]: "- (u * u) \ x * x" for u x :: real by (rule order_trans [where y = 0]) auto lemma realpow_square_minus_le [simp]: "- u\<^sup>2 \ x\<^sup>2" for u x :: real by (auto simp add: power2_eq_square) subsection \Density of the Reals\ lemma field_lbound_gt_zero: "0 < d1 \ 0 < d2 \ \e. 0 < e \ e < d1 \ e < d2" for d1 d2 :: "'a::linordered_field" by (rule exI [where x = "min d1 d2 / 2"]) (simp add: min_def) lemma field_less_half_sum: "x < y \ x < (x + y) / 2" for x y :: "'a::linordered_field" by auto lemma field_sum_of_halves: "x / 2 + x / 2 = x" for x :: "'a::linordered_field" by simp +subsection \Archimedean properties and useful consequences\ + +text\Bernoulli's inequality\ +proposition Bernoulli_inequality: + fixes x :: real + assumes "-1 \ x" + shows "1 + n * x \ (1 + x) ^ n" +proof (induct n) + case 0 + then show ?case by simp +next + case (Suc n) + have "1 + Suc n * x \ 1 + (Suc n)*x + n * x^2" + by (simp add: algebra_simps) + also have "... = (1 + x) * (1 + n*x)" + by (auto simp: power2_eq_square algebra_simps) + also have "... \ (1 + x) ^ Suc n" + using Suc.hyps assms mult_left_mono by fastforce + finally show ?case . +qed + +corollary Bernoulli_inequality_even: + fixes x :: real + assumes "even n" + shows "1 + n * x \ (1 + x) ^ n" +proof (cases "-1 \ x \ n=0") + case True + then show ?thesis + by (auto simp: Bernoulli_inequality) +next + case False + then have "real n \ 1" + by simp + with False have "n * x \ -1" + by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one) + then have "1 + n * x \ 0" + by auto + also have "... \ (1 + x) ^ n" + using assms + using zero_le_even_power by blast + finally show ?thesis . +qed + +corollary real_arch_pow: + fixes x :: real + assumes x: "1 < x" + shows "\n. y < x^n" +proof - + from x have x0: "x - 1 > 0" + by arith + from reals_Archimedean3[OF x0, rule_format, of y] + obtain n :: nat where n: "y < real n * (x - 1)" by metis + from x0 have x00: "x- 1 \ -1" by arith + from Bernoulli_inequality[OF x00, of n] n + have "y < x^n" by auto + then show ?thesis by metis +qed + +corollary real_arch_pow_inv: + fixes x y :: real + assumes y: "y > 0" + and x1: "x < 1" + shows "\n. x^n < y" +proof (cases "x > 0") + case True + with x1 have ix: "1 < 1/x" by (simp add: field_simps) + from real_arch_pow[OF ix, of "1/y"] + obtain n where n: "1/y < (1/x)^n" by blast + then show ?thesis using y \x > 0\ + by (auto simp add: field_simps) +next + case False + with y x1 show ?thesis + by (metis less_le_trans not_less power_one_right) +qed + +lemma forall_pos_mono: + "(\d e::real. d < e \ P d \ P e) \ + (\n::nat. n \ 0 \ P (inverse (real n))) \ (\e. 0 < e \ P e)" + by (metis real_arch_inverse) + +lemma forall_pos_mono_1: + "(\d e::real. d < e \ P d \ P e) \ + (\n. P (inverse (real (Suc n)))) \ 0 < e \ P e" + apply (rule forall_pos_mono) + apply auto + apply (metis Suc_pred of_nat_Suc) + done + + subsection \Floor and Ceiling Functions from the Reals to the Integers\ (* FIXME: theorems for negative numerals. Many duplicates, e.g. from Archimedean_Field.thy. *) lemma real_of_nat_less_numeral_iff [simp]: "real n < numeral w \ n < numeral w" for n :: nat by (metis of_nat_less_iff of_nat_numeral) lemma numeral_less_real_of_nat_iff [simp]: "numeral w < real n \ numeral w < n" for n :: nat by (metis of_nat_less_iff of_nat_numeral) lemma numeral_le_real_of_nat_iff [simp]: "numeral n \ real m \ numeral n \ m" for m :: nat by (metis not_le real_of_nat_less_numeral_iff) lemma of_int_floor_cancel [simp]: "of_int \x\ = x \ (\n::int. x = of_int n)" by (metis floor_of_int) lemma floor_eq: "real_of_int n < x \ x < real_of_int n + 1 \ \x\ = n" by linarith lemma floor_eq2: "real_of_int n \ x \ x < real_of_int n + 1 \ \x\ = n" by (fact floor_unique) lemma floor_eq3: "real n < x \ x < real (Suc n) \ nat \x\ = n" by linarith lemma floor_eq4: "real n \ x \ x < real (Suc n) \ nat \x\ = n" by linarith lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \ real_of_int \r\" by linarith lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real_of_int \r\" by linarith lemma real_of_int_floor_add_one_ge [simp]: "r \ real_of_int \r\ + 1" by linarith lemma real_of_int_floor_add_one_gt [simp]: "r < real_of_int \r\ + 1" by linarith lemma floor_divide_real_eq_div: assumes "0 \ b" shows "\a / real_of_int b\ = \a\ div b" proof (cases "b = 0") case True then show ?thesis by simp next case False with assms have b: "b > 0" by simp have "j = i div b" if "real_of_int i \ a" "a < 1 + real_of_int i" "real_of_int j * real_of_int b \ a" "a < real_of_int b + real_of_int j * real_of_int b" for i j :: int proof - from that have "i < b + j * b" by (metis le_less_trans of_int_add of_int_less_iff of_int_mult) moreover have "j * b < 1 + i" proof - have "real_of_int (j * b) < real_of_int i + 1" using \a < 1 + real_of_int i\ \real_of_int j * real_of_int b \ a\ by force then show "j * b < 1 + i" by linarith qed ultimately have "(j - i div b) * b \ i mod b" "i mod b < ((j - i div b) + 1) * b" by (auto simp: field_simps) then have "(j - i div b) * b < 1 * b" "0 * b < ((j - i div b) + 1) * b" using pos_mod_bound [OF b, of i] pos_mod_sign [OF b, of i] by linarith+ then show ?thesis using b unfolding mult_less_cancel_right by auto qed with b show ?thesis by (auto split: floor_split simp: field_simps) qed lemma floor_one_divide_eq_div_numeral [simp]: "\1 / numeral b::real\ = 1 div numeral b" by (metis floor_divide_of_int_eq of_int_1 of_int_numeral) lemma floor_minus_one_divide_eq_div_numeral [simp]: "\- (1 / numeral b)::real\ = - 1 div numeral b" by (metis (mono_tags, hide_lams) div_minus_right minus_divide_right floor_divide_of_int_eq of_int_neg_numeral of_int_1) lemma floor_divide_eq_div_numeral [simp]: "\numeral a / numeral b::real\ = numeral a div numeral b" by (metis floor_divide_of_int_eq of_int_numeral) lemma floor_minus_divide_eq_div_numeral [simp]: "\- (numeral a / numeral b)::real\ = - numeral a div numeral b" by (metis divide_minus_left floor_divide_of_int_eq of_int_neg_numeral of_int_numeral) lemma of_int_ceiling_cancel [simp]: "of_int \x\ = x \ (\n::int. x = of_int n)" using ceiling_of_int by metis lemma ceiling_eq: "of_int n < x \ x \ of_int n + 1 \ \x\ = n + 1" by (simp add: ceiling_unique) lemma of_int_ceiling_diff_one_le [simp]: "of_int \r\ - 1 \ r" by linarith lemma of_int_ceiling_le_add_one [simp]: "of_int \r\ \ r + 1" by linarith lemma ceiling_le: "x \ of_int a \ \x\ \ a" by (simp add: ceiling_le_iff) lemma ceiling_divide_eq_div: "\of_int a / of_int b\ = - (- a div b)" by (metis ceiling_def floor_divide_of_int_eq minus_divide_left of_int_minus) lemma ceiling_divide_eq_div_numeral [simp]: "\numeral a / numeral b :: real\ = - (- numeral a div numeral b)" using ceiling_divide_eq_div[of "numeral a" "numeral b"] by simp lemma ceiling_minus_divide_eq_div_numeral [simp]: "\- (numeral a / numeral b :: real)\ = - (numeral a div numeral b)" using ceiling_divide_eq_div[of "- numeral a" "numeral b"] by simp text \ The following lemmas are remnants of the erstwhile functions natfloor and natceiling. \ lemma nat_floor_neg: "x \ 0 \ nat \x\ = 0" for x :: real by linarith lemma le_nat_floor: "real x \ a \ x \ nat \a\" by linarith lemma le_mult_nat_floor: "nat \a\ * nat \b\ \ nat \a * b\" by (cases "0 \ a \ 0 \ b") (auto simp add: nat_mult_distrib[symmetric] nat_mono le_mult_floor) lemma nat_ceiling_le_eq [simp]: "nat \x\ \ a \ x \ real a" by linarith lemma real_nat_ceiling_ge: "x \ real (nat \x\)" by linarith lemma Rats_no_top_le: "\q \ \. x \ q" for x :: real by (auto intro!: bexI[of _ "of_nat (nat \x\)"]) linarith lemma Rats_no_bot_less: "\q \ \. q < x" for x :: real by (auto intro!: bexI[of _ "of_int (\x\ - 1)"]) linarith subsection \Exponentiation with floor\ lemma floor_power: assumes "x = of_int \x\" shows "\x ^ n\ = \x\ ^ n" proof - have "x ^ n = of_int (\x\ ^ n)" using assms by (induct n arbitrary: x) simp_all then show ?thesis by (metis floor_of_int) qed lemma floor_numeral_power [simp]: "\numeral x ^ n\ = numeral x ^ n" by (metis floor_of_int of_int_numeral of_int_power) lemma ceiling_numeral_power [simp]: "\numeral x ^ n\ = numeral x ^ n" by (metis ceiling_of_int of_int_numeral of_int_power) subsection \Implementation of rational real numbers\ text \Formal constructor\ definition Ratreal :: "rat \ real" where [code_abbrev, simp]: "Ratreal = real_of_rat" code_datatype Ratreal text \Quasi-Numerals\ lemma [code_abbrev]: "real_of_rat (numeral k) = numeral k" "real_of_rat (- numeral k) = - numeral k" "real_of_rat (rat_of_int a) = real_of_int a" by simp_all lemma [code_post]: "real_of_rat 0 = 0" "real_of_rat 1 = 1" "real_of_rat (- 1) = - 1" "real_of_rat (1 / numeral k) = 1 / numeral k" "real_of_rat (numeral k / numeral l) = numeral k / numeral l" "real_of_rat (- (1 / numeral k)) = - (1 / numeral k)" "real_of_rat (- (numeral k / numeral l)) = - (numeral k / numeral l)" by (simp_all add: of_rat_divide of_rat_minus) text \Operations\ lemma zero_real_code [code]: "0 = Ratreal 0" by simp lemma one_real_code [code]: "1 = Ratreal 1" by simp instantiation real :: equal begin definition "HOL.equal x y \ x - y = 0" for x :: real instance by standard (simp add: equal_real_def) lemma real_equal_code [code]: "HOL.equal (Ratreal x) (Ratreal y) \ HOL.equal x y" by (simp add: equal_real_def equal) lemma [code nbe]: "HOL.equal x x \ True" for x :: real by (rule equal_refl) end lemma real_less_eq_code [code]: "Ratreal x \ Ratreal y \ x \ y" by (simp add: of_rat_less_eq) lemma real_less_code [code]: "Ratreal x < Ratreal y \ x < y" by (simp add: of_rat_less) lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)" by (simp add: of_rat_add) lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)" by (simp add: of_rat_mult) lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)" by (simp add: of_rat_minus) lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)" by (simp add: of_rat_diff) lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)" by (simp add: of_rat_inverse) lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)" by (simp add: of_rat_divide) lemma real_floor_code [code]: "\Ratreal x\ = \x\" by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff of_int_floor_le of_rat_of_int_eq real_less_eq_code) text \Quickcheck\ definition (in term_syntax) valterm_ratreal :: "rat \ (unit \ Code_Evaluation.term) \ real \ (unit \ Code_Evaluation.term)" where [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\} k" notation fcomp (infixl "\>" 60) notation scomp (infixl "\\" 60) instantiation real :: random begin definition "Quickcheck_Random.random i = Quickcheck_Random.random i \\ (\r. Pair (valterm_ratreal r))" instance .. end no_notation fcomp (infixl "\>" 60) no_notation scomp (infixl "\\" 60) instantiation real :: exhaustive begin definition "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (\r. f (Ratreal r)) d" instance .. end instantiation real :: full_exhaustive begin definition "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (\r. f (valterm_ratreal r)) d" instance .. end instantiation real :: narrowing begin definition "narrowing_real = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing" instance .. end subsection \Setup for Nitpick\ declaration \ Nitpick_HOL.register_frac_type \<^type_name>\real\ [(\<^const_name>\zero_real_inst.zero_real\, \<^const_name>\Nitpick.zero_frac\), (\<^const_name>\one_real_inst.one_real\, \<^const_name>\Nitpick.one_frac\), (\<^const_name>\plus_real_inst.plus_real\, \<^const_name>\Nitpick.plus_frac\), (\<^const_name>\times_real_inst.times_real\, \<^const_name>\Nitpick.times_frac\), (\<^const_name>\uminus_real_inst.uminus_real\, \<^const_name>\Nitpick.uminus_frac\), (\<^const_name>\inverse_real_inst.inverse_real\, \<^const_name>\Nitpick.inverse_frac\), (\<^const_name>\ord_real_inst.less_real\, \<^const_name>\Nitpick.less_frac\), (\<^const_name>\ord_real_inst.less_eq_real\, \<^const_name>\Nitpick.less_eq_frac\)] \ lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real times_real_inst.times_real uminus_real_inst.uminus_real zero_real_inst.zero_real subsection \Setup for SMT\ ML_file \Tools/SMT/smt_real.ML\ ML_file \Tools/SMT/z3_real.ML\ lemma [z3_rule]: "0 + x = x" "x + 0 = x" "0 * x = 0" "1 * x = x" "-x = -1 * x" "x + y = y + x" for x y :: real by auto subsection \Setup for Argo\ ML_file \Tools/Argo/argo_real.ML\ end