# Theory Cartesian_Euclidean_Space

theory Cartesian_Euclidean_Space
imports Cartesian_Space Derivative
```(* Title:      HOL/Analysis/Cartesian_Euclidean_Space.thy
Some material by Jose DivasÃ³n, Tim Makarios and L C Paulson
*)

section ‹Instantiates the finite Cartesian product of Euclidean spaces as a Euclidean space›

theory Cartesian_Euclidean_Space
imports Cartesian_Space Derivative
begin

lemma subspace_special_hyperplane: "subspace {x. x \$ k = 0}"

lemma sum_mult_product:
"sum h {..<A * B :: nat} = (∑i∈{..<A}. ∑j∈{..<B}. h (j + i * B))"
unfolding sum_nat_group[of h B A, unfolded atLeast0LessThan, symmetric]
proof (rule sum.cong, simp, rule sum.reindex_cong)
fix i
show "inj_on (λj. j + i * B) {..<B}" by (auto intro!: inj_onI)
show "{i * B..<i * B + B} = (λj. j + i * B) ` {..<B}"
proof safe
fix j assume "j ∈ {i * B..<i * B + B}"
then show "j ∈ (λj. j + i * B) ` {..<B}"
by (auto intro!: image_eqI[of _ _ "j - i * B"])
qed simp
qed simp

lemma interval_cbox_cart: "{a::real^'n..b} = cbox a b"
by (auto simp add: less_eq_vec_def mem_box Basis_vec_def inner_axis)

lemma differentiable_vec:
fixes S :: "'a::euclidean_space set"
shows "vec differentiable_on S"

lemma continuous_vec [continuous_intros]:
fixes x :: "'a::euclidean_space"
shows "isCont vec x"
apply (clarsimp simp add: continuous_def LIM_def dist_vec_def L2_set_def)
apply (rule_tac x="r / sqrt (real CARD('b))" in exI)
by (simp add: mult.commute pos_less_divide_eq real_sqrt_mult)

lemma box_vec_eq_empty [simp]:
shows "cbox (vec a) (vec b) = {} ⟷ cbox a b = {}"
"box (vec a) (vec b) = {} ⟷ box a b = {}"
by (auto simp: Basis_vec_def mem_box box_eq_empty inner_axis)

subsection‹Closures and interiors of halfspaces›

lemma interior_halfspace_le [simp]:
assumes "a ≠ 0"
shows "interior {x. a ∙ x ≤ b} = {x. a ∙ x < b}"
proof -
have *: "a ∙ x < b" if x: "x ∈ S" and S: "S ⊆ {x. a ∙ x ≤ b}" and "open S" for S x
proof -
obtain e where "e>0" and e: "cball x e ⊆ S"
using ‹open S› open_contains_cball x by blast
then have "x + (e / norm a) *⇩R a ∈ cball x e"
then have "x + (e / norm a) *⇩R a ∈ S"
using e by blast
then have "x + (e / norm a) *⇩R a ∈ {x. a ∙ x ≤ b}"
using S by blast
moreover have "e * (a ∙ a) / norm a > 0"
by (simp add: ‹0 < e› assms)
ultimately show ?thesis
qed
show ?thesis
by (rule interior_unique) (auto simp: open_halfspace_lt *)
qed

lemma interior_halfspace_ge [simp]:
"a ≠ 0 ⟹ interior {x. a ∙ x ≥ b} = {x. a ∙ x > b}"
using interior_halfspace_le [of "-a" "-b"] by simp

lemma interior_halfspace_component_le [simp]:
"interior {x. x\$k ≤ a} = {x :: (real^'n). x\$k < a}" (is "?LE")
and interior_halfspace_component_ge [simp]:
"interior {x. x\$k ≥ a} = {x :: (real^'n). x\$k > a}" (is "?GE")
proof -
have "axis k (1::real) ≠ 0"
moreover have "axis k (1::real) ∙ x = x\$k" for x
ultimately show ?LE ?GE
using interior_halfspace_le [of "axis k (1::real)" a]
interior_halfspace_ge [of "axis k (1::real)" a] by auto
qed

lemma closure_halfspace_lt [simp]:
assumes "a ≠ 0"
shows "closure {x. a ∙ x < b} = {x. a ∙ x ≤ b}"
proof -
have [simp]: "-{x. a ∙ x < b} = {x. a ∙ x ≥ b}"
by (force simp:)
then show ?thesis
using interior_halfspace_ge [of a b] assms
by (force simp: closure_interior)
qed

lemma closure_halfspace_gt [simp]:
"a ≠ 0 ⟹ closure {x. a ∙ x > b} = {x. a ∙ x ≥ b}"
using closure_halfspace_lt [of "-a" "-b"] by simp

lemma closure_halfspace_component_lt [simp]:
"closure {x. x\$k < a} = {x :: (real^'n). x\$k ≤ a}" (is "?LE")
and closure_halfspace_component_gt [simp]:
"closure {x. x\$k > a} = {x :: (real^'n). x\$k ≥ a}" (is "?GE")
proof -
have "axis k (1::real) ≠ 0"
moreover have "axis k (1::real) ∙ x = x\$k" for x
ultimately show ?LE ?GE
using closure_halfspace_lt [of "axis k (1::real)" a]
closure_halfspace_gt [of "axis k (1::real)" a] by auto
qed

lemma interior_hyperplane [simp]:
assumes "a ≠ 0"
shows "interior {x. a ∙ x = b} = {}"
proof -
have [simp]: "{x. a ∙ x = b} = {x. a ∙ x ≤ b} ∩ {x. a ∙ x ≥ b}"
by (force simp:)
then show ?thesis
by (auto simp: assms)
qed

lemma frontier_halfspace_le:
assumes "a ≠ 0 ∨ b ≠ 0"
shows "frontier {x. a ∙ x ≤ b} = {x. a ∙ x = b}"
proof (cases "a = 0")
case True with assms show ?thesis by simp
next
case False then show ?thesis
by (force simp: frontier_def closed_halfspace_le)
qed

lemma frontier_halfspace_ge:
assumes "a ≠ 0 ∨ b ≠ 0"
shows "frontier {x. a ∙ x ≥ b} = {x. a ∙ x = b}"
proof (cases "a = 0")
case True with assms show ?thesis by simp
next
case False then show ?thesis
by (force simp: frontier_def closed_halfspace_ge)
qed

lemma frontier_halfspace_lt:
assumes "a ≠ 0 ∨ b ≠ 0"
shows "frontier {x. a ∙ x < b} = {x. a ∙ x = b}"
proof (cases "a = 0")
case True with assms show ?thesis by simp
next
case False then show ?thesis
by (force simp: frontier_def interior_open open_halfspace_lt)
qed

lemma frontier_halfspace_gt:
assumes "a ≠ 0 ∨ b ≠ 0"
shows "frontier {x. a ∙ x > b} = {x. a ∙ x = b}"
proof (cases "a = 0")
case True with assms show ?thesis by simp
next
case False then show ?thesis
by (force simp: frontier_def interior_open open_halfspace_gt)
qed

lemma interior_standard_hyperplane:
"interior {x :: (real^'n). x\$k = a} = {}"
proof -
have "axis k (1::real) ≠ 0"
moreover have "axis k (1::real) ∙ x = x\$k" for x
ultimately show ?thesis
using interior_hyperplane [of "axis k (1::real)" a]
by force
qed

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)

text‹Two sometimes fruitful ways of looking at matrix-vector multiplication.›

lemma matrix_mult_dot: "A *v x = (χ i. inner (A\$i) x)"

lemma adjoint_matrix: "adjoint(λx. (A::real^'n^'m) *v x) = (λx. transpose A *v x)"
apply (simp add: transpose_def inner_vec_def matrix_vector_mult_def
sum_distrib_right sum_distrib_left)
apply (subst sum.swap)
done

lemma matrix_adjoint: assumes lf: "linear (f :: real^'n ⇒ real ^'m)"
shows "matrix(adjoint f) = transpose(matrix f)"
proof -
also have "… = transpose(matrix f)"
apply rule
done
finally show ?thesis .
qed

lemma matrix_vector_mul_bounded_linear[intro, simp]: "bounded_linear (( *v) A)" for A :: "'a::{euclidean_space,real_algebra_1}^'n^'m"
using matrix_vector_mul_linear[of A]

lemma
fixes A :: "'a::{euclidean_space,real_algebra_1}^'n^'m"
shows matrix_vector_mult_linear_continuous_at [continuous_intros]: "isCont (( *v) A) z"
and matrix_vector_mult_linear_continuous_on [continuous_intros]: "continuous_on S (( *v) A)"

lemma scalar_invertible:
fixes A :: "('a::real_algebra_1)^'m^'n"
assumes "k ≠ 0" and "invertible A"
shows "invertible (k *⇩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 *⇩R A) ** ((1/k) *⇩R A') = mat 1" "((1/k) *⇩R A') ** (k *⇩R A) = mat 1"
thus "invertible (k *⇩R A)"
unfolding invertible_def by auto
qed

lemma scalar_invertible_iff:
fixes A :: "('a::real_algebra_1)^'m^'n"
assumes "k ≠ 0" and "invertible A"
shows "invertible (k *⇩R A) ⟷ k ≠ 0 ∧ invertible A"

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 *⇩R x) v* A = k *⇩R (x v* A)"
by (metis matrix_vector_mult_scaleR transpose_matrix_vector)

lemma vector_scaleR_matrix_ac:
fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
shows "x v* (k *⇩R A) = k *⇩R (x v* A)"
proof -
have "x v* (k *⇩R A) = (k *⇩R x) v* A"
unfolding vector_matrix_mult_def
with scaleR_vector_matrix_assoc
show "x v* (k *⇩R A) = k *⇩R (x v* A)"
by auto
qed

subsection‹Some bounds on components etc. relative to operator norm›

lemma norm_column_le_onorm:
fixes A :: "real^'n^'m"
shows "norm(column i A) ≤ onorm(( *v) A)"
proof -
have "norm (χ j. A \$ j \$ i) ≤ norm (A *v axis i 1)"
also have "… ≤ onorm (( *v) A)"
using onorm [OF matrix_vector_mul_bounded_linear, of A "axis i 1"] by auto
finally have "norm (χ j. A \$ j \$ i) ≤ onorm (( *v) A)" .
then show ?thesis
unfolding column_def .
qed

lemma matrix_component_le_onorm:
fixes A :: "real^'n^'m"
shows "¦A \$ i \$ j¦ ≤ onorm(( *v) A)"
proof -
have "¦A \$ i \$ j¦ ≤ norm (χ n. (A \$ n \$ j))"
by (metis (full_types, lifting) component_le_norm_cart vec_lambda_beta)
also have "… ≤ onorm (( *v) A)"
by (metis (no_types) column_def norm_column_le_onorm)
finally show ?thesis .
qed

lemma component_le_onorm:
fixes f :: "real^'m ⇒ real^'n"
shows "linear f ⟹ ¦matrix f \$ i \$ j¦ ≤ onorm f"
by (metis linear_matrix_vector_mul_eq matrix_component_le_onorm matrix_vector_mul)

lemma onorm_le_matrix_component_sum:
fixes A :: "real^'n^'m"
shows "onorm(( *v) A) ≤ (∑i∈UNIV. ∑j∈UNIV. ¦A \$ i \$ j¦)"
proof (rule onorm_le)
fix x
have "norm (A *v x) ≤ (∑i∈UNIV. ¦(A *v x) \$ i¦)"
by (rule norm_le_l1_cart)
also have "… ≤ (∑i∈UNIV. ∑j∈UNIV. ¦A \$ i \$ j¦ * norm x)"
proof (rule sum_mono)
fix i
have "¦(A *v x) \$ i¦ ≤ ¦∑j∈UNIV. A \$ i \$ j * x \$ j¦"
also have "… ≤ (∑j∈UNIV. ¦A \$ i \$ j * x \$ j¦)"
by (rule sum_abs)
also have "… ≤ (∑j∈UNIV. ¦A \$ i \$ j¦ * norm x)"
by (rule sum_mono) (simp add: abs_mult component_le_norm_cart mult_left_mono)
finally show "¦(A *v x) \$ i¦ ≤ (∑j∈UNIV. ¦A \$ i \$ j¦ * norm x)" .
qed
finally show "norm (A *v x) ≤ (∑i∈UNIV. ∑j∈UNIV. ¦A \$ i \$ j¦) * norm x"
qed

lemma onorm_le_matrix_component:
fixes A :: "real^'n^'m"
assumes "⋀i j. abs(A\$i\$j) ≤ B"
shows "onorm(( *v) A) ≤ real (CARD('m)) * real (CARD('n)) * B"
proof (rule onorm_le)
fix x :: "real^'n::_"
have "norm (A *v x) ≤ (∑i∈UNIV. ¦(A *v x) \$ i¦)"
by (rule norm_le_l1_cart)
also have "… ≤ (∑i::'m ∈UNIV. real (CARD('n)) * B * norm x)"
proof (rule sum_mono)
fix i
have "¦(A *v x) \$ i¦ ≤ norm(A \$ i) * norm x"
also have "… ≤ (∑j∈UNIV. ¦A \$ i \$ j¦) * norm x"
also have "… ≤ real (CARD('n)) * B * norm x"
by (simp add: assms sum_bounded_above mult_right_mono)
finally show "¦(A *v x) \$ i¦ ≤ real (CARD('n)) * B * norm x" .
qed
also have "… ≤ CARD('m) * real (CARD('n)) * B * norm x"
by simp
finally show "norm (A *v x) ≤ CARD('m) * real (CARD('n)) * B * norm x" .
qed

subsection ‹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

lemma rational_approximation:
assumes "e > 0"
obtains r::real where "r ∈ ℚ" "¦r - x¦ < e"
using Rats_dense_in_real [of "x - e/2" "x + e/2"] assms by auto

lemma matrix_rational_approximation:
fixes A :: "real^'n^'m"
assumes "e > 0"
obtains B where "⋀i j. B\$i\$j ∈ ℚ" "onorm(λx. (A - B) *v x) < e"
proof -
have "∀i j. ∃q ∈ ℚ. ¦q - A \$ i \$ j¦ < e / (2 * CARD('m) * CARD('n))"
using assms by (force intro: rational_approximation [of "e / (2 * CARD('m) * CARD('n))"])
then obtain B where B: "⋀i j. B\$i\$j ∈ ℚ" and Bclo: "⋀i j. ¦B\$i\$j - A \$ i \$ j¦ < e / (2 * CARD('m) * CARD('n))"
by (auto simp: lambda_skolem Bex_def)
show ?thesis
proof
have "onorm (( *v) (A - B)) ≤ real CARD('m) * real CARD('n) *
(e / (2 * real CARD('m) * real CARD('n)))"
apply (rule onorm_le_matrix_component)
using Bclo by (simp add: abs_minus_commute less_imp_le)
also have "… < e"
using ‹0 < e› by (simp add: divide_simps)
finally show "onorm (( *v) (A - B)) < e" .
qed (use B in auto)
qed

lemma vector_sub_project_orthogonal_cart: "(b::real^'n) ∙ (x - ((b ∙ x) / (b ∙ b)) *s b) = 0"
unfolding inner_simps scalar_mult_eq_scaleR by auto

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 infnorm_cart:"infnorm (x::real^'n) = Sup {¦x\$i¦ |i. i∈UNIV}"
by (simp add: infnorm_def inner_axis Basis_vec_def) (metis (lifting) inner_axis real_inner_1_right)

lemma component_le_infnorm_cart: "¦x\$i¦ ≤ infnorm (x::real^'n)"
using Basis_le_infnorm[of "axis i 1" x]
by (simp add: Basis_vec_def axis_eq_axis inner_axis)

lemma continuous_component[continuous_intros]: "continuous F f ⟹ continuous F (λx. f x \$ i)"
unfolding continuous_def by (rule tendsto_vec_nth)

lemma continuous_on_component[continuous_intros]: "continuous_on s f ⟹ continuous_on s (λx. f x \$ i)"
unfolding continuous_on_def by (fast intro: tendsto_vec_nth)

lemma continuous_on_vec_lambda[continuous_intros]:
"(⋀i. continuous_on S (f i)) ⟹ continuous_on S (λx. χ i. f i x)"
unfolding continuous_on_def by (auto intro: tendsto_vec_lambda)

lemma closed_positive_orthant: "closed {x::real^'n. ∀i. 0 ≤x\$i}"
by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)

lemma bounded_component_cart: "bounded s ⟹ bounded ((λx. x \$ i) ` s)"
unfolding bounded_def
apply clarify
apply (rule_tac x="x \$ i" in exI)
apply (rule_tac x="e" in exI)
apply clarify
apply (rule order_trans [OF dist_vec_nth_le], simp)
done

lemma compact_lemma_cart:
fixes f :: "nat ⇒ 'a::heine_borel ^ 'n"
assumes f: "bounded (range f)"
shows "∃l r. strict_mono r ∧
(∀e>0. eventually (λn. ∀i∈d. dist (f (r n) \$ i) (l \$ i) < e) sequentially)"
(is "?th d")
proof -
have "∀d' ⊆ d. ?th d'"
by (rule compact_lemma_general[where unproj=vec_lambda])
(auto intro!: f bounded_component_cart simp: vec_lambda_eta)
then show "?th d" by simp
qed

instance vec :: (heine_borel, finite) heine_borel
proof
fix f :: "nat ⇒ 'a ^ 'b"
assume f: "bounded (range f)"
then obtain l r where r: "strict_mono r"
and l: "∀e>0. eventually (λn. ∀i∈UNIV. dist (f (r n) \$ i) (l \$ i) < e) sequentially"
using compact_lemma_cart [OF f] by blast
let ?d = "UNIV::'b set"
{ fix e::real assume "e>0"
hence "0 < e / (real_of_nat (card ?d))"
using zero_less_card_finite divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
with l have "eventually (λn. ∀i. dist (f (r n) \$ i) (l \$ i) < e / (real_of_nat (card ?d))) sequentially"
by simp
moreover
{ fix n
assume n: "∀i. dist (f (r n) \$ i) (l \$ i) < e / (real_of_nat (card ?d))"
have "dist (f (r n)) l ≤ (∑i∈?d. dist (f (r n) \$ i) (l \$ i))"
unfolding dist_vec_def using zero_le_dist by (rule L2_set_le_sum)
also have "… < (∑i∈?d. e / (real_of_nat (card ?d)))"
by (rule sum_strict_mono) (simp_all add: n)
finally have "dist (f (r n)) l < e" by simp
}
ultimately have "eventually (λn. dist (f (r n)) l < e) sequentially"
by (rule eventually_mono)
}
hence "((f ∘ r) ⤏ l) sequentially" unfolding o_def tendsto_iff by simp
with r show "∃l r. strict_mono r ∧ ((f ∘ r) ⤏ l) sequentially" by auto
qed

lemma interval_cart:
fixes a :: "real^'n"
shows "box a b = {x::real^'n. ∀i. a\$i < x\$i ∧ x\$i < b\$i}"
and "cbox a b = {x::real^'n. ∀i. a\$i ≤ x\$i ∧ x\$i ≤ b\$i}"
by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def mem_box Basis_vec_def inner_axis)

lemma mem_box_cart:
fixes a :: "real^'n"
shows "x ∈ box a b ⟷ (∀i. a\$i < x\$i ∧ x\$i < b\$i)"
and "x ∈ cbox a b ⟷ (∀i. a\$i ≤ x\$i ∧ x\$i ≤ b\$i)"
using interval_cart[of a b] by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def)

lemma interval_eq_empty_cart:
fixes a :: "real^'n"
shows "(box a b = {} ⟷ (∃i. b\$i ≤ a\$i))" (is ?th1)
and "(cbox a b = {} ⟷ (∃i. b\$i < a\$i))" (is ?th2)
proof -
{ fix i x assume as:"b\$i ≤ a\$i" and x:"x∈box a b"
hence "a \$ i < x \$ i ∧ x \$ i < b \$ i" unfolding mem_box_cart by auto
hence "a\$i < b\$i" by auto
hence False using as by auto }
moreover
{ assume as:"∀i. ¬ (b\$i ≤ a\$i)"
let ?x = "(1/2) *⇩R (a + b)"
{ fix i
have "a\$i < b\$i" using as[THEN spec[where x=i]] by auto
hence "a\$i < ((1/2) *⇩R (a+b)) \$ i" "((1/2) *⇩R (a+b)) \$ i < b\$i"
by auto }
hence "box a b ≠ {}" using mem_box_cart(1)[of "?x" a b] by auto }
ultimately show ?th1 by blast

{ fix i x assume as:"b\$i < a\$i" and x:"x∈cbox a b"
hence "a \$ i ≤ x \$ i ∧ x \$ i ≤ b \$ i" unfolding mem_box_cart by auto
hence "a\$i ≤ b\$i" by auto
hence False using as by auto }
moreover
{ assume as:"∀i. ¬ (b\$i < a\$i)"
let ?x = "(1/2) *⇩R (a + b)"
{ fix i
have "a\$i ≤ b\$i" using as[THEN spec[where x=i]] by auto
hence "a\$i ≤ ((1/2) *⇩R (a+b)) \$ i" "((1/2) *⇩R (a+b)) \$ i ≤ b\$i"
by auto }
hence "cbox a b ≠ {}" using mem_box_cart(2)[of "?x" a b] by auto  }
ultimately show ?th2 by blast
qed

lemma interval_ne_empty_cart:
fixes a :: "real^'n"
shows "cbox a b ≠ {} ⟷ (∀i. a\$i ≤ b\$i)"
and "box a b ≠ {} ⟷ (∀i. a\$i < b\$i)"
unfolding interval_eq_empty_cart[of a b] by (auto simp add: not_less not_le)
(* BH: Why doesn't just "auto" work here? *)

lemma subset_interval_imp_cart:
fixes a :: "real^'n"
shows "(∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i) ⟹ cbox c d ⊆ cbox a b"
and "(∀i. a\$i < c\$i ∧ d\$i < b\$i) ⟹ cbox c d ⊆ box a b"
and "(∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i) ⟹ box c d ⊆ cbox a b"
and "(∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i) ⟹ box c d ⊆ box a b"
unfolding subset_eq[unfolded Ball_def] unfolding mem_box_cart
by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)

lemma interval_sing:
fixes a :: "'a::linorder^'n"
shows "{a .. a} = {a} ∧ {a<..<a} = {}"
apply (auto simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
done

lemma subset_interval_cart:
fixes a :: "real^'n"
shows "cbox c d ⊆ cbox a b ⟷ (∀i. c\$i ≤ d\$i) --> (∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i)" (is ?th1)
and "cbox c d ⊆ box a b ⟷ (∀i. c\$i ≤ d\$i) --> (∀i. a\$i < c\$i ∧ d\$i < b\$i)" (is ?th2)
and "box c d ⊆ cbox a b ⟷ (∀i. c\$i < d\$i) --> (∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i)" (is ?th3)
and "box c d ⊆ box a b ⟷ (∀i. c\$i < d\$i) --> (∀i. a\$i ≤ c\$i ∧ d\$i ≤ b\$i)" (is ?th4)
using subset_box[of c d a b] by (simp_all add: Basis_vec_def inner_axis)

lemma disjoint_interval_cart:
fixes a::"real^'n"
shows "cbox a b ∩ cbox c d = {} ⟷ (∃i. (b\$i < a\$i ∨ d\$i < c\$i ∨ b\$i < c\$i ∨ d\$i < a\$i))" (is ?th1)
and "cbox a b ∩ box c d = {} ⟷ (∃i. (b\$i < a\$i ∨ d\$i ≤ c\$i ∨ b\$i ≤ c\$i ∨ d\$i ≤ a\$i))" (is ?th2)
and "box a b ∩ cbox c d = {} ⟷ (∃i. (b\$i ≤ a\$i ∨ d\$i < c\$i ∨ b\$i ≤ c\$i ∨ d\$i ≤ a\$i))" (is ?th3)
and "box a b ∩ box c d = {} ⟷ (∃i. (b\$i ≤ a\$i ∨ d\$i ≤ c\$i ∨ b\$i ≤ c\$i ∨ d\$i ≤ a\$i))" (is ?th4)
using disjoint_interval[of a b c d] by (simp_all add: Basis_vec_def inner_axis)

lemma Int_interval_cart:
fixes a :: "real^'n"
shows "cbox a b ∩ cbox c d =  {(χ i. max (a\$i) (c\$i)) .. (χ i. min (b\$i) (d\$i))}"
unfolding Int_interval
by (auto simp: mem_box less_eq_vec_def)
(auto simp: Basis_vec_def inner_axis)

lemma closed_interval_left_cart:
fixes b :: "real^'n"
shows "closed {x::real^'n. ∀i. x\$i ≤ b\$i}"
by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)

lemma closed_interval_right_cart:
fixes a::"real^'n"
shows "closed {x::real^'n. ∀i. a\$i ≤ x\$i}"
by (simp add: Collect_all_eq closed_INT closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)

lemma is_interval_cart:
"is_interval (s::(real^'n) set) ⟷
(∀a∈s. ∀b∈s. ∀x. (∀i. ((a\$i ≤ x\$i ∧ x\$i ≤ b\$i) ∨ (b\$i ≤ x\$i ∧ x\$i ≤ a\$i))) ⟶ x ∈ s)"
by (simp add: is_interval_def Ball_def Basis_vec_def inner_axis imp_ex)

lemma closed_halfspace_component_le_cart: "closed {x::real^'n. x\$i ≤ a}"
by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)

lemma closed_halfspace_component_ge_cart: "closed {x::real^'n. x\$i ≥ a}"
by (simp add: closed_Collect_le continuous_on_const continuous_on_id continuous_on_component)

lemma open_halfspace_component_lt_cart: "open {x::real^'n. x\$i < a}"
by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)

lemma open_halfspace_component_gt_cart: "open {x::real^'n. x\$i  > a}"
by (simp add: open_Collect_less continuous_on_const continuous_on_id continuous_on_component)

lemma Lim_component_le_cart:
fixes f :: "'a ⇒ real^'n"
assumes "(f ⤏ l) net" "¬ (trivial_limit net)"  "eventually (λx. f x \$i ≤ b) net"
shows "l\$i ≤ b"
by (rule tendsto_le[OF assms(2) tendsto_const tendsto_vec_nth, OF assms(1, 3)])

lemma Lim_component_ge_cart:
fixes f :: "'a ⇒ real^'n"
assumes "(f ⤏ l) net"  "¬ (trivial_limit net)"  "eventually (λx. b ≤ (f x)\$i) net"
shows "b ≤ l\$i"
by (rule tendsto_le[OF assms(2) tendsto_vec_nth tendsto_const, OF assms(1, 3)])

lemma Lim_component_eq_cart:
fixes f :: "'a ⇒ real^'n"
assumes net: "(f ⤏ l) net" "~(trivial_limit net)" and ev:"eventually (λx. f(x)\$i = b) net"
shows "l\$i = b"
using ev[unfolded order_eq_iff eventually_conj_iff] and
Lim_component_ge_cart[OF net, of b i] and
Lim_component_le_cart[OF net, of i b] by auto

lemma connected_ivt_component_cart:
fixes x :: "real^'n"
shows "connected s ⟹ x ∈ s ⟹ y ∈ s ⟹ x\$k ≤ a ⟹ a ≤ y\$k ⟹ (∃z∈s.  z\$k = a)"
using connected_ivt_hyperplane[of s x y "axis k 1" a]
by (auto simp add: inner_axis inner_commute)

lemma subspace_substandard_cart: "vec.subspace {x. (∀i. P i ⟶ x\$i = 0)}"
unfolding vec.subspace_def by auto

lemma closed_substandard_cart:
"closed {x::'a::real_normed_vector ^ 'n. ∀i. P i ⟶ x\$i = 0}"
proof -
{ fix i::'n
have "closed {x::'a ^ 'n. P i ⟶ x\$i = 0}"
by (cases "P i") (simp_all add: closed_Collect_eq continuous_on_const continuous_on_id continuous_on_component) }
thus ?thesis
unfolding Collect_all_eq by (simp add: closed_INT)
qed

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"
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 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)

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

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 *⇩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 *⇩R i)"
by (rule vector_cart)

subsection "Convex Euclidean Space"

lemma Cart_1:"(1::real^'n) = ∑Basis"
using const_vector_cart[of 1] by (simp add: one_vec_def)

lemmas vector_component_simps = vector_minus_component vector_smult_component vector_add_component less_eq_vec_def vec_lambda_beta vector_uminus_component

lemma convex_box_cart:
assumes "⋀i. convex {x. P i x}"
shows "convex {x. ∀i. P i (x\$i)}"
using assms unfolding convex_def by auto

lemma convex_positive_orthant_cart: "convex {x::real^'n. (∀i. 0 ≤ x\$i)}"
by (rule convex_box_cart) (simp add: atLeast_def[symmetric])

lemma unit_interval_convex_hull_cart:
"cbox (0::real^'n) 1 = convex hull {x. ∀i. (x\$i = 0) ∨ (x\$i = 1)}"
unfolding Cart_1 unit_interval_convex_hull[where 'a="real^'n"] box_real[symmetric]
by (rule arg_cong[where f="λx. convex hull x"]) (simp add: Basis_vec_def inner_axis)

lemma cube_convex_hull_cart:
assumes "0 < d"
obtains s::"(real^'n) set"
where "finite s" "cbox (x - (χ i. d)) (x + (χ i. d)) = convex hull s"
proof -
from assms obtain s where "finite s"
and "cbox (x - sum (( *⇩R) d) Basis) (x + sum (( *⇩R) d) Basis) = convex hull s"
by (rule cube_convex_hull)
with that[of s] show thesis
qed

subsection "Derivative"

definition "jacobian f net = matrix(frechet_derivative f net)"

lemma jacobian_works:
"(f::(real^'a) ⇒ (real^'b)) differentiable net ⟷
(f has_derivative (λh. (jacobian f net) *v h)) net" (is "?lhs = ?rhs")
proof
assume ?lhs then show ?rhs
by (simp add: frechet_derivative_works has_derivative_linear jacobian_def)
next
assume ?rhs then show ?lhs
by (rule differentiableI)
qed

subsection ‹Component of the differential must be zero if it exists at a local
maximum or minimum for that corresponding component›

lemma differential_zero_maxmin_cart:
fixes f::"real^'a ⇒ real^'b"
assumes "0 < e" "((∀y ∈ ball x e. (f y)\$k ≤ (f x)\$k) ∨ (∀y∈ball x e. (f x)\$k ≤ (f y)\$k))"
"f differentiable (at x)"
shows "jacobian f (at x) \$ k = 0"
using differential_zero_maxmin_component[of "axis k 1" e x f] assms
vector_cart[of "λj. frechet_derivative f (at x) j \$ k"]
by (simp add: Basis_vec_def axis_eq_axis inner_axis jacobian_def matrix_def)

subsection ‹Lemmas for working on @{typ "real^1"}›

lemma forall_1[simp]: "(∀i::1. P i) ⟷ P 1"
by (metis (full_types) num1_eq_iff)

lemma ex_1[simp]: "(∃x::1. P x) ⟷ P 1"
by auto (metis (full_types) num1_eq_iff)

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 UNIV_1 [simp]: "UNIV = {1::1}"

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 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 num1_eqI:
fixes a::num1 shows "a = b"
by (metis (full_types) UNIV_1 UNIV_I empty_iff insert_iff)

lemma num1_eq1 [simp]:
fixes a::num1 shows "a = 1"
by (rule num1_eqI)

instantiation num1 :: cart_one
begin

instance
proof
show "CARD(1) = Suc 0" by auto
qed

end

instantiation num1 :: linorder begin
definition "a < b ⟷ Rep_num1 a < Rep_num1 b"
definition "a ≤ b ⟷ Rep_num1 a ≤ Rep_num1 b"
instance
by intro_classes (auto simp: less_eq_num1_def less_num1_def intro: num1_eqI)
end

instance num1 :: wellorder
by intro_classes (auto simp: less_eq_num1_def less_num1_def)

subsection‹The collapse of the general concepts to dimension one›

lemma vector_one: "(x::'a ^1) = (χ i. (x\$1))"

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)"

lemma dist_vector_1:
fixes x :: "'a::real_normed_vector^1"
shows "dist x y = dist (x\$1) (y\$1)"

lemma norm_real: "norm(x::real ^ 1) = ¦x\$1¦"

lemma dist_real: "dist(x::real ^ 1) y = ¦(x\$1) - (y\$1)¦"
by (auto simp add: norm_real dist_norm)

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 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
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 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)"
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
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)"

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"
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‹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 vec_cbox_1_eq [simp]:
shows "vec ` cbox u v = cbox (vec u) (vec v ::real^1)"
by (force simp: Basis_vec_def cart_eq_inner_axis [symmetric] mem_box)

lemma vec_nth_cbox_1_eq [simp]:
fixes u v :: "'a::euclidean_space^1"
shows "(λx. x \$ 1) ` cbox u v = cbox (u\$1) (v\$1)"
by (auto simp: Basis_vec_def cart_eq_inner_axis [symmetric] mem_box image_iff Bex_def inner_axis) (metis vec_component)

lemma vec_nth_1_iff_cbox [simp]:
fixes a b :: "'a::euclidean_space"
shows "(λx::'a^1. x \$ 1) ` S = cbox a b ⟷ S = cbox (vec a) (vec b)"
(is "?lhs = ?rhs")
proof
assume L: ?lhs show ?rhs
proof (intro equalityI subsetI)
fix x
assume "x ∈ S"
then have "x \$ 1 ∈ (λv. v \$ (1::1)) ` cbox (vec a) (vec b)"
using L by auto
then show "x ∈ cbox (vec a) (vec b)"
by (metis (no_types, lifting) imageE vector_one_nth)
next
fix x :: "'a^1"
assume "x ∈ cbox (vec a) (vec b)"
then show "x ∈ S"
by (metis (no_types, lifting) L imageE imageI vec_component vec_nth_cbox_1_eq vector_one_nth)
qed
qed simp

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 "∀⇩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 "∀⇩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 ( *⇩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‹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)
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)
done

lemma bounded_linear_component_cart[intro]: "bounded_linear (λx::real^'n. x \$ k)"
apply (rule bounded_linear_intro[where K=1])
using component_le_norm_cart[of _ k] unfolding real_norm_def by auto

lemma interval_split_cart:
"{a..b::real^'n} ∩ {x. x\$k ≤ c} = {a .. (χ i. if i = k then min (b\$k) c else b\$i)}"
"cbox a b ∩ {x. x\$k ≥ c} = {(χ i. if i = k then max (a\$k) c else a\$i) .. b}"
apply (rule_tac[!] set_eqI)
unfolding Int_iff mem_box_cart mem_Collect_eq interval_cbox_cart
unfolding vec_lambda_beta
by auto

lemmas cartesian_euclidean_space_uniform_limit_intros[uniform_limit_intros] =
bounded_linear.uniform_limit[OF blinfun.bounded_linear_right]
bounded_linear.uniform_limit[OF bounded_linear_vec_nth]
bounded_linear.uniform_limit[OF bounded_linear_component_cart]

end
```