src/HOL/Analysis/Change_Of_Vars.thy
 author nipkow Sat Dec 29 15:43:53 2018 +0100 (6 months ago) changeset 69529 4ab9657b3257 parent 69325 4b6ddc5989fc child 69661 a03a63b81f44 permissions -rw-r--r--
capitalize proper names in lemma names
```     1 (*  Title:      HOL/Analysis/Change_Of_Vars.thy
```
```     2     Authors:    LC Paulson, based on material from HOL Light
```
```     3 *)
```
```     4
```
```     5 section\<open>Change of Variables Theorems\<close>
```
```     6
```
```     7 theory Change_Of_Vars
```
```     8   imports Vitali_Covering_Theorem Determinants
```
```     9
```
```    10 begin
```
```    11
```
```    12 subsection%important\<open>Induction on matrix row operations\<close>
```
```    13
```
```    14 lemma%unimportant induct_matrix_row_operations:
```
```    15   fixes P :: "real^'n^'n \<Rightarrow> bool"
```
```    16   assumes zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
```
```    17     and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
```
```    18     and swap_cols: "\<And>A m n. \<lbrakk>P A; m \<noteq> n\<rbrakk> \<Longrightarrow> P(\<chi> i j. A \$ i \$ Fun.swap m n id j)"
```
```    19     and row_op: "\<And>A m n c. \<lbrakk>P A; m \<noteq> n\<rbrakk>
```
```    20                    \<Longrightarrow> P(\<chi> i. if i = m then row m A + c *\<^sub>R row n A else row i A)"
```
```    21   shows "P A"
```
```    22 proof -
```
```    23   have "P A" if "(\<And>i j. \<lbrakk>j \<in> -K;  i \<noteq> j\<rbrakk> \<Longrightarrow> A\$i\$j = 0)" for A K
```
```    24   proof -
```
```    25     have "finite K"
```
```    26       by simp
```
```    27     then show ?thesis using that
```
```    28     proof (induction arbitrary: A rule: finite_induct)
```
```    29       case empty
```
```    30       with diagonal show ?case
```
```    31         by simp
```
```    32     next
```
```    33       case (insert k K)
```
```    34       note insertK = insert
```
```    35       have "P A" if kk: "A\$k\$k \<noteq> 0"
```
```    36         and 0: "\<And>i j. \<lbrakk>j \<in> - insert k K; i \<noteq> j\<rbrakk> \<Longrightarrow> A\$i\$j = 0"
```
```    37                "\<And>i. \<lbrakk>i \<in> -L; i \<noteq> k\<rbrakk> \<Longrightarrow> A\$i\$k = 0" for A L
```
```    38       proof -
```
```    39         have "finite L"
```
```    40           by simp
```
```    41         then show ?thesis using 0 kk
```
```    42         proof (induction arbitrary: A rule: finite_induct)
```
```    43           case (empty B)
```
```    44           show ?case
```
```    45           proof (rule insertK)
```
```    46             fix i j
```
```    47             assume "i \<in> - K" "j \<noteq> i"
```
```    48             show "B \$ j \$ i = 0"
```
```    49               using \<open>j \<noteq> i\<close> \<open>i \<in> - K\<close> empty
```
```    50               by (metis ComplD ComplI Compl_eq_Diff_UNIV Diff_empty UNIV_I insert_iff)
```
```    51           qed
```
```    52         next
```
```    53           case (insert l L B)
```
```    54           show ?case
```
```    55           proof (cases "k = l")
```
```    56             case True
```
```    57             with insert show ?thesis
```
```    58               by auto
```
```    59           next
```
```    60             case False
```
```    61             let ?C = "\<chi> i. if i = l then row l B - (B \$ l \$ k / B \$ k \$ k) *\<^sub>R row k B else row i B"
```
```    62             have 1: "\<lbrakk>j \<in> - insert k K; i \<noteq> j\<rbrakk> \<Longrightarrow> ?C \$ i \$ j = 0" for j i
```
```    63               by (auto simp: insert.prems(1) row_def)
```
```    64             have 2: "?C \$ i \$ k = 0"
```
```    65               if "i \<in> - L" "i \<noteq> k" for i
```
```    66             proof (cases "i=l")
```
```    67               case True
```
```    68               with that insert.prems show ?thesis
```
```    69                 by (simp add: row_def)
```
```    70             next
```
```    71               case False
```
```    72               with that show ?thesis
```
```    73                 by (simp add: insert.prems(2) row_def)
```
```    74             qed
```
```    75             have 3: "?C \$ k \$ k \<noteq> 0"
```
```    76               by (auto simp: insert.prems row_def \<open>k \<noteq> l\<close>)
```
```    77             have PC: "P ?C"
```
```    78               using insert.IH [OF 1 2 3] by auto
```
```    79             have eqB: "(\<chi> i. if i = l then row l ?C + (B \$ l \$ k / B \$ k \$ k) *\<^sub>R row k ?C else row i ?C) = B"
```
```    80               using \<open>k \<noteq> l\<close> by (simp add: vec_eq_iff row_def)
```
```    81             show ?thesis
```
```    82               using row_op [OF PC, of l k, where c = "B\$l\$k / B\$k\$k"] eqB \<open>k \<noteq> l\<close>
```
```    83               by (simp add: cong: if_cong)
```
```    84           qed
```
```    85         qed
```
```    86       qed
```
```    87       then have nonzero_hyp: "P A"
```
```    88         if kk: "A\$k\$k \<noteq> 0" and zeroes: "\<And>i j. j \<in> - insert k K \<and> i\<noteq>j \<Longrightarrow> A\$i\$j = 0" for A
```
```    89         by (auto simp: intro!: kk zeroes)
```
```    90       show ?case
```
```    91       proof (cases "row k A = 0")
```
```    92         case True
```
```    93         with zero_row show ?thesis by auto
```
```    94       next
```
```    95         case False
```
```    96         then obtain l where l: "A\$k\$l \<noteq> 0"
```
```    97           by (auto simp: row_def zero_vec_def vec_eq_iff)
```
```    98         show ?thesis
```
```    99         proof (cases "k = l")
```
```   100           case True
```
```   101           with l nonzero_hyp insert.prems show ?thesis
```
```   102             by blast
```
```   103         next
```
```   104           case False
```
```   105           have *: "A \$ i \$ Fun.swap k l id j = 0" if "j \<noteq> k" "j \<notin> K" "i \<noteq> j" for i j
```
```   106             using False l insert.prems that
```
```   107             by (auto simp: swap_def insert split: if_split_asm)
```
```   108           have "P (\<chi> i j. (\<chi> i j. A \$ i \$ Fun.swap k l id j) \$ i \$ Fun.swap k l id j)"
```
```   109             by (rule swap_cols [OF nonzero_hyp False]) (auto simp: l *)
```
```   110           moreover
```
```   111           have "(\<chi> i j. (\<chi> i j. A \$ i \$ Fun.swap k l id j) \$ i \$ Fun.swap k l id j) = A"
```
```   112             by (metis (no_types, lifting) id_apply o_apply swap_id_idempotent vec_lambda_unique vec_lambda_unique)
```
```   113           ultimately show ?thesis
```
```   114             by simp
```
```   115         qed
```
```   116       qed
```
```   117     qed
```
```   118   qed
```
```   119   then show ?thesis
```
```   120     by blast
```
```   121 qed
```
```   122
```
```   123 lemma%unimportant induct_matrix_elementary:
```
```   124   fixes P :: "real^'n^'n \<Rightarrow> bool"
```
```   125   assumes mult: "\<And>A B. \<lbrakk>P A; P B\<rbrakk> \<Longrightarrow> P(A ** B)"
```
```   126     and zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
```
```   127     and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
```
```   128     and swap1: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
```
```   129     and idplus: "\<And>m n c. m \<noteq> n \<Longrightarrow> P(\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j))"
```
```   130   shows "P A"
```
```   131 proof -
```
```   132   have swap: "P (\<chi> i j. A \$ i \$ Fun.swap m n id j)"  (is "P ?C")
```
```   133     if "P A" "m \<noteq> n" for A m n
```
```   134   proof -
```
```   135     have "A ** (\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j) = ?C"
```
```   136       by (simp add: matrix_matrix_mult_def mat_def vec_eq_iff if_distrib sum.delta_remove)
```
```   137     then show ?thesis
```
```   138       using mult swap1 that by metis
```
```   139   qed
```
```   140   have row: "P (\<chi> i. if i = m then row m A + c *\<^sub>R row n A else row i A)"  (is "P ?C")
```
```   141     if "P A" "m \<noteq> n" for A m n c
```
```   142   proof -
```
```   143     let ?B = "\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j)"
```
```   144     have "?B ** A = ?C"
```
```   145       using \<open>m \<noteq> n\<close> unfolding matrix_matrix_mult_def row_def of_bool_def
```
```   146       by (auto simp: vec_eq_iff if_distrib [of "\<lambda>x. x * y" for y] sum.remove cong: if_cong)
```
```   147     then show ?thesis
```
```   148       by (rule subst) (auto simp: that mult idplus)
```
```   149   qed
```
```   150   show ?thesis
```
```   151     by (rule induct_matrix_row_operations [OF zero_row diagonal swap row])
```
```   152 qed
```
```   153
```
```   154 lemma%unimportant induct_matrix_elementary_alt:
```
```   155   fixes P :: "real^'n^'n \<Rightarrow> bool"
```
```   156   assumes mult: "\<And>A B. \<lbrakk>P A; P B\<rbrakk> \<Longrightarrow> P(A ** B)"
```
```   157     and zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
```
```   158     and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
```
```   159     and swap1: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
```
```   160     and idplus: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j))"
```
```   161   shows "P A"
```
```   162 proof -
```
```   163   have *: "P (\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j))"
```
```   164     if "m \<noteq> n" for m n c
```
```   165   proof (cases "c = 0")
```
```   166     case True
```
```   167     with diagonal show ?thesis by auto
```
```   168   next
```
```   169     case False
```
```   170     then have eq: "(\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j)) =
```
```   171                       (\<chi> i j. if i = j then (if j = n then inverse c else 1) else 0) **
```
```   172                       (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)) **
```
```   173                       (\<chi> i j. if i = j then if j = n then c else 1 else 0)"
```
```   174       using \<open>m \<noteq> n\<close>
```
```   175       apply (simp add: matrix_matrix_mult_def vec_eq_iff of_bool_def if_distrib [of "\<lambda>x. y * x" for y] cong: if_cong)
```
```   176       apply (simp add: if_if_eq_conj sum.neutral conj_commute cong: conj_cong)
```
```   177       done
```
```   178     show ?thesis
```
```   179       apply (subst eq)
```
```   180       apply (intro mult idplus that)
```
```   181        apply (auto intro: diagonal)
```
```   182       done
```
```   183   qed
```
```   184   show ?thesis
```
```   185     by (rule induct_matrix_elementary) (auto intro: assms *)
```
```   186 qed
```
```   187
```
```   188 lemma%unimportant matrix_vector_mult_matrix_matrix_mult_compose:
```
```   189   "(*v) (A ** B) = (*v) A \<circ> (*v) B"
```
```   190   by (auto simp: matrix_vector_mul_assoc)
```
```   191
```
```   192 lemma%unimportant induct_linear_elementary:
```
```   193   fixes f :: "real^'n \<Rightarrow> real^'n"
```
```   194   assumes "linear f"
```
```   195     and comp: "\<And>f g. \<lbrakk>linear f; linear g; P f; P g\<rbrakk> \<Longrightarrow> P(f \<circ> g)"
```
```   196     and zeroes: "\<And>f i. \<lbrakk>linear f; \<And>x. (f x) \$ i = 0\<rbrakk> \<Longrightarrow> P f"
```
```   197     and const: "\<And>c. P(\<lambda>x. \<chi> i. c i * x\$i)"
```
```   198     and swap: "\<And>m n::'n. m \<noteq> n \<Longrightarrow> P(\<lambda>x. \<chi> i. x \$ Fun.swap m n id i)"
```
```   199     and idplus: "\<And>m n::'n. m \<noteq> n \<Longrightarrow> P(\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i)"
```
```   200   shows "P f"
```
```   201 proof -
```
```   202   have "P ((*v) A)" for A
```
```   203   proof (rule induct_matrix_elementary_alt)
```
```   204     fix A B
```
```   205     assume "P ((*v) A)" and "P ((*v) B)"
```
```   206     then show "P ((*v) (A ** B))"
```
```   207       by (auto simp add: matrix_vector_mult_matrix_matrix_mult_compose matrix_vector_mul_linear
```
```   208           intro!: comp)
```
```   209   next
```
```   210     fix A :: "real^'n^'n" and i
```
```   211     assume "row i A = 0"
```
```   212     show "P ((*v) A)"
```
```   213       using matrix_vector_mul_linear
```
```   214       by (rule zeroes[where i=i])
```
```   215         (metis \<open>row i A = 0\<close> inner_zero_left matrix_vector_mul_component row_def vec_lambda_eta)
```
```   216   next
```
```   217     fix A :: "real^'n^'n"
```
```   218     assume 0: "\<And>i j. i \<noteq> j \<Longrightarrow> A \$ i \$ j = 0"
```
```   219     have "A \$ i \$ i * x \$ i = (\<Sum>j\<in>UNIV. A \$ i \$ j * x \$ j)" for x and i :: "'n"
```
```   220       by (simp add: 0 comm_monoid_add_class.sum.remove [where x=i])
```
```   221     then have "(\<lambda>x. \<chi> i. A \$ i \$ i * x \$ i) = ((*v) A)"
```
```   222       by (auto simp: 0 matrix_vector_mult_def)
```
```   223     then show "P ((*v) A)"
```
```   224       using const [of "\<lambda>i. A \$ i \$ i"] by simp
```
```   225   next
```
```   226     fix m n :: "'n"
```
```   227     assume "m \<noteq> n"
```
```   228     have eq: "(\<Sum>j\<in>UNIV. if i = Fun.swap m n id j then x \$ j else 0) =
```
```   229               (\<Sum>j\<in>UNIV. if j = Fun.swap m n id i then x \$ j else 0)"
```
```   230       for i and x :: "real^'n"
```
```   231       unfolding swap_def by (rule sum.cong) auto
```
```   232     have "(\<lambda>x::real^'n. \<chi> i. x \$ Fun.swap m n id i) = ((*v) (\<chi> i j. if i = Fun.swap m n id j then 1 else 0))"
```
```   233       by (auto simp: mat_def matrix_vector_mult_def eq if_distrib [of "\<lambda>x. x * y" for y] cong: if_cong)
```
```   234     with swap [OF \<open>m \<noteq> n\<close>] show "P ((*v) (\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j))"
```
```   235       by (simp add: mat_def matrix_vector_mult_def)
```
```   236   next
```
```   237     fix m n :: "'n"
```
```   238     assume "m \<noteq> n"
```
```   239     then have "x \$ m + x \$ n = (\<Sum>j\<in>UNIV. of_bool (j = n \<or> m = j) * x \$ j)" for x :: "real^'n"
```
```   240       by (auto simp: of_bool_def if_distrib [of "\<lambda>x. x * y" for y] sum.remove cong: if_cong)
```
```   241     then have "(\<lambda>x::real^'n. \<chi> i. if i = m then x \$ m + x \$ n else x \$ i) =
```
```   242                ((*v) (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)))"
```
```   243       unfolding matrix_vector_mult_def of_bool_def
```
```   244       by (auto simp: vec_eq_iff if_distrib [of "\<lambda>x. x * y" for y] cong: if_cong)
```
```   245     then show "P ((*v) (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)))"
```
```   246       using idplus [OF \<open>m \<noteq> n\<close>] by simp
```
```   247   qed
```
```   248   then show ?thesis
```
```   249     by (metis \<open>linear f\<close> matrix_vector_mul)
```
```   250 qed
```
```   251
```
```   252
```
```   253 proposition%important
```
```   254   fixes a :: "real^'n"
```
```   255   assumes "m \<noteq> n" and ab_ne: "cbox a b \<noteq> {}" and an: "0 \<le> a\$n"
```
```   256   shows measurable_shear_interval: "(\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i) ` (cbox a b) \<in> lmeasurable"
```
```   257        (is  "?f ` _ \<in> _")
```
```   258    and measure_shear_interval: "measure lebesgue ((\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i) ` cbox a b)
```
```   259                = measure lebesgue (cbox a b)" (is "?Q")
```
```   260 proof%unimportant -
```
```   261   have lin: "linear ?f"
```
```   262     by (rule linearI) (auto simp: plus_vec_def scaleR_vec_def algebra_simps)
```
```   263   show fab: "?f ` cbox a b \<in> lmeasurable"
```
```   264     by (simp add: lin measurable_linear_image_interval)
```
```   265   let ?c = "\<chi> i. if i = m then b\$m + b\$n else b\$i"
```
```   266   let ?mn = "axis m 1 - axis n (1::real)"
```
```   267   have eq1: "measure lebesgue (cbox a ?c)
```
```   268             = measure lebesgue (?f ` cbox a b)
```
```   269             + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m})
```
```   270             + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})"
```
```   271   proof (rule measure_Un3_negligible)
```
```   272     show "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m} \<in> lmeasurable" "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} \<in> lmeasurable"
```
```   273       by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
```
```   274     have "negligible {x. ?mn \<bullet> x = a\$m}"
```
```   275       by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
```
```   276     moreover have "?f ` cbox a b \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}) \<subseteq> {x. ?mn \<bullet> x = a\$m}"
```
```   277       using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
```
```   278     ultimately show "negligible ((?f ` cbox a b) \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}))"
```
```   279       by (rule negligible_subset)
```
```   280     have "negligible {x. ?mn \<bullet> x = b\$m}"
```
```   281       by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
```
```   282     moreover have "(?f ` cbox a b) \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}) \<subseteq> {x. ?mn \<bullet> x = b\$m}"
```
```   283       using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
```
```   284     ultimately show "negligible (?f ` cbox a b \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}))"
```
```   285       by (rule negligible_subset)
```
```   286     have "negligible {x. ?mn \<bullet> x = b\$m}"
```
```   287       by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
```
```   288     moreover have "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})) \<subseteq> {x. ?mn \<bullet> x = b\$m}"
```
```   289       using \<open>m \<noteq> n\<close> ab_ne
```
```   290       apply (auto simp: algebra_simps mem_box_cart inner_axis')
```
```   291       apply (drule_tac x=m in spec)+
```
```   292       apply simp
```
```   293       done
```
```   294     ultimately show "negligible (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}))"
```
```   295       by (rule negligible_subset)
```
```   296     show "?f ` cbox a b \<union> cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<union> cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} = cbox a ?c" (is "?lhs = _")
```
```   297     proof
```
```   298       show "?lhs \<subseteq> cbox a ?c"
```
```   299         by (auto simp: mem_box_cart add_mono) (meson add_increasing2 an order_trans)
```
```   300       show "cbox a ?c \<subseteq> ?lhs"
```
```   301         apply (auto simp: algebra_simps image_iff inner_axis' lambda_add_Galois [OF \<open>m \<noteq> n\<close>])
```
```   302         apply (auto simp: mem_box_cart split: if_split_asm)
```
```   303         done
```
```   304     qed
```
```   305   qed (fact fab)
```
```   306   let ?d = "\<chi> i. if i = m then a \$ m - b \$ m else 0"
```
```   307   have eq2: "measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}) + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})
```
```   308            = measure lebesgue (cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i))"
```
```   309   proof (rule measure_translate_add[of "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m}" "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}"
```
```   310      "(\<chi> i. if i = m then a\$m - b\$m else 0)" "cbox a (\<chi> i. if i = m then a\$m + b\$n else b\$i)"])
```
```   311     show "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m}) \<in> lmeasurable"
```
```   312       "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} \<in> lmeasurable"
```
```   313       by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
```
```   314     have "\<And>x. \<lbrakk>x \$ n + a \$ m \<le> x \$ m\<rbrakk>
```
```   315          \<Longrightarrow> x \<in> (+) (\<chi> i. if i = m then a \$ m - b \$ m else 0) ` {x. x \$ n + b \$ m \<le> x \$ m}"
```
```   316       using \<open>m \<noteq> n\<close>
```
```   317       by (rule_tac x="x - (\<chi> i. if i = m then a\$m - b\$m else 0)" in image_eqI)
```
```   318          (simp_all add: mem_box_cart)
```
```   319     then have imeq: "(+) ?d ` {x. b \$ m \<le> ?mn \<bullet> x} = {x. a \$ m \<le> ?mn \<bullet> x}"
```
```   320       using \<open>m \<noteq> n\<close> by (auto simp: mem_box_cart inner_axis' algebra_simps)
```
```   321     have "\<And>x. \<lbrakk>0 \<le> a \$ n; x \$ n + a \$ m \<le> x \$ m;
```
```   322                 \<forall>i. i \<noteq> m \<longrightarrow> a \$ i \<le> x \$ i \<and> x \$ i \<le> b \$ i\<rbrakk>
```
```   323          \<Longrightarrow> a \$ m \<le> x \$ m"
```
```   324       using \<open>m \<noteq> n\<close>  by force
```
```   325     then have "(+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x})
```
```   326             = cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i) \<inter> {x. a \$ m \<le> ?mn \<bullet> x}"
```
```   327       using an ab_ne
```
```   328       apply (simp add: cbox_translation [symmetric] translation_Int interval_ne_empty_cart imeq)
```
```   329       apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib)
```
```   330       by (metis (full_types) add_mono mult_2_right)
```
```   331     then show "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<union>
```
```   332           (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x}) =
```
```   333           cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i)"  (is "?lhs = ?rhs")
```
```   334       using an \<open>m \<noteq> n\<close>
```
```   335       apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib, force)
```
```   336         apply (drule_tac x=n in spec)+
```
```   337       by (meson ab_ne add_mono_thms_linordered_semiring(3) dual_order.trans interval_ne_empty_cart(1))
```
```   338     have "negligible{x. ?mn \<bullet> x = a\$m}"
```
```   339       by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
```
```   340     moreover have "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter>
```
```   341                                  (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x})) \<subseteq> {x. ?mn \<bullet> x = a\$m}"
```
```   342       using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
```
```   343     ultimately show "negligible (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter>
```
```   344                                  (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x}))"
```
```   345       by (rule negligible_subset)
```
```   346   qed
```
```   347   have ac_ne: "cbox a ?c \<noteq> {}"
```
```   348     using ab_ne an
```
```   349     by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
```
```   350   have ax_ne: "cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i) \<noteq> {}"
```
```   351     using ab_ne an
```
```   352     by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
```
```   353   have eq3: "measure lebesgue (cbox a ?c) = measure lebesgue (cbox a (\<chi> i. if i = m then a\$m + b\$n else b\$i)) + measure lebesgue (cbox a b)"
```
```   354     by (simp add: content_cbox_if_cart ab_ne ac_ne ax_ne algebra_simps prod.delta_remove
```
```   355              if_distrib [of "\<lambda>u. u - z" for z] prod.remove)
```
```   356   show ?Q
```
```   357     using eq1 eq2 eq3
```
```   358     by (simp add: algebra_simps)
```
```   359 qed
```
```   360
```
```   361
```
```   362 proposition%important
```
```   363   fixes S :: "(real^'n) set"
```
```   364   assumes "S \<in> lmeasurable"
```
```   365   shows measurable_stretch: "((\<lambda>x. \<chi> k. m k * x\$k) ` S) \<in> lmeasurable" (is  "?f ` S \<in> _")
```
```   366     and measure_stretch: "measure lebesgue ((\<lambda>x. \<chi> k. m k * x\$k) ` S) = \<bar>prod m UNIV\<bar> * measure lebesgue S"
```
```   367     (is "?MEQ")
```
```   368 proof%unimportant -
```
```   369   have "(?f ` S) \<in> lmeasurable \<and> ?MEQ"
```
```   370   proof (cases "\<forall>k. m k \<noteq> 0")
```
```   371     case True
```
```   372     have m0: "0 < \<bar>prod m UNIV\<bar>"
```
```   373       using True by simp
```
```   374     have "(indicat_real (?f ` S) has_integral \<bar>prod m UNIV\<bar> * measure lebesgue S) UNIV"
```
```   375     proof (clarsimp simp add: has_integral_alt [where i=UNIV])
```
```   376       fix e :: "real"
```
```   377       assume "e > 0"
```
```   378       have "(indicat_real S has_integral (measure lebesgue S)) UNIV"
```
```   379         using assms lmeasurable_iff_has_integral by blast
```
```   380       then obtain B where "B>0"
```
```   381         and B: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow>
```
```   382                         \<exists>z. (indicat_real S has_integral z) (cbox a b) \<and>
```
```   383                             \<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
```
```   384         by (simp add: has_integral_alt [where i=UNIV]) (metis (full_types) divide_pos_pos m0  m0 \<open>e > 0\<close>)
```
```   385       show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
```
```   386                   (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox a b) \<and>
```
```   387                        \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)"
```
```   388       proof (intro exI conjI allI)
```
```   389         let ?C = "Max (range (\<lambda>k. \<bar>m k\<bar>)) * B"
```
```   390         show "?C > 0"
```
```   391           using True \<open>B > 0\<close> by (simp add: Max_gr_iff)
```
```   392         show "ball 0 ?C \<subseteq> cbox u v \<longrightarrow>
```
```   393                   (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
```
```   394                        \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)" for u v
```
```   395         proof
```
```   396           assume uv: "ball 0 ?C \<subseteq> cbox u v"
```
```   397           with \<open>?C > 0\<close> have cbox_ne: "cbox u v \<noteq> {}"
```
```   398             using centre_in_ball by blast
```
```   399           let ?\<alpha> = "\<lambda>k. u\$k / m k"
```
```   400           let ?\<beta> = "\<lambda>k. v\$k / m k"
```
```   401           have invm0: "\<And>k. inverse (m k) \<noteq> 0"
```
```   402             using True by auto
```
```   403           have "ball 0 B \<subseteq> (\<lambda>x. \<chi> k. x \$ k / m k) ` ball 0 ?C"
```
```   404           proof clarsimp
```
```   405             fix x :: "real^'n"
```
```   406             assume x: "norm x < B"
```
```   407             have [simp]: "\<bar>Max (range (\<lambda>k. \<bar>m k\<bar>))\<bar> = Max (range (\<lambda>k. \<bar>m k\<bar>))"
```
```   408               by (meson Max_ge abs_ge_zero abs_of_nonneg finite finite_imageI order_trans rangeI)
```
```   409             have "norm (\<chi> k. m k * x \$ k) \<le> norm (Max (range (\<lambda>k. \<bar>m k\<bar>)) *\<^sub>R x)"
```
```   410               by (rule norm_le_componentwise_cart) (auto simp: abs_mult intro: mult_right_mono)
```
```   411             also have "\<dots> < ?C"
```
```   412               using x by simp (metis \<open>B > 0\<close> \<open>?C > 0\<close> mult.commute real_mult_less_iff1 zero_less_mult_pos)
```
```   413             finally have "norm (\<chi> k. m k * x \$ k) < ?C" .
```
```   414             then show "x \<in> (\<lambda>x. \<chi> k. x \$ k / m k) ` ball 0 ?C"
```
```   415               using stretch_Galois [of "inverse \<circ> m"] True by (auto simp: image_iff field_simps)
```
```   416           qed
```
```   417           then have Bsub: "ball 0 B \<subseteq> cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k))"
```
```   418             using cbox_ne uv image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
```
```   419             by (force simp: field_simps)
```
```   420           obtain z where zint: "(indicat_real S has_integral z) (cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k)))"
```
```   421                    and zless: "\<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
```
```   422             using B [OF Bsub] by blast
```
```   423           have ind: "indicat_real (?f ` S) = (\<lambda>x. indicator S (\<chi> k. x\$k / m k))"
```
```   424             using True stretch_Galois [of m] by (force simp: indicator_def)
```
```   425           show "\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
```
```   426                        \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e"
```
```   427           proof (simp add: ind, intro conjI exI)
```
```   428             have "((\<lambda>x. indicat_real S (\<chi> k. x \$ k/ m k)) has_integral z *\<^sub>R \<bar>prod m UNIV\<bar>)
```
```   429                 ((\<lambda>x. \<chi> k. x \$ k * m k) ` cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k)))"
```
```   430               using True has_integral_stretch_cart [OF zint, of "inverse \<circ> m"]
```
```   431               by (simp add: field_simps prod_dividef)
```
```   432             moreover have "((\<lambda>x. \<chi> k. x \$ k * m k) ` cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k))) = cbox u v"
```
```   433               using True image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
```
```   434                 image_stretch_interval_cart [of "\<lambda>k. 1" u v, symmetric] \<open>cbox u v \<noteq> {}\<close>
```
```   435               by (simp add: field_simps image_comp o_def)
```
```   436             ultimately show "((\<lambda>x. indicat_real S (\<chi> k. x \$ k/ m k)) has_integral z *\<^sub>R \<bar>prod m UNIV\<bar>) (cbox u v)"
```
```   437               by simp
```
```   438             have "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar>
```
```   439                  = \<bar>prod m UNIV\<bar> * \<bar>z - measure lebesgue S\<bar>"
```
```   440               by (metis (no_types, hide_lams) abs_abs abs_scaleR mult.commute real_scaleR_def right_diff_distrib')
```
```   441             also have "\<dots> < e"
```
```   442               using zless True by (simp add: field_simps)
```
```   443             finally show "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e" .
```
```   444           qed
```
```   445         qed
```
```   446       qed
```
```   447     qed
```
```   448     then show ?thesis
```
```   449       by (auto simp: has_integral_integrable integral_unique lmeasure_integral_UNIV measurable_integrable)
```
```   450   next
```
```   451     case False
```
```   452     then obtain k where "m k = 0" and prm: "prod m UNIV = 0"
```
```   453       by auto
```
```   454     have nfS: "negligible (?f ` S)"
```
```   455       by (rule negligible_subset [OF negligible_standard_hyperplane_cart]) (use \<open>m k = 0\<close> in auto)
```
```   456     then have "(?f ` S) \<in> lmeasurable"
```
```   457       by (simp add: negligible_iff_measure)
```
```   458     with nfS show ?thesis
```
```   459       by (simp add: prm negligible_iff_measure0)
```
```   460   qed
```
```   461   then show "(?f ` S) \<in> lmeasurable" ?MEQ
```
```   462     by metis+
```
```   463 qed
```
```   464
```
```   465
```
```   466 proposition%important
```
```   467  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```   468   assumes "linear f" "S \<in> lmeasurable"
```
```   469   shows measurable_linear_image: "(f ` S) \<in> lmeasurable"
```
```   470     and measure_linear_image: "measure lebesgue (f ` S) = \<bar>det (matrix f)\<bar> * measure lebesgue S" (is "?Q f S")
```
```   471 proof%unimportant -
```
```   472   have "\<forall>S \<in> lmeasurable. (f ` S) \<in> lmeasurable \<and> ?Q f S"
```
```   473   proof (rule induct_linear_elementary [OF \<open>linear f\<close>]; intro ballI)
```
```   474     fix f g and S :: "(real,'n) vec set"
```
```   475     assume "linear f" and "linear g"
```
```   476       and f [rule_format]: "\<forall>S \<in> lmeasurable. f ` S \<in> lmeasurable \<and> ?Q f S"
```
```   477       and g [rule_format]: "\<forall>S \<in> lmeasurable. g ` S \<in> lmeasurable \<and> ?Q g S"
```
```   478       and S: "S \<in> lmeasurable"
```
```   479     then have gS: "g ` S \<in> lmeasurable"
```
```   480       by blast
```
```   481     show "(f \<circ> g) ` S \<in> lmeasurable \<and> ?Q (f \<circ> g) S"
```
```   482       using f [OF gS] g [OF S] matrix_compose [OF \<open>linear g\<close> \<open>linear f\<close>]
```
```   483       by (simp add: o_def image_comp abs_mult det_mul)
```
```   484   next
```
```   485     fix f :: "real^'n::_ \<Rightarrow> real^'n::_" and i and S :: "(real^'n::_) set"
```
```   486     assume "linear f" and 0: "\<And>x. f x \$ i = 0" and "S \<in> lmeasurable"
```
```   487     then have "\<not> inj f"
```
```   488       by (metis (full_types) linear_injective_imp_surjective one_neq_zero surjE vec_component)
```
```   489     have detf: "det (matrix f) = 0"
```
```   490       using \<open>\<not> inj f\<close> det_nz_iff_inj[OF \<open>linear f\<close>] by blast
```
```   491     show "f ` S \<in> lmeasurable \<and> ?Q f S"
```
```   492     proof
```
```   493       show "f ` S \<in> lmeasurable"
```
```   494         using lmeasurable_iff_indicator_has_integral \<open>linear f\<close> \<open>\<not> inj f\<close> negligible_UNIV negligible_linear_singular_image by blast
```
```   495       have "measure lebesgue (f ` S) = 0"
```
```   496         by (meson \<open>\<not> inj f\<close> \<open>linear f\<close> negligible_imp_measure0 negligible_linear_singular_image)
```
```   497       also have "\<dots> = \<bar>det (matrix f)\<bar> * measure lebesgue S"
```
```   498         by (simp add: detf)
```
```   499       finally show "?Q f S" .
```
```   500     qed
```
```   501   next
```
```   502     fix c and S :: "(real^'n::_) set"
```
```   503     assume "S \<in> lmeasurable"
```
```   504     show "(\<lambda>a. \<chi> i. c i * a \$ i) ` S \<in> lmeasurable \<and> ?Q (\<lambda>a. \<chi> i. c i * a \$ i) S"
```
```   505     proof
```
```   506       show "(\<lambda>a. \<chi> i. c i * a \$ i) ` S \<in> lmeasurable"
```
```   507         by (simp add: \<open>S \<in> lmeasurable\<close> measurable_stretch)
```
```   508       show "?Q (\<lambda>a. \<chi> i. c i * a \$ i) S"
```
```   509         by (simp add: measure_stretch [OF \<open>S \<in> lmeasurable\<close>, of c] axis_def matrix_def det_diagonal)
```
```   510     qed
```
```   511   next
```
```   512     fix m :: "'n" and n :: "'n" and S :: "(real, 'n) vec set"
```
```   513     assume "m \<noteq> n" and "S \<in> lmeasurable"
```
```   514     let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. v \$ Fun.swap m n id i"
```
```   515     have lin: "linear ?h"
```
```   516       by (rule linearI) (simp_all add: plus_vec_def scaleR_vec_def)
```
```   517     have meq: "measure lebesgue ((\<lambda>v::(real, 'n) vec. \<chi> i. v \$ Fun.swap m n id i) ` cbox a b)
```
```   518              = measure lebesgue (cbox a b)" for a b
```
```   519     proof (cases "cbox a b = {}")
```
```   520       case True then show ?thesis
```
```   521         by simp
```
```   522     next
```
```   523       case False
```
```   524       then have him: "?h ` (cbox a b) \<noteq> {}"
```
```   525         by blast
```
```   526       have eq: "?h ` (cbox a b) = cbox (?h a) (?h b)"
```
```   527         by (auto simp: image_iff lambda_swap_Galois mem_box_cart) (metis swap_id_eq)+
```
```   528       show ?thesis
```
```   529         using him prod.permute [OF permutes_swap_id, where S=UNIV and g="\<lambda>i. (b - a)\$i", symmetric]
```
```   530         by (simp add: eq content_cbox_cart False)
```
```   531     qed
```
```   532     have "(\<chi> i j. if Fun.swap m n id i = j then 1 else 0) = (\<chi> i j. if j = Fun.swap m n id i then 1 else (0::real))"
```
```   533       by (auto intro!: Cart_lambda_cong)
```
```   534     then have "matrix ?h = transpose(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
```
```   535       by (auto simp: matrix_eq transpose_def axis_def mat_def matrix_def)
```
```   536     then have 1: "\<bar>det (matrix ?h)\<bar> = 1"
```
```   537       by (simp add: det_permute_columns permutes_swap_id sign_swap_id abs_mult)
```
```   538     show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
```
```   539     proof
```
```   540       show "?h ` S \<in> lmeasurable" "?Q ?h S"
```
```   541         using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force+
```
```   542     qed
```
```   543   next
```
```   544     fix m n :: "'n" and S :: "(real, 'n) vec set"
```
```   545     assume "m \<noteq> n" and "S \<in> lmeasurable"
```
```   546     let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i"
```
```   547     have lin: "linear ?h"
```
```   548       by (rule linearI) (auto simp: algebra_simps plus_vec_def scaleR_vec_def vec_eq_iff)
```
```   549     consider "m < n" | " n < m"
```
```   550       using \<open>m \<noteq> n\<close> less_linear by blast
```
```   551     then have 1: "det(matrix ?h) = 1"
```
```   552     proof cases
```
```   553       assume "m < n"
```
```   554       have *: "matrix ?h \$ i \$ j = (0::real)" if "j < i" for i j :: 'n
```
```   555       proof -
```
```   556         have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
```
```   557           using axis_def by blast
```
```   558         then have "(\<chi> p q. if p = m then axis q 1 \$ m + axis q 1 \$ n else axis q 1 \$ p) \$ i \$ j = (0::real)"
```
```   559           using \<open>j < i\<close> axis_def \<open>m < n\<close> by auto
```
```   560         with \<open>m < n\<close> show ?thesis
```
```   561           by (auto simp: matrix_def axis_def cong: if_cong)
```
```   562       qed
```
```   563       show ?thesis
```
```   564         using \<open>m \<noteq> n\<close> by (subst det_upperdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
```
```   565     next
```
```   566       assume "n < m"
```
```   567       have *: "matrix ?h \$ i \$ j = (0::real)" if "j > i" for i j :: 'n
```
```   568       proof -
```
```   569         have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
```
```   570           using axis_def by blast
```
```   571         then have "(\<chi> p q. if p = m then axis q 1 \$ m + axis q 1 \$ n else axis q 1 \$ p) \$ i \$ j = (0::real)"
```
```   572           using \<open>j > i\<close> axis_def \<open>m > n\<close> by auto
```
```   573         with \<open>m > n\<close> show ?thesis
```
```   574           by (auto simp: matrix_def axis_def cong: if_cong)
```
```   575       qed
```
```   576       show ?thesis
```
```   577         using \<open>m \<noteq> n\<close>
```
```   578         by (subst det_lowerdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
```
```   579     qed
```
```   580     have meq: "measure lebesgue (?h ` (cbox a b)) = measure lebesgue (cbox a b)" for a b
```
```   581     proof (cases "cbox a b = {}")
```
```   582       case True then show ?thesis by simp
```
```   583     next
```
```   584       case False
```
```   585       then have ne: "(+) (\<chi> i. if i = n then - a \$ n else 0) ` cbox a b \<noteq> {}"
```
```   586         by auto
```
```   587       let ?v = "\<chi> i. if i = n then - a \$ n else 0"
```
```   588       have "?h ` cbox a b
```
```   589             = (+) (\<chi> i. if i = m \<or> i = n then a \$ n else 0) ` ?h ` (+) ?v ` (cbox a b)"
```
```   590         using \<open>m \<noteq> n\<close> unfolding image_comp o_def by (force simp: vec_eq_iff)
```
```   591       then have "measure lebesgue (?h ` (cbox a b))
```
```   592                = measure lebesgue ((\<lambda>v. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i) `
```
```   593                                    (+) ?v ` cbox a b)"
```
```   594         by (rule ssubst) (rule measure_translation)
```
```   595       also have "\<dots> = measure lebesgue ((\<lambda>v. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i) ` cbox (?v +a) (?v + b))"
```
```   596         by (metis (no_types, lifting) cbox_translation)
```
```   597       also have "\<dots> = measure lebesgue ((+) (\<chi> i. if i = n then - a \$ n else 0) ` cbox a b)"
```
```   598         apply (subst measure_shear_interval)
```
```   599         using \<open>m \<noteq> n\<close> ne apply auto
```
```   600         apply (simp add: cbox_translation)
```
```   601         by (metis cbox_borel cbox_translation measure_completion sets_lborel)
```
```   602       also have "\<dots> = measure lebesgue (cbox a b)"
```
```   603         by (rule measure_translation)
```
```   604         finally show ?thesis .
```
```   605       qed
```
```   606     show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
```
```   607       using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force
```
```   608   qed
```
```   609   with assms show "(f ` S) \<in> lmeasurable" "?Q f S"
```
```   610     by metis+
```
```   611 qed
```
```   612
```
```   613
```
```   614 lemma%unimportant
```
```   615  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```   616   assumes f: "orthogonal_transformation f" and S: "S \<in> lmeasurable"
```
```   617   shows measurable_orthogonal_image: "f ` S \<in> lmeasurable"
```
```   618     and measure_orthogonal_image: "measure lebesgue (f ` S) = measure lebesgue S"
```
```   619 proof -
```
```   620   have "linear f"
```
```   621     by (simp add: f orthogonal_transformation_linear)
```
```   622   then show "f ` S \<in> lmeasurable"
```
```   623     by (metis S measurable_linear_image)
```
```   624   show "measure lebesgue (f ` S) = measure lebesgue S"
```
```   625     by (simp add: measure_linear_image \<open>linear f\<close> S f)
```
```   626 qed
```
```   627
```
```   628 subsection%important\<open>\<open>F_sigma\<close> and \<open>G_delta\<close> sets.\<close>(*FIX ME mv *)
```
```   629
```
```   630 (*https://en.wikipedia.org/wiki/F\<sigma>_set*)
```
```   631 inductive%important fsigma :: "'a::topological_space set \<Rightarrow> bool" where
```
```   632   "(\<And>n::nat. closed (F n)) \<Longrightarrow> fsigma (\<Union>(F ` UNIV))"
```
```   633
```
```   634 inductive%important gdelta :: "'a::topological_space set \<Rightarrow> bool" where
```
```   635   "(\<And>n::nat. open (F n)) \<Longrightarrow> gdelta (\<Inter>(F ` UNIV))"
```
```   636
```
```   637 lemma%important fsigma_Union_compact:
```
```   638   fixes S :: "'a::{real_normed_vector,heine_borel} set"
```
```   639   shows "fsigma S \<longleftrightarrow> (\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = \<Union>(F ` UNIV))"
```
```   640 proof%unimportant safe
```
```   641   assume "fsigma S"
```
```   642   then obtain F :: "nat \<Rightarrow> 'a set" where F: "range F \<subseteq> Collect closed" "S = \<Union>(F ` UNIV)"
```
```   643     by (meson fsigma.cases image_subsetI mem_Collect_eq)
```
```   644   then have "\<exists>D::nat \<Rightarrow> 'a set. range D \<subseteq> Collect compact \<and> \<Union>(D ` UNIV) = F i" for i
```
```   645     using closed_Union_compact_subsets [of "F i"]
```
```   646     by (metis image_subsetI mem_Collect_eq range_subsetD)
```
```   647   then obtain D :: "nat \<Rightarrow> nat \<Rightarrow> 'a set"
```
```   648     where D: "\<And>i. range (D i) \<subseteq> Collect compact \<and> \<Union>((D i) ` UNIV) = F i"
```
```   649     by metis
```
```   650   let ?DD = "\<lambda>n. (\<lambda>(i,j). D i j) (prod_decode n)"
```
```   651   show "\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = \<Union>(F ` UNIV)"
```
```   652   proof (intro exI conjI)
```
```   653     show "range ?DD \<subseteq> Collect compact"
```
```   654       using D by clarsimp (metis mem_Collect_eq rangeI split_conv subsetCE surj_pair)
```
```   655     show "S = \<Union> (range ?DD)"
```
```   656     proof
```
```   657       show "S \<subseteq> \<Union> (range ?DD)"
```
```   658         using D F
```
```   659         by clarsimp (metis UN_iff old.prod.case prod_decode_inverse prod_encode_eq)
```
```   660       show "\<Union> (range ?DD) \<subseteq> S"
```
```   661         using D F  by fastforce
```
```   662     qed
```
```   663   qed
```
```   664 next
```
```   665   fix F :: "nat \<Rightarrow> 'a set"
```
```   666   assume "range F \<subseteq> Collect compact" and "S = \<Union>(F ` UNIV)"
```
```   667   then show "fsigma (\<Union>(F ` UNIV))"
```
```   668     by (simp add: compact_imp_closed fsigma.intros image_subset_iff)
```
```   669 qed
```
```   670
```
```   671 lemma%unimportant gdelta_imp_fsigma: "gdelta S \<Longrightarrow> fsigma (- S)"
```
```   672 proof (induction rule: gdelta.induct)
```
```   673   case (1 F)
```
```   674   have "- \<Inter>(F ` UNIV) = (\<Union>i. -(F i))"
```
```   675     by auto
```
```   676   then show ?case
```
```   677     by (simp add: fsigma.intros closed_Compl 1)
```
```   678 qed
```
```   679
```
```   680 lemma%unimportant fsigma_imp_gdelta: "fsigma S \<Longrightarrow> gdelta (- S)"
```
```   681 proof (induction rule: fsigma.induct)
```
```   682   case (1 F)
```
```   683   have "- \<Union>(F ` UNIV) = (\<Inter>i. -(F i))"
```
```   684     by auto
```
```   685   then show ?case
```
```   686     by (simp add: 1 gdelta.intros open_closed)
```
```   687 qed
```
```   688
```
```   689 lemma%unimportant gdelta_complement: "gdelta(- S) \<longleftrightarrow> fsigma S"
```
```   690   using fsigma_imp_gdelta gdelta_imp_fsigma by force
```
```   691
```
```   692 text\<open>A Lebesgue set is almost an \<open>F_sigma\<close> or \<open>G_delta\<close>.\<close>
```
```   693 lemma%unimportant lebesgue_set_almost_fsigma:
```
```   694   assumes "S \<in> sets lebesgue"
```
```   695   obtains C T where "fsigma C" "negligible T" "C \<union> T = S" "disjnt C T"
```
```   696 proof -
```
```   697   { fix n::nat
```
```   698     have "\<exists>T. closed T \<and> T \<subseteq> S \<and> S - T \<in> lmeasurable \<and> measure lebesgue (S-T) < 1 / Suc n"
```
```   699       using sets_lebesgue_inner_closed [OF assms]
```
```   700       by (metis divide_pos_pos less_numeral_extra(1) of_nat_0_less_iff zero_less_Suc)
```
```   701   }
```
```   702   then obtain F where F: "\<And>n::nat. closed (F n) \<and> F n \<subseteq> S \<and> S - F n \<in> lmeasurable \<and> measure lebesgue (S - F n) < 1 / Suc n"
```
```   703     by metis
```
```   704   let ?C = "\<Union>(F ` UNIV)"
```
```   705   show thesis
```
```   706   proof
```
```   707     show "fsigma ?C"
```
```   708       using F by (simp add: fsigma.intros)
```
```   709     show "negligible (S - ?C)"
```
```   710     proof (clarsimp simp add: negligible_outer_le)
```
```   711       fix e :: "real"
```
```   712       assume "0 < e"
```
```   713       then obtain n where n: "1 / Suc n < e"
```
```   714         using nat_approx_posE by metis
```
```   715       show "\<exists>T. S - (\<Union>x. F x) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
```
```   716       proof (intro exI conjI)
```
```   717         show "measure lebesgue (S - F n) \<le> e"
```
```   718           by (meson F n less_trans not_le order.asym)
```
```   719       qed (use F in auto)
```
```   720     qed
```
```   721     show "?C \<union> (S - ?C) = S"
```
```   722       using F by blast
```
```   723     show "disjnt ?C (S - ?C)"
```
```   724       by (auto simp: disjnt_def)
```
```   725   qed
```
```   726 qed
```
```   727
```
```   728 lemma%unimportant lebesgue_set_almost_gdelta:
```
```   729   assumes "S \<in> sets lebesgue"
```
```   730   obtains C T where "gdelta C" "negligible T" "S \<union> T = C" "disjnt S T"
```
```   731 proof -
```
```   732   have "-S \<in> sets lebesgue"
```
```   733     using assms Compl_in_sets_lebesgue by blast
```
```   734   then obtain C T where C: "fsigma C" "negligible T" "C \<union> T = -S" "disjnt C T"
```
```   735     using lebesgue_set_almost_fsigma by metis
```
```   736   show thesis
```
```   737   proof
```
```   738     show "gdelta (-C)"
```
```   739       by (simp add: \<open>fsigma C\<close> fsigma_imp_gdelta)
```
```   740     show "S \<union> T = -C" "disjnt S T"
```
```   741       using C by (auto simp: disjnt_def)
```
```   742   qed (use C in auto)
```
```   743 qed
```
```   744
```
```   745
```
```   746 proposition%important measure_semicontinuous_with_hausdist_explicit:
```
```   747   assumes "bounded S" and neg: "negligible(frontier S)" and "e > 0"
```
```   748   obtains d where "d > 0"
```
```   749                   "\<And>T. \<lbrakk>T \<in> lmeasurable; \<And>y. y \<in> T \<Longrightarrow> \<exists>x. x \<in> S \<and> dist x y < d\<rbrakk>
```
```   750                         \<Longrightarrow> measure lebesgue T < measure lebesgue S + e"
```
```   751 proof%unimportant (cases "S = {}")
```
```   752   case True
```
```   753   with that \<open>e > 0\<close> show ?thesis by force
```
```   754 next
```
```   755   case False
```
```   756   then have frS: "frontier S \<noteq> {}"
```
```   757     using \<open>bounded S\<close> frontier_eq_empty not_bounded_UNIV by blast
```
```   758   have "S \<in> lmeasurable"
```
```   759     by (simp add: \<open>bounded S\<close> measurable_Jordan neg)
```
```   760   have null: "(frontier S) \<in> null_sets lebesgue"
```
```   761     by (metis neg negligible_iff_null_sets)
```
```   762   have "frontier S \<in> lmeasurable" and mS0: "measure lebesgue (frontier S) = 0"
```
```   763     using neg negligible_imp_measurable negligible_iff_measure by blast+
```
```   764   with \<open>e > 0\<close> lmeasurable_outer_open
```
```   765   obtain U where "open U"
```
```   766     and U: "frontier S \<subseteq> U" "U - frontier S \<in> lmeasurable" "measure lebesgue (U - frontier S) < e"
```
```   767     by (metis fmeasurableD)
```
```   768   with null have "U \<in> lmeasurable"
```
```   769     by (metis borel_open measurable_Diff_null_set sets_completionI_sets sets_lborel)
```
```   770   have "measure lebesgue (U - frontier S) = measure lebesgue U"
```
```   771     using mS0 by (simp add: \<open>U \<in> lmeasurable\<close> fmeasurableD measure_Diff_null_set null)
```
```   772   with U have mU: "measure lebesgue U < e"
```
```   773     by simp
```
```   774   show ?thesis
```
```   775   proof
```
```   776     have "U \<noteq> UNIV"
```
```   777       using \<open>U \<in> lmeasurable\<close> by auto
```
```   778     then have "- U \<noteq> {}"
```
```   779       by blast
```
```   780     with \<open>open U\<close> \<open>frontier S \<subseteq> U\<close> show "setdist (frontier S) (- U) > 0"
```
```   781       by (auto simp: \<open>bounded S\<close> open_closed compact_frontier_bounded setdist_gt_0_compact_closed frS)
```
```   782     fix T
```
```   783     assume "T \<in> lmeasurable"
```
```   784       and T: "\<And>t. t \<in> T \<Longrightarrow> \<exists>y. y \<in> S \<and> dist y t < setdist (frontier S) (- U)"
```
```   785     then have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue (T - S)"
```
```   786       by (simp add: \<open>S \<in> lmeasurable\<close> measure_diff_le_measure_setdiff)
```
```   787     also have "\<dots>  \<le> measure lebesgue U"
```
```   788     proof -
```
```   789       have "T - S \<subseteq> U"
```
```   790       proof clarify
```
```   791         fix x
```
```   792         assume "x \<in> T" and "x \<notin> S"
```
```   793         then obtain y where "y \<in> S" and y: "dist y x < setdist (frontier S) (- U)"
```
```   794           using T by blast
```
```   795         have "closed_segment x y \<inter> frontier S \<noteq> {}"
```
```   796           using connected_Int_frontier \<open>x \<notin> S\<close> \<open>y \<in> S\<close> by blast
```
```   797         then obtain z where z: "z \<in> closed_segment x y" "z \<in> frontier S"
```
```   798           by auto
```
```   799         with y have "dist z x < setdist(frontier S) (- U)"
```
```   800           by (auto simp: dist_commute dest!: dist_in_closed_segment)
```
```   801         with z have False if "x \<in> -U"
```
```   802           using setdist_le_dist [OF \<open>z \<in> frontier S\<close> that] by auto
```
```   803         then show "x \<in> U"
```
```   804           by blast
```
```   805       qed
```
```   806       then show ?thesis
```
```   807         by (simp add: \<open>S \<in> lmeasurable\<close> \<open>T \<in> lmeasurable\<close> \<open>U \<in> lmeasurable\<close> fmeasurableD measure_mono_fmeasurable sets.Diff)
```
```   808     qed
```
```   809     finally have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue U" .
```
```   810     with mU show "measure lebesgue T < measure lebesgue S + e"
```
```   811       by linarith
```
```   812   qed
```
```   813 qed
```
```   814
```
```   815 proposition%important lebesgue_regular_inner:
```
```   816  assumes "S \<in> sets lebesgue"
```
```   817  obtains K C where "negligible K" "\<And>n::nat. compact(C n)" "S = (\<Union>n. C n) \<union> K"
```
```   818 proof%unimportant -
```
```   819   have "\<exists>T. closed T \<and> T \<subseteq> S \<and> (S - T) \<in> lmeasurable \<and> measure lebesgue (S - T) < (1/2)^n" for n
```
```   820     using sets_lebesgue_inner_closed assms
```
```   821     by (metis sets_lebesgue_inner_closed zero_less_divide_1_iff zero_less_numeral zero_less_power)
```
```   822   then obtain C where clo: "\<And>n. closed (C n)" and subS: "\<And>n. C n \<subseteq> S"
```
```   823     and mea: "\<And>n. (S - C n) \<in> lmeasurable"
```
```   824     and less: "\<And>n. measure lebesgue (S - C n) < (1/2)^n"
```
```   825     by metis
```
```   826   have "\<exists>F. (\<forall>n::nat. compact(F n)) \<and> (\<Union>n. F n) = C m" for m::nat
```
```   827     by (metis clo closed_Union_compact_subsets)
```
```   828   then obtain D :: "[nat,nat] \<Rightarrow> 'a set" where D: "\<And>m n. compact(D m n)" "\<And>m. (\<Union>n. D m n) = C m"
```
```   829     by metis
```
```   830   let ?C = "from_nat_into (\<Union>m. range (D m))"
```
```   831   have "countable (\<Union>m. range (D m))"
```
```   832     by blast
```
```   833   have "range (from_nat_into (\<Union>m. range (D m))) = (\<Union>m. range (D m))"
```
```   834     using range_from_nat_into by simp
```
```   835   then have CD: "\<exists>m n. ?C k = D m n"  for k
```
```   836     by (metis (mono_tags, lifting) UN_iff rangeE range_eqI)
```
```   837   show thesis
```
```   838   proof
```
```   839     show "negligible (S - (\<Union>n. C n))"
```
```   840     proof (clarsimp simp: negligible_outer_le)
```
```   841       fix e :: "real"
```
```   842       assume "e > 0"
```
```   843       then obtain n where n: "(1/2)^n < e"
```
```   844         using real_arch_pow_inv [of e "1/2"] by auto
```
```   845       show "\<exists>T. S - (\<Union>n. C n) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
```
```   846       proof (intro exI conjI)
```
```   847         show "S - (\<Union>n. C n) \<subseteq> S - C n"
```
```   848           by blast
```
```   849         show "S - C n \<in> lmeasurable"
```
```   850           by (simp add: mea)
```
```   851         show "measure lebesgue (S - C n) \<le> e"
```
```   852           using less [of n] n by simp
```
```   853       qed
```
```   854     qed
```
```   855     show "compact (?C n)" for n
```
```   856       using CD D by metis
```
```   857     show "S = (\<Union>n. ?C n) \<union> (S - (\<Union>n. C n))" (is "_ = ?rhs")
```
```   858     proof
```
```   859       show "S \<subseteq> ?rhs"
```
```   860         using D by fastforce
```
```   861       show "?rhs \<subseteq> S"
```
```   862         using subS D CD by auto (metis Sup_upper range_eqI subsetCE)
```
```   863     qed
```
```   864   qed
```
```   865 qed
```
```   866
```
```   867
```
```   868 lemma%unimportant sets_lebesgue_continuous_image:
```
```   869   assumes T: "T \<in> sets lebesgue" and contf: "continuous_on S f"
```
```   870     and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)" and "T \<subseteq> S"
```
```   871  shows "f ` T \<in> sets lebesgue"
```
```   872 proof -
```
```   873   obtain K C where "negligible K" and com: "\<And>n::nat. compact(C n)" and Teq: "T = (\<Union>n. C n) \<union> K"
```
```   874     using lebesgue_regular_inner [OF T] by metis
```
```   875   then have comf: "\<And>n::nat. compact(f ` C n)"
```
```   876     by (metis Un_subset_iff Union_upper \<open>T \<subseteq> S\<close> compact_continuous_image contf continuous_on_subset rangeI)
```
```   877   have "((\<Union>n. f ` C n) \<union> f ` K) \<in> sets lebesgue"
```
```   878   proof (rule sets.Un)
```
```   879     have "K \<subseteq> S"
```
```   880       using Teq \<open>T \<subseteq> S\<close> by blast
```
```   881     show "(\<Union>n. f ` C n) \<in> sets lebesgue"
```
```   882     proof (rule sets.countable_Union)
```
```   883       show "range (\<lambda>n. f ` C n) \<subseteq> sets lebesgue"
```
```   884         using borel_compact comf by (auto simp: borel_compact)
```
```   885     qed auto
```
```   886     show "f ` K \<in> sets lebesgue"
```
```   887       by (simp add: \<open>K \<subseteq> S\<close> \<open>negligible K\<close> negim negligible_imp_sets)
```
```   888   qed
```
```   889   then show ?thesis
```
```   890     by (simp add: Teq image_Un image_Union)
```
```   891 qed
```
```   892
```
```   893 lemma%unimportant differentiable_image_in_sets_lebesgue:
```
```   894   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
```
```   895   assumes S: "S \<in> sets lebesgue" and dim: "DIM('m) \<le> DIM('n)" and f: "f differentiable_on S"
```
```   896   shows "f`S \<in> sets lebesgue"
```
```   897 proof (rule sets_lebesgue_continuous_image [OF S])
```
```   898   show "continuous_on S f"
```
```   899     by (meson differentiable_imp_continuous_on f)
```
```   900   show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
```
```   901     using differentiable_on_subset f
```
```   902     by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
```
```   903 qed auto
```
```   904
```
```   905 lemma%unimportant sets_lebesgue_on_continuous_image:
```
```   906   assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and contf: "continuous_on S f"
```
```   907     and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)"
```
```   908   shows "f ` X \<in> sets (lebesgue_on (f ` S))"
```
```   909 proof -
```
```   910   have "X \<subseteq> S"
```
```   911     by (metis S X sets.Int_space_eq2 sets_restrict_space_iff)
```
```   912   moreover have "f ` S \<in> sets lebesgue"
```
```   913     using S contf negim sets_lebesgue_continuous_image by blast
```
```   914   moreover have "f ` X \<in> sets lebesgue"
```
```   915     by (metis S X contf negim sets_lebesgue_continuous_image sets_restrict_space_iff space_restrict_space space_restrict_space2)
```
```   916   ultimately show ?thesis
```
```   917     by (auto simp: sets_restrict_space_iff)
```
```   918 qed
```
```   919
```
```   920 lemma%unimportant differentiable_image_in_sets_lebesgue_on:
```
```   921   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
```
```   922   assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and dim: "DIM('m) \<le> DIM('n)"
```
```   923        and f: "f differentiable_on S"
```
```   924      shows "f ` X \<in> sets (lebesgue_on (f`S))"
```
```   925 proof (rule sets_lebesgue_on_continuous_image [OF S X])
```
```   926   show "continuous_on S f"
```
```   927     by (meson differentiable_imp_continuous_on f)
```
```   928   show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
```
```   929     using differentiable_on_subset f
```
```   930     by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
```
```   931 qed
```
```   932
```
```   933
```
```   934 proposition%important
```
```   935  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```   936   assumes S: "S \<in> lmeasurable"
```
```   937   and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```   938   and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
```
```   939   and bounded: "\<And>x. x \<in> S \<Longrightarrow> \<bar>det (matrix (f' x))\<bar> \<le> B"
```
```   940   shows measurable_bounded_differentiable_image:
```
```   941        "f ` S \<in> lmeasurable"
```
```   942     and measure_bounded_differentiable_image:
```
```   943        "measure lebesgue (f ` S) \<le> B * measure lebesgue S" (is "?M")
```
```   944 proof%unimportant -
```
```   945   have "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> B * measure lebesgue S"
```
```   946   proof (cases "B < 0")
```
```   947     case True
```
```   948     then have "S = {}"
```
```   949       by (meson abs_ge_zero bounded empty_iff equalityI less_le_trans linorder_not_less subsetI)
```
```   950     then show ?thesis
```
```   951       by auto
```
```   952   next
```
```   953     case False
```
```   954     then have "B \<ge> 0"
```
```   955       by arith
```
```   956     let ?\<mu> = "measure lebesgue"
```
```   957     have f_diff: "f differentiable_on S"
```
```   958       using deriv by (auto simp: differentiable_on_def differentiable_def)
```
```   959     have eps: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> (B+e) * ?\<mu> S" (is "?ME")
```
```   960               if "e > 0" for e
```
```   961     proof -
```
```   962       have eps_d: "f ` S \<in> lmeasurable"  "?\<mu> (f ` S) \<le> (B+e) * (?\<mu> S + d)" (is "?MD")
```
```   963                   if "d > 0" for d
```
```   964       proof -
```
```   965         obtain T where "open T" "S \<subseteq> T" and TS: "(T-S) \<in> lmeasurable" and "?\<mu> (T-S) < d"
```
```   966           using S \<open>d > 0\<close> lmeasurable_outer_open by blast
```
```   967         with S have "T \<in> lmeasurable" and Tless: "?\<mu> T < ?\<mu> S + d"
```
```   968           by (auto simp: measurable_measure_Diff dest!: fmeasurable_Diff_D)
```
```   969         have "\<exists>r. 0 < r \<and> r < d \<and> ball x r \<subseteq> T \<and> f ` (S \<inter> ball x r) \<in> lmeasurable \<and>
```
```   970                   ?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)"
```
```   971           if "x \<in> S" "d > 0" for x d
```
```   972         proof -
```
```   973           have lin: "linear (f' x)"
```
```   974             and lim0: "((\<lambda>y. (f y - (f x + f' x (y - x))) /\<^sub>R norm(y - x)) \<longlongrightarrow> 0) (at x within S)"
```
```   975             using deriv \<open>x \<in> S\<close> by (auto simp: has_derivative_within bounded_linear.linear field_simps)
```
```   976           have bo: "bounded (f' x ` ball 0 1)"
```
```   977             by (simp add: bounded_linear_image linear_linear lin)
```
```   978           have neg: "negligible (frontier (f' x ` ball 0 1))"
```
```   979             using deriv has_derivative_linear \<open>x \<in> S\<close>
```
```   980             by (auto intro!: negligible_convex_frontier [OF convex_linear_image])
```
```   981           have 0: "0 < e * unit_ball_vol (real CARD('n))"
```
```   982             using  \<open>e > 0\<close> by simp
```
```   983           obtain k where "k > 0" and k:
```
```   984                   "\<And>U. \<lbrakk>U \<in> lmeasurable; \<And>y. y \<in> U \<Longrightarrow> \<exists>z. z \<in> f' x ` ball 0 1 \<and> dist z y < k\<rbrakk>
```
```   985                         \<Longrightarrow> ?\<mu> U < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
```
```   986             using measure_semicontinuous_with_hausdist_explicit [OF bo neg 0] by blast
```
```   987           obtain l where "l > 0" and l: "ball x l \<subseteq> T"
```
```   988             using \<open>x \<in> S\<close> \<open>open T\<close> \<open>S \<subseteq> T\<close> openE by blast
```
```   989           obtain \<zeta> where "0 < \<zeta>"
```
```   990             and \<zeta>: "\<And>y. \<lbrakk>y \<in> S; y \<noteq> x; dist y x < \<zeta>\<rbrakk>
```
```   991                         \<Longrightarrow> norm (f y - (f x + f' x (y - x))) / norm (y - x) < k"
```
```   992             using lim0 \<open>k > 0\<close> by (force simp: Lim_within field_simps)
```
```   993           define r where "r \<equiv> min (min l (\<zeta>/2)) (min 1 (d/2))"
```
```   994           show ?thesis
```
```   995           proof (intro exI conjI)
```
```   996             show "r > 0" "r < d"
```
```   997               using \<open>l > 0\<close> \<open>\<zeta> > 0\<close> \<open>d > 0\<close> by (auto simp: r_def)
```
```   998             have "r \<le> l"
```
```   999               by (auto simp: r_def)
```
```  1000             with l show "ball x r \<subseteq> T"
```
```  1001               by auto
```
```  1002             have ex_lessK: "\<exists>x' \<in> ball 0 1. dist (f' x x') ((f y - f x) /\<^sub>R r) < k"
```
```  1003               if "y \<in> S" and "dist x y < r" for y
```
```  1004             proof (cases "y = x")
```
```  1005               case True
```
```  1006               with lin linear_0 \<open>k > 0\<close> that show ?thesis
```
```  1007                 by (rule_tac x=0 in bexI) (auto simp: linear_0)
```
```  1008             next
```
```  1009               case False
```
```  1010               then show ?thesis
```
```  1011               proof (rule_tac x="(y - x) /\<^sub>R r" in bexI)
```
```  1012                 have "f' x ((y - x) /\<^sub>R r) = f' x (y - x) /\<^sub>R r"
```
```  1013                   by (simp add: lin linear_scale)
```
```  1014                 then have "dist (f' x ((y - x) /\<^sub>R r)) ((f y - f x) /\<^sub>R r) = norm (f' x (y - x) /\<^sub>R r - (f y - f x) /\<^sub>R r)"
```
```  1015                   by (simp add: dist_norm)
```
```  1016                 also have "\<dots> = norm (f' x (y - x) - (f y - f x)) / r"
```
```  1017                   using \<open>r > 0\<close> by (simp add: scale_right_diff_distrib [symmetric] divide_simps)
```
```  1018                 also have "\<dots> \<le> norm (f y - (f x + f' x (y - x))) / norm (y - x)"
```
```  1019                   using that \<open>r > 0\<close> False by (simp add: algebra_simps divide_simps dist_norm norm_minus_commute mult_right_mono)
```
```  1020                 also have "\<dots> < k"
```
```  1021                   using that \<open>0 < \<zeta>\<close> by (simp add: dist_commute r_def  \<zeta> [OF \<open>y \<in> S\<close> False])
```
```  1022                 finally show "dist (f' x ((y - x) /\<^sub>R r)) ((f y - f x) /\<^sub>R r) < k" .
```
```  1023                 show "(y - x) /\<^sub>R r \<in> ball 0 1"
```
```  1024                   using that \<open>r > 0\<close> by (simp add: dist_norm divide_simps norm_minus_commute)
```
```  1025               qed
```
```  1026             qed
```
```  1027             let ?rfs = "(\<lambda>x. x /\<^sub>R r) ` (+) (- f x) ` f ` (S \<inter> ball x r)"
```
```  1028             have rfs_mble: "?rfs \<in> lmeasurable"
```
```  1029             proof (rule bounded_set_imp_lmeasurable)
```
```  1030               have "f differentiable_on S \<inter> ball x r"
```
```  1031                 using f_diff by (auto simp: fmeasurableD differentiable_on_subset)
```
```  1032               with S show "?rfs \<in> sets lebesgue"
```
```  1033                 by (auto simp: sets.Int intro!: lebesgue_sets_translation differentiable_image_in_sets_lebesgue)
```
```  1034               let ?B = "(\<lambda>(x, y). x + y) ` (f' x ` ball 0 1 \<times> ball 0 k)"
```
```  1035               have "bounded ?B"
```
```  1036                 by (simp add: bounded_plus [OF bo])
```
```  1037               moreover have "?rfs \<subseteq> ?B"
```
```  1038                 apply (auto simp: dist_norm image_iff dest!: ex_lessK)
```
```  1039                 by (metis (no_types, hide_lams) add.commute diff_add_cancel dist_0_norm dist_commute dist_norm mem_ball)
```
```  1040               ultimately show "bounded (?rfs)"
```
```  1041                 by (rule bounded_subset)
```
```  1042             qed
```
```  1043             then have "(\<lambda>x. r *\<^sub>R x) ` ?rfs \<in> lmeasurable"
```
```  1044               by (simp add: measurable_linear_image)
```
```  1045             with \<open>r > 0\<close> have "(+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
```
```  1046               by (simp add: image_comp o_def)
```
```  1047             then have "(+) (f x) ` (+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
```
```  1048               using  measurable_translation by blast
```
```  1049             then show fsb: "f ` (S \<inter> ball x r) \<in> lmeasurable"
```
```  1050               by (simp add: image_comp o_def)
```
```  1051             have "?\<mu> (f ` (S \<inter> ball x r)) = ?\<mu> (?rfs) * r ^ CARD('n)"
```
```  1052               using \<open>r > 0\<close> by (simp add: measure_translation measure_linear_image measurable_translation fsb field_simps)
```
```  1053             also have "\<dots> \<le> (\<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))) * r ^ CARD('n)"
```
```  1054             proof -
```
```  1055               have "?\<mu> (?rfs) < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
```
```  1056                 using rfs_mble by (force intro: k dest!: ex_lessK)
```
```  1057               then have "?\<mu> (?rfs) < \<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))"
```
```  1058                 by (simp add: lin measure_linear_image [of "f' x"] content_ball)
```
```  1059               with \<open>r > 0\<close> show ?thesis
```
```  1060                 by auto
```
```  1061             qed
```
```  1062             also have "\<dots> \<le> (B + e) * ?\<mu> (ball x r)"
```
```  1063               using bounded [OF \<open>x \<in> S\<close>] \<open>r > 0\<close> by (simp add: content_ball algebra_simps)
```
```  1064             finally show "?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)" .
```
```  1065           qed
```
```  1066         qed
```
```  1067         then obtain r where
```
```  1068           r0d: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> 0 < r x d \<and> r x d < d"
```
```  1069           and rT: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> ball x (r x d) \<subseteq> T"
```
```  1070           and r: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow>
```
```  1071                   (f ` (S \<inter> ball x (r x d))) \<in> lmeasurable \<and>
```
```  1072                   ?\<mu> (f ` (S \<inter> ball x (r x d))) \<le> (B + e) * ?\<mu> (ball x (r x d))"
```
```  1073           by metis
```
```  1074         obtain C where "countable C" and Csub: "C \<subseteq> {(x,r x t) |x t. x \<in> S \<and> 0 < t}"
```
```  1075           and pwC: "pairwise (\<lambda>i j. disjnt (ball (fst i) (snd i)) (ball (fst j) (snd j))) C"
```
```  1076           and negC: "negligible(S - (\<Union>i \<in> C. ball (fst i) (snd i)))"
```
```  1077           apply (rule Vitali_covering_theorem_balls [of S "{(x,r x t) |x t. x \<in> S \<and> 0 < t}" fst snd])
```
```  1078            apply auto
```
```  1079           by (metis dist_eq_0_iff r0d)
```
```  1080         let ?UB = "(\<Union>(x,s) \<in> C. ball x s)"
```
```  1081         have eq: "f ` (S \<inter> ?UB) = (\<Union>(x,s) \<in> C. f ` (S \<inter> ball x s))"
```
```  1082           by auto
```
```  1083         have mle: "?\<mu> (\<Union>(x,s) \<in> K. f ` (S \<inter> ball x s)) \<le> (B + e) * (?\<mu> S + d)"  (is "?l \<le> ?r")
```
```  1084           if "K \<subseteq> C" and "finite K" for K
```
```  1085         proof -
```
```  1086           have gt0: "b > 0" if "(a, b) \<in> K" for a b
```
```  1087             using Csub that \<open>K \<subseteq> C\<close> r0d by auto
```
```  1088           have inj: "inj_on (\<lambda>(x, y). ball x y) K"
```
```  1089             by (force simp: inj_on_def ball_eq_ball_iff dest: gt0)
```
```  1090           have disjnt: "disjoint ((\<lambda>(x, y). ball x y) ` K)"
```
```  1091             using pwC that
```
```  1092             apply (clarsimp simp: pairwise_def case_prod_unfold ball_eq_ball_iff)
```
```  1093             by (metis subsetD fst_conv snd_conv)
```
```  1094           have "?l \<le> (\<Sum>i\<in>K. ?\<mu> (case i of (x, s) \<Rightarrow> f ` (S \<inter> ball x s)))"
```
```  1095           proof (rule measure_UNION_le [OF \<open>finite K\<close>], clarify)
```
```  1096             fix x r
```
```  1097             assume "(x,r) \<in> K"
```
```  1098             then have "x \<in> S"
```
```  1099               using Csub \<open>K \<subseteq> C\<close> by auto
```
```  1100             show "f ` (S \<inter> ball x r) \<in> sets lebesgue"
```
```  1101               by (meson Int_lower1 S differentiable_on_subset f_diff fmeasurableD lmeasurable_ball order_refl sets.Int differentiable_image_in_sets_lebesgue)
```
```  1102           qed
```
```  1103           also have "\<dots> \<le> (\<Sum>(x,s) \<in> K. (B + e) * ?\<mu> (ball x s))"
```
```  1104             apply (rule sum_mono)
```
```  1105             using Csub r \<open>K \<subseteq> C\<close> by auto
```
```  1106           also have "\<dots> = (B + e) * (\<Sum>(x,s) \<in> K. ?\<mu> (ball x s))"
```
```  1107             by (simp add: prod.case_distrib sum_distrib_left)
```
```  1108           also have "\<dots> = (B + e) * sum ?\<mu> ((\<lambda>(x, y). ball x y) ` K)"
```
```  1109             using \<open>B \<ge> 0\<close> \<open>e > 0\<close> by (simp add: inj sum.reindex prod.case_distrib)
```
```  1110           also have "\<dots> = (B + e) * ?\<mu> (\<Union>(x,s) \<in> K. ball x s)"
```
```  1111             using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that
```
```  1112             by (subst measure_Union') (auto simp: disjnt measure_Union')
```
```  1113           also have "\<dots> \<le> (B + e) * ?\<mu> T"
```
```  1114             using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that apply simp
```
```  1115             apply (rule measure_mono_fmeasurable [OF _ _ \<open>T \<in> lmeasurable\<close>])
```
```  1116             using Csub rT by force+
```
```  1117           also have "\<dots> \<le> (B + e) * (?\<mu> S + d)"
```
```  1118             using \<open>B \<ge> 0\<close> \<open>e > 0\<close> Tless by simp
```
```  1119           finally show ?thesis .
```
```  1120         qed
```
```  1121         have fSUB_mble: "(f ` (S \<inter> ?UB)) \<in> lmeasurable"
```
```  1122           unfolding eq using Csub r False \<open>e > 0\<close> that
```
```  1123           by (auto simp: intro!: fmeasurable_UN_bound [OF \<open>countable C\<close> _ mle])
```
```  1124         have fSUB_meas: "?\<mu> (f ` (S \<inter> ?UB)) \<le> (B + e) * (?\<mu> S + d)"  (is "?MUB")
```
```  1125           unfolding eq using Csub r False \<open>e > 0\<close> that
```
```  1126           by (auto simp: intro!: measure_UN_bound [OF \<open>countable C\<close> _ mle])
```
```  1127         have neg: "negligible ((f ` (S \<inter> ?UB) - f ` S) \<union> (f ` S - f ` (S \<inter> ?UB)))"
```
```  1128         proof (rule negligible_subset [OF negligible_differentiable_image_negligible [OF order_refl negC, where f=f]])
```
```  1129           show "f differentiable_on S - (\<Union>i\<in>C. ball (fst i) (snd i))"
```
```  1130             by (meson DiffE differentiable_on_subset subsetI f_diff)
```
```  1131         qed force
```
```  1132         show "f ` S \<in> lmeasurable"
```
```  1133           by (rule lmeasurable_negligible_symdiff [OF fSUB_mble neg])
```
```  1134         show ?MD
```
```  1135           using fSUB_meas measure_negligible_symdiff [OF fSUB_mble neg] by simp
```
```  1136       qed
```
```  1137       show "f ` S \<in> lmeasurable"
```
```  1138         using eps_d [of 1] by simp
```
```  1139       show ?ME
```
```  1140       proof (rule field_le_epsilon)
```
```  1141         fix \<delta> :: real
```
```  1142         assume "0 < \<delta>"
```
```  1143         then show "?\<mu> (f ` S) \<le> (B + e) * ?\<mu> S + \<delta>"
```
```  1144           using eps_d [of "\<delta> / (B+e)"] \<open>e > 0\<close> \<open>B \<ge> 0\<close> by (auto simp: divide_simps mult_ac)
```
```  1145       qed
```
```  1146     qed
```
```  1147     show ?thesis
```
```  1148     proof (cases "?\<mu> S = 0")
```
```  1149       case True
```
```  1150       with eps have "?\<mu> (f ` S) = 0"
```
```  1151         by (metis mult_zero_right not_le zero_less_measure_iff)
```
```  1152       then show ?thesis
```
```  1153         using eps [of 1] by (simp add: True)
```
```  1154     next
```
```  1155       case False
```
```  1156       have "?\<mu> (f ` S) \<le> B * ?\<mu> S"
```
```  1157       proof (rule field_le_epsilon)
```
```  1158         fix e :: real
```
```  1159         assume "e > 0"
```
```  1160         then show "?\<mu> (f ` S) \<le> B * ?\<mu> S + e"
```
```  1161           using eps [of "e / ?\<mu> S"] False by (auto simp: algebra_simps zero_less_measure_iff)
```
```  1162       qed
```
```  1163       with eps [of 1] show ?thesis by auto
```
```  1164     qed
```
```  1165   qed
```
```  1166   then show "f ` S \<in> lmeasurable" ?M by blast+
```
```  1167 qed
```
```  1168
```
```  1169 lemma%important
```
```  1170  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  1171   assumes S: "S \<in> lmeasurable"
```
```  1172     and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  1173     and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
```
```  1174   shows m_diff_image_weak: "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1175 proof%unimportant -
```
```  1176   let ?\<mu> = "measure lebesgue"
```
```  1177   have aint_S: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
```
```  1178     using int unfolding absolutely_integrable_on_def by auto
```
```  1179   define m where "m \<equiv> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1180   have *: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
```
```  1181     if "e > 0" for e
```
```  1182   proof -
```
```  1183     define T where "T \<equiv> \<lambda>n. {x \<in> S. n * e \<le> \<bar>det (matrix (f' x))\<bar> \<and>
```
```  1184                                      \<bar>det (matrix (f' x))\<bar> < (Suc n) * e}"
```
```  1185     have meas_t: "T n \<in> lmeasurable" for n
```
```  1186     proof -
```
```  1187       have *: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<in> borel_measurable (lebesgue_on S)"
```
```  1188         using aint_S by (simp add: S borel_measurable_restrict_space_iff fmeasurableD set_integrable_def)
```
```  1189       have [intro]: "x \<in> sets (lebesgue_on S) \<Longrightarrow> x \<in> sets lebesgue" for x
```
```  1190         using S sets_restrict_space_subset by blast
```
```  1191       have "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> sets lebesgue"
```
```  1192         using * by (auto simp: borel_measurable_iff_halfspace_ge space_restrict_space)
```
```  1193       then have 1: "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> lmeasurable"
```
```  1194         using S by (simp add: fmeasurableI2)
```
```  1195       have "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> sets lebesgue"
```
```  1196         using * by (auto simp: borel_measurable_iff_halfspace_less space_restrict_space)
```
```  1197       then have 2: "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> lmeasurable"
```
```  1198         using S by (simp add: fmeasurableI2)
```
```  1199       show ?thesis
```
```  1200         using fmeasurable.Int [OF 1 2] by (simp add: T_def Int_def cong: conj_cong)
```
```  1201     qed
```
```  1202     have aint_T: "\<And>k. (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on T k"
```
```  1203       using set_integrable_subset [OF aint_S] meas_t T_def by blast
```
```  1204     have Seq: "S = (\<Union>n. T n)"
```
```  1205       apply (auto simp: T_def)
```
```  1206       apply (rule_tac x="nat(floor(abs(det(matrix(f' x))) / e))" in exI)
```
```  1207       using that apply auto
```
```  1208       using of_int_floor_le pos_le_divide_eq apply blast
```
```  1209       by (metis add.commute pos_divide_less_eq real_of_int_floor_add_one_gt)
```
```  1210     have meas_ft: "f ` T n \<in> lmeasurable" for n
```
```  1211     proof (rule measurable_bounded_differentiable_image)
```
```  1212       show "T n \<in> lmeasurable"
```
```  1213         by (simp add: meas_t)
```
```  1214     next
```
```  1215       fix x :: "(real,'n) vec"
```
```  1216       assume "x \<in> T n"
```
```  1217       show "(f has_derivative f' x) (at x within T n)"
```
```  1218         by (metis (no_types, lifting) \<open>x \<in> T n\<close> deriv has_derivative_within_subset mem_Collect_eq subsetI T_def)
```
```  1219       show "\<bar>det (matrix (f' x))\<bar> \<le> (Suc n) * e"
```
```  1220         using \<open>x \<in> T n\<close> T_def by auto
```
```  1221     next
```
```  1222       show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T n"
```
```  1223         using aint_T absolutely_integrable_on_def by blast
```
```  1224     qed
```
```  1225     have disT: "disjoint (range T)"
```
```  1226       unfolding disjoint_def
```
```  1227     proof clarsimp
```
```  1228       show "T m \<inter> T n = {}" if "T m \<noteq> T n" for m n
```
```  1229         using that
```
```  1230       proof (induction m n rule: linorder_less_wlog)
```
```  1231         case (less m n)
```
```  1232         with \<open>e > 0\<close> show ?case
```
```  1233           unfolding T_def
```
```  1234           proof (clarsimp simp add: Collect_conj_eq [symmetric])
```
```  1235             fix x
```
```  1236             assume "e > 0"  "m < n"  "n * e \<le> \<bar>det (matrix (f' x))\<bar>"  "\<bar>det (matrix (f' x))\<bar> < (1 + real m) * e"
```
```  1237             then have "n < 1 + real m"
```
```  1238               by (metis (no_types, hide_lams) less_le_trans mult.commute not_le real_mult_le_cancel_iff2)
```
```  1239             then show "False"
```
```  1240               using less.hyps by linarith
```
```  1241           qed
```
```  1242       qed auto
```
```  1243     qed
```
```  1244     have injT: "inj_on T ({n. T n \<noteq> {}})"
```
```  1245       unfolding inj_on_def
```
```  1246     proof clarsimp
```
```  1247       show "m = n" if "T m = T n" "T n \<noteq> {}" for m n
```
```  1248         using that
```
```  1249       proof (induction m n rule: linorder_less_wlog)
```
```  1250         case (less m n)
```
```  1251         have False if "T n \<subseteq> T m" "x \<in> T n" for x
```
```  1252           using \<open>e > 0\<close> \<open>m < n\<close> that
```
```  1253           apply (auto simp: T_def  mult.commute intro: less_le_trans dest!: subsetD)
```
```  1254           by (metis add.commute less_le_trans nat_less_real_le not_le real_mult_le_cancel_iff2)
```
```  1255         then show ?case
```
```  1256           using less.prems by blast
```
```  1257       qed auto
```
```  1258     qed
```
```  1259     have sum_eq_Tim: "(\<Sum>k\<le>n. f (T k)) = sum f (T ` {..n})" if "f {} = 0" for f :: "_ \<Rightarrow> real" and n
```
```  1260     proof (subst sum.reindex_nontrivial)
```
```  1261       fix i j  assume "i \<in> {..n}" "j \<in> {..n}" "i \<noteq> j" "T i = T j"
```
```  1262       with that  injT [unfolded inj_on_def] show "f (T i) = 0"
```
```  1263         by simp metis
```
```  1264     qed (use atMost_atLeast0 in auto)
```
```  1265     let ?B = "m + e * ?\<mu> S"
```
```  1266     have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" for n
```
```  1267     proof -
```
```  1268       have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> (\<Sum>k\<le>n. ((k+1) * e) * ?\<mu>(T k))"
```
```  1269       proof (rule sum_mono [OF measure_bounded_differentiable_image])
```
```  1270         show "(f has_derivative f' x) (at x within T k)" if "x \<in> T k" for k x
```
```  1271           using that unfolding T_def by (blast intro: deriv has_derivative_within_subset)
```
```  1272         show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k" for k
```
```  1273           using absolutely_integrable_on_def aint_T by blast
```
```  1274         show "\<bar>det (matrix (f' x))\<bar> \<le> real (k + 1) * e" if "x \<in> T k" for k x
```
```  1275           using T_def that by auto
```
```  1276       qed (use meas_t in auto)
```
```  1277       also have "\<dots> \<le> (\<Sum>k\<le>n. (k * e) * ?\<mu>(T k)) + (\<Sum>k\<le>n. e * ?\<mu>(T k))"
```
```  1278         by (simp add: algebra_simps sum.distrib)
```
```  1279       also have "\<dots> \<le> ?B"
```
```  1280       proof (rule add_mono)
```
```  1281         have "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) = (\<Sum>k\<le>n. integral (T k) (\<lambda>x. k * e))"
```
```  1282           by (simp add: lmeasure_integral [OF meas_t]
```
```  1283                    flip: integral_mult_right integral_mult_left)
```
```  1284         also have "\<dots> \<le> (\<Sum>k\<le>n. integral (T k) (\<lambda>x.  (abs (det (matrix (f' x))))))"
```
```  1285         proof (rule sum_mono)
```
```  1286           fix k
```
```  1287           assume "k \<in> {..n}"
```
```  1288           show "integral (T k) (\<lambda>x. k * e) \<le> integral (T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1289           proof (rule integral_le [OF integrable_on_const [OF meas_t]])
```
```  1290             show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k"
```
```  1291               using absolutely_integrable_on_def aint_T by blast
```
```  1292           next
```
```  1293             fix x assume "x \<in> T k"
```
```  1294             show "k * e \<le> \<bar>det (matrix (f' x))\<bar>"
```
```  1295               using \<open>x \<in> T k\<close> T_def by blast
```
```  1296           qed
```
```  1297         qed
```
```  1298         also have "\<dots> = sum (\<lambda>T. integral T (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) (T ` {..n})"
```
```  1299           by (auto intro: sum_eq_Tim)
```
```  1300         also have "\<dots> = integral (\<Union>k\<le>n. T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1301         proof (rule integral_unique [OF has_integral_Union, symmetric])
```
```  1302           fix S  assume "S \<in> T ` {..n}"
```
```  1303           then show "((\<lambda>x. \<bar>det (matrix (f' x))\<bar>) has_integral integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) S"
```
```  1304           using absolutely_integrable_on_def aint_T by blast
```
```  1305         next
```
```  1306           show "pairwise (\<lambda>S S'. negligible (S \<inter> S')) (T ` {..n})"
```
```  1307             using disT unfolding disjnt_iff by (auto simp: pairwise_def intro!: empty_imp_negligible)
```
```  1308         qed auto
```
```  1309         also have "\<dots> \<le> m"
```
```  1310           unfolding m_def
```
```  1311         proof (rule integral_subset_le)
```
```  1312           have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on (\<Union>k\<le>n. T k)"
```
```  1313             apply (rule set_integrable_subset [OF aint_S])
```
```  1314              apply (intro measurable meas_t fmeasurableD)
```
```  1315             apply (force simp: Seq)
```
```  1316             done
```
```  1317           then show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on (\<Union>k\<le>n. T k)"
```
```  1318             using absolutely_integrable_on_def by blast
```
```  1319         qed (use Seq int in auto)
```
```  1320         finally show "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) \<le> m" .
```
```  1321       next
```
```  1322         have "(\<Sum>k\<le>n. ?\<mu> (T k)) = sum ?\<mu> (T ` {..n})"
```
```  1323           by (auto intro: sum_eq_Tim)
```
```  1324         also have "\<dots> = ?\<mu> (\<Union>k\<le>n. T k)"
```
```  1325           using S disT by (auto simp: pairwise_def meas_t intro: measure_Union' [symmetric])
```
```  1326         also have "\<dots> \<le> ?\<mu> S"
```
```  1327           using S by (auto simp: Seq intro: meas_t fmeasurableD measure_mono_fmeasurable)
```
```  1328         finally have "(\<Sum>k\<le>n. ?\<mu> (T k)) \<le> ?\<mu> S" .
```
```  1329         then show "(\<Sum>k\<le>n. e * ?\<mu> (T k)) \<le> e * ?\<mu> S"
```
```  1330           by (metis less_eq_real_def ordered_comm_semiring_class.comm_mult_left_mono sum_distrib_left that)
```
```  1331       qed
```
```  1332       finally show "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" .
```
```  1333     qed
```
```  1334     moreover have "measure lebesgue (\<Union>k\<le>n. f ` T k) \<le> (\<Sum>k\<le>n. ?\<mu> (f ` T k))" for n
```
```  1335       by (simp add: fmeasurableD meas_ft measure_UNION_le)
```
```  1336     ultimately have B_ge_m: "?\<mu> (\<Union>k\<le>n. (f ` T k)) \<le> ?B" for n
```
```  1337       by (meson order_trans)
```
```  1338     have "(\<Union>n. f ` T n) \<in> lmeasurable"
```
```  1339       by (rule fmeasurable_countable_Union [OF meas_ft B_ge_m])
```
```  1340     moreover have "?\<mu> (\<Union>n. f ` T n) \<le> m + e * ?\<mu> S"
```
```  1341       by (rule measure_countable_Union_le [OF meas_ft B_ge_m])
```
```  1342     ultimately show "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
```
```  1343       by (auto simp: Seq image_Union)
```
```  1344   qed
```
```  1345   show ?thesis
```
```  1346   proof
```
```  1347     show "f ` S \<in> lmeasurable"
```
```  1348       using * linordered_field_no_ub by blast
```
```  1349     let ?x = "m - ?\<mu> (f ` S)"
```
```  1350     have False if "?\<mu> (f ` S) > integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1351     proof -
```
```  1352       have ml: "m < ?\<mu> (f ` S)"
```
```  1353         using m_def that by blast
```
```  1354       then have "?\<mu> S \<noteq> 0"
```
```  1355         using "*"(2) bgauge_existence_lemma by fastforce
```
```  1356       with ml have 0: "0 < - (m - ?\<mu> (f ` S))/2 / ?\<mu> S"
```
```  1357         using that zero_less_measure_iff by force
```
```  1358       then show ?thesis
```
```  1359         using * [OF 0] that by (auto simp: divide_simps m_def split: if_split_asm)
```
```  1360     qed
```
```  1361     then show "?\<mu> (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1362       by fastforce
```
```  1363   qed
```
```  1364 qed
```
```  1365
```
```  1366
```
```  1367 theorem%important
```
```  1368  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  1369   assumes S: "S \<in> sets lebesgue"
```
```  1370     and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  1371     and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
```
```  1372   shows measurable_differentiable_image: "f ` S \<in> lmeasurable"
```
```  1373     and measure_differentiable_image:
```
```  1374        "measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is "?M")
```
```  1375 proof%unimportant -
```
```  1376   let ?I = "\<lambda>n::nat. cbox (vec (-n)) (vec n) \<inter> S"
```
```  1377   let ?\<mu> = "measure lebesgue"
```
```  1378   have "x \<in> cbox (vec (- real (nat \<lceil>norm x\<rceil>))) (vec (real (nat \<lceil>norm x\<rceil>)))" for x :: "real^'n::_"
```
```  1379     apply (auto simp: mem_box_cart)
```
```  1380     apply (metis abs_le_iff component_le_norm_cart minus_le_iff of_nat_ceiling order.trans)
```
```  1381     by (meson abs_le_D1 norm_bound_component_le_cart real_nat_ceiling_ge)
```
```  1382   then have Seq: "S = (\<Union>n. ?I n)"
```
```  1383     by auto
```
```  1384   have fIn: "f ` ?I n \<in> lmeasurable"
```
```  1385        and mfIn: "?\<mu> (f ` ?I n) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is ?MN) for n
```
```  1386   proof -
```
```  1387     have In: "?I n \<in> lmeasurable"
```
```  1388       by (simp add: S bounded_Int bounded_set_imp_lmeasurable sets.Int)
```
```  1389     moreover have "\<And>x. x \<in> ?I n \<Longrightarrow> (f has_derivative f' x) (at x within ?I n)"
```
```  1390       by (meson Int_iff deriv has_derivative_within_subset subsetI)
```
```  1391     moreover have int_In: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on ?I n"
```
```  1392     proof -
```
```  1393       have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
```
```  1394         using int absolutely_integrable_integrable_bound by force
```
```  1395       then have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on ?I n"
```
```  1396         by (metis (no_types) Int_lower1 In fmeasurableD inf_commute set_integrable_subset)
```
```  1397       then show ?thesis
```
```  1398         using absolutely_integrable_on_def by blast
```
```  1399     qed
```
```  1400     ultimately have "f ` ?I n \<in> lmeasurable" "?\<mu> (f ` ?I n) \<le> integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1401       using m_diff_image_weak by metis+
```
```  1402     moreover have "integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1403       by (simp add: int_In int integral_subset_le)
```
```  1404     ultimately show "f ` ?I n \<in> lmeasurable" ?MN
```
```  1405       by auto
```
```  1406   qed
```
```  1407   have "?I k \<subseteq> ?I n" if "k \<le> n" for k n
```
```  1408     by (rule Int_mono) (use that in \<open>auto simp: subset_interval_imp_cart\<close>)
```
```  1409   then have "(\<Union>k\<le>n. f ` ?I k) = f ` ?I n" for n
```
```  1410     by (fastforce simp add:)
```
```  1411   with mfIn have "?\<mu> (\<Union>k\<le>n. f ` ?I k) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" for n
```
```  1412     by simp
```
```  1413   then have "(\<Union>n. f ` ?I n) \<in> lmeasurable" "?\<mu> (\<Union>n. f ` ?I n) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  1414     by (rule fmeasurable_countable_Union [OF fIn] measure_countable_Union_le [OF fIn])+
```
```  1415   then show "f ` S \<in> lmeasurable" ?M
```
```  1416     by (metis Seq image_UN)+
```
```  1417 qed
```
```  1418
```
```  1419
```
```  1420 lemma%unimportant borel_measurable_simple_function_limit_increasing:
```
```  1421   fixes f :: "'a::euclidean_space \<Rightarrow> real"
```
```  1422   shows "(f \<in> borel_measurable lebesgue \<and> (\<forall>x. 0 \<le> f x)) \<longleftrightarrow>
```
```  1423          (\<exists>g. (\<forall>n x. 0 \<le> g n x \<and> g n x \<le> f x) \<and> (\<forall>n x. g n x \<le> (g(Suc n) x)) \<and>
```
```  1424               (\<forall>n. g n \<in> borel_measurable lebesgue) \<and> (\<forall>n. finite(range (g n))) \<and>
```
```  1425               (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x))"
```
```  1426          (is "?lhs = ?rhs")
```
```  1427 proof
```
```  1428   assume f: ?lhs
```
```  1429   have leb_f: "{x. a \<le> f x \<and> f x < b} \<in> sets lebesgue" for a b
```
```  1430   proof -
```
```  1431     have "{x. a \<le> f x \<and> f x < b} = {x. f x < b} - {x. f x < a}"
```
```  1432       by auto
```
```  1433     also have "\<dots> \<in> sets lebesgue"
```
```  1434       using borel_measurable_vimage_halfspace_component_lt [of f UNIV] f by auto
```
```  1435     finally show ?thesis .
```
```  1436   qed
```
```  1437   have "g n x \<le> f x"
```
```  1438         if inc_g: "\<And>n x. 0 \<le> g n x \<and> g n x \<le> g (Suc n) x"
```
```  1439            and meas_g: "\<And>n. g n \<in> borel_measurable lebesgue"
```
```  1440            and fin: "\<And>n. finite(range (g n))" and lim: "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x" for g n x
```
```  1441   proof -
```
```  1442     have "\<exists>r>0. \<forall>N. \<exists>n\<ge>N. dist (g n x) (f x) \<ge> r" if "g n x > f x"
```
```  1443     proof -
```
```  1444       have g: "g n x \<le> g (N + n) x" for N
```
```  1445         by (rule transitive_stepwise_le) (use inc_g in auto)
```
```  1446       have "\<exists>na\<ge>N. g n x - f x \<le> dist (g na x) (f x)" for N
```
```  1447         apply (rule_tac x="N+n" in exI)
```
```  1448         using g [of N] by (auto simp: dist_norm)
```
```  1449       with that show ?thesis
```
```  1450         using diff_gt_0_iff_gt by blast
```
```  1451     qed
```
```  1452     with lim show ?thesis
```
```  1453       apply (auto simp: lim_sequentially)
```
```  1454       by (meson less_le_not_le not_le_imp_less)
```
```  1455   qed
```
```  1456   moreover
```
```  1457   let ?\<Omega> = "\<lambda>n k. indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n}"
```
```  1458   let ?g = "\<lambda>n x. (\<Sum>k::real | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x)"
```
```  1459   have "\<exists>g. (\<forall>n x. 0 \<le> g n x \<and> g n x \<le> (g(Suc n) x)) \<and>
```
```  1460              (\<forall>n. g n \<in> borel_measurable lebesgue) \<and> (\<forall>n. finite(range (g n))) \<and>(\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x)"
```
```  1461   proof (intro exI allI conjI)
```
```  1462     show "0 \<le> ?g n x" for n x
```
```  1463     proof (clarify intro!: ordered_comm_monoid_add_class.sum_nonneg)
```
```  1464       fix k::real
```
```  1465       assume "k \<in> \<int>" and k: "\<bar>k\<bar> \<le> 2 ^ (2*n)"
```
```  1466       show "0 \<le> k/2^n * ?\<Omega> n k x"
```
```  1467         using f \<open>k \<in> \<int>\<close> apply (auto simp: indicator_def divide_simps Ints_def)
```
```  1468         apply (drule spec [where x=x])
```
```  1469         using zero_le_power [of "2::real" n] mult_nonneg_nonneg [of "f x" "2^n"]
```
```  1470         by linarith
```
```  1471     qed
```
```  1472     show "?g n x \<le> ?g (Suc n) x" for n x
```
```  1473     proof -
```
```  1474       have "?g n x =
```
```  1475             (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
```
```  1476               k/2^n * (indicator {y. k/2^n \<le> f y \<and> f y < (k+1/2)/2^n} x +
```
```  1477               indicator {y. (k+1/2)/2^n \<le> f y \<and> f y < (k+1)/2^n} x))"
```
```  1478         by (rule sum.cong [OF refl]) (simp add: indicator_def divide_simps)
```
```  1479       also have "\<dots> = (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1/2)/2^n} x) +
```
```  1480                        (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * indicator {y. (k+1/2)/2^n \<le> f y \<and> f y < (k+1)/2^n} x)"
```
```  1481         by (simp add:  comm_monoid_add_class.sum.distrib algebra_simps)
```
```  1482       also have "\<dots> = (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). (2 * k)/2 ^ Suc n * indicator {y. (2 * k)/2 ^ Suc n \<le> f y \<and> f y < (2 * k+1)/2 ^ Suc n} x) +
```
```  1483                        (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). (2 * k)/2 ^ Suc n * indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < ((2 * k+1) + 1)/2 ^ Suc n} x)"
```
```  1484         by (force simp: field_simps indicator_def intro: sum.cong)
```
```  1485       also have "\<dots> \<le> (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2 * Suc n). k/2 ^ Suc n * (indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x))"
```
```  1486                 (is "?a + _ \<le> ?b")
```
```  1487       proof -
```
```  1488         have *: "\<lbrakk>sum f I \<le> sum h I; a + sum h I \<le> b\<rbrakk> \<Longrightarrow> a + sum f I \<le> b" for I a b f and h :: "real\<Rightarrow>real"
```
```  1489           by linarith
```
```  1490         let ?h = "\<lambda>k. (2*k+1)/2 ^ Suc n *
```
```  1491                       (indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < ((2*k+1) + 1)/2 ^ Suc n} x)"
```
```  1492         show ?thesis
```
```  1493         proof (rule *)
```
```  1494           show "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
```
```  1495                   2 * k/2 ^ Suc n * indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < (2 * k+1 + 1)/2 ^ Suc n} x)
```
```  1496                 \<le> sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
```
```  1497             by (rule sum_mono) (simp add: indicator_def divide_simps)
```
```  1498         next
```
```  1499           have \<alpha>: "?a = (\<Sum>k \<in> (*)2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
```
```  1500                          k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
```
```  1501             by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
```
```  1502           have \<beta>: "sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
```
```  1503                    = (\<Sum>k \<in> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
```
```  1504                       k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
```
```  1505             by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
```
```  1506           have 0: "(*) 2 ` {k \<in> \<int>. P k} \<inter> (\<lambda>x. 2 * x + 1) ` {k \<in> \<int>. P k} = {}" for P :: "real \<Rightarrow> bool"
```
```  1507           proof -
```
```  1508             have "2 * i \<noteq> 2 * j + 1" for i j :: int by arith
```
```  1509             thus ?thesis
```
```  1510               unfolding Ints_def by auto (use of_int_eq_iff in fastforce)
```
```  1511           qed
```
```  1512           have "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
```
```  1513                 = (\<Sum>k \<in> (*)2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
```
```  1514                   k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
```
```  1515             unfolding \<alpha> \<beta>
```
```  1516             using finite_abs_int_segment [of "2 ^ (2*n)"]
```
```  1517             by (subst sum_Un) (auto simp: 0)
```
```  1518           also have "\<dots> \<le> ?b"
```
```  1519           proof (rule sum_mono2)
```
```  1520             show "finite {k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)}"
```
```  1521               by (rule finite_abs_int_segment)
```
```  1522             show "(*) 2 ` {k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2^(2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2^(2*n)} \<subseteq> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)}"
```
```  1523               apply auto
```
```  1524               using one_le_power [of "2::real" "2*n"]  by linarith
```
```  1525             have *: "\<lbrakk>x \<in> (S \<union> T) - U; \<And>x. x \<in> S \<Longrightarrow> x \<in> U; \<And>x. x \<in> T \<Longrightarrow> x \<in> U\<rbrakk> \<Longrightarrow> P x" for S T U P
```
```  1526               by blast
```
```  1527             have "0 \<le> b" if "b \<in> \<int>" "f x * (2 * 2^n) < b + 1" for b
```
```  1528             proof -
```
```  1529               have "0 \<le> f x * (2 * 2^n)"
```
```  1530                 by (simp add: f)
```
```  1531               also have "\<dots> < b+1"
```
```  1532                 by (simp add: that)
```
```  1533               finally show "0 \<le> b"
```
```  1534                 using \<open>b \<in> \<int>\<close> by (auto simp: elim!: Ints_cases)
```
```  1535             qed
```
```  1536             then show "0 \<le> b/2 ^ Suc n * indicator {y. b/2 ^ Suc n \<le> f y \<and> f y < (b + 1)/2 ^ Suc n} x"
```
```  1537                   if "b \<in> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)} -
```
```  1538                           ((*) 2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})" for b
```
```  1539               using that by (simp add: indicator_def divide_simps)
```
```  1540           qed
```
```  1541           finally show "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<le> ?b" .
```
```  1542         qed
```
```  1543       qed
```
```  1544       finally show ?thesis .
```
```  1545     qed
```
```  1546     show "?g n \<in> borel_measurable lebesgue" for n
```
```  1547       apply (intro borel_measurable_indicator borel_measurable_times borel_measurable_sum)
```
```  1548       using leb_f sets_restrict_UNIV by auto
```
```  1549     show "finite (range (?g n))" for n
```
```  1550     proof -
```
```  1551       have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x)
```
```  1552               \<in> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}" for x
```
```  1553       proof (cases "\<exists>k. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n) \<and> k/2^n \<le> f x \<and> f x < (k+1)/2^n")
```
```  1554         case True
```
```  1555         then show ?thesis
```
```  1556           by (blast intro: indicator_sum_eq)
```
```  1557       next
```
```  1558         case False
```
```  1559         then have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) = 0"
```
```  1560           by auto
```
```  1561         then show ?thesis by force
```
```  1562       qed
```
```  1563       then have "range (?g n) \<subseteq> ((\<lambda>k. (k/2^n)) ` {k. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n)})"
```
```  1564         by auto
```
```  1565       moreover have "finite ((\<lambda>k::real. (k/2^n)) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})"
```
```  1566         by (intro finite_imageI finite_abs_int_segment)
```
```  1567       ultimately show ?thesis
```
```  1568         by (rule finite_subset)
```
```  1569     qed
```
```  1570     show "(\<lambda>n. ?g n x) \<longlonglongrightarrow> f x" for x
```
```  1571     proof (clarsimp simp add: lim_sequentially)
```
```  1572       fix e::real
```
```  1573       assume "e > 0"
```
```  1574       obtain N1 where N1: "2 ^ N1 > abs(f x)"
```
```  1575         using real_arch_pow by fastforce
```
```  1576       obtain N2 where N2: "(1/2) ^ N2 < e"
```
```  1577         using real_arch_pow_inv \<open>e > 0\<close> by fastforce
```
```  1578       have "dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) (f x) < e" if "N1 + N2 \<le> n" for n
```
```  1579       proof -
```
```  1580         let ?m = "real_of_int \<lfloor>2^n * f x\<rfloor>"
```
```  1581         have "\<bar>?m\<bar> \<le> 2^n * 2^N1"
```
```  1582           using N1 apply (simp add: f)
```
```  1583           by (meson floor_mono le_floor_iff less_le_not_le mult_le_cancel_left_pos zero_less_numeral zero_less_power)
```
```  1584         also have "\<dots> \<le> 2 ^ (2*n)"
```
```  1585           by (metis that add_leD1 add_le_cancel_left mult.commute mult_2_right one_less_numeral_iff
```
```  1586                     power_add power_increasing_iff semiring_norm(76))
```
```  1587         finally have m_le: "\<bar>?m\<bar> \<le> 2 ^ (2*n)" .
```
```  1588         have "?m/2^n \<le> f x" "f x < (?m + 1)/2^n"
```
```  1589           by (auto simp: mult.commute pos_divide_le_eq mult_imp_less_div_pos)
```
```  1590         then have eq: "dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) (f x)
```
```  1591                      = dist (?m/2^n) (f x)"
```
```  1592           by (subst indicator_sum_eq [of ?m]) (auto simp: m_le)
```
```  1593         have "\<bar>2^n\<bar> * \<bar>?m/2^n - f x\<bar> = \<bar>2^n * (?m/2^n - f x)\<bar>"
```
```  1594           by (simp add: abs_mult)
```
```  1595         also have "\<dots> < 2 ^ N2 * e"
```
```  1596           using N2 by (simp add: divide_simps mult.commute) linarith
```
```  1597         also have "\<dots> \<le> \<bar>2^n\<bar> * e"
```
```  1598           using that \<open>e > 0\<close> by auto
```
```  1599         finally have "dist (?m/2^n) (f x) < e"
```
```  1600           by (simp add: dist_norm)
```
```  1601         then show ?thesis
```
```  1602           using eq by linarith
```
```  1603       qed
```
```  1604       then show "\<exists>no. \<forall>n\<ge>no. dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k * ?\<Omega> n k x/2^n) (f x) < e"
```
```  1605         by force
```
```  1606     qed
```
```  1607   qed
```
```  1608   ultimately show ?rhs
```
```  1609     by metis
```
```  1610 next
```
```  1611   assume RHS: ?rhs
```
```  1612   with borel_measurable_simple_function_limit [of f UNIV, unfolded borel_measurable_UNIV_eq]
```
```  1613   show ?lhs
```
```  1614     by (blast intro: order_trans)
```
```  1615 qed
```
```  1616
```
```  1617 subsection%important\<open>Borel measurable Jacobian determinant\<close>
```
```  1618
```
```  1619 lemma%unimportant lemma_partial_derivatives0:
```
```  1620   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
```
```  1621   assumes "linear f" and lim0: "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within S)"
```
```  1622     and lb: "\<And>v. v \<noteq> 0 \<Longrightarrow> (\<exists>k>0. \<forall>e>0. \<exists>x. x \<in> S - {0} \<and> norm x < e \<and> k * norm x \<le> \<bar>v \<bullet> x\<bar>)"
```
```  1623   shows "f x = 0"
```
```  1624 proof -
```
```  1625   interpret linear f by fact
```
```  1626   have "dim {x. f x = 0} \<le> DIM('a)"
```
```  1627     by (rule dim_subset_UNIV)
```
```  1628   moreover have False if less: "dim {x. f x = 0} < DIM('a)"
```
```  1629   proof -
```
```  1630     obtain d where "d \<noteq> 0" and d: "\<And>y. f y = 0 \<Longrightarrow> d \<bullet> y = 0"
```
```  1631       using orthogonal_to_subspace_exists [OF less] orthogonal_def
```
```  1632       by (metis (mono_tags, lifting) mem_Collect_eq span_base)
```
```  1633     then obtain k where "k > 0"
```
```  1634       and k: "\<And>e. e > 0 \<Longrightarrow> \<exists>y. y \<in> S - {0} \<and> norm y < e \<and> k * norm y \<le> \<bar>d \<bullet> y\<bar>"
```
```  1635       using lb by blast
```
```  1636     have "\<exists>h. \<forall>n. ((h n \<in> S \<and> h n \<noteq> 0 \<and> k * norm (h n) \<le> \<bar>d \<bullet> h n\<bar>) \<and> norm (h n) < 1 / real (Suc n)) \<and>
```
```  1637                norm (h (Suc n)) < norm (h n)"
```
```  1638     proof (rule dependent_nat_choice)
```
```  1639       show "\<exists>y. (y \<in> S \<and> y \<noteq> 0 \<and> k * norm y \<le> \<bar>d \<bullet> y\<bar>) \<and> norm y < 1 / real (Suc 0)"
```
```  1640         by simp (metis DiffE insertCI k not_less not_one_le_zero)
```
```  1641     qed (use k [of "min (norm x) (1/(Suc n + 1))" for x n] in auto)
```
```  1642     then obtain \<alpha> where \<alpha>: "\<And>n. \<alpha> n \<in> S - {0}" and kd: "\<And>n. k * norm(\<alpha> n) \<le> \<bar>d \<bullet> \<alpha> n\<bar>"
```
```  1643          and norm_lt: "\<And>n. norm(\<alpha> n) < 1/(Suc n)"
```
```  1644       by force
```
```  1645     let ?\<beta> = "\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)"
```
```  1646     have com: "\<And>g. (\<forall>n. g n \<in> sphere (0::'a) 1)
```
```  1647               \<Longrightarrow> \<exists>l \<in> sphere 0 1. \<exists>\<rho>::nat\<Rightarrow>nat. strict_mono \<rho> \<and> (g \<circ> \<rho>) \<longlonglongrightarrow> l"
```
```  1648       using compact_sphere compact_def by metis
```
```  1649     moreover have "\<forall>n. ?\<beta> n \<in> sphere 0 1"
```
```  1650       using \<alpha> by auto
```
```  1651     ultimately obtain l::'a and \<rho>::"nat\<Rightarrow>nat"
```
```  1652        where l: "l \<in> sphere 0 1" and "strict_mono \<rho>" and to_l: "(?\<beta> \<circ> \<rho>) \<longlonglongrightarrow> l"
```
```  1653       by meson
```
```  1654     moreover have "continuous (at l) (\<lambda>x. (\<bar>d \<bullet> x\<bar> - k))"
```
```  1655       by (intro continuous_intros)
```
```  1656     ultimately have lim_dl: "((\<lambda>x. (\<bar>d \<bullet> x\<bar> - k)) \<circ> (?\<beta> \<circ> \<rho>)) \<longlonglongrightarrow> (\<bar>d \<bullet> l\<bar> - k)"
```
```  1657       by (meson continuous_imp_tendsto)
```
```  1658     have "\<forall>\<^sub>F i in sequentially. 0 \<le> ((\<lambda>x. \<bar>d \<bullet> x\<bar> - k) \<circ> ((\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>)) i"
```
```  1659       using \<alpha> kd by (auto simp: divide_simps)
```
```  1660     then have "k \<le> \<bar>d \<bullet> l\<bar>"
```
```  1661       using tendsto_lowerbound [OF lim_dl, of 0] by auto
```
```  1662     moreover have "d \<bullet> l = 0"
```
```  1663     proof (rule d)
```
```  1664       show "f l = 0"
```
```  1665       proof (rule LIMSEQ_unique [of "f \<circ> ?\<beta> \<circ> \<rho>"])
```
```  1666         have "isCont f l"
```
```  1667           using \<open>linear f\<close> linear_continuous_at linear_conv_bounded_linear by blast
```
```  1668         then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> f l"
```
```  1669           unfolding comp_assoc
```
```  1670           using to_l continuous_imp_tendsto by blast
```
```  1671         have "\<alpha> \<longlonglongrightarrow> 0"
```
```  1672           using norm_lt LIMSEQ_norm_0 by metis
```
```  1673         with \<open>strict_mono \<rho>\<close> have "(\<alpha> \<circ> \<rho>) \<longlonglongrightarrow> 0"
```
```  1674           by (metis LIMSEQ_subseq_LIMSEQ)
```
```  1675         with lim0 \<alpha> have "((\<lambda>x. f x /\<^sub>R norm x) \<circ> (\<alpha> \<circ> \<rho>)) \<longlonglongrightarrow> 0"
```
```  1676           by (force simp: tendsto_at_iff_sequentially)
```
```  1677         then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> 0"
```
```  1678           by (simp add: o_def scale)
```
```  1679       qed
```
```  1680     qed
```
```  1681     ultimately show False
```
```  1682       using \<open>k > 0\<close> by auto
```
```  1683   qed
```
```  1684   ultimately have dim: "dim {x. f x = 0} = DIM('a)"
```
```  1685     by force
```
```  1686   then show ?thesis
```
```  1687     using dim_eq_full
```
```  1688     by (metis (mono_tags, lifting) eq_0_on_span eucl.span_Basis linear_axioms linear_eq_stdbasis
```
```  1689         mem_Collect_eq module_hom_zero span_base span_raw_def)
```
```  1690 qed
```
```  1691
```
```  1692 lemma%unimportant lemma_partial_derivatives:
```
```  1693   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
```
```  1694   assumes "linear f" and lim: "((\<lambda>x. f (x - a) /\<^sub>R norm (x - a)) \<longlongrightarrow> 0) (at a within S)"
```
```  1695     and lb: "\<And>v. v \<noteq> 0 \<Longrightarrow> (\<exists>k>0.  \<forall>e>0. \<exists>x \<in> S - {a}. norm(a - x) < e \<and> k * norm(a - x) \<le> \<bar>v \<bullet> (x - a)\<bar>)"
```
```  1696   shows "f x = 0"
```
```  1697 proof -
```
```  1698   have "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within (\<lambda>x. x-a) ` S)"
```
```  1699     using lim by (simp add: Lim_within dist_norm)
```
```  1700   then show ?thesis
```
```  1701   proof (rule lemma_partial_derivatives0 [OF \<open>linear f\<close>])
```
```  1702     fix v :: "'a"
```
```  1703     assume v: "v \<noteq> 0"
```
```  1704     show "\<exists>k>0. \<forall>e>0. \<exists>x. x \<in> (\<lambda>x. x - a) ` S - {0} \<and> norm x < e \<and> k * norm x \<le> \<bar>v \<bullet> x\<bar>"
```
```  1705       using lb [OF v] by (force simp:  norm_minus_commute)
```
```  1706   qed
```
```  1707 qed
```
```  1708
```
```  1709
```
```  1710 proposition%important borel_measurable_partial_derivatives:
```
```  1711   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n"
```
```  1712   assumes S: "S \<in> sets lebesgue"
```
```  1713     and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  1714   shows "(\<lambda>x. (matrix(f' x)\$m\$n)) \<in> borel_measurable (lebesgue_on S)"
```
```  1715 proof%unimportant -
```
```  1716   have contf: "continuous_on S f"
```
```  1717     using continuous_on_eq_continuous_within f has_derivative_continuous by blast
```
```  1718   have "{x \<in> S.  (matrix (f' x)\$m\$n) \<le> b} \<in> sets lebesgue" for b
```
```  1719   proof (rule sets_negligible_symdiff)
```
```  1720     let ?T = "{x \<in> S. \<forall>e>0. \<exists>d>0. \<exists>A. A\$m\$n < b \<and> (\<forall>i j. A\$i\$j \<in> \<rat>) \<and>
```
```  1721                        (\<forall>y \<in> S. norm(y - x) < d \<longrightarrow> norm(f y - f x - A *v (y - x)) \<le> e * norm(y - x))}"
```
```  1722     let ?U = "S \<inter>
```
```  1723               (\<Inter>e \<in> {e \<in> \<rat>. e > 0}.
```
```  1724                 \<Union>A \<in> {A. A\$m\$n < b \<and> (\<forall>i j. A\$i\$j \<in> \<rat>)}.
```
```  1725                   \<Union>d \<in> {d \<in> \<rat>. 0 < d}.
```
```  1726                      S \<inter> (\<Inter>y \<in> S. {x \<in> S. norm(y - x) < d \<longrightarrow> norm(f y - f x - A *v (y - x)) \<le> e * norm(y - x)}))"
```
```  1727     have "?T = ?U"
```
```  1728     proof (intro set_eqI iffI)
```
```  1729       fix x
```
```  1730       assume xT: "x \<in> ?T"
```
```  1731       then show "x \<in> ?U"
```
```  1732       proof (clarsimp simp add:)
```
```  1733         fix q :: real
```
```  1734         assume "q \<in> \<rat>" "q > 0"
```
```  1735         then obtain d A where "d > 0" and A: "A \$ m \$ n < b" "\<And>i j. A \$ i \$ j \<in> \<rat>"
```
```  1736           "\<And>y. \<lbrakk>y\<in>S;  norm (y - x) < d\<rbrakk> \<Longrightarrow> norm (f y - f x - A *v (y - x)) \<le> q * norm (y - x)"
```
```  1737           using xT by auto
```
```  1738         then obtain \<delta> where "d > \<delta>" "\<delta> > 0" "\<delta> \<in> \<rat>"
```
```  1739           using Rats_dense_in_real by blast
```
```  1740         with A show "\<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
```
```  1741                          (\<exists>s. s \<in> \<rat> \<and> 0 < s \<and> (\<forall>y\<in>S. norm (y - x) < s \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> q * norm (y - x)))"
```
```  1742           by force
```
```  1743       qed
```
```  1744     next
```
```  1745       fix x
```
```  1746       assume xU: "x \<in> ?U"
```
```  1747       then show "x \<in> ?T"
```
```  1748       proof clarsimp
```
```  1749         fix e :: "real"
```
```  1750         assume "e > 0"
```
```  1751         then obtain \<epsilon> where \<epsilon>: "e > \<epsilon>" "\<epsilon> > 0" "\<epsilon> \<in> \<rat>"
```
```  1752           using Rats_dense_in_real by blast
```
```  1753         with xU obtain A r where "x \<in> S" and Ar: "A \$ m \$ n < b" "\<forall>i j. A \$ i \$ j \<in> \<rat>" "r \<in> \<rat>" "r > 0"
```
```  1754           and "\<forall>y\<in>S. norm (y - x) < r \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> \<epsilon> * norm (y - x)"
```
```  1755           by (auto simp: split: if_split_asm)
```
```  1756         then have "\<forall>y\<in>S. norm (y - x) < r \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)"
```
```  1757           by (meson \<open>e > \<epsilon>\<close> less_eq_real_def mult_right_mono norm_ge_zero order_trans)
```
```  1758         then show "\<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and> (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
```
```  1759           using \<open>x \<in> S\<close> Ar by blast
```
```  1760       qed
```
```  1761     qed
```
```  1762     moreover have "?U \<in> sets lebesgue"
```
```  1763     proof -
```
```  1764       have coQ: "countable {e \<in> \<rat>. 0 < e}"
```
```  1765         using countable_Collect countable_rat by blast
```
```  1766       have ne: "{e \<in> \<rat>. (0::real) < e} \<noteq> {}"
```
```  1767         using zero_less_one Rats_1 by blast
```
```  1768       have coA: "countable {A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>)}"
```
```  1769       proof (rule countable_subset)
```
```  1770         show "countable {A. \<forall>i j. A \$ i \$ j \<in> \<rat>}"
```
```  1771           using countable_vector [OF countable_vector, of "\<lambda>i j. \<rat>"] by (simp add: countable_rat)
```
```  1772       qed blast
```
```  1773       have *: "\<lbrakk>U \<noteq> {} \<Longrightarrow> closedin (subtopology euclidean S) (S \<inter> \<Inter> U)\<rbrakk>
```
```  1774                \<Longrightarrow> closedin (subtopology euclidean S) (S \<inter> \<Inter> U)" for U
```
```  1775         by fastforce
```
```  1776       have eq: "{x::(real,'m)vec. P x \<and> (Q x \<longrightarrow> R x)} = {x. P x \<and> \<not> Q x} \<union> {x. P x \<and> R x}" for P Q R
```
```  1777         by auto
```
```  1778       have sets: "S \<inter> (\<Inter>y\<in>S. {x \<in> S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)})
```
```  1779                   \<in> sets lebesgue" for e A d
```
```  1780       proof -
```
```  1781         have clo: "closedin (subtopology euclidean S)
```
```  1782                      {x \<in> S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)}"
```
```  1783           for y
```
```  1784         proof -
```
```  1785           have cont1: "continuous_on S (\<lambda>x. norm (y - x))"
```
```  1786           and  cont2: "continuous_on S (\<lambda>x. e * norm (y - x) - norm (f y - f x - (A *v y - A *v x)))"
```
```  1787             by (force intro: contf continuous_intros)+
```
```  1788           have clo1: "closedin (subtopology euclidean S) {x \<in> S. d \<le> norm (y - x)}"
```
```  1789             using continuous_closedin_preimage [OF cont1, of "{d..}"] by (simp add: vimage_def Int_def)
```
```  1790           have clo2: "closedin (subtopology euclidean S)
```
```  1791                        {x \<in> S. norm (f y - f x - (A *v y - A *v x)) \<le> e * norm (y - x)}"
```
```  1792             using continuous_closedin_preimage [OF cont2, of "{0..}"] by (simp add: vimage_def Int_def)
```
```  1793           show ?thesis
```
```  1794             by (auto simp: eq not_less matrix_vector_mult_diff_distrib intro: clo1 clo2)
```
```  1795         qed
```
```  1796         show ?thesis
```
```  1797           by (rule lebesgue_closedin [of S]) (force intro: * S clo)+
```
```  1798       qed
```
```  1799       show ?thesis
```
```  1800         by (intro sets sets.Int S sets.countable_UN'' sets.countable_INT'' coQ coA) auto
```
```  1801     qed
```
```  1802     ultimately show "?T \<in> sets lebesgue"
```
```  1803       by simp
```
```  1804     let ?M = "(?T - {x \<in> S. matrix (f' x) \$ m \$ n \<le> b} \<union> ({x \<in> S. matrix (f' x) \$ m \$ n \<le> b} - ?T))"
```
```  1805     let ?\<Theta> = "\<lambda>x v. \<forall>\<xi>>0. \<exists>e>0. \<forall>y \<in> S-{x}. norm (x - y) < e \<longrightarrow> \<bar>v \<bullet> (y - x)\<bar> < \<xi> * norm (x - y)"
```
```  1806     have nN: "negligible {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
```
```  1807       unfolding negligible_eq_zero_density
```
```  1808     proof clarsimp
```
```  1809       fix x v and r e :: "real"
```
```  1810       assume "x \<in> S" "v \<noteq> 0" "r > 0" "e > 0"
```
```  1811       and Theta [rule_format]: "?\<Theta> x v"
```
```  1812       moreover have "(norm v * e / 2) / CARD('m) ^ CARD('m) > 0"
```
```  1813         by (simp add: \<open>v \<noteq> 0\<close> \<open>e > 0\<close>)
```
```  1814       ultimately obtain d where "d > 0"
```
```  1815          and dless: "\<And>y. \<lbrakk>y \<in> S - {x}; norm (x - y) < d\<rbrakk> \<Longrightarrow>
```
```  1816                         \<bar>v \<bullet> (y - x)\<bar> < ((norm v * e / 2) / CARD('m) ^ CARD('m)) * norm (x - y)"
```
```  1817         by metis
```
```  1818       let ?W = "ball x (min d r) \<inter> {y. \<bar>v \<bullet> (y - x)\<bar> < (norm v * e/2 * min d r) / CARD('m) ^ CARD('m)}"
```
```  1819       have "open {x. \<bar>v \<bullet> (x - a)\<bar> < b}" for a b
```
```  1820         by (intro open_Collect_less continuous_intros)
```
```  1821       show "\<exists>d>0. d \<le> r \<and>
```
```  1822             (\<exists>U. {x' \<in> S. \<exists>v\<noteq>0. ?\<Theta> x' v} \<inter> ball x d \<subseteq> U \<and>
```
```  1823                  U \<in> lmeasurable \<and> measure lebesgue U < e * content (ball x d))"
```
```  1824       proof (intro exI conjI)
```
```  1825         show "0 < min d r" "min d r \<le> r"
```
```  1826           using \<open>r > 0\<close> \<open>d > 0\<close> by auto
```
```  1827         show "{x' \<in> S. \<exists>v. v \<noteq> 0 \<and> (\<forall>\<xi>>0. \<exists>e>0. \<forall>z\<in>S - {x'}. norm (x' - z) < e \<longrightarrow> \<bar>v \<bullet> (z - x')\<bar> < \<xi> * norm (x' - z))} \<inter> ball x (min d r) \<subseteq> ?W"
```
```  1828           proof (clarsimp simp: dist_norm norm_minus_commute)
```
```  1829             fix y w
```
```  1830             assume "y \<in> S" "w \<noteq> 0"
```
```  1831               and less [rule_format]:
```
```  1832                     "\<forall>\<xi>>0. \<exists>e>0. \<forall>z\<in>S - {y}. norm (y - z) < e \<longrightarrow> \<bar>w \<bullet> (z - y)\<bar> < \<xi> * norm (y - z)"
```
```  1833               and d: "norm (y - x) < d" and r: "norm (y - x) < r"
```
```  1834             show "\<bar>v \<bullet> (y - x)\<bar> < norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
```
```  1835             proof (cases "y = x")
```
```  1836               case True
```
```  1837               with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> \<open>v \<noteq> 0\<close> show ?thesis
```
```  1838                 by simp
```
```  1839             next
```
```  1840               case False
```
```  1841               have "\<bar>v \<bullet> (y - x)\<bar> < norm v * e / 2 / real (CARD('m) ^ CARD('m)) * norm (x - y)"
```
```  1842                 apply (rule dless)
```
```  1843                 using False \<open>y \<in> S\<close> d by (auto simp: norm_minus_commute)
```
```  1844               also have "\<dots> \<le> norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
```
```  1845                 using d r \<open>e > 0\<close> by (simp add: field_simps norm_minus_commute mult_left_mono)
```
```  1846               finally show ?thesis .
```
```  1847             qed
```
```  1848           qed
```
```  1849           show "?W \<in> lmeasurable"
```
```  1850             by (simp add: fmeasurable_Int_fmeasurable borel_open)
```
```  1851           obtain k::'m where True
```
```  1852             by metis
```
```  1853           obtain T where T: "orthogonal_transformation T" and v: "v = T(norm v *\<^sub>R axis k (1::real))"
```
```  1854             using rotation_rightward_line by metis
```
```  1855           define b where "b \<equiv> norm v"
```
```  1856           have "b > 0"
```
```  1857             using \<open>v \<noteq> 0\<close> by (auto simp: b_def)
```
```  1858           obtain eqb: "inv T v = b *\<^sub>R axis k (1::real)" and "inj T" "bij T" and invT: "orthogonal_transformation (inv T)"
```
```  1859             by (metis UNIV_I b_def  T v bij_betw_inv_into_left orthogonal_transformation_inj orthogonal_transformation_bij orthogonal_transformation_inv)
```
```  1860           let ?v = "\<chi> i. min d r / CARD('m)"
```
```  1861           let ?v' = "\<chi> i. if i = k then (e/2 * min d r) / CARD('m) ^ CARD('m) else min d r"
```
```  1862           let ?x' = "inv T x"
```
```  1863           let ?W' = "(ball ?x' (min d r) \<inter> {y. \<bar>(y - ?x')\$k\<bar> < e * min d r / (2 * CARD('m) ^ CARD('m))})"
```
```  1864           have abs: "x - e \<le> y \<and> y \<le> x + e \<longleftrightarrow> abs(y - x) \<le> e" for x y e::real
```
```  1865             by auto
```
```  1866           have "?W = T ` ?W'"
```
```  1867           proof -
```
```  1868             have 1: "T ` (ball (inv T x) (min d r)) = ball x (min d r)"
```
```  1869               by (simp add: T image_orthogonal_transformation_ball orthogonal_transformation_surj surj_f_inv_f)
```
```  1870             have 2: "{y. \<bar>v \<bullet> (y - x)\<bar> < b * e * min d r / (2 * real CARD('m) ^ CARD('m))} =
```
```  1871                       T ` {y. \<bar>y \$ k - ?x' \$ k\<bar> < e * min d r / (2 * real CARD('m) ^ CARD('m))}"
```
```  1872             proof -
```
```  1873               have *: "\<bar>T (b *\<^sub>R axis k 1) \<bullet> (y - x)\<bar> = b * \<bar>inv T y \$ k - ?x' \$ k\<bar>" for y
```
```  1874               proof -
```
```  1875                 have "\<bar>T (b *\<^sub>R axis k 1) \<bullet> (y - x)\<bar> = \<bar>(b *\<^sub>R axis k 1) \<bullet> inv T (y - x)\<bar>"
```
```  1876                   by (metis (no_types, hide_lams) b_def eqb invT orthogonal_transformation_def v)
```
```  1877                 also have "\<dots> = b * \<bar>(axis k 1) \<bullet> inv T (y - x)\<bar>"
```
```  1878                   using \<open>b > 0\<close> by (simp add: abs_mult)
```
```  1879                 also have "\<dots> = b * \<bar>inv T y \$ k - ?x' \$ k\<bar>"
```
```  1880                   using orthogonal_transformation_linear [OF invT]
```
```  1881                   by (simp add: inner_axis' linear_diff)
```
```  1882                 finally show ?thesis
```
```  1883                   by simp
```
```  1884               qed
```
```  1885               show ?thesis
```
```  1886                 using v b_def [symmetric]
```
```  1887                 using \<open>b > 0\<close> by (simp add: * bij_image_Collect_eq [OF \<open>bij T\<close>] mult_less_cancel_left_pos times_divide_eq_right [symmetric] del: times_divide_eq_right)
```
```  1888             qed
```
```  1889             show ?thesis
```
```  1890               using \<open>b > 0\<close> by (simp add: image_Int \<open>inj T\<close> 1 2 b_def [symmetric])
```
```  1891           qed
```
```  1892           moreover have "?W' \<in> lmeasurable"
```
```  1893             by (auto intro: fmeasurable_Int_fmeasurable)
```
```  1894           ultimately have "measure lebesgue ?W = measure lebesgue ?W'"
```
```  1895             by (metis measure_orthogonal_image T)
```
```  1896           also have "\<dots> \<le> measure lebesgue (cbox (?x' - ?v') (?x' + ?v'))"
```
```  1897           proof (rule measure_mono_fmeasurable)
```
```  1898             show "?W' \<subseteq> cbox (?x' - ?v') (?x' + ?v')"
```
```  1899               apply (clarsimp simp add: mem_box_cart abs dist_norm norm_minus_commute simp del: min_less_iff_conj min.bounded_iff)
```
```  1900               by (metis component_le_norm_cart less_eq_real_def le_less_trans vector_minus_component)
```
```  1901           qed auto
```
```  1902           also have "\<dots> \<le> e/2 * measure lebesgue (cbox (?x' - ?v) (?x' + ?v))"
```
```  1903           proof -
```
```  1904             have "cbox (?x' - ?v) (?x' + ?v) \<noteq> {}"
```
```  1905               using \<open>r > 0\<close> \<open>d > 0\<close> by (auto simp: interval_eq_empty_cart divide_less_0_iff)
```
```  1906             with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> show ?thesis
```
```  1907               apply (simp add: content_cbox_if_cart mem_box_cart)
```
```  1908               apply (auto simp: prod_nonneg)
```
```  1909               apply (simp add: abs if_distrib prod.delta_remove prod_constant field_simps power_diff split: if_split_asm)
```
```  1910               done
```
```  1911           qed
```
```  1912           also have "\<dots> \<le> e/2 * measure lebesgue (cball ?x' (min d r))"
```
```  1913           proof (rule mult_left_mono [OF measure_mono_fmeasurable])
```
```  1914             have *: "norm (?x' - y) \<le> min d r"
```
```  1915               if y: "\<And>i. \<bar>?x' \$ i - y \$ i\<bar> \<le> min d r / real CARD('m)" for y
```
```  1916             proof -
```
```  1917               have "norm (?x' - y) \<le> (\<Sum>i\<in>UNIV. \<bar>(?x' - y) \$ i\<bar>)"
```
```  1918                 by (rule norm_le_l1_cart)
```
```  1919               also have "\<dots> \<le> real CARD('m) * (min d r / real CARD('m))"
```
```  1920                 by (rule sum_bounded_above) (use y in auto)
```
```  1921               finally show ?thesis
```
```  1922                 by simp
```
```  1923             qed
```
```  1924             show "cbox (?x' - ?v) (?x' + ?v) \<subseteq> cball ?x' (min d r)"
```
```  1925               apply (clarsimp simp only: mem_box_cart dist_norm mem_cball intro!: *)
```
```  1926               by (simp add: abs_diff_le_iff abs_minus_commute)
```
```  1927           qed (use \<open>e > 0\<close> in auto)
```
```  1928           also have "\<dots> < e * content (cball ?x' (min d r))"
```
```  1929             using \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> by auto
```
```  1930           also have "\<dots> = e * content (ball x (min d r))"
```
```  1931             using \<open>r > 0\<close> \<open>d > 0\<close> by (simp add: content_cball content_ball)
```
```  1932           finally show "measure lebesgue ?W < e * content (ball x (min d r))" .
```
```  1933       qed
```
```  1934     qed
```
```  1935     have *: "(\<And>x. (x \<notin> S) \<Longrightarrow> (x \<in> T \<longleftrightarrow> x \<in> U)) \<Longrightarrow> (T - U) \<union> (U - T) \<subseteq> S" for S T U :: "(real,'m) vec set"
```
```  1936       by blast
```
```  1937     have MN: "?M \<subseteq> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
```
```  1938     proof (rule *)
```
```  1939       fix x
```
```  1940       assume x: "x \<notin> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
```
```  1941       show "(x \<in> ?T) \<longleftrightarrow> (x \<in> {x \<in> S. matrix (f' x) \$ m \$ n \<le> b})"
```
```  1942       proof (cases "x \<in> S")
```
```  1943         case True
```
```  1944         then have x: "\<not> ?\<Theta> x v" if "v \<noteq> 0" for v
```
```  1945           using x that by force
```
```  1946         show ?thesis
```
```  1947         proof (rule iffI; clarsimp)
```
```  1948           assume b: "\<forall>e>0. \<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
```
```  1949                                     (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
```
```  1950                      (is "\<forall>e>0. \<exists>d>0. \<exists>A. ?\<Phi> e d A")
```
```  1951           then have "\<forall>k. \<exists>d>0. \<exists>A. ?\<Phi> (1 / Suc k) d A"
```
```  1952             by (metis (no_types, hide_lams) less_Suc_eq_0_disj of_nat_0_less_iff zero_less_divide_1_iff)
```
```  1953           then obtain \<delta> A where \<delta>: "\<And>k. \<delta> k > 0"
```
```  1954                            and Ab: "\<And>k. A k \$ m \$ n < b"
```
```  1955                            and A: "\<And>k y. \<lbrakk>y \<in> S; norm (y - x) < \<delta> k\<rbrakk> \<Longrightarrow>
```
```  1956                                           norm (f y - f x - A k *v (y - x)) \<le> 1/(Suc k) * norm (y - x)"
```
```  1957             by metis
```
```  1958           have "\<forall>i j. \<exists>a. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> a"
```
```  1959           proof (intro allI)
```
```  1960             fix i j
```
```  1961             have vax: "(A n *v axis j 1) \$ i = A n \$ i \$ j" for n
```
```  1962               by (metis cart_eq_inner_axis matrix_vector_mul_component)
```
```  1963             let ?CA = "{x. Cauchy (\<lambda>n. (A n) *v x)}"
```
```  1964             have "subspace ?CA"
```
```  1965               unfolding subspace_def convergent_eq_Cauchy [symmetric]
```
```  1966                 by (force simp: algebra_simps intro: tendsto_intros)
```
```  1967             then have CA_eq: "?CA = span ?CA"
```
```  1968               by (metis span_eq_iff)
```
```  1969             also have "\<dots> = UNIV"
```
```  1970             proof -
```
```  1971               have "dim ?CA \<le> CARD('m)"
```
```  1972                 using dim_subset_UNIV[of ?CA]
```
```  1973                 by auto
```
```  1974               moreover have "False" if less: "dim ?CA < CARD('m)"
```
```  1975               proof -
```
```  1976                 obtain d where "d \<noteq> 0" and d: "\<And>y. y \<in> span ?CA \<Longrightarrow> orthogonal d y"
```
```  1977                   using less by (force intro: orthogonal_to_subspace_exists [of ?CA])
```
```  1978                 with x [OF \<open>d \<noteq> 0\<close>] obtain \<xi> where "\<xi> > 0"
```
```  1979                   and \<xi>: "\<And>e. e > 0 \<Longrightarrow> \<exists>y \<in> S - {x}. norm (x - y) < e \<and> \<xi> * norm (x - y) \<le> \<bar>d \<bullet> (y - x)\<bar>"
```
```  1980                   by (fastforce simp: not_le Bex_def)
```
```  1981                 obtain \<gamma> z where \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
```
```  1982                            and \<gamma>le:   "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
```
```  1983                            and \<gamma>x:    "\<gamma> \<longlonglongrightarrow> x"
```
```  1984                            and z:     "(\<lambda>n. (\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x)) \<longlonglongrightarrow> z"
```
```  1985                 proof -
```
```  1986                   have "\<exists>\<gamma>. (\<forall>i. (\<gamma> i \<in> S - {x} \<and>
```
```  1987                                   \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar> \<and> norm(\<gamma> i - x) < 1/Suc i) \<and>
```
```  1988                                  norm(\<gamma>(Suc i) - x) < norm(\<gamma> i - x))"
```
```  1989                   proof (rule dependent_nat_choice)
```
```  1990                     show "\<exists>y. y \<in> S - {x} \<and> \<xi> * norm (y - x) \<le> \<bar>d \<bullet> (y - x)\<bar> \<and> norm (y - x) < 1 / Suc 0"
```
```  1991                       using \<xi> [of 1] by (auto simp: dist_norm norm_minus_commute)
```
```  1992                   next
```
```  1993                     fix y i
```
```  1994                     assume "y \<in> S - {x} \<and> \<xi> * norm (y - x) \<le> \<bar>d \<bullet> (y - x)\<bar> \<and> norm (y - x) < 1/Suc i"
```
```  1995                     then have "min (norm(y - x)) (1/((Suc i) + 1)) > 0"
```
```  1996                       by auto
```
```  1997                     then obtain y' where "y' \<in> S - {x}" and y': "norm (x - y') < min (norm (y - x)) (1/((Suc i) + 1))"
```
```  1998                                          "\<xi> * norm (x - y') \<le> \<bar>d \<bullet> (y' - x)\<bar>"
```
```  1999                       using \<xi> by metis
```
```  2000                     with \<xi> show "\<exists>y'. (y' \<in> S - {x} \<and> \<xi> * norm (y' - x) \<le> \<bar>d \<bullet> (y' - x)\<bar> \<and>
```
```  2001                               norm (y' - x) < 1/(Suc (Suc i))) \<and> norm (y' - x) < norm (y - x)"
```
```  2002                       by (auto simp: dist_norm norm_minus_commute)
```
```  2003                   qed
```
```  2004                   then obtain \<gamma> where
```
```  2005                         \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
```
```  2006                         and \<gamma>le: "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
```
```  2007                         and \<gamma>conv: "\<And>i. norm(\<gamma> i - x) < 1/(Suc i)"
```
```  2008                     by blast
```
```  2009                   let ?f = "\<lambda>i. (\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x)"
```
```  2010                   have "?f i \<in> sphere 0 1" for i
```
```  2011                     using \<gamma>Sx by auto
```
```  2012                   then obtain l \<rho> where "l \<in> sphere 0 1" "strict_mono \<rho>" and l: "(?f \<circ> \<rho>) \<longlonglongrightarrow> l"
```
```  2013                     using compact_sphere [of "0::(real,'m) vec" 1]  unfolding compact_def by meson
```
```  2014                   show thesis
```
```  2015                   proof
```
```  2016                     show "(\<gamma> \<circ> \<rho>) i \<in> S - {x}" "\<xi> * norm ((\<gamma> \<circ> \<rho>) i - x) \<le> \<bar>d \<bullet> ((\<gamma> \<circ> \<rho>) i - x)\<bar>" for i
```
```  2017                       using \<gamma>Sx \<gamma>le by auto
```
```  2018                     have "\<gamma> \<longlonglongrightarrow> x"
```
```  2019                     proof (clarsimp simp add: LIMSEQ_def dist_norm)
```
```  2020                       fix r :: "real"
```
```  2021                       assume "r > 0"
```
```  2022                       with real_arch_invD obtain no where "no \<noteq> 0" "real no > 1/r"
```
```  2023                         by (metis divide_less_0_1_iff not_less_iff_gr_or_eq of_nat_0_eq_iff reals_Archimedean2)
```
```  2024                       with \<gamma>conv show "\<exists>no. \<forall>n\<ge>no. norm (\<gamma> n - x) < r"
```
```  2025                         by (metis \<open>r > 0\<close> add.commute divide_inverse inverse_inverse_eq inverse_less_imp_less less_trans mult.left_neutral nat_le_real_less of_nat_Suc)
```
```  2026                     qed
```
```  2027                     with \<open>strict_mono \<rho>\<close> show "(\<gamma> \<circ> \<rho>) \<longlonglongrightarrow> x"
```
```  2028                       by (metis LIMSEQ_subseq_LIMSEQ)
```
```  2029                     show "(\<lambda>n. ((\<gamma> \<circ> \<rho>) n - x) /\<^sub>R norm ((\<gamma> \<circ> \<rho>) n - x)) \<longlonglongrightarrow> l"
```
```  2030                       using l by (auto simp: o_def)
```
```  2031                   qed
```
```  2032                 qed
```
```  2033                 have "isCont (\<lambda>x. (\<bar>d \<bullet> x\<bar> - \<xi>)) z"
```
```  2034                   by (intro continuous_intros)
```
```  2035                 from isCont_tendsto_compose [OF this z]
```
```  2036                 have lim: "(\<lambda>y. \<bar>d \<bullet> ((\<gamma> y - x) /\<^sub>R norm (\<gamma> y - x))\<bar> - \<xi>) \<longlonglongrightarrow> \<bar>d \<bullet> z\<bar> - \<xi>"
```
```  2037                   by auto
```
```  2038                 moreover have "\<forall>\<^sub>F i in sequentially. 0 \<le> \<bar>d \<bullet> ((\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x))\<bar> - \<xi>"
```
```  2039                 proof (rule eventuallyI)
```
```  2040                   fix n
```
```  2041                   show "0 \<le> \<bar>d \<bullet> ((\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x))\<bar> - \<xi>"
```
```  2042                   using \<gamma>le [of n] \<gamma>Sx by (auto simp: abs_mult divide_simps)
```
```  2043                 qed
```
```  2044                 ultimately have "\<xi> \<le> \<bar>d \<bullet> z\<bar>"
```
```  2045                   using tendsto_lowerbound [where a=0] by fastforce
```
```  2046                 have "Cauchy (\<lambda>n. (A n) *v z)"
```
```  2047                 proof (clarsimp simp add: Cauchy_def)
```
```  2048                   fix \<epsilon> :: "real"
```
```  2049                   assume "0 < \<epsilon>"
```
```  2050                   then obtain N::nat where "N > 0" and N: "\<epsilon>/2 > 1/N"
```
```  2051                     by (metis half_gt_zero inverse_eq_divide neq0_conv real_arch_inverse)
```
```  2052                   show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (A m *v z) (A n *v z) < \<epsilon>"
```
```  2053                   proof (intro exI allI impI)
```
```  2054                     fix i j
```
```  2055                     assume ij: "N \<le> i" "N \<le> j"
```
```  2056                     let ?V = "\<lambda>i k. A i *v ((\<gamma> k - x) /\<^sub>R norm (\<gamma> k - x))"
```
```  2057                     have "\<forall>\<^sub>F k in sequentially. dist (\<gamma> k) x < min (\<delta> i) (\<delta> j)"
```
```  2058                       using \<gamma>x [unfolded tendsto_iff] by (meson min_less_iff_conj \<delta>)
```
```  2059                     then have even: "\<forall>\<^sub>F k in sequentially. norm (?V i k - ?V j k) - 2 / N \<le> 0"
```
```  2060                     proof (rule eventually_mono, clarsimp)
```
```  2061                       fix p
```
```  2062                       assume p: "dist (\<gamma> p) x < \<delta> i" "dist (\<gamma> p) x < \<delta> j"
```
```  2063                       let ?C = "\<lambda>k. f (\<gamma> p) - f x - A k *v (\<gamma> p - x)"
```
```  2064                       have "norm ((A i - A j) *v (\<gamma> p - x)) = norm (?C j - ?C i)"
```
```  2065                         by (simp add: algebra_simps)
```
```  2066                       also have "\<dots> \<le> norm (?C j) + norm (?C i)"
```
```  2067                         using norm_triangle_ineq4 by blast
```
```  2068                       also have "\<dots> \<le> 1/(Suc j) * norm (\<gamma> p - x) + 1/(Suc i) * norm (\<gamma> p - x)"
```
```  2069                         by (metis A Diff_iff \<gamma>Sx dist_norm p add_mono)
```
```  2070                       also have "\<dots> \<le> 1/N * norm (\<gamma> p - x) + 1/N * norm (\<gamma> p - x)"
```
```  2071                         apply (intro add_mono mult_right_mono)
```
```  2072                         using ij \<open>N > 0\<close> by (auto simp: field_simps)
```
```  2073                       also have "\<dots> = 2 / N * norm (\<gamma> p - x)"
```
```  2074                         by simp
```
```  2075                       finally have no_le: "norm ((A i - A j) *v (\<gamma> p - x)) \<le> 2 / N * norm (\<gamma> p - x)" .
```
```  2076                       have "norm (?V i p - ?V j p) =
```
```  2077                             norm ((A i - A j) *v ((\<gamma> p - x) /\<^sub>R norm (\<gamma> p - x)))"
```
```  2078                         by (simp add: algebra_simps)
```
```  2079                       also have "\<dots> = norm ((A i - A j) *v (\<gamma> p - x)) / norm (\<gamma> p - x)"
```
```  2080                         by (simp add: divide_inverse matrix_vector_mult_scaleR)
```
```  2081                       also have "\<dots> \<le> 2 / N"
```
```  2082                         using no_le by (auto simp: divide_simps)
```
```  2083                       finally show "norm (?V i p - ?V j p) \<le> 2 / N" .
```
```  2084                     qed
```
```  2085                     have "isCont (\<lambda>w. (norm(A i *v w - A j *v w) - 2 / N)) z"
```
```  2086                       by (intro continuous_intros)
```
```  2087                     from isCont_tendsto_compose [OF this z]
```
```  2088                     have lim: "(\<lambda>w. norm (A i *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x)) -
```
```  2089                                     A j *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x))) - 2 / N)
```
```  2090                                \<longlonglongrightarrow> norm (A i *v z - A j *v z) - 2 / N"
```
```  2091                       by auto
```
```  2092                     have "dist (A i *v z) (A j *v z) \<le> 2 / N"
```
```  2093                       using tendsto_upperbound [OF lim even] by (auto simp: dist_norm)
```
```  2094                     with N show "dist (A i *v z) (A j *v z) < \<epsilon>"
```
```  2095                       by linarith
```
```  2096                   qed
```
```  2097                 qed
```
```  2098                 then have "d \<bullet> z = 0"
```
```  2099                   using CA_eq d orthogonal_def by auto
```
```  2100                 then show False
```
```  2101                   using \<open>0 < \<xi>\<close> \<open>\<xi> \<le> \<bar>d \<bullet> z\<bar>\<close> by auto
```
```  2102               qed
```
```  2103               ultimately show ?thesis
```
```  2104                 using dim_eq_full by fastforce
```
```  2105             qed
```
```  2106             finally have "?CA = UNIV" .
```
```  2107             then have "Cauchy (\<lambda>n. (A n) *v axis j 1)"
```
```  2108               by auto
```
```  2109             then obtain L where "(\<lambda>n. A n *v axis j 1) \<longlonglongrightarrow> L"
```
```  2110               by (auto simp: Cauchy_convergent_iff convergent_def)
```
```  2111             then have "(\<lambda>x. (A x *v axis j 1) \$ i) \<longlonglongrightarrow> L \$ i"
```
```  2112               by (rule tendsto_vec_nth)
```
```  2113             then show "\<exists>a. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> a"
```
```  2114               by (force simp: vax)
```
```  2115           qed
```
```  2116           then obtain B where B: "\<And>i j. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> B \$ i \$ j"
```
```  2117             by (auto simp: lambda_skolem)
```
```  2118           have lin_df: "linear (f' x)"
```
```  2119                and lim_df: "((\<lambda>y. (1 / norm (y - x)) *\<^sub>R (f y - (f x + f' x (y - x)))) \<longlongrightarrow> 0) (at x within S)"
```
```  2120             using \<open>x \<in> S\<close> assms by (auto simp: has_derivative_within linear_linear)
```
```  2121           moreover
```
```  2122           interpret linear "f' x" by fact
```
```  2123           have "(matrix (f' x) - B) *v w = 0" for w
```
```  2124           proof (rule lemma_partial_derivatives [of "(*v) (matrix (f' x) - B)"])
```
```  2125             show "linear ((*v) (matrix (f' x) - B))"
```
```  2126               by (rule matrix_vector_mul_linear)
```
```  2127             have "((\<lambda>y. ((f x + f' x (y - x)) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
```
```  2128               using tendsto_minus [OF lim_df] by (simp add: algebra_simps divide_simps)
```
```  2129             then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
```
```  2130             proof (rule Lim_transform)
```
```  2131               have "((\<lambda>y. ((f y + B *v x - (f x + B *v y)) /\<^sub>R norm (y - x))) \<longlongrightarrow> 0) (at x within S)"
```
```  2132               proof (clarsimp simp add: Lim_within dist_norm)
```
```  2133                 fix e :: "real"
```
```  2134                 assume "e > 0"
```
```  2135                 then obtain q::nat where "q \<noteq> 0" and qe2: "1/q < e/2"
```
```  2136                   by (metis divide_pos_pos inverse_eq_divide real_arch_inverse zero_less_numeral)
```
```  2137                 let ?g = "\<lambda>p. sum  (\<lambda>i. sum (\<lambda>j. abs((A p - B)\$i\$j)) UNIV) UNIV"
```
```  2138                 have "(\<lambda>k. onorm (\<lambda>y. (A k - B) *v y)) \<longlonglongrightarrow> 0"
```
```  2139                 proof (rule Lim_null_comparison)
```
```  2140                   show "\<forall>\<^sub>F k in sequentially. norm (onorm (\<lambda>y. (A k - B) *v y)) \<le> ?g k"
```
```  2141                   proof (rule eventually_sequentiallyI)
```
```  2142                     fix k :: "nat"
```
```  2143                     assume "0 \<le> k"
```
```  2144                     have "0 \<le> onorm ((*v) (A k - B))"
```
```  2145                       using matrix_vector_mul_bounded_linear
```
```  2146                       by (rule onorm_pos_le)
```
```  2147                     then show "norm (onorm ((*v) (A k - B))) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>(A k - B) \$ i \$ j\<bar>)"
```
```  2148                       by (simp add: onorm_le_matrix_component_sum del: vector_minus_component)
```
```  2149                   qed
```
```  2150                 next
```
```  2151                   show "?g \<longlonglongrightarrow> 0"
```
```  2152                     using B Lim_null tendsto_rabs_zero_iff by (fastforce intro!: tendsto_null_sum)
```
```  2153                 qed
```
```  2154                 with \<open>e > 0\<close> obtain p where "\<And>n. n \<ge> p \<Longrightarrow> \<bar>onorm ((*v) (A n - B))\<bar> < e/2"
```
```  2155                   unfolding lim_sequentially by (metis diff_zero dist_real_def divide_pos_pos zero_less_numeral)
```
```  2156                 then have pqe2: "\<bar>onorm ((*v) (A (p + q) - B))\<bar> < e/2" (*17 [`abs (onorm (\y. A (p + q) ** y - B ** y)) < e / &2`]*)
```
```  2157                   using le_add1 by blast
```
```  2158                 show "\<exists>d>0. \<forall>y\<in>S. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow>
```
```  2159                            inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
```
```  2160                 proof (intro exI, safe)
```
```  2161                   show "0 < \<delta>(p + q)"
```
```  2162                     by (simp add: \<delta>)
```
```  2163                 next
```
```  2164                   fix y
```
```  2165                   assume y: "y \<in> S" "norm (y - x) < \<delta>(p + q)" and "y \<noteq> x"
```
```  2166                   have *: "\<lbrakk>norm(b - c) < e - d; norm(y - x - b) \<le> d\<rbrakk> \<Longrightarrow> norm(y - x - c) < e"
```
```  2167                     for b c d e x and y:: "real^'n"
```
```  2168                     using norm_triangle_ineq2 [of "y - x - c" "y - x - b"] by simp
```
```  2169                   have "norm (f y - f x - B *v (y - x)) < e * norm (y - x)"
```
```  2170                   proof (rule *)
```
```  2171                     show "norm (f y - f x - A (p + q) *v (y - x)) \<le> norm (y - x) / (Suc (p + q))"
```
```  2172                       using A [OF y] by simp
```
```  2173                     have "norm (A (p + q) *v (y - x) - B *v (y - x)) \<le> onorm(\<lambda>x. (A(p + q) - B) *v x) * norm(y - x)"
```
```  2174                       by (metis linear_linear matrix_vector_mul_linear matrix_vector_mult_diff_rdistrib onorm)
```
```  2175                     also have "\<dots> < (e/2) * norm (y - x)"
```
```  2176                       using \<open>y \<noteq> x\<close> pqe2 by auto
```
```  2177                     also have "\<dots> \<le> (e - 1 / (Suc (p + q))) * norm (y - x)"
```
```  2178                     proof (rule mult_right_mono)
```
```  2179                       have "1 / Suc (p + q) \<le> 1 / q"
```
```  2180                         using \<open>q \<noteq> 0\<close> by (auto simp: divide_simps)
```
```  2181                       also have "\<dots> < e/2"
```
```  2182                         using qe2 by auto
```
```  2183                       finally show "e / 2 \<le> e - 1 / real (Suc (p + q))"
```
```  2184                         by linarith
```
```  2185                     qed auto
```
```  2186                     finally show "norm (A (p + q) *v (y - x) - B *v (y - x)) < e * norm (y - x) - norm (y - x) / real (Suc (p + q))"
```
```  2187                       by (simp add: algebra_simps)
```
```  2188                   qed
```
```  2189                   then show "inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
```
```  2190                     using \<open>y \<noteq> x\<close> by (simp add: divide_simps algebra_simps)
```
```  2191                 qed
```
```  2192               qed
```
```  2193               then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R
```
```  2194                            norm (y - x) - (f x + f' x (y - x) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0)
```
```  2195                           (at x within S)"
```
```  2196                 by (simp add: algebra_simps diff lin_df matrix_vector_mul_linear scalar_mult_eq_scaleR)
```
```  2197             qed
```
```  2198           qed (use x in \<open>simp; auto simp: not_less\<close>)
```
```  2199           ultimately have "f' x = (*v) B"
```
```  2200             by (force simp: algebra_simps scalar_mult_eq_scaleR)
```
```  2201           show "matrix (f' x) \$ m \$ n \<le> b"
```
```  2202           proof (rule tendsto_upperbound [of "\<lambda>i. (A i \$ m \$ n)" _ sequentially])
```
```  2203             show "(\<lambda>i. A i \$ m \$ n) \<longlonglongrightarrow> matrix (f' x) \$ m \$ n"
```
```  2204               by (simp add: B \<open>f' x = (*v) B\<close>)
```
```  2205             show "\<forall>\<^sub>F i in sequentially. A i \$ m \$ n \<le> b"
```
```  2206               by (simp add: Ab less_eq_real_def)
```
```  2207           qed auto
```
```  2208         next
```
```  2209           fix e :: "real"
```
```  2210           assume "x \<in> S" and b: "matrix (f' x) \$ m \$ n \<le> b" and "e > 0"
```
```  2211           then obtain d where "d>0"
```
```  2212             and d: "\<And>y. y\<in>S \<Longrightarrow> 0 < dist y x \<and> dist y x < d \<longrightarrow> norm (f y - f x - f' x (y - x)) / (norm (y - x))
```
```  2213                   < e/2"
```
```  2214             using f [OF \<open>x \<in> S\<close>] unfolding Deriv.has_derivative_at_within Lim_within
```
```  2215             by (auto simp: field_simps dest: spec [of _ "e/2"])
```
```  2216           let ?A = "matrix(f' x) - (\<chi> i j. if i = m \<and> j = n then e / 4 else 0)"
```
```  2217           obtain B where BRats: "\<And>i j. B\$i\$j \<in> \<rat>" and Bo_e6: "onorm((*v) (?A - B)) < e/6"
```
```  2218             using matrix_rational_approximation \<open>e > 0\<close>
```
```  2219             by (metis zero_less_divide_iff zero_less_numeral)
```
```  2220           show "\<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
```
```  2221                 (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
```
```  2222           proof (intro exI conjI ballI allI impI)
```
```  2223             show "d>0"
```
```  2224               by (rule \<open>d>0\<close>)
```
```  2225             show "B \$ m \$ n < b"
```
```  2226             proof -
```
```  2227               have "\<bar>matrix ((*v) (?A - B)) \$ m \$ n\<bar> \<le> onorm ((*v) (?A - B))"
```
```  2228                 using component_le_onorm [OF matrix_vector_mul_linear, of _ m n] by metis
```
```  2229               then show ?thesis
```
```  2230                 using b Bo_e6 by simp
```
```  2231             qed
```
```  2232             show "B \$ i \$ j \<in> \<rat>" for i j
```
```  2233               using BRats by auto
```
```  2234             show "norm (f y - f x - B *v (y - x)) \<le> e * norm (y - x)"
```
```  2235               if "y \<in> S" and y: "norm (y - x) < d" for y
```
```  2236             proof (cases "y = x")
```
```  2237               case True then show ?thesis
```
```  2238                 by simp
```
```  2239             next
```
```  2240               case False
```
```  2241               have *: "norm(d' - d) \<le> e/2 \<Longrightarrow> norm(y - (x + d')) < e/2 \<Longrightarrow> norm(y - x - d) \<le> e" for d d' e and x y::"real^'n"
```
```  2242                 using norm_triangle_le [of "d' - d" "y - (x + d')"] by simp
```
```  2243               show ?thesis
```
```  2244               proof (rule *)
```
```  2245                 have split246: "\<lbrakk>norm y \<le> e / 6; norm(x - y) \<le> e / 4\<rbrakk> \<Longrightarrow> norm x \<le> e/2" if "e > 0" for e and x y :: "real^'n"
```
```  2246                   using norm_triangle_le [of y "x-y" "e/2"] \<open>e > 0\<close> by simp
```
```  2247                 have "linear (f' x)"
```
```  2248                   using True f has_derivative_linear by blast
```
```  2249                 then have "norm (f' x (y - x) - B *v (y - x)) = norm ((matrix (f' x) - B) *v (y - x))"
```
```  2250                   by (simp add: matrix_vector_mult_diff_rdistrib)
```
```  2251                 also have "\<dots> \<le> (e * norm (y - x)) / 2"
```
```  2252                 proof (rule split246)
```
```  2253                   have "norm ((?A - B) *v (y - x)) / norm (y - x) \<le> onorm(\<lambda>x. (?A - B) *v x)"
```
```  2254                     by (rule le_onorm) auto
```
```  2255                   also have  "\<dots> < e/6"
```
```  2256                     by (rule Bo_e6)
```
```  2257                   finally have "norm ((?A - B) *v (y - x)) / norm (y - x) < e / 6" .
```
```  2258                   then show "norm ((?A - B) *v (y - x)) \<le> e * norm (y - x) / 6"
```
```  2259                     by (simp add: divide_simps False)
```
```  2260                   have "norm ((matrix (f' x) - B) *v (y - x) - ((?A - B) *v (y - x))) = norm ((\<chi> i j. if i = m \<and> j = n then e / 4 else 0) *v (y - x))"
```
```  2261                     by (simp add: algebra_simps)
```
```  2262                   also have "\<dots> = norm((e/4) *\<^sub>R (y - x)\$n *\<^sub>R axis m (1::real))"
```
```  2263                   proof -
```
```  2264                     have "(\<Sum>j\<in>UNIV. (if i = m \<and> j = n then e / 4 else 0) * (y \$ j - x \$ j)) * 4 = e * (y \$ n - x \$ n) * axis m 1 \$ i" for i
```
```  2265                     proof (cases "i=m")
```
```  2266                       case True then show ?thesis
```
```  2267                         by (auto simp: if_distrib [of "\<lambda>z. z * _"] cong: if_cong)
```
```  2268                     next
```
```  2269                       case False then show ?thesis
```
```  2270                         by (simp add: axis_def)
```
```  2271                     qed
```
```  2272                     then have "(\<chi> i j. if i = m \<and> j = n then e / 4 else 0) *v (y - x) = (e/4) *\<^sub>R (y - x)\$n *\<^sub>R axis m (1::real)"
```
```  2273                       by (auto simp: vec_eq_iff matrix_vector_mult_def)
```
```  2274                     then show ?thesis
```
```  2275                       by metis
```
```  2276                   qed
```
```  2277                   also have "\<dots> \<le> e * norm (y - x) / 4"
```
```  2278                     using \<open>e > 0\<close> apply (simp add: norm_mult abs_mult)
```
```  2279                     by (metis component_le_norm_cart vector_minus_component)
```
```  2280                   finally show "norm ((matrix (f' x) - B) *v (y - x) - ((?A - B) *v (y - x))) \<le> e * norm (y - x) / 4" .
```
```  2281                   show "0 < e * norm (y - x)"
```
```  2282                     by (simp add: False \<open>e > 0\<close>)
```
```  2283                 qed
```
```  2284                 finally show "norm (f' x (y - x) - B *v (y - x)) \<le> (e * norm (y - x)) / 2" .
```
```  2285                 show "norm (f y - (f x + f' x (y - x))) < (e * norm (y - x)) / 2"
```
```  2286                   using False d [OF \<open>y \<in> S\<close>] y by (simp add: dist_norm field_simps)
```
```  2287               qed
```
```  2288             qed
```
```  2289           qed
```
```  2290         qed
```
```  2291       qed auto
```
```  2292     qed
```
```  2293     show "negligible ?M"
```
```  2294       using negligible_subset [OF nN MN] .
```
```  2295   qed
```
```  2296   then show ?thesis
```
```  2297     by (simp add: borel_measurable_vimage_halfspace_component_le sets_restrict_space_iff assms)
```
```  2298 qed
```
```  2299
```
```  2300
```
```  2301 theorem%important borel_measurable_det_Jacobian:
```
```  2302  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  2303   assumes S: "S \<in> sets lebesgue" and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  2304   shows "(\<lambda>x. det(matrix(f' x))) \<in> borel_measurable (lebesgue_on S)"
```
```  2305   unfolding det_def
```
```  2306   by%unimportant (intro measurable) (auto intro: f borel_measurable_partial_derivatives [OF S])
```
```  2307
```
```  2308 text\<open>The localisation wrt S uses the same argument for many similar results.\<close>
```
```  2309 (*See HOL Light's MEASURABLE_ON_LEBESGUE_MEASURABLE_PREIMAGE_BOREL, etc.*)
```
```  2310 lemma%important borel_measurable_lebesgue_on_preimage_borel:
```
```  2311   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
```
```  2312   assumes "S \<in> sets lebesgue"
```
```  2313   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
```
```  2314          (\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
```
```  2315 proof%unimportant -
```
```  2316   have "{x. (if x \<in> S then f x else 0) \<in> T} \<in> sets lebesgue \<longleftrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue"
```
```  2317          if "T \<in> sets borel" for T
```
```  2318     proof (cases "0 \<in> T")
```
```  2319       case True
```
```  2320       then have "{x \<in> S. f x \<in> T} = {x. (if x \<in> S then f x else 0) \<in> T} \<inter> S"
```
```  2321                 "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T} \<union> -S"
```
```  2322         by auto
```
```  2323       then show ?thesis
```
```  2324         by (metis (no_types, lifting) Compl_in_sets_lebesgue assms sets.Int sets.Un)
```
```  2325     next
```
```  2326       case False
```
```  2327       then have "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T}"
```
```  2328         by auto
```
```  2329       then show ?thesis
```
```  2330         by auto
```
```  2331     qed
```
```  2332     then show ?thesis
```
```  2333       unfolding borel_measurable_lebesgue_preimage_borel borel_measurable_UNIV [OF assms, symmetric]
```
```  2334       by blast
```
```  2335 qed
```
```  2336
```
```  2337 lemma%unimportant sets_lebesgue_almost_borel:
```
```  2338   assumes "S \<in> sets lebesgue"
```
```  2339   obtains B N where "B \<in> sets borel" "negligible N" "B \<union> N = S"
```
```  2340 proof -
```
```  2341   obtain T N N' where "S = T \<union> N" "N \<subseteq> N'" "N' \<in> null_sets lborel" "T \<in> sets borel"
```
```  2342     using sets_completionE [OF assms] by auto
```
```  2343   then show thesis
```
```  2344     by (metis negligible_iff_null_sets negligible_subset null_sets_completionI that)
```
```  2345 qed
```
```  2346
```
```  2347 lemma%unimportant double_lebesgue_sets:
```
```  2348  assumes S: "S \<in> sets lebesgue" and T: "T \<in> sets lebesgue" and fim: "f ` S \<subseteq> T"
```
```  2349  shows "(\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue) \<longleftrightarrow>
```
```  2350           f \<in> borel_measurable (lebesgue_on S) \<and>
```
```  2351           (\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue)"
```
```  2352          (is "?lhs \<longleftrightarrow> _ \<and> ?rhs")
```
```  2353   unfolding borel_measurable_lebesgue_on_preimage_borel [OF S]
```
```  2354 proof (intro iffI allI conjI impI, safe)
```
```  2355   fix V :: "'b set"
```
```  2356   assume *: "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
```
```  2357     and "V \<in> sets borel"
```
```  2358   then have V: "V \<in> sets lebesgue"
```
```  2359     by simp
```
```  2360   have "{x \<in> S. f x \<in> V} = {x \<in> S. f x \<in> T \<inter> V}"
```
```  2361     using fim by blast
```
```  2362   also have "{x \<in> S. f x \<in> T \<inter> V} \<in> sets lebesgue"
```
```  2363     using T V * le_inf_iff by blast
```
```  2364   finally show "{x \<in> S. f x \<in> V} \<in> sets lebesgue" .
```
```  2365 next
```
```  2366   fix U :: "'b set"
```
```  2367   assume "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
```
```  2368          "negligible U" "U \<subseteq> T"
```
```  2369   then show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
```
```  2370     using negligible_imp_sets by blast
```
```  2371 next
```
```  2372   fix U :: "'b set"
```
```  2373   assume 1 [rule_format]: "(\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
```
```  2374      and 2 [rule_format]: "\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
```
```  2375      and "U \<in> sets lebesgue" "U \<subseteq> T"
```
```  2376   then obtain C N where C: "C \<in> sets borel \<and> negligible N \<and> C \<union> N = U"
```
```  2377     using sets_lebesgue_almost_borel
```
```  2378     by metis
```
```  2379   then have "{x \<in> S. f x \<in> C} \<in> sets lebesgue"
```
```  2380     by (blast intro: 1)
```
```  2381   moreover have "{x \<in> S. f x \<in> N} \<in> sets lebesgue"
```
```  2382     using C \<open>U \<subseteq> T\<close> by (blast intro: 2)
```
```  2383   moreover have "{x \<in> S. f x \<in> C \<union> N} = {x \<in> S. f x \<in> C} \<union> {x \<in> S. f x \<in> N}"
```
```  2384     by auto
```
```  2385   ultimately show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
```
```  2386     using C by auto
```
```  2387 qed
```
```  2388
```
```  2389
```
```  2390 subsection%important\<open>Simplest case of Sard's theorem (we don't need continuity of derivative)\<close>
```
```  2391
```
```  2392 lemma%important Sard_lemma00:
```
```  2393   fixes P :: "'b::euclidean_space set"
```
```  2394   assumes "a \<ge> 0" and a: "a *\<^sub>R i \<noteq> 0" and i: "i \<in> Basis"
```
```  2395     and P: "P \<subseteq> {x. a *\<^sub>R i \<bullet> x = 0}"
```
```  2396     and "0 \<le> m" "0 \<le> e"
```
```  2397  obtains S where "S \<in> lmeasurable"
```
```  2398             and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
```
```  2399             and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (DIM('b) - 1)"
```
```  2400 proof%unimportant -
```
```  2401   have "a > 0"
```
```  2402     using assms by simp
```
```  2403   let ?v = "(\<Sum>j\<in>Basis. (if j = i then e else m) *\<^sub>R j)"
```
```  2404   show thesis
```
```  2405   proof
```
```  2406     have "- e \<le> x \<bullet> i" "x \<bullet> i \<le> e"
```
```  2407       if "t \<in> P" "norm (x - t) \<le> e" for x t
```
```  2408       using \<open>a > 0\<close> that Basis_le_norm [of i "x-t"] P i
```
```  2409       by (auto simp: inner_commute algebra_simps)
```
```  2410     moreover have "- m \<le> x \<bullet> j" "x \<bullet> j \<le> m"
```
```  2411       if "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e" "j \<in> Basis" and "j \<noteq> i"
```
```  2412       for x t j
```
```  2413       using that Basis_le_norm [of j x] by auto
```
```  2414     ultimately
```
```  2415     show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> cbox (-?v) ?v"
```
```  2416       by (auto simp: mem_box)
```
```  2417     have *: "\<forall>k\<in>Basis. - ?v \<bullet> k \<le> ?v \<bullet> k"
```
```  2418       using \<open>0 \<le> m\<close> \<open>0 \<le> e\<close> by (auto simp: inner_Basis)
```
```  2419     have 2: "2 ^ DIM('b) = 2 * 2 ^ (DIM('b) - Suc 0)"
```
```  2420       by (metis DIM_positive Suc_pred power_Suc)
```
```  2421     show "measure lebesgue (cbox (-?v) ?v) \<le> 2 * e * (2 * m) ^ (DIM('b) - 1)"
```
```  2422       using \<open>i \<in> Basis\<close>
```
```  2423       by (simp add: content_cbox [OF *] prod.distrib prod.If_cases Diff_eq [symmetric] 2)
```
```  2424   qed blast
```
```  2425 qed
```
```  2426
```
```  2427 text\<open>As above, but reorienting the vector (HOL Light's @text{GEOM\_BASIS\_MULTIPLE\_TAC})\<close>
```
```  2428 lemma%unimportant Sard_lemma0:
```
```  2429   fixes P :: "(real^'n::{finite,wellorder}) set"
```
```  2430   assumes "a \<noteq> 0"
```
```  2431     and P: "P \<subseteq> {x. a \<bullet> x = 0}" and "0 \<le> m" "0 \<le> e"
```
```  2432   obtains S where "S \<in> lmeasurable"
```
```  2433     and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
```
```  2434     and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
```
```  2435 proof -
```
```  2436   obtain T and k::'n where T: "orthogonal_transformation T" and a: "a = T (norm a *\<^sub>R axis k (1::real))"
```
```  2437     using rotation_rightward_line by metis
```
```  2438   have Tinv [simp]: "T (inv T x) = x" for x
```
```  2439     by (simp add: T orthogonal_transformation_surj surj_f_inv_f)
```
```  2440   obtain S where S: "S \<in> lmeasurable"
```
```  2441     and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> T-`P. norm(z - t) \<le> e)} \<subseteq> S"
```
```  2442     and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
```
```  2443   proof (rule Sard_lemma00 [of "norm a" "axis k (1::real)" "T-`P" m e])
```
```  2444     have "norm a *\<^sub>R axis k 1 \<bullet> x = 0" if "T x \<in> P" for x
```
```  2445     proof -
```
```  2446       have "a \<bullet> T x = 0"
```
```  2447         using P that by blast
```
```  2448       then show ?thesis
```
```  2449         by (metis (no_types, lifting) T a orthogonal_orthogonal_transformation orthogonal_def)
```
```  2450     qed
```
```  2451     then show "T -` P \<subseteq> {x. norm a *\<^sub>R axis k 1 \<bullet> x = 0}"
```
```  2452       by auto
```
```  2453   qed (use assms T in auto)
```
```  2454   show thesis
```
```  2455   proof
```
```  2456     show "T ` S \<in> lmeasurable"
```
```  2457       using S measurable_orthogonal_image T by blast
```
```  2458     have "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> T ` {z. norm z \<le> m \<and> (\<exists>t\<in>T -` P. norm (z - t) \<le> e)}"
```
```  2459     proof clarsimp
```
```  2460       fix x t
```
```  2461       assume "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e"
```
```  2462       then have "norm (inv T x) \<le> m"
```
```  2463         using orthogonal_transformation_inv [OF T] by (simp add: orthogonal_transformation_norm)
```
```  2464       moreover have "\<exists>t\<in>T -` P. norm (inv T x - t) \<le> e"
```
```  2465       proof
```
```  2466         have "T (inv T x - inv T t) = x - t"
```
```  2467           using T linear_diff orthogonal_transformation_def
```
```  2468           by (metis (no_types, hide_lams) Tinv)
```
```  2469         then have "norm (inv T x - inv T t) = norm (x - t)"
```
```  2470           by (metis T orthogonal_transformation_norm)
```
```  2471         then show "norm (inv T x - inv T t) \<le> e"
```
```  2472           using \<open>norm (x - t) \<le> e\<close> by linarith
```
```  2473        next
```
```  2474          show "inv T t \<in> T -` P"
```
```  2475            using \<open>t \<in> P\<close> by force
```
```  2476       qed
```
```  2477       ultimately show "x \<in> T ` {z. norm z \<le> m \<and> (\<exists>t\<in>T -` P. norm (z - t) \<le> e)}"
```
```  2478         by force
```
```  2479     qed
```
```  2480     then show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> T ` S"
```
```  2481       using image_mono [OF subS] by (rule order_trans)
```
```  2482     show "measure lebesgue (T ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
```
```  2483       using mS T by (simp add: S measure_orthogonal_image)
```
```  2484   qed
```
```  2485 qed
```
```  2486
```
```  2487 text\<open>As above, but translating the sets (HOL Light's @text{GEN\_GEOM\_ORIGIN\_TAC})\<close>
```
```  2488 lemma%important Sard_lemma1:
```
```  2489   fixes P :: "(real^'n::{finite,wellorder}) set"
```
```  2490    assumes P: "dim P < CARD('n)" and "0 \<le> m" "0 \<le> e"
```
```  2491  obtains S where "S \<in> lmeasurable"
```
```  2492             and "{z. norm(z - w) \<le> m \<and> (\<exists>t \<in> P. norm(z - w - t) \<le> e)} \<subseteq> S"
```
```  2493             and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
```
```  2494 proof%unimportant -
```
```  2495   obtain a where "a \<noteq> 0" "P \<subseteq> {x. a \<bullet> x = 0}"
```
```  2496     using lowdim_subset_hyperplane [of P] P span_base by auto
```
```  2497   then obtain S where S: "S \<in> lmeasurable"
```
```  2498     and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
```
```  2499     and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
```
```  2500     by (rule Sard_lemma0 [OF _ _ \<open>0 \<le> m\<close> \<open>0 \<le> e\<close>])
```
```  2501   show thesis
```
```  2502   proof
```
```  2503     show "(+)w ` S \<in> lmeasurable"
```
```  2504       by (metis measurable_translation S)
```
```  2505     show "{z. norm (z - w) \<le> m \<and> (\<exists>t\<in>P. norm (z - w - t) \<le> e)} \<subseteq> (+)w ` S"
```
```  2506       using subS by force
```
```  2507     show "measure lebesgue ((+)w ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
```
```  2508       by (metis measure_translation mS)
```
```  2509   qed
```
```  2510 qed
```
```  2511
```
```  2512 lemma%important Sard_lemma2:
```
```  2513   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n::{finite,wellorder}"
```
```  2514   assumes mlen: "CARD('m) \<le> CARD('n)" (is "?m \<le> ?n")
```
```  2515     and "B > 0" "bounded S"
```
```  2516     and derS: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  2517     and rank: "\<And>x. x \<in> S \<Longrightarrow> rank(matrix(f' x)) < CARD('n)"
```
```  2518     and B: "\<And>x. x \<in> S \<Longrightarrow> onorm(f' x) \<le> B"
```
```  2519   shows "negligible(f ` S)"
```
```  2520 proof%unimportant -
```
```  2521   have lin_f': "\<And>x. x \<in> S \<Longrightarrow> linear(f' x)"
```
```  2522     using derS has_derivative_linear by blast
```
```  2523   show ?thesis
```
```  2524   proof (clarsimp simp add: negligible_outer_le)
```
```  2525     fix e :: "real"
```
```  2526     assume "e > 0"
```
```  2527     obtain c where csub: "S \<subseteq> cbox (- (vec c)) (vec c)" and "c > 0"
```
```  2528     proof -
```
```  2529       obtain b where b: "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> b"
```
```  2530         using \<open>bounded S\<close> by (auto simp: bounded_iff)
```
```  2531       show thesis
```
```  2532       proof
```
```  2533         have "- \<bar>b\<bar> - 1 \<le> x \$ i \<and> x \$ i \<le> \<bar>b\<bar> + 1" if "x \<in> S" for x i
```
```  2534           using component_le_norm_cart [of x i] b [OF that] by auto
```
```  2535         then show "S \<subseteq> cbox (- vec (\<bar>b\<bar> + 1)) (vec (\<bar>b\<bar> + 1))"
```
```  2536           by (auto simp: mem_box_cart)
```
```  2537       qed auto
```
```  2538     qed
```
```  2539     then have box_cc: "box (- (vec c)) (vec c) \<noteq> {}" and cbox_cc: "cbox (- (vec c)) (vec c) \<noteq> {}"
```
```  2540       by (auto simp: interval_eq_empty_cart)
```
```  2541     obtain d where "d > 0" "d \<le> B"
```
```  2542              and d: "(d * 2) * (4 * B) ^ (?n - 1) \<le> e / (2*c) ^ ?m / ?m ^ ?m"
```
```  2543       apply (rule that [of "min B (e / (2*c) ^ ?m / ?m ^ ?m / (4 * B) ^ (?n - 1) / 2)"])
```
```  2544       using \<open>B > 0\<close> \<open>c > 0\<close> \<open>e > 0\<close>
```
```  2545       by (simp_all add: divide_simps min_mult_distrib_right)
```
```  2546     have "\<exists>r. 0 < r \<and> r \<le> 1/2 \<and>
```
```  2547               (x \<in> S
```
```  2548                \<longrightarrow> (\<forall>y. y \<in> S \<and> norm(y - x) < r
```
```  2549                        \<longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)))" for x
```
```  2550     proof (cases "x \<in> S")
```
```  2551       case True
```
```  2552       then obtain r where "r > 0"
```
```  2553               and "\<And>y. \<lbrakk>y \<in> S; norm (y - x) < r\<rbrakk>
```
```  2554                        \<Longrightarrow> norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
```
```  2555         using derS \<open>d > 0\<close> by (force simp: has_derivative_within_alt)
```
```  2556       then show ?thesis
```
```  2557         by (rule_tac x="min r (1/2)" in exI) simp
```
```  2558     next
```
```  2559       case False
```
```  2560       then show ?thesis
```
```  2561         by (rule_tac x="1/2" in exI) simp
```
```  2562     qed
```
```  2563     then obtain r where r12: "\<And>x. 0 < r x \<and> r x \<le> 1/2"
```
```  2564             and r: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S; norm(y - x) < r x\<rbrakk>
```
```  2565                           \<Longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)"
```
```  2566       by metis
```
```  2567     then have ga: "gauge (\<lambda>x. ball x (r x))"
```
```  2568       by (auto simp: gauge_def)
```
```  2569     obtain \<D> where \<D>: "countable \<D>" and sub_cc: "\<Union>\<D> \<subseteq> cbox (- vec c) (vec c)"
```
```  2570       and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>u v. K = cbox u v)"
```
```  2571       and djointish: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
```
```  2572       and covered: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> ball x (r x)"
```
```  2573       and close: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i::'m. v \$ i - u \$ i = 2*c / 2^n"
```
```  2574       and covers: "S \<subseteq> \<Union>\<D>"
```
```  2575       apply (rule covering_lemma [OF csub box_cc ga])
```
```  2576       apply (auto simp: Basis_vec_def cart_eq_inner_axis [symmetric])
```
```  2577       done
```
```  2578     let ?\<mu> = "measure lebesgue"
```
```  2579     have "\<exists>T. T \<in> lmeasurable \<and> f ` (K \<inter> S) \<subseteq> T \<and> ?\<mu> T \<le> e / (2*c) ^ ?m * ?\<mu> K"
```
```  2580       if "K \<in> \<D>" for K
```
```  2581     proof -
```
```  2582       obtain u v where uv: "K = cbox u v"
```
```  2583         using cbox \<open>K \<in> \<D>\<close> by blast
```
```  2584       then have uv_ne: "cbox u v \<noteq> {}"
```
```  2585         using cbox that by fastforce
```
```  2586       obtain x where x: "x \<in> S \<inter> cbox u v" "cbox u v \<subseteq> ball x (r x)"
```
```  2587         using \<open>K \<in> \<D>\<close> covered uv by blast
```
```  2588       then have "dim (range (f' x)) < ?n"
```
```  2589         using rank_dim_range [of "matrix (f' x)"] x rank[of x]
```
```  2590         by (auto simp: matrix_works scalar_mult_eq_scaleR lin_f')
```
```  2591       then obtain T where T: "T \<in> lmeasurable"
```
```  2592             and subT: "{z. norm(z - f x) \<le> (2 * B) * norm(v - u) \<and> (\<exists>t \<in> range (f' x). norm(z - f x - t) \<le> d * norm(v - u))} \<subseteq> T"
```
```  2593             and measT: "?\<mu> T \<le> (2 * (d * norm(v - u))) * (2 * ((2 * B) * norm(v - u))) ^ (?n - 1)"
```
```  2594                         (is "_ \<le> ?DVU")
```
```  2595         apply (rule Sard_lemma1 [of "range (f' x)" "(2 * B) * norm(v - u)" "d * norm(v - u)" "f x"])
```
```  2596         using \<open>B > 0\<close> \<open>d > 0\<close> by simp_all
```
```  2597       show ?thesis
```
```  2598       proof (intro exI conjI)
```
```  2599         have "f ` (K \<inter> S) \<subseteq> {z. norm(z - f x) \<le> (2 * B) * norm(v - u) \<and> (\<exists>t \<in> range (f' x). norm(z - f x - t) \<le> d * norm(v - u))}"
```
```  2600           unfolding uv
```
```  2601         proof (clarsimp simp: mult.assoc, intro conjI)
```
```  2602           fix y
```
```  2603           assume y: "y \<in> cbox u v" and "y \<in> S"
```
```  2604           then have "norm (y - x) < r x"
```
```  2605             by (metis dist_norm mem_ball norm_minus_commute subsetCE x(2))
```
```  2606           then have le_dyx: "norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
```
```  2607             using r [of x y] x \<open>y \<in> S\<close> by blast
```
```  2608           have yx_le: "norm (y - x) \<le> norm (v - u)"
```
```  2609           proof (rule norm_le_componentwise_cart)
```
```  2610             show "norm ((y - x) \$ i) \<le> norm ((v - u) \$ i)" for i
```
```  2611               using x y by (force simp: mem_box_cart dest!: spec [where x=i])
```
```  2612           qed
```
```  2613           have *: "\<lbrakk>norm(y - x - z) \<le> d; norm z \<le> B; d \<le> B\<rbrakk> \<Longrightarrow> norm(y - x) \<le> 2 * B"
```
```  2614             for x y z :: "real^'n::_" and d B
```
```  2615             using norm_triangle_ineq2 [of "y - x" z] by auto
```
```  2616           show "norm (f y - f x) \<le> 2 * (B * norm (v - u))"
```
```  2617           proof (rule * [OF le_dyx])
```
```  2618             have "norm (f' x (y - x)) \<le> onorm (f' x) * norm (y - x)"
```
```  2619               using onorm [of "f' x" "y-x"] by (meson IntE lin_f' linear_linear x(1))
```
```  2620             also have "\<dots> \<le> B * norm (v - u)"
```
```  2621             proof (rule mult_mono)
```
```  2622               show "onorm (f' x) \<le> B"
```
```  2623                 using B x by blast
```
```  2624             qed (use \<open>B > 0\<close> yx_le in auto)
```
```  2625             finally show "norm (f' x (y - x)) \<le> B * norm (v - u)" .
```
```  2626             show "d * norm (y - x) \<le> B * norm (v - u)"
```
```  2627               using \<open>B > 0\<close> by (auto intro: mult_mono [OF \<open>d \<le> B\<close> yx_le])
```
```  2628           qed
```
```  2629           show "\<exists>t. norm (f y - f x - f' x t) \<le> d * norm (v - u)"
```
```  2630             apply (rule_tac x="y-x" in exI)
```
```  2631             using \<open>d > 0\<close> yx_le le_dyx mult_left_mono [where c=d]
```
```  2632             by (meson order_trans real_mult_le_cancel_iff2)
```
```  2633         qed
```
```  2634         with subT show "f ` (K \<inter> S) \<subseteq> T" by blast
```
```  2635         show "?\<mu> T \<le> e / (2*c) ^ ?m * ?\<mu> K"
```
```  2636         proof (rule order_trans [OF measT])
```
```  2637           have "?DVU = (d * 2 * (4 * B) ^ (?n - 1)) * norm (v - u)^?n"
```
```  2638             using \<open>c > 0\<close>
```
```  2639             apply (simp add: algebra_simps power_mult_distrib)
```
```  2640             by (metis Suc_pred power_Suc zero_less_card_finite)
```
```  2641           also have "\<dots> \<le> (e / (2*c) ^ ?m / (?m ^ ?m)) * norm(v - u) ^ ?n"
```
```  2642             by (rule mult_right_mono [OF d]) auto
```
```  2643           also have "\<dots> \<le> e / (2*c) ^ ?m * ?\<mu> K"
```
```  2644           proof -
```
```  2645             have "u \<in> ball (x) (r x)" "v \<in> ball x (r x)"
```
```  2646               using box_ne_empty(1) contra_subsetD [OF x(2)] mem_box(2) uv_ne by fastforce+
```
```  2647             moreover have "r x \<le> 1/2"
```
```  2648               using r12 by auto
```
```  2649             ultimately have "norm (v - u) \<le> 1"
```
```  2650               using norm_triangle_half_r [of x u 1 v]
```
```  2651               by (metis (no_types, hide_lams) dist_commute dist_norm less_eq_real_def less_le_trans mem_ball)
```
```  2652             then have "norm (v - u) ^ ?n \<le> norm (v - u) ^ ?m"
```
```  2653               by (simp add: power_decreasing [OF mlen])
```
```  2654             also have "\<dots> \<le> ?\<mu> K * real (?m ^ ?m)"
```
```  2655             proof -
```
```  2656               obtain n where n: "\<And>i. v\$i - u\$i = 2 * c / 2^n"
```
```  2657                 using close [of u v] \<open>K \<in> \<D>\<close> uv by blast
```
```  2658               have "norm (v - u) ^ ?m \<le> (\<Sum>i\<in>UNIV. \<bar>(v - u) \$ i\<bar>) ^ ?m"
```
```  2659                 by (intro norm_le_l1_cart power_mono) auto
```
```  2660               also have "\<dots> \<le> (\<Prod>i\<in>UNIV. v \$ i - u \$ i) * real CARD('m) ^ CARD('m)"
```
```  2661                 by (simp add: n field_simps \<open>c > 0\<close> less_eq_real_def)
```
```  2662               also have "\<dots> = ?\<mu> K * real (?m ^ ?m)"
```
```  2663                 by (simp add: uv uv_ne content_cbox_cart)
```
```  2664               finally show ?thesis .
```
```  2665             qed
```
```  2666             finally have *: "1 / real (?m ^ ?m) * norm (v - u) ^ ?n \<le> ?\<mu> K"
```
```  2667               by (simp add: divide_simps)
```
```  2668             show ?thesis
```
```  2669               using mult_left_mono [OF *, of "e / (2*c) ^ ?m"] \<open>c > 0\<close> \<open>e > 0\<close> by auto
```
```  2670           qed
```
```  2671           finally show "?DVU \<le> e / (2*c) ^ ?m * ?\<mu> K" .
```
```  2672         qed
```
```  2673       qed (use T in auto)
```
```  2674     qed
```
```  2675     then obtain g where meas_g: "\<And>K. K \<in> \<D> \<Longrightarrow> g K \<in> lmeasurable"
```
```  2676                     and sub_g: "\<And>K. K \<in> \<D> \<Longrightarrow> f ` (K \<inter> S) \<subseteq> g K"
```
```  2677                     and le_g: "\<And>K. K \<in> \<D> \<Longrightarrow> ?\<mu> (g K) \<le> e / (2*c)^?m * ?\<mu> K"
```
```  2678       by metis
```
```  2679     have le_e: "?\<mu> (\<Union>i\<in>\<F>. g i) \<le> e"
```
```  2680       if "\<F> \<subseteq> \<D>" "finite \<F>" for \<F>
```
```  2681     proof -
```
```  2682       have "?\<mu> (\<Union>i\<in>\<F>. g i) \<le> (\<Sum>i\<in>\<F>. ?\<mu> (g i))"
```
```  2683         using meas_g \<open>\<F> \<subseteq> \<D>\<close> by (auto intro: measure_UNION_le [OF \<open>finite \<F>\<close>])
```
```  2684       also have "\<dots> \<le> (\<Sum>K\<in>\<F>. e / (2*c) ^ ?m * ?\<mu> K)"
```
```  2685         using \<open>\<F> \<subseteq> \<D>\<close> sum_mono [OF le_g] by (meson le_g subsetCE sum_mono)
```
```  2686       also have "\<dots> = e / (2*c) ^ ?m * (\<Sum>K\<in>\<F>. ?\<mu> K)"
```
```  2687         by (simp add: sum_distrib_left)
```
```  2688       also have "\<dots> \<le> e"
```
```  2689       proof -
```
```  2690         have "\<F> division_of \<Union>\<F>"
```
```  2691         proof (rule division_ofI)
```
```  2692           show "K \<subseteq> \<Union>\<F>"  "K \<noteq> {}" "\<exists>a b. K = cbox a b" if "K \<in> \<F>" for K
```
```  2693             using \<open>K \<in> \<F>\<close> covered cbox \<open>\<F> \<subseteq> \<D>\<close> by (auto simp: Union_upper)
```
```  2694           show "interior K \<inter> interior L = {}" if "K \<in> \<F>" and "L \<in> \<F>" and "K \<noteq> L" for K L
```
```  2695             by (metis (mono_tags, lifting) \<open>\<F> \<subseteq> \<D>\<close> pairwiseD djointish pairwise_subset that)
```
```  2696         qed (use that in auto)
```
```  2697         then have "sum ?\<mu> \<F> \<le> ?\<mu> (\<Union>\<F>)"
```
```  2698           by (simp add: content_division)
```
```  2699         also have "\<dots> \<le> ?\<mu> (cbox (- vec c) (vec c) :: (real, 'm) vec set)"
```
```  2700         proof (rule measure_mono_fmeasurable)
```
```  2701           show "\<Union>\<F> \<subseteq> cbox (- vec c) (vec c)"
```
```  2702             by (meson Sup_subset_mono sub_cc order_trans \<open>\<F> \<subseteq> \<D>\<close>)
```
```  2703         qed (use \<open>\<F> division_of \<Union>\<F>\<close> lmeasurable_division in auto)
```
```  2704         also have "\<dots> = content (cbox (- vec c) (vec c) :: (real, 'm) vec set)"
```
```  2705           by simp
```
```  2706         also have "\<dots> \<le> (2 ^ ?m * c ^ ?m)"
```
```  2707           using \<open>c > 0\<close> by (simp add: content_cbox_if_cart)
```
```  2708         finally have "sum ?\<mu> \<F> \<le> (2 ^ ?m * c ^ ?m)" .
```
```  2709         then show ?thesis
```
```  2710           using \<open>e > 0\<close> \<open>c > 0\<close> by (auto simp: divide_simps mult_less_0_iff)
```
```  2711       qed
```
```  2712       finally show ?thesis .
```
```  2713     qed
```
```  2714     show "\<exists>T. f ` S \<subseteq> T \<and> T \<in> lmeasurable \<and> ?\<mu> T \<le> e"
```
```  2715     proof (intro exI conjI)
```
```  2716       show "f ` S \<subseteq> \<Union> (g ` \<D>)"
```
```  2717         using covers sub_g by force
```
```  2718       show "\<Union> (g ` \<D>) \<in> lmeasurable"
```
```  2719         by (rule fmeasurable_UN_bound [OF \<open>countable \<D>\<close> meas_g le_e])
```
```  2720       show "?\<mu> (\<Union> (g ` \<D>)) \<le> e"
```
```  2721         by (rule measure_UN_bound [OF \<open>countable \<D>\<close> meas_g le_e])
```
```  2722     qed
```
```  2723   qed
```
```  2724 qed
```
```  2725
```
```  2726
```
```  2727 theorem%important baby_Sard:
```
```  2728   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n::{finite,wellorder}"
```
```  2729   assumes mlen: "CARD('m) \<le> CARD('n)"
```
```  2730     and der: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  2731     and rank: "\<And>x. x \<in> S \<Longrightarrow> rank(matrix(f' x)) < CARD('n)"
```
```  2732   shows "negligible(f ` S)"
```
```  2733 proof%unimportant -
```
```  2734   let ?U = "\<lambda>n. {x \<in> S. norm(x) \<le> n \<and> onorm(f' x) \<le> real n}"
```
```  2735   have "\<And>x. x \<in> S \<Longrightarrow> \<exists>n. norm x \<le> real n \<and> onorm (f' x) \<le> real n"
```
```  2736     by (meson linear order_trans real_arch_simple)
```
```  2737   then have eq: "S = (\<Union>n. ?U n)"
```
```  2738     by auto
```
```  2739   have "negligible (f ` ?U n)" for n
```
```  2740   proof (rule Sard_lemma2 [OF mlen])
```
```  2741     show "0 < real n + 1"
```
```  2742       by auto
```
```  2743     show "bounded (?U n)"
```
```  2744       using bounded_iff by blast
```
```  2745     show "(f has_derivative f' x) (at x within ?U n)" if "x \<in> ?U n" for x
```
```  2746       using der that by (force intro: has_derivative_subset)
```
```  2747   qed (use rank in auto)
```
```  2748   then show ?thesis
```
```  2749     by (subst eq) (simp add: image_Union negligible_Union_nat)
```
```  2750 qed
```
```  2751
```
```  2752
```
```  2753 subsection%important\<open>A one-way version of change-of-variables not assuming injectivity. \<close>
```
```  2754
```
```  2755 lemma%important integral_on_image_ubound_weak:
```
```  2756   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real"
```
```  2757   assumes S: "S \<in> sets lebesgue"
```
```  2758       and f: "f \<in> borel_measurable (lebesgue_on (g ` S))"
```
```  2759       and nonneg_fg:  "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
```
```  2760       and der_g:   "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  2761       and det_int_fg: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
```
```  2762       and meas_gim: "\<And>T. \<lbrakk>T \<subseteq> g ` S; T \<in> sets lebesgue\<rbrakk> \<Longrightarrow> {x \<in> S. g x \<in> T} \<in> sets lebesgue"
```
```  2763     shows "f integrable_on (g ` S) \<and>
```
```  2764            integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
```
```  2765          (is "_ \<and> _ \<le> ?b")
```
```  2766 proof%unimportant -
```
```  2767   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar>"
```
```  2768   have cont_g: "continuous_on S g"
```
```  2769     using der_g has_derivative_continuous_on by blast
```
```  2770   have [simp]: "space (lebesgue_on S) = S"
```
```  2771     by (simp add: S)
```
```  2772   have gS_in_sets_leb: "g ` S \<in> sets lebesgue"
```
```  2773     apply (rule differentiable_image_in_sets_lebesgue)
```
```  2774     using der_g by (auto simp: S differentiable_def differentiable_on_def)
```
```  2775   obtain h where nonneg_h: "\<And>n x. 0 \<le> h n x"
```
```  2776     and h_le_f: "\<And>n x. x \<in> S \<Longrightarrow> h n (g x) \<le> f (g x)"
```
```  2777     and h_inc: "\<And>n x. h n x \<le> h (Suc n) x"
```
```  2778     and h_meas: "\<And>n. h n \<in> borel_measurable lebesgue"
```
```  2779     and fin_R: "\<And>n. finite(range (h n))"
```
```  2780     and lim: "\<And>x. x \<in> g ` S \<Longrightarrow> (\<lambda>n. h n x) \<longlonglongrightarrow> f x"
```
```  2781   proof -
```
```  2782     let ?f = "\<lambda>x. if x \<in> g ` S then f x else 0"
```
```  2783     have "?f \<in> borel_measurable lebesgue \<and> (\<forall>x. 0 \<le> ?f x)"
```
```  2784       by (auto simp: gS_in_sets_leb f nonneg_fg measurable_restrict_space_iff [symmetric])
```
```  2785     then show ?thesis
```
```  2786       apply (clarsimp simp add: borel_measurable_simple_function_limit_increasing)
```
```  2787       apply (rename_tac h)
```
```  2788       by (rule_tac h=h in that) (auto split: if_split_asm)
```
```  2789   qed
```
```  2790   have h_lmeas: "{t. h n (g t) = y} \<inter> S \<in> sets lebesgue" for y n
```
```  2791   proof -
```
```  2792     have "space (lebesgue_on (UNIV::(real,'n) vec set)) = UNIV"
```
```  2793       by simp
```
```  2794     then have "((h n) -`{y} \<inter> g ` S) \<in> sets (lebesgue_on (g ` S))"
```
```  2795       by (metis Int_commute borel_measurable_vimage h_meas image_eqI inf_top.right_neutral sets_restrict_space space_borel space_completion space_lborel)
```
```  2796     then have "({u. h n u = y} \<inter> g ` S) \<in> sets lebesgue"
```
```  2797       using gS_in_sets_leb
```
```  2798       by (simp add: integral_indicator fmeasurableI2 sets_restrict_space_iff vimage_def)
```
```  2799     then have "{x \<in> S. g x \<in> ({u. h n u = y} \<inter> g ` S)} \<in> sets lebesgue"
```
```  2800       using meas_gim[of "({u. h n u = y} \<inter> g ` S)"] by force
```
```  2801     moreover have "{t. h n (g t) = y} \<inter> S = {x \<in> S. g x \<in> ({u. h n u = y} \<inter> g ` S)}"
```
```  2802       by blast
```
```  2803     ultimately show ?thesis
```
```  2804       by auto
```
```  2805   qed
```
```  2806   have hint: "h n integrable_on g ` S \<and> integral (g ` S) (h n) \<le> integral S (\<lambda>x. ?D x * h n (g x))"
```
```  2807           (is "?INT \<and> ?lhs \<le> ?rhs")  for n
```
```  2808   proof -
```
```  2809     let ?R = "range (h n)"
```
```  2810     have hn_eq: "h n = (\<lambda>x. \<Sum>y\<in>?R. y * indicat_real {x. h n x = y} x)"
```
```  2811       by (simp add: indicator_def if_distrib fin_R cong: if_cong)
```
```  2812     have yind: "(\<lambda>t. y * indicator{x. h n x = y} t) integrable_on (g ` S) \<and>
```
```  2813                 (integral (g ` S) (\<lambda>t. y * indicator {x. h n x = y} t))
```
```  2814                  \<le> integral S (\<lambda>t. \<bar>det (matrix (g' t))\<bar> * y * indicator {x. h n x = y} (g t))"
```
```  2815        if y: "y \<in> ?R" for y::real
```
```  2816     proof (cases "y=0")
```
```  2817       case True
```
```  2818       then show ?thesis using gS_in_sets_leb integrable_0 by force
```
```  2819     next
```
```  2820       case False
```
```  2821       with that have "y > 0"
```
```  2822         using less_eq_real_def nonneg_h by fastforce
```
```  2823       have "(\<lambda>x. if x \<in> {t. h n (g t) = y} then ?D x else 0) integrable_on S"
```
```  2824       proof (rule measurable_bounded_by_integrable_imp_integrable)
```
```  2825         have "(\<lambda>x. ?D x) \<in> borel_measurable (lebesgue_on ({t. h n (g t) = y} \<inter> S))"
```
```  2826           apply (intro borel_measurable_abs borel_measurable_det_Jacobian [OF h_lmeas, where f=g])
```
```  2827           by (meson der_g IntD2 has_derivative_within_subset inf_le2)
```
```  2828         then have "(\<lambda>x. if x \<in> {t. h n (g t) = y} \<inter> S then ?D x else 0) \<in> borel_measurable lebesgue"
```
```  2829           by (rule borel_measurable_If_I [OF _ h_lmeas])
```
```  2830         then show "(\<lambda>x. if x \<in> {t. h n (g t) = y} then ?D x else 0) \<in> borel_measurable (lebesgue_on S)"
```
```  2831           by (simp add: if_if_eq_conj Int_commute borel_measurable_UNIV [OF S, symmetric])
```
```  2832         show "(\<lambda>x. ?D x *\<^sub>R f (g x) /\<^sub>R y) integrable_on S"
```
```  2833           by (rule integrable_cmul) (use det_int_fg in auto)
```
```  2834         show "norm (if x \<in> {t. h n (g t) = y} then ?D x else 0) \<le> ?D x *\<^sub>R f (g x) /\<^sub>R y"
```
```  2835           if "x \<in> S" for x
```
```  2836           using nonneg_h [of n x] \<open>y > 0\<close> nonneg_fg [of x] h_le_f [of x n] that
```
```  2837           by (auto simp: divide_simps ordered_semiring_class.mult_left_mono)
```
```  2838       qed (use S in auto)
```
```  2839       then have int_det: "(\<lambda>t. \<bar>det (matrix (g' t))\<bar>) integrable_on ({t. h n (g t) = y} \<inter> S)"
```
```  2840         using integrable_restrict_Int by force
```
```  2841       have "(g ` ({t. h n (g t) = y} \<inter> S)) \<in> lmeasurable"
```
```  2842         apply (rule measurable_differentiable_image [OF h_lmeas])
```
```  2843          apply (blast intro: has_derivative_within_subset [OF der_g])
```
```  2844         apply (rule int_det)
```
```  2845         done
```
```  2846       moreover have "g ` ({t. h n (g t) = y} \<inter> S) = {x. h n x = y} \<inter> g ` S"
```
```  2847         by blast
```
```  2848       moreover have "measure lebesgue (g ` ({t. h n (g t) = y} \<inter> S))
```
```  2849                      \<le> integral ({t. h n (g t) = y} \<inter> S) (\<lambda>t. \<bar>det (matrix (g' t))\<bar>)"
```
```  2850         apply (rule measure_differentiable_image [OF h_lmeas _ int_det])
```
```  2851         apply (blast intro: has_derivative_within_subset [OF der_g])
```
```  2852         done
```
```  2853       ultimately show ?thesis
```
```  2854         using \<open>y > 0\<close> integral_restrict_Int [of S "{t. h n (g t) = y}" "\<lambda>t. \<bar>det (matrix (g' t))\<bar> * y"]
```
```  2855         apply (simp add: integrable_on_indicator integrable_on_cmult_iff integral_indicator)
```
```  2856         apply (simp add: indicator_def if_distrib cong: if_cong)
```
```  2857         done
```
```  2858     qed
```
```  2859     have hn_int: "h n integrable_on g ` S"
```
```  2860       apply (subst hn_eq)
```
```  2861       using yind by (force intro: integrable_sum [OF fin_R])
```
```  2862     then show ?thesis
```
```  2863     proof
```
```  2864       have "?lhs = integral (g ` S) (\<lambda>x. \<Sum>y\<in>range (h n). y * indicat_real {x. h n x = y} x)"
```
```  2865         by (metis hn_eq)
```
```  2866       also have "\<dots> = (\<Sum>y\<in>range (h n). integral (g ` S) (\<lambda>x. y * indicat_real {x. h n x = y} x))"
```
```  2867         by (rule integral_sum [OF fin_R]) (use yind in blast)
```
```  2868       also have "\<dots> \<le> (\<Sum>y\<in>range (h n). integral S (\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)))"
```
```  2869         using yind by (force intro: sum_mono)
```
```  2870       also have "\<dots> = integral S (\<lambda>u. \<Sum>y\<in>range (h n). \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u))"
```
```  2871       proof (rule integral_sum [OF fin_R, symmetric])
```
```  2872         fix y assume y: "y \<in> ?R"
```
```  2873         with nonneg_h have "y \<ge> 0"
```
```  2874           by auto
```
```  2875         show "(\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)) integrable_on S"
```
```  2876         proof (rule measurable_bounded_by_integrable_imp_integrable)
```
```  2877           have "(\<lambda>x. indicat_real {x. h n x = y} (g x)) \<in> borel_measurable (lebesgue_on S)"
```
```  2878             using h_lmeas S
```
```  2879             by (auto simp: indicator_vimage [symmetric] borel_measurable_indicator_iff sets_restrict_space_iff)
```
```  2880           then show "(\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)) \<in> borel_measurable (lebesgue_on S)"
```
```  2881             by (intro borel_measurable_times borel_measurable_abs borel_measurable_const borel_measurable_det_Jacobian [OF S der_g])
```
```  2882         next
```
```  2883           fix x
```
```  2884           assume "x \<in> S"
```
```  2885           have "y * indicat_real {x. h n x = y} (g x) \<le> f (g x)"
```
```  2886             by (metis (full_types) \<open>x \<in> S\<close> h_le_f indicator_def mem_Collect_eq mult.right_neutral mult_zero_right nonneg_fg)
```
```  2887           with \<open>y \<ge> 0\<close> show "norm (?D x * y * indicat_real {x. h n x = y} (g x)) \<le> ?D x * f(g x)"
```
```  2888             by (simp add: abs_mult mult.assoc mult_left_mono)
```
```  2889         qed (use S det_int_fg in auto)
```
```  2890       qed
```
```  2891       also have "\<dots> = integral S (\<lambda>T. \<bar>det (matrix (g' T))\<bar> *
```
```  2892                                         (\<Sum>y\<in>range (h n). y * indicat_real {x. h n x = y} (g T)))"
```
```  2893         by (simp add: sum_distrib_left mult.assoc)
```
```  2894       also have "\<dots> = ?rhs"
```
```  2895         by (metis hn_eq)
```
```  2896       finally show "integral (g ` S) (h n) \<le> ?rhs" .
```
```  2897     qed
```
```  2898   qed
```
```  2899   have le: "integral S (\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) \<le> ?b" for n
```
```  2900   proof (rule integral_le)
```
```  2901     show "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) integrable_on S"
```
```  2902     proof (rule measurable_bounded_by_integrable_imp_integrable)
```
```  2903       have "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> *\<^sub>R h n (g T)) \<in> borel_measurable (lebesgue_on S)"
```
```  2904       proof (intro borel_measurable_scaleR borel_measurable_abs borel_measurable_det_Jacobian \<open>S \<in> sets lebesgue\<close>)
```
```  2905         have eq: "{x \<in> S. f x \<le> a} = (\<Union>b \<in> (f ` S) \<inter> atMost a. {x. f x = b} \<inter> S)" for f and a::real
```
```  2906           by auto
```
```  2907         have "finite ((\<lambda>x. h n (g x)) ` S \<inter> {..a})" for a
```
```  2908           by (force intro: finite_subset [OF _ fin_R])
```
```  2909         with h_lmeas [of n] show "(\<lambda>x. h n (g x)) \<in> borel_measurable (lebesgue_on S)"
```
```  2910           apply (simp add: borel_measurable_vimage_halfspace_component_le \<open>S \<in> sets lebesgue\<close> sets_restrict_space_iff eq)
```
```  2911           by (metis (mono_tags) SUP_inf sets.finite_UN)
```
```  2912       qed (use der_g in blast)
```
```  2913       then show "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) \<in> borel_measurable (lebesgue_on S)"
```
```  2914         by simp
```
```  2915       show "norm (?D x * h n (g x)) \<le> ?D x *\<^sub>R f (g x)"
```
```  2916         if "x \<in> S" for x
```
```  2917         by (simp add: h_le_f mult_left_mono nonneg_h that)
```
```  2918     qed (use S det_int_fg in auto)
```
```  2919     show "?D x * h n (g x) \<le> ?D x * f (g x)" if "x \<in> S" for x
```
```  2920       by (simp add: \<open>x \<in> S\<close> h_le_f mult_left_mono)
```
```  2921     show "(\<lambda>x. ?D x * f (g x)) integrable_on S"
```
```  2922       using det_int_fg by blast
```
```  2923   qed
```
```  2924   have "f integrable_on g ` S \<and> (\<lambda>k. integral (g ` S) (h k)) \<longlonglongrightarrow> integral (g ` S) f"
```
```  2925   proof (rule monotone_convergence_increasing)
```
```  2926     have "\<bar>integral (g ` S) (h n)\<bar> \<le> integral S (\<lambda>x. ?D x * f (g x))" for n
```
```  2927     proof -
```
```  2928       have "\<bar>integral (g ` S) (h n)\<bar> = integral (g ` S) (h n)"
```
```  2929         using hint by (simp add: integral_nonneg nonneg_h)
```
```  2930       also have "\<dots> \<le> integral S (\<lambda>x. ?D x * f (g x))"
```
```  2931         using hint le by (meson order_trans)
```
```  2932       finally show ?thesis .
```
```  2933     qed
```
```  2934     then show "bounded (range (\<lambda>k. integral (g ` S) (h k)))"
```
```  2935       by (force simp: bounded_iff)
```
```  2936   qed (use h_inc lim hint in auto)
```
```  2937   moreover have "integral (g ` S) (h n) \<le> integral S (\<lambda>x. ?D x * f (g x))" for n
```
```  2938     using hint by (blast intro: le order_trans)
```
```  2939   ultimately show ?thesis
```
```  2940     by (auto intro: Lim_bounded)
```
```  2941 qed
```
```  2942
```
```  2943
```
```  2944 lemma%important integral_on_image_ubound_nonneg:
```
```  2945   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real"
```
```  2946   assumes nonneg_fg: "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
```
```  2947       and der_g:   "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  2948       and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
```
```  2949   shows "f integrable_on (g ` S) \<and> integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
```
```  2950          (is "_ \<and> _ \<le> ?b")
```
```  2951 proof%unimportant -
```
```  2952   let ?D = "\<lambda>x. det (matrix (g' x))"
```
```  2953   define S' where "S' \<equiv> {x \<in> S. ?D x * f(g x) \<noteq> 0}"
```
```  2954   then have der_gS': "\<And>x. x \<in> S' \<Longrightarrow> (g has_derivative g' x) (at x within S')"
```
```  2955     by (metis (mono_tags, lifting) der_g has_derivative_within_subset mem_Collect_eq subset_iff)
```
```  2956   have "(\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) integrable_on UNIV"
```
```  2957     by (simp add: integrable_restrict_UNIV intS)
```
```  2958   then have Df_borel: "(\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> borel_measurable lebesgue"
```
```  2959     using integrable_imp_measurable borel_measurable_UNIV_eq by blast
```
```  2960   have S': "S' \<in> sets lebesgue"
```
```  2961   proof -
```
```  2962     from Df_borel borel_measurable_vimage_open [of _ UNIV]
```
```  2963     have "{x. (if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> T} \<in> sets lebesgue"
```
```  2964       if "open T" for T
```
```  2965       using that unfolding borel_measurable_UNIV_eq
```
```  2966       by (fastforce simp add: dest!: spec)
```
```  2967     then have "{x. (if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> -{0}} \<in> sets lebesgue"
```
```  2968       using open_Compl by blast
```
```  2969     then show ?thesis
```
```  2970       by (simp add: S'_def conj_ac split: if_split_asm cong: conj_cong)
```
```  2971   qed
```
```  2972   then have gS': "g ` S' \<in> sets lebesgue"
```
```  2973   proof (rule differentiable_image_in_sets_lebesgue)
```
```  2974     show "g differentiable_on S'"
```
```  2975       using der_g unfolding S'_def differentiable_def differentiable_on_def
```
```  2976       by (blast intro: has_derivative_within_subset)
```
```  2977   qed auto
```
```  2978   have f: "f \<in> borel_measurable (lebesgue_on (g ` S'))"
```
```  2979   proof (clarsimp simp add: borel_measurable_vimage_open)
```
```  2980     fix T :: "real set"
```
```  2981     assume "open T"
```
```  2982     have "{x \<in> g ` S'. f x \<in> T} = g ` {x \<in> S'. f(g x) \<in> T}"
```
```  2983       by blast
```
```  2984     moreover have "g ` {x \<in> S'. f(g x) \<in> T} \<in> sets lebesgue"
```
```  2985     proof (rule differentiable_image_in_sets_lebesgue)
```
```  2986       let ?h = "\<lambda>x. \<bar>?D x\<bar> * f (g x) /\<^sub>R \<bar>?D x\<bar>"
```
```  2987       have "(\<lambda>x. if x \<in> S' then \<bar>?D x\<bar> * f (g x) else 0) = (\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0)"
```
```  2988         by (auto simp: S'_def)
```
```  2989       also have "\<dots> \<in> borel_measurable lebesgue"
```
```  2990         by (rule Df_borel)
```
```  2991       finally have *: "(\<lambda>x. \<bar>?D x\<bar> * f (g x)) \<in> borel_measurable (lebesgue_on S')"
```
```  2992         by (simp add: borel_measurable_If_D)
```
```  2993       have "?h \<in> borel_measurable (lebesgue_on S')"
```
```  2994         by (intro * S' der_gS' borel_measurable_det_Jacobian measurable) (blast intro: der_gS')
```
```  2995       moreover have "?h x = f(g x)" if "x \<in> S'" for x
```
```  2996         using that by (auto simp: S'_def)
```
```  2997       ultimately have "(\<lambda>x. f(g x)) \<in> borel_measurable (lebesgue_on S')"
```
```  2998         by (metis (no_types, lifting) measurable_lebesgue_cong)
```
```  2999       then show "{x \<in> S'. f (g x) \<in> T} \<in> sets lebesgue"
```
```  3000         by (simp add: \<open>S' \<in> sets lebesgue\<close> \<open>open T\<close> borel_measurable_vimage_open sets_restrict_space_iff)
```
```  3001       show "g differentiable_on {x \<in> S'. f (g x) \<in> T}"
```
```  3002         using der_g unfolding S'_def differentiable_def differentiable_on_def
```
```  3003         by (blast intro: has_derivative_within_subset)
```
```  3004     qed auto
```
```  3005     ultimately have "{x \<in> g ` S'. f x \<in> T} \<in> sets lebesgue"
```
```  3006       by metis
```
```  3007     then show "{x \<in> g ` S'. f x \<in> T} \<in> sets (lebesgue_on (g ` S'))"
```
```  3008       by (simp add: \<open>g ` S' \<in> sets lebesgue\<close> sets_restrict_space_iff)
```
```  3009   qed
```
```  3010   have intS': "(\<lambda>x. \<bar>?D x\<bar> * f (g x)) integrable_on S'"
```
```  3011     using intS
```
```  3012     by (rule integrable_spike_set) (auto simp: S'_def intro: empty_imp_negligible)
```
```  3013   have lebS': "{x \<in> S'. g x \<in> T} \<in> sets lebesgue" if "T \<subseteq> g ` S'" "T \<in> sets lebesgue" for T
```
```  3014   proof -
```
```  3015     have "g \<in> borel_measurable (lebesgue_on S')"
```
```  3016       using der_gS' has_derivative_continuous_on S'
```
```  3017       by (blast intro: continuous_imp_measurable_on_sets_lebesgue)
```
```  3018     moreover have "{x \<in> S'. g x \<in> U} \<in> sets lebesgue" if "negligible U" "U \<subseteq> g ` S'" for U
```
```  3019     proof (intro negligible_imp_sets negligible_differentiable_vimage that)
```
```  3020       fix x
```
```  3021       assume x: "x \<in> S'"
```
```  3022       then have "linear (g' x)"
```
```  3023         using der_gS' has_derivative_linear by blast
```
```  3024       with x show "inj (g' x)"
```
```  3025         by (auto simp: S'_def det_nz_iff_inj)
```
```  3026     qed (use der_gS' in auto)
```
```  3027     ultimately show ?thesis
```
```  3028       using double_lebesgue_sets [OF S' gS' order_refl] that by blast
```
```  3029   qed
```
```  3030   have int_gS': "f integrable_on g ` S' \<and> integral (g ` S') f \<le> integral S' (\<lambda>x. \<bar>?D x\<bar> * f(g x))"
```
```  3031     using integral_on_image_ubound_weak [OF S' f nonneg_fg der_gS' intS' lebS'] S'_def by blast
```
```  3032   have "negligible (g ` {x \<in> S. det(matrix(g' x)) = 0})"
```
```  3033   proof (rule baby_Sard, simp_all)
```
```  3034     fix x
```
```  3035     assume x: "x \<in> S \<and> det (matrix (g' x)) = 0"
```
```  3036     then show "(g has_derivative g' x) (at x within {x \<in> S. det (matrix (g' x)) = 0})"
```
```  3037       by (metis (no_types, lifting) der_g has_derivative_within_subset mem_Collect_eq subsetI)
```
```  3038     then show "rank (matrix (g' x)) < CARD('n)"
```
```  3039       using det_nz_iff_inj matrix_vector_mul_linear x
```
```  3040       by (fastforce simp add: less_rank_noninjective)
```
```  3041   qed
```
```  3042   then have negg: "negligible (g ` S - g ` {x \<in> S. ?D x \<noteq> 0})"
```
```  3043     by (rule negligible_subset) (auto simp: S'_def)
```
```  3044   have null: "g ` {x \<in> S. ?D x \<noteq> 0} - g ` S = {}"
```
```  3045     by (auto simp: S'_def)
```
```  3046   let ?F = "{x \<in> S. f (g x) \<noteq> 0}"
```
```  3047   have eq: "g ` S' = g ` ?F \<inter> g ` {x \<in> S. ?D x \<noteq> 0}"
```
```  3048     by (auto simp: S'_def image_iff)
```
```  3049   show ?thesis
```
```  3050   proof
```
```  3051     have "((\<lambda>x. if x \<in> g ` ?F then f x else 0) integrable_on g ` {x \<in> S. ?D x \<noteq> 0})"
```
```  3052       using int_gS' eq integrable_restrict_Int [where f=f]
```
```  3053       by simp
```
```  3054     then have "f integrable_on g ` {x \<in> S. ?D x \<noteq> 0}"
```
```  3055       by (auto simp: image_iff elim!: integrable_eq)
```
```  3056     then show "f integrable_on g ` S"
```
```  3057       apply (rule integrable_spike_set [OF _ empty_imp_negligible negligible_subset])
```
```  3058       using negg null by auto
```
```  3059     have "integral (g ` S) f = integral (g ` {x \<in> S. ?D x \<noteq> 0}) f"
```
```  3060       using negg by (auto intro: negligible_subset integral_spike_set)
```
```  3061     also have "\<dots> = integral (g ` {x \<in> S. ?D x \<noteq> 0}) (\<lambda>x. if x \<in> g ` ?F then f x else 0)"
```
```  3062       by (auto simp: image_iff intro!: integral_cong)
```
```  3063     also have "\<dots> = integral (g ` S') f"
```
```  3064       using  eq integral_restrict_Int by simp
```
```  3065     also have "\<dots> \<le> integral S' (\<lambda>x. \<bar>?D x\<bar> * f(g x))"
```
```  3066       by (metis int_gS')
```
```  3067     also have "\<dots> \<le> ?b"
```
```  3068       by (rule integral_subset_le [OF _ intS' intS]) (use nonneg_fg S'_def in auto)
```
```  3069     finally show "integral (g ` S) f \<le> ?b" .
```
```  3070   qed
```
```  3071 qed
```
```  3072
```
```  3073
```
```  3074 lemma%unimportant absolutely_integrable_on_image_real:
```
```  3075   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
```
```  3076   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3077     and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) absolutely_integrable_on S"
```
```  3078   shows "f absolutely_integrable_on (g ` S)"
```
```  3079 proof -
```
```  3080   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f (g x)"
```
```  3081   let ?N = "{x \<in> S. f (g x) < 0}" and ?P = "{x \<in> S. f (g x) > 0}"
```
```  3082   have eq: "{x. (if x \<in> S then ?D x else 0) > 0} = {x \<in> S. ?D x > 0}"
```
```  3083            "{x. (if x \<in> S then ?D x else 0) < 0} = {x \<in> S. ?D x < 0}"
```
```  3084     by auto
```
```  3085   have "?D integrable_on S"
```
```  3086     using intS absolutely_integrable_on_def by blast
```
```  3087   then have "(\<lambda>x. if x \<in> S then ?D x else 0) integrable_on UNIV"
```
```  3088     by (simp add: integrable_restrict_UNIV)
```
```  3089   then have D_borel: "(\<lambda>x. if x \<in> S then ?D x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
```
```  3090     using integrable_imp_measurable borel_measurable_UNIV_eq by blast
```
```  3091   then have Dlt: "{x \<in> S. ?D x < 0} \<in> sets lebesgue"
```
```  3092     unfolding borel_measurable_vimage_halfspace_component_lt
```
```  3093     by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3094   from D_borel have Dgt: "{x \<in> S. ?D x > 0} \<in> sets lebesgue"
```
```  3095     unfolding borel_measurable_vimage_halfspace_component_gt
```
```  3096     by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3097
```
```  3098   have dfgbm: "?D \<in> borel_measurable (lebesgue_on S)"
```
```  3099     using intS absolutely_integrable_on_def integrable_imp_measurable by blast
```
```  3100   have der_gN: "(g has_derivative g' x) (at x within ?N)" if "x \<in> ?N" for x
```
```  3101       using der_g has_derivative_within_subset that by force
```
```  3102   have "(\<lambda>x. - f x) integrable_on g ` ?N \<and>
```
```  3103          integral (g ` ?N) (\<lambda>x. - f x) \<le> integral ?N (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x))"
```
```  3104   proof (rule integral_on_image_ubound_nonneg [OF _ der_gN])
```
```  3105     have 1: "?D integrable_on {x \<in> S. ?D x < 0}"
```
```  3106       using Dlt
```
```  3107       by (auto intro: set_lebesgue_integral_eq_integral [OF set_integrable_subset] intS)
```
```  3108     have "uminus \<circ> (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x)) integrable_on ?N"
```
```  3109       by (simp add: o_def mult_less_0_iff empty_imp_negligible integrable_spike_set [OF 1])
```
```  3110     then show "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x)) integrable_on ?N"
```
```  3111       by (simp add: integrable_neg_iff o_def)
```
```  3112   qed auto
```
```  3113   then have "f integrable_on g ` ?N"
```
```  3114     by (simp add: integrable_neg_iff)
```
```  3115   moreover have "g ` ?N = {y \<in> g ` S. f y < 0}"
```
```  3116     by auto
```
```  3117   ultimately have "f integrable_on {y \<in> g ` S. f y < 0}"
```
```  3118     by simp
```
```  3119   then have N: "f absolutely_integrable_on {y \<in> g ` S. f y < 0}"
```
```  3120     by (rule absolutely_integrable_absolutely_integrable_ubound) auto
```
```  3121
```
```  3122   have der_gP: "(g has_derivative g' x) (at x within ?P)" if "x \<in> ?P" for x
```
```  3123       using der_g has_derivative_within_subset that by force
```
```  3124   have "f integrable_on g ` ?P \<and> integral (g ` ?P) f \<le> integral ?P ?D"
```
```  3125   proof (rule integral_on_image_ubound_nonneg [OF _ der_gP])
```
```  3126     have "?D integrable_on {x \<in> S. 0 < ?D x}"
```
```  3127       using Dgt
```
```  3128       by (auto intro: set_lebesgue_integral_eq_integral [OF set_integrable_subset] intS)
```
```  3129     then show "?D integrable_on ?P"
```
```  3130       apply (rule integrable_spike_set)
```
```  3131       by (auto simp: zero_less_mult_iff empty_imp_negligible)
```
```  3132   qed auto
```
```  3133   then have "f integrable_on g ` ?P"
```
```  3134     by metis
```
```  3135   moreover have "g ` ?P = {y \<in> g ` S. f y > 0}"
```
```  3136     by auto
```
```  3137   ultimately have "f integrable_on {y \<in> g ` S. f y > 0}"
```
```  3138     by simp
```
```  3139   then have P: "f absolutely_integrable_on {y \<in> g ` S. f y > 0}"
```
```  3140     by (rule absolutely_integrable_absolutely_integrable_lbound) auto
```
```  3141   have "(\<lambda>x. if x \<in> g ` S \<and> f x < 0 \<or> x \<in> g ` S \<and> 0 < f x then f x else 0) = (\<lambda>x. if x \<in> g ` S then f x else 0)"
```
```  3142     by auto
```
```  3143   then show ?thesis
```
```  3144     using absolutely_integrable_Un [OF N P] absolutely_integrable_restrict_UNIV [symmetric, where f=f]
```
```  3145     by simp
```
```  3146 qed
```
```  3147
```
```  3148
```
```  3149 proposition%important absolutely_integrable_on_image:
```
```  3150   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3151   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3152     and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S"
```
```  3153   shows "f absolutely_integrable_on (g ` S)"
```
```  3154   apply (rule absolutely_integrable_componentwise [OF absolutely_integrable_on_image_real [OF der_g]])
```
```  3155   using%unimportant absolutely_integrable_component [OF intS]  by%unimportant auto
```
```  3156
```
```  3157 proposition%important integral_on_image_ubound:
```
```  3158   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
```
```  3159   assumes"\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
```
```  3160     and "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3161     and "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
```
```  3162   shows "integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
```
```  3163   using%unimportant integral_on_image_ubound_nonneg [OF assms] by%unimportant simp
```
```  3164
```
```  3165
```
```  3166 subsection%important\<open>Change-of-variables theorem\<close>
```
```  3167
```
```  3168 text\<open>The classic change-of-variables theorem. We have two versions with quite general hypotheses,
```
```  3169 the first that the transforming function has a continuous inverse, the second that the base set is
```
```  3170 Lebesgue measurable.\<close>
```
```  3171 lemma%unimportant cov_invertible_nonneg_le:
```
```  3172   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
```
```  3173   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3174     and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
```
```  3175     and f0: "\<And>y. y \<in> T \<Longrightarrow> 0 \<le> f y"
```
```  3176     and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
```
```  3177     and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
```
```  3178     and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
```
```  3179   shows "f integrable_on T \<and> (integral T f) \<le> b \<longleftrightarrow>
```
```  3180              (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S \<and>
```
```  3181              integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) \<le> b"
```
```  3182         (is "?lhs = ?rhs")
```
```  3183 proof -
```
```  3184   have Teq: "T = g`S" and Seq: "S = h`T"
```
```  3185     using hg gh image_iff by fastforce+
```
```  3186   have gS: "g differentiable_on S"
```
```  3187     by (meson der_g differentiable_def differentiable_on_def)
```
```  3188   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f (g x)"
```
```  3189   show ?thesis
```
```  3190   proof
```
```  3191     assume ?lhs
```
```  3192     then have fT: "f integrable_on T" and intf: "integral T f \<le> b"
```
```  3193       by blast+
```
```  3194     show ?rhs
```
```  3195     proof
```
```  3196       let ?fgh = "\<lambda>x. \<bar>det (matrix (h' x))\<bar> * (\<bar>det (matrix (g' (h x)))\<bar> * f (g (h x)))"
```
```  3197       have ddf: "?fgh x = f x"
```
```  3198         if "x \<in> T" for x
```
```  3199       proof -
```
```  3200         have "matrix (h' x) ** matrix (g' (h x)) = mat 1"
```
```  3201           using that id[OF that] der_g[of "h x"] gh[OF that] left_inverse_linear has_derivative_linear
```
```  3202           by (subst matrix_compose[symmetric]) (force simp: matrix_id_mat_1 has_derivative_linear)+
```
```  3203         then have "\<bar>det (matrix (h' x))\<bar> * \<bar>det (matrix (g' (h x)))\<bar> = 1"
```
```  3204           by (metis abs_1 abs_mult det_I det_mul)
```
```  3205         then show ?thesis
```
```  3206           by (simp add: gh that)
```
```  3207       qed
```
```  3208       have "?D integrable_on (h ` T)"
```
```  3209       proof (intro set_lebesgue_integral_eq_integral absolutely_integrable_on_image_real)
```
```  3210         show "(\<lambda>x. ?fgh x) absolutely_integrable_on T"
```
```  3211         proof (subst absolutely_integrable_on_iff_nonneg)
```
```  3212           show "(\<lambda>x. ?fgh x) integrable_on T"
```
```  3213             using ddf fT integrable_eq by force
```
```  3214         qed (simp add: zero_le_mult_iff f0 gh)
```
```  3215       qed (use der_h in auto)
```
```  3216       with Seq show "(\<lambda>x. ?D x) integrable_on S"
```
```  3217         by simp
```
```  3218       have "integral S (\<lambda>x. ?D x) \<le> integral T (\<lambda>x. ?fgh x)"
```
```  3219         unfolding Seq
```
```  3220       proof (rule integral_on_image_ubound)
```
```  3221         show "(\<lambda>x. ?fgh x) integrable_on T"
```
```  3222         using ddf fT integrable_eq by force
```
```  3223       qed (use f0 gh der_h in auto)
```
```  3224       also have "\<dots> = integral T f"
```
```  3225         by (force simp: ddf intro: integral_cong)
```
```  3226       also have "\<dots> \<le> b"
```
```  3227         by (rule intf)
```
```  3228       finally show "integral S (\<lambda>x. ?D x) \<le> b" .
```
```  3229     qed
```
```  3230   next
```
```  3231     assume R: ?rhs
```
```  3232     then have "f integrable_on g ` S"
```
```  3233       using der_g f0 hg integral_on_image_ubound_nonneg by blast
```
```  3234     moreover have "integral (g ` S) f \<le> integral S (\<lambda>x. ?D x)"
```
```  3235       by (rule integral_on_image_ubound [OF f0 der_g]) (use R Teq in auto)
```
```  3236     ultimately show ?lhs
```
```  3237       using R by (simp add: Teq)
```
```  3238   qed
```
```  3239 qed
```
```  3240
```
```  3241
```
```  3242 lemma%unimportant cov_invertible_nonneg_eq:
```
```  3243   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
```
```  3244   assumes "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3245       and "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
```
```  3246       and "\<And>y. y \<in> T \<Longrightarrow> 0 \<le> f y"
```
```  3247       and "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
```
```  3248       and "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
```
```  3249       and "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
```
```  3250   shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) has_integral b) S \<longleftrightarrow> (f has_integral b) T"
```
```  3251   using cov_invertible_nonneg_le [OF assms]
```
```  3252   by (simp add: has_integral_iff) (meson eq_iff)
```
```  3253
```
```  3254
```
```  3255 lemma%unimportant cov_invertible_real:
```
```  3256   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
```
```  3257   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3258       and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
```
```  3259       and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
```
```  3260       and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
```
```  3261       and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
```
```  3262   shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) absolutely_integrable_on S \<and>
```
```  3263            integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) = b \<longleftrightarrow>
```
```  3264          f absolutely_integrable_on T \<and> integral T f = b"
```
```  3265          (is "?lhs = ?rhs")
```
```  3266 proof -
```
```  3267   have Teq: "T = g`S" and Seq: "S = h`T"
```
```  3268     using hg gh image_iff by fastforce+
```
```  3269   let ?DP = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)" and ?DN = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * -f(g x)"
```
```  3270   have "+": "(?DP has_integral b) {x \<in> S. f (g x) > 0} \<longleftrightarrow> (f has_integral b) {y \<in> T. f y > 0}" for b
```
```  3271   proof (rule cov_invertible_nonneg_eq)
```
```  3272     have *: "(\<lambda>x. f (g x)) -` Y \<inter> {x \<in> S. f (g x) > 0}
```
```  3273           = ((\<lambda>x. f (g x)) -` Y \<inter> S) \<inter> {x \<in> S. f (g x) > 0}" for Y
```
```  3274       by auto
```
```  3275     show "(g has_derivative g' x) (at x within {x \<in> S. f (g x) > 0})" if "x \<in> {x \<in> S. f (g x) > 0}" for x
```
```  3276       using that der_g has_derivative_within_subset by fastforce
```
```  3277     show "(h has_derivative h' y) (at y within {y \<in> T. f y > 0})" if "y \<in> {y \<in> T. f y > 0}" for y
```
```  3278       using that der_h has_derivative_within_subset by fastforce
```
```  3279   qed (use gh hg id in auto)
```
```  3280   have "-": "(?DN has_integral b) {x \<in> S. f (g x) < 0} \<longleftrightarrow> ((\<lambda>x. - f x) has_integral b) {y \<in> T. f y < 0}" for b
```
```  3281   proof (rule cov_invertible_nonneg_eq)
```
```  3282     have *: "(\<lambda>x. - f (g x)) -` y \<inter> {x \<in> S. f (g x) < 0}
```
```  3283           = ((\<lambda>x. f (g x)) -` uminus ` y \<inter> S) \<inter> {x \<in> S. f (g x) < 0}" for y
```
```  3284       using image_iff by fastforce
```
```  3285     show "(g has_derivative g' x) (at x within {x \<in> S. f (g x) < 0})" if "x \<in> {x \<in> S. f (g x) < 0}" for x
```
```  3286       using that der_g has_derivative_within_subset by fastforce
```
```  3287     show "(h has_derivative h' y) (at y within {y \<in> T. f y < 0})" if "y \<in> {y \<in> T. f y < 0}" for y
```
```  3288       using that der_h has_derivative_within_subset by fastforce
```
```  3289   qed (use gh hg id in auto)
```
```  3290   show ?thesis
```
```  3291   proof
```
```  3292     assume LHS: ?lhs
```
```  3293     have eq: "{x. (if x \<in> S then ?DP x else 0) > 0} = {x \<in> S. ?DP x > 0}"
```
```  3294       "{x. (if x \<in> S then ?DP x else 0) < 0} = {x \<in> S. ?DP x < 0}"
```
```  3295       by auto
```
```  3296     have "?DP integrable_on S"
```
```  3297       using LHS absolutely_integrable_on_def by blast
```
```  3298     then have "(\<lambda>x. if x \<in> S then ?DP x else 0) integrable_on UNIV"
```
```  3299       by (simp add: integrable_restrict_UNIV)
```
```  3300     then have D_borel: "(\<lambda>x. if x \<in> S then ?DP x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
```
```  3301       using integrable_imp_measurable borel_measurable_UNIV_eq by blast
```
```  3302     then have SN: "{x \<in> S. ?DP x < 0} \<in> sets lebesgue"
```
```  3303       unfolding borel_measurable_vimage_halfspace_component_lt
```
```  3304       by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3305     from D_borel have SP: "{x \<in> S. ?DP x > 0} \<in> sets lebesgue"
```
```  3306       unfolding borel_measurable_vimage_halfspace_component_gt
```
```  3307       by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3308     have "?DP absolutely_integrable_on {x \<in> S. ?DP x > 0}"
```
```  3309       using LHS by (fast intro!: set_integrable_subset [OF _, of _ S] SP)
```
```  3310     then have aP: "?DP absolutely_integrable_on {x \<in> S. f (g x) > 0}"
```
```  3311       by (rule absolutely_integrable_spike_set) (auto simp: zero_less_mult_iff empty_imp_negligible)
```
```  3312     have "?DP absolutely_integrable_on {x \<in> S. ?DP x < 0}"
```
```  3313       using LHS by (fast intro!: set_integrable_subset [OF _, of _ S] SN)
```
```  3314     then have aN: "?DP absolutely_integrable_on {x \<in> S. f (g x) < 0}"
```
```  3315       by (rule absolutely_integrable_spike_set) (auto simp: mult_less_0_iff empty_imp_negligible)
```
```  3316     have fN: "f integrable_on {y \<in> T. f y < 0}"
```
```  3317              "integral {y \<in> T. f y < 0} f = integral {x \<in> S. f (g x) < 0} ?DP"
```
```  3318       using "-" [of "integral {x \<in> S. f(g x) < 0} ?DN"] aN
```
```  3319       by (auto simp: set_lebesgue_integral_eq_integral has_integral_iff integrable_neg_iff)
```
```  3320     have faN: "f absolutely_integrable_on {y \<in> T. f y < 0}"
```
```  3321       apply (rule absolutely_integrable_integrable_bound [where g = "\<lambda>x. - f x"])
```
```  3322       using fN by (auto simp: integrable_neg_iff)
```
```  3323     have fP: "f integrable_on {y \<in> T. f y > 0}"
```
```  3324              "integral {y \<in> T. f y > 0} f = integral {x \<in> S. f (g x) > 0} ?DP"
```
```  3325       using "+" [of "integral {x \<in> S. f(g x) > 0} ?DP"] aP
```
```  3326       by (auto simp: set_lebesgue_integral_eq_integral has_integral_iff integrable_neg_iff)
```
```  3327     have faP: "f absolutely_integrable_on {y \<in> T. f y > 0}"
```
```  3328       apply (rule absolutely_integrable_integrable_bound [where g = f])
```
```  3329       using fP by auto
```
```  3330     have fa: "f absolutely_integrable_on ({y \<in> T. f y < 0} \<union> {y \<in> T. f y > 0})"
```
```  3331       by (rule absolutely_integrable_Un [OF faN faP])
```
```  3332     show ?rhs
```
```  3333     proof
```
```  3334       have eq: "((if x \<in> T \<and> f x < 0 \<or> x \<in> T \<and> 0 < f x then 1 else 0) * f x)
```
```  3335               = (if x \<in> T then 1 else 0) * f x" for x
```
```  3336         by auto
```
```  3337       show "f absolutely_integrable_on T"
```
```  3338         using fa by (simp add: indicator_def set_integrable_def eq)
```
```  3339       have [simp]: "{y \<in> T. f y < 0} \<inter> {y \<in> T. 0 < f y} = {}" for T and f :: "(real^'n::_) \<Rightarrow> real"
```
```  3340         by auto
```
```  3341       have "integral T f = integral ({y \<in> T. f y < 0} \<union> {y \<in> T. f y > 0}) f"
```
```  3342         by (intro empty_imp_negligible integral_spike_set) (auto simp: eq)
```
```  3343       also have "\<dots> = integral {y \<in> T. f y < 0} f + integral {y \<in> T. f y > 0} f"
```
```  3344         using fN fP by simp
```
```  3345       also have "\<dots> = integral {x \<in> S. f (g x) < 0} ?DP + integral {x \<in> S. 0 < f (g x)} ?DP"
```
```  3346         by (simp add: fN fP)
```
```  3347       also have "\<dots> = integral ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. 0 < f (g x)}) ?DP"
```
```  3348         using aP aN by (simp add: set_lebesgue_integral_eq_integral)
```
```  3349       also have "\<dots> = integral S ?DP"
```
```  3350         by (intro empty_imp_negligible integral_spike_set) auto
```
```  3351       also have "\<dots> = b"
```
```  3352         using LHS by simp
```
```  3353       finally show "integral T f = b" .
```
```  3354     qed
```
```  3355   next
```
```  3356     assume RHS: ?rhs
```
```  3357     have eq: "{x. (if x \<in> T then f x else 0) > 0} = {x \<in> T. f x > 0}"
```
```  3358              "{x. (if x \<in> T then f x else 0) < 0} = {x \<in> T. f x < 0}"
```
```  3359       by auto
```
```  3360     have "f integrable_on T"
```
```  3361       using RHS absolutely_integrable_on_def by blast
```
```  3362     then have "(\<lambda>x. if x \<in> T then f x else 0) integrable_on UNIV"
```
```  3363       by (simp add: integrable_restrict_UNIV)
```
```  3364     then have D_borel: "(\<lambda>x. if x \<in> T then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
```
```  3365       using integrable_imp_measurable borel_measurable_UNIV_eq by blast
```
```  3366     then have TN: "{x \<in> T. f x < 0} \<in> sets lebesgue"
```
```  3367       unfolding borel_measurable_vimage_halfspace_component_lt
```
```  3368       by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3369     from D_borel have TP: "{x \<in> T. f x > 0} \<in> sets lebesgue"
```
```  3370       unfolding borel_measurable_vimage_halfspace_component_gt
```
```  3371       by (drule_tac x=0 in spec) (auto simp: eq)
```
```  3372     have aint: "f absolutely_integrable_on {y. y \<in> T \<and> 0 < (f y)}"
```
```  3373                "f absolutely_integrable_on {y. y \<in> T \<and> (f y) < 0}"
```
```  3374          and intT: "integral T f = b"
```
```  3375       using set_integrable_subset [of _ T] TP TN RHS
```
```  3376       by blast+
```
```  3377     show ?lhs
```
```  3378     proof
```
```  3379       have fN: "f integrable_on {v \<in> T. f v < 0}"
```
```  3380         using absolutely_integrable_on_def aint by blast
```
```  3381       then have DN: "(?DN has_integral integral {y \<in> T. f y < 0} (\<lambda>x. - f x)) {x \<in> S. f (g x) < 0}"
```
```  3382         using "-" [of "integral {y \<in> T. f y < 0} (\<lambda>x. - f x)"]
```
```  3383         by (simp add: has_integral_neg_iff integrable_integral)
```
```  3384       have aDN: "?DP absolutely_integrable_on {x \<in> S. f (g x) < 0}"
```
```  3385         apply (rule absolutely_integrable_integrable_bound [where g = ?DN])
```
```  3386         using DN hg by (fastforce simp: abs_mult integrable_neg_iff)+
```
```  3387       have fP: "f integrable_on {v \<in> T. f v > 0}"
```
```  3388         using absolutely_integrable_on_def aint by blast
```
```  3389       then have DP: "(?DP has_integral integral {y \<in> T. f y > 0} f) {x \<in> S. f (g x) > 0}"
```
```  3390         using "+" [of "integral {y \<in> T. f y > 0} f"]
```
```  3391         by (simp add: has_integral_neg_iff integrable_integral)
```
```  3392       have aDP: "?DP absolutely_integrable_on {x \<in> S. f (g x) > 0}"
```
```  3393         apply (rule absolutely_integrable_integrable_bound [where g = ?DP])
```
```  3394         using DP hg by (fastforce simp: integrable_neg_iff)+
```
```  3395       have eq: "(if x \<in> S then 1 else 0) * ?DP x = (if x \<in> S \<and> f (g x) < 0 \<or> x \<in> S \<and> f (g x) > 0 then 1 else 0) * ?DP x" for x
```
```  3396         by force
```
```  3397       have "?DP absolutely_integrable_on ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. f (g x) > 0})"
```
```  3398         by (rule absolutely_integrable_Un [OF aDN aDP])
```
```  3399       then show I: "?DP absolutely_integrable_on S"
```
```  3400         by (simp add: indicator_def eq set_integrable_def)
```
```  3401       have [simp]: "{y \<in> S. f y < 0} \<inter> {y \<in> S. 0 < f y} = {}" for S and f :: "(real^'n::_) \<Rightarrow> real"
```
```  3402         by auto
```
```  3403       have "integral S ?DP = integral ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. f (g x) > 0}) ?DP"
```
```  3404         by (intro empty_imp_negligible integral_spike_set) auto
```
```  3405       also have "\<dots> = integral {x \<in> S. f (g x) < 0} ?DP + integral {x \<in> S. 0 < f (g x)} ?DP"
```
```  3406         using aDN aDP by (simp add: set_lebesgue_integral_eq_integral)
```
```  3407       also have "\<dots> = - integral {y \<in> T. f y < 0} (\<lambda>x. - f x) + integral {y \<in> T. f y > 0} f"
```
```  3408         using DN DP by (auto simp: has_integral_iff)
```
```  3409       also have "\<dots> = integral ({x \<in> T. f x < 0} \<union> {x \<in> T. 0 < f x}) f"
```
```  3410         by (simp add: fN fP)
```
```  3411       also have "\<dots> = integral T f"
```
```  3412         by (intro empty_imp_negligible integral_spike_set) auto
```
```  3413       also have "\<dots> = b"
```
```  3414         using intT by simp
```
```  3415       finally show "integral S ?DP = b" .
```
```  3416     qed
```
```  3417   qed
```
```  3418 qed
```
```  3419
```
```  3420
```
```  3421 lemma%unimportant cv_inv_version3:
```
```  3422   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3423   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3424     and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
```
```  3425     and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
```
```  3426     and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
```
```  3427     and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
```
```  3428   shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3429              integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
```
```  3430          \<longleftrightarrow> f absolutely_integrable_on T \<and> integral T f = b"
```
```  3431 proof -
```
```  3432   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)"
```
```  3433   have "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x) \$ i) absolutely_integrable_on S \<and> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * (f(g x) \$ i)) = b \$ i) \<longleftrightarrow>
```
```  3434         ((\<lambda>x. f x \$ i) absolutely_integrable_on T \<and> integral T (\<lambda>x. f x \$ i) = b \$ i)" for i
```
```  3435     by (rule cov_invertible_real [OF der_g der_h hg gh id])
```
```  3436   then have "?D absolutely_integrable_on S \<and> (?D has_integral b) S \<longleftrightarrow>
```
```  3437         f absolutely_integrable_on T \<and> (f has_integral b) T"
```
```  3438     unfolding absolutely_integrable_componentwise_iff [where f=f] has_integral_componentwise_iff [of f]
```
```  3439               absolutely_integrable_componentwise_iff [where f="?D"] has_integral_componentwise_iff [of ?D]
```
```  3440     by (auto simp: all_conj_distrib Basis_vec_def cart_eq_inner_axis [symmetric]
```
```  3441            has_integral_iff set_lebesgue_integral_eq_integral)
```
```  3442   then show ?thesis
```
```  3443     using absolutely_integrable_on_def by blast
```
```  3444 qed
```
```  3445
```
```  3446
```
```  3447 lemma%unimportant cv_inv_version4:
```
```  3448   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3449   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S) \<and> invertible(matrix(g' x))"
```
```  3450     and hg: "\<And>x. x \<in> S \<Longrightarrow> continuous_on (g ` S) h \<and> h(g x) = x"
```
```  3451   shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3452              integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
```
```  3453          \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3454 proof -
```
```  3455   have "\<forall>x. \<exists>h'. x \<in> S
```
```  3456            \<longrightarrow> (g has_derivative g' x) (at x within S) \<and> linear h' \<and> g' x \<circ> h' = id \<and> h' \<circ> g' x = id"
```
```  3457     using der_g matrix_invertible has_derivative_linear by blast
```
```  3458   then obtain h' where h':
```
```  3459     "\<And>x. x \<in> S
```
```  3460            \<Longrightarrow> (g has_derivative g' x) (at x within S) \<and>
```
```  3461                linear (h' x) \<and> g' x \<circ> (h' x) = id \<and> (h' x) \<circ> g' x = id"
```
```  3462     by metis
```
```  3463   show ?thesis
```
```  3464   proof (rule cv_inv_version3)
```
```  3465     show "\<And>y. y \<in> g ` S \<Longrightarrow> (h has_derivative h' (h y)) (at y within g ` S)"
```
```  3466       using h' hg
```
```  3467       by (force simp: continuous_on_eq_continuous_within intro!: has_derivative_inverse_within)
```
```  3468   qed (use h' hg in auto)
```
```  3469 qed
```
```  3470
```
```  3471
```
```  3472 proposition%important has_absolute_integral_change_of_variables_invertible:
```
```  3473   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3474   assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3475       and hg: "\<And>x. x \<in> S \<Longrightarrow> h(g x) = x"
```
```  3476       and conth: "continuous_on (g ` S) h"
```
```  3477   shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b \<longleftrightarrow>
```
```  3478          f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3479     (is "?lhs = ?rhs")
```
```  3480 proof%unimportant -
```
```  3481   let ?S = "{x \<in> S. invertible (matrix (g' x))}" and ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)"
```
```  3482   have *: "?D absolutely_integrable_on ?S \<and> integral ?S ?D = b
```
```  3483            \<longleftrightarrow> f absolutely_integrable_on (g ` ?S) \<and> integral (g ` ?S) f = b"
```
```  3484   proof (rule cv_inv_version4)
```
```  3485     show "(g has_derivative g' x) (at x within ?S) \<and> invertible (matrix (g' x))"
```
```  3486       if "x \<in> ?S" for x
```
```  3487       using der_g that has_derivative_within_subset that by fastforce
```
```  3488     show "continuous_on (g ` ?S) h \<and> h (g x) = x"
```
```  3489       if "x \<in> ?S" for x
```
```  3490       using that continuous_on_subset [OF conth]  by (simp add: hg image_mono)
```
```  3491   qed
```
```  3492   have "(g has_derivative g' x) (at x within {x \<in> S. rank (matrix (g' x)) < CARD('m)})" if "x \<in> S" for x
```
```  3493     by (metis (no_types, lifting) der_g has_derivative_within_subset mem_Collect_eq subsetI that)
```
```  3494   then have "negligible (g ` {x \<in> S. \<not> invertible (matrix (g' x))})"
```
```  3495     by (auto simp: invertible_det_nz det_eq_0_rank intro: baby_Sard)
```
```  3496   then have neg: "negligible {x \<in> g ` S. x \<notin> g ` ?S \<and> f x \<noteq> 0}"
```
```  3497     by (auto intro: negligible_subset)
```
```  3498   have [simp]: "{x \<in> g ` ?S. x \<notin> g ` S \<and> f x \<noteq> 0} = {}"
```
```  3499     by auto
```
```  3500   have "?D absolutely_integrable_on ?S \<and> integral ?S ?D = b
```
```  3501     \<longleftrightarrow> ?D absolutely_integrable_on S \<and> integral S ?D = b"
```
```  3502     apply (intro conj_cong absolutely_integrable_spike_set_eq)
```
```  3503       apply(auto simp: integral_spike_set invertible_det_nz empty_imp_negligible neg)
```
```  3504     done
```
```  3505   moreover
```
```  3506   have "f absolutely_integrable_on (g ` ?S) \<and> integral (g ` ?S) f = b
```
```  3507     \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3508     by (auto intro!: conj_cong absolutely_integrable_spike_set_eq integral_spike_set neg)
```
```  3509   ultimately
```
```  3510   show ?thesis
```
```  3511     using "*" by blast
```
```  3512 qed
```
```  3513
```
```  3514
```
```  3515
```
```  3516 lemma%unimportant has_absolute_integral_change_of_variables_compact:
```
```  3517   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3518   assumes "compact S"
```
```  3519       and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3520       and inj: "inj_on g S"
```
```  3521   shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3522              integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
```
```  3523       \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b)"
```
```  3524 proof -
```
```  3525   obtain h where hg: "\<And>x. x \<in> S \<Longrightarrow> h(g x) = x"
```
```  3526     using inj by (metis the_inv_into_f_f)
```
```  3527   have conth: "continuous_on (g ` S) h"
```
```  3528     by (metis \<open>compact S\<close> continuous_on_inv der_g has_derivative_continuous_on hg)
```
```  3529   show ?thesis
```
```  3530     by (rule has_absolute_integral_change_of_variables_invertible [OF der_g hg conth])
```
```  3531 qed
```
```  3532
```
```  3533
```
```  3534 lemma%unimportant has_absolute_integral_change_of_variables_compact_family:
```
```  3535   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3536   assumes compact: "\<And>n::nat. compact (F n)"
```
```  3537       and der_g: "\<And>x. x \<in> (\<Union>n. F n) \<Longrightarrow> (g has_derivative g' x) (at x within (\<Union>n. F n))"
```
```  3538       and inj: "inj_on g (\<Union>n. F n)"
```
```  3539   shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on (\<Union>n. F n) \<and>
```
```  3540              integral (\<Union>n. F n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
```
```  3541       \<longleftrightarrow> f absolutely_integrable_on (g ` (\<Union>n. F n)) \<and> integral (g ` (\<Union>n. F n)) f = b)"
```
```  3542 proof -
```
```  3543   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)"
```
```  3544   let ?U = "\<lambda>n. \<Union>m\<le>n. F m"
```
```  3545   let ?lift = "vec::real\<Rightarrow>real^1"
```
```  3546   have F_leb: "F m \<in> sets lebesgue" for m
```
```  3547     by (simp add: compact borel_compact)
```
```  3548   have iff: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)) absolutely_integrable_on (?U n) \<and>
```
```  3549              integral (?U n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)) = b
```
```  3550          \<longleftrightarrow> f absolutely_integrable_on (g ` (?U n)) \<and> integral (g ` (?U n)) f = b" for n b and f :: "real^'m::_ \<Rightarrow> real^'k"
```
```  3551   proof (rule has_absolute_integral_change_of_variables_compact)
```
```  3552     show "compact (?U n)"
```
```  3553       by (simp add: compact compact_UN)
```
```  3554     show "(g has_derivative g' x) (at x within (?U n))"
```
```  3555       if "x \<in> ?U n" for x
```
```  3556       using that by (blast intro!: has_derivative_within_subset [OF der_g])
```
```  3557     show "inj_on g (?U n)"
```
```  3558       using inj by (auto simp: inj_on_def)
```
```  3559   qed
```
```  3560   show ?thesis
```
```  3561     unfolding image_UN
```
```  3562   proof safe
```
```  3563     assume DS: "?D absolutely_integrable_on (\<Union>n. F n)"
```
```  3564       and b: "b = integral (\<Union>n. F n) ?D"
```
```  3565     have DU: "\<And>n. ?D absolutely_integrable_on (?U n)"
```
```  3566              "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
```
```  3567       using integral_countable_UN [OF DS F_leb] by auto
```
```  3568     with iff have fag: "f absolutely_integrable_on g ` (?U n)"
```
```  3569       and fg_int: "integral (\<Union>m\<le>n. g ` F m) f = integral (?U n) ?D" for n
```
```  3570       by (auto simp: image_UN)
```
```  3571     let ?h = "\<lambda>x. if x \<in> (\<Union>m. g ` F m) then norm(f x) else 0"
```
```  3572     have "(\<lambda>x. if x \<in> (\<Union>m. g ` F m) then f x else 0) absolutely_integrable_on UNIV"
```
```  3573     proof (rule dominated_convergence_absolutely_integrable)
```
```  3574       show "(\<lambda>x. if x \<in> (\<Union>m\<le>k. g ` F m) then f x else 0) absolutely_integrable_on UNIV" for k
```
```  3575         unfolding absolutely_integrable_restrict_UNIV
```
```  3576         using fag by (simp add: image_UN)
```
```  3577       let ?nf = "\<lambda>n x. if x \<in> (\<Union>m\<le>n. g ` F m) then norm(f x) else 0"
```
```  3578       show "?h integrable_on UNIV"
```
```  3579       proof (rule monotone_convergence_increasing [THEN conjunct1])
```
```  3580         show "?nf k integrable_on UNIV" for k
```
```  3581           using fag
```
```  3582           unfolding integrable_restrict_UNIV absolutely_integrable_on_def by (simp add: image_UN)
```
```  3583         { fix n
```
```  3584           have "(norm \<circ> ?D) absolutely_integrable_on ?U n"
```
```  3585             by (intro absolutely_integrable_norm DU)
```
```  3586           then have "integral (g ` ?U n) (norm \<circ> f) = integral (?U n) (norm \<circ> ?D)"
```
```  3587             using iff [of n "vec \<circ> norm \<circ> f" "integral (?U n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R (?lift \<circ> norm \<circ> f) (g x))"]
```
```  3588             unfolding absolutely_integrable_on_1_iff integral_on_1_eq by (auto simp: o_def)
```
```  3589         }
```
```  3590         moreover have "bounded (range (\<lambda>k. integral (?U k) (norm \<circ> ?D)))"
```
```  3591           unfolding bounded_iff
```
```  3592         proof (rule exI, clarify)
```
```  3593           fix k
```
```  3594           show "norm (integral (?U k) (norm \<circ> ?D)) \<le> integral (\<Union>n. F n) (norm \<circ> ?D)"
```
```  3595             unfolding integral_restrict_UNIV [of _ "norm \<circ> ?D", symmetric]
```
```  3596           proof (rule integral_norm_bound_integral)
```
```  3597             show "(\<lambda>x. if x \<in> \<Union> (F ` {..k}) then (norm \<circ> ?D) x else 0) integrable_on UNIV"
```
```  3598               "(\<lambda>x. if x \<in> (\<Union>n. F n) then (norm \<circ> ?D) x else 0) integrable_on UNIV"
```
```  3599               using DU(1) DS
```
```  3600               unfolding absolutely_integrable_on_def o_def integrable_restrict_UNIV by auto
```
```  3601           qed auto
```
```  3602         qed
```
```  3603         ultimately show "bounded (range (\<lambda>k. integral UNIV (?nf k)))"
```
```  3604           by (simp add: integral_restrict_UNIV image_UN [symmetric] o_def)
```
```  3605       next
```
```  3606         show "(\<lambda>k. if x \<in> (\<Union>m\<le>k. g ` F m) then norm (f x) else 0)
```
```  3607               \<longlonglongrightarrow> (if x \<in> (\<Union>m. g ` F m) then norm (f x) else 0)" for x
```
```  3608           by (force intro: Lim_eventually eventually_sequentiallyI)
```
```  3609       qed auto
```
```  3610     next
```
```  3611       show "(\<lambda>k. if x \<in> (\<Union>m\<le>k. g ` F m) then f x else 0)
```
```  3612             \<longlonglongrightarrow> (if x \<in> (\<Union>m. g ` F m) then f x else 0)" for x
```
```  3613       proof clarsimp
```
```  3614         fix m y
```
```  3615         assume "y \<in> F m"
```
```  3616         show "(\<lambda>k. if \<exists>x\<in>{..k}. g y \<in> g ` F x then f (g y) else 0) \<longlonglongrightarrow> f (g y)"
```
```  3617           using \<open>y \<in> F m\<close> by (force intro: Lim_eventually eventually_sequentiallyI [of m])
```
```  3618       qed
```
```  3619     qed auto
```
```  3620     then show fai: "f absolutely_integrable_on (\<Union>m. g ` F m)"
```
```  3621       using absolutely_integrable_restrict_UNIV by blast
```
```  3622     show "integral ((\<Union>x. g ` F x)) f = integral (\<Union>n. F n) ?D"
```
```  3623     proof (rule LIMSEQ_unique)
```
```  3624       show "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>x. g ` F x) f"
```
```  3625         unfolding fg_int [symmetric]
```
```  3626       proof (rule integral_countable_UN [OF fai])
```
```  3627         show "g ` F m \<in> sets lebesgue" for m
```
```  3628         proof (rule differentiable_image_in_sets_lebesgue [OF F_leb])
```
```  3629           show "g differentiable_on F m"
```
```  3630             by (meson der_g differentiableI UnionI differentiable_on_def differentiable_on_subset rangeI subsetI)
```
```  3631         qed auto
```
```  3632       qed
```
```  3633     next
```
```  3634       show "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
```
```  3635         by (rule DU)
```
```  3636     qed
```
```  3637   next
```
```  3638     assume fs: "f absolutely_integrable_on (\<Union>x. g ` F x)"
```
```  3639       and b: "b = integral ((\<Union>x. g ` F x)) f"
```
```  3640     have gF_leb: "g ` F m \<in> sets lebesgue" for m
```
```  3641     proof (rule differentiable_image_in_sets_lebesgue [OF F_leb])
```
```  3642       show "g differentiable_on F m"
```
```  3643         using der_g unfolding differentiable_def differentiable_on_def
```
```  3644         by (meson Sup_upper UNIV_I UnionI has_derivative_within_subset image_eqI)
```
```  3645     qed auto
```
```  3646     have fgU: "\<And>n. f absolutely_integrable_on (\<Union>m\<le>n. g ` F m)"
```
```  3647       "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>m. g ` F m) f"
```
```  3648       using integral_countable_UN [OF fs gF_leb] by auto
```
```  3649     with iff have DUn: "?D absolutely_integrable_on ?U n"
```
```  3650       and D_int: "integral (?U n) ?D = integral (\<Union>m\<le>n. g ` F m) f" for n
```
```  3651       by (auto simp: image_UN)
```
```  3652     let ?h = "\<lambda>x. if x \<in> (\<Union>n. F n) then norm(?D x) else 0"
```
```  3653     have "(\<lambda>x. if x \<in> (\<Union>n. F n) then ?D x else 0) absolutely_integrable_on UNIV"
```
```  3654     proof (rule dominated_convergence_absolutely_integrable)
```
```  3655       show "(\<lambda>x. if x \<in> ?U k then ?D x else 0) absolutely_integrable_on UNIV" for k
```
```  3656         unfolding absolutely_integrable_restrict_UNIV using DUn by simp
```
```  3657       let ?nD = "\<lambda>n x. if x \<in> ?U n then norm(?D x) else 0"
```
```  3658       show "?h integrable_on UNIV"
```
```  3659       proof (rule monotone_convergence_increasing [THEN conjunct1])
```
```  3660         show "?nD k integrable_on UNIV" for k
```
```  3661           using DUn
```
```  3662           unfolding integrable_restrict_UNIV absolutely_integrable_on_def by (simp add: image_UN)
```
```  3663         { fix n::nat
```
```  3664           have "(norm \<circ> f) absolutely_integrable_on (\<Union>m\<le>n. g ` F m)"
```
```  3665             apply (rule absolutely_integrable_norm)
```
```  3666             using fgU by blast
```
```  3667           then have "integral (?U n) (norm \<circ> ?D) = integral (g ` ?U n) (norm \<circ> f)"
```
```  3668             using iff [of n "?lift \<circ> norm \<circ> f" "integral (g ` ?U n) (?lift \<circ> norm \<circ> f)"]
```
```  3669             unfolding absolutely_integrable_on_1_iff integral_on_1_eq image_UN by (auto simp: o_def)
```
```  3670         }
```
```  3671         moreover have "bounded (range (\<lambda>k. integral (g ` ?U k) (norm \<circ> f)))"
```
```  3672           unfolding bounded_iff
```
```  3673         proof (rule exI, clarify)
```
```  3674           fix k
```
```  3675           show "norm (integral (g ` ?U k) (norm \<circ> f)) \<le> integral (g ` (\<Union>n. F n)) (norm \<circ> f)"
```
```  3676             unfolding integral_restrict_UNIV [of _ "norm \<circ> f", symmetric]
```
```  3677           proof (rule integral_norm_bound_integral)
```
```  3678             show "(\<lambda>x. if x \<in> g ` ?U k then (norm \<circ> f) x else 0) integrable_on UNIV"
```
```  3679                  "(\<lambda>x. if x \<in> g ` (\<Union>n. F n) then (norm \<circ> f) x else 0) integrable_on UNIV"
```
```  3680               using fgU fs
```
```  3681               unfolding absolutely_integrable_on_def o_def integrable_restrict_UNIV
```
```  3682               by (auto simp: image_UN)
```
```  3683           qed auto
```
```  3684         qed
```
```  3685         ultimately show "bounded (range (\<lambda>k. integral UNIV (?nD k)))"
```
```  3686           unfolding integral_restrict_UNIV image_UN [symmetric] o_def by simp
```
```  3687       next
```
```  3688         show "(\<lambda>k. if x \<in> ?U k then norm (?D x) else 0) \<longlonglongrightarrow> (if x \<in> (\<Union>n. F n) then norm (?D x) else 0)" for x
```
```  3689           by (force intro: Lim_eventually eventually_sequentiallyI)
```
```  3690       qed auto
```
```  3691     next
```
```  3692       show "(\<lambda>k. if x \<in> ?U k then ?D x else 0) \<longlonglongrightarrow> (if x \<in> (\<Union>n. F n) then ?D x else 0)" for x
```
```  3693       proof clarsimp
```
```  3694         fix n
```
```  3695         assume "x \<in> F n"
```
```  3696         show "(\<lambda>m. if \<exists>j\<in>{..m}. x \<in> F j then ?D x else 0) \<longlonglongrightarrow> ?D x"
```
```  3697           using \<open>x \<in> F n\<close> by (auto intro!: Lim_eventually eventually_sequentiallyI [of n])
```
```  3698       qed
```
```  3699     qed auto
```
```  3700     then show Dai: "?D absolutely_integrable_on (\<Union>n. F n)"
```
```  3701       unfolding absolutely_integrable_restrict_UNIV by simp
```
```  3702     show "integral (\<Union>n. F n) ?D = integral ((\<Union>x. g ` F x)) f"
```
```  3703     proof (rule LIMSEQ_unique)
```
```  3704       show "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>x. g ` F x) f"
```
```  3705         by (rule fgU)
```
```  3706       show "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
```
```  3707         unfolding D_int [symmetric] by (rule integral_countable_UN [OF Dai F_leb])
```
```  3708     qed
```
```  3709   qed
```
```  3710 qed
```
```  3711
```
```  3712
```
```  3713 proposition%important has_absolute_integral_change_of_variables:
```
```  3714   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3715   assumes S: "S \<in> sets lebesgue"
```
```  3716     and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3717     and inj: "inj_on g S"
```
```  3718   shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3719            integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
```
```  3720      \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3721 proof%unimportant -
```
```  3722   obtain C N where "fsigma C" "negligible N" and CNS: "C \<union> N = S" and "disjnt C N"
```
```  3723     using lebesgue_set_almost_fsigma [OF S] .
```
```  3724   then obtain F :: "nat \<Rightarrow> (real^'m::_) set"
```
```  3725     where F: "range F \<subseteq> Collect compact" and Ceq: "C = Union(range F)"
```
```  3726     using fsigma_Union_compact by metis
```
```  3727   let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)"
```
```  3728   have "?D absolutely_integrable_on C \<and> integral C ?D = b
```
```  3729     \<longleftrightarrow> f absolutely_integrable_on (g ` C) \<and> integral (g ` C) f = b"
```
```  3730     unfolding Ceq
```
```  3731   proof (rule has_absolute_integral_change_of_variables_compact_family)
```
```  3732     fix n x
```
```  3733     assume "x \<in> \<Union>(F ` UNIV)"
```
```  3734     then show "(g has_derivative g' x) (at x within \<Union>(F ` UNIV))"
```
```  3735       using Ceq \<open>C \<union> N = S\<close> der_g has_derivative_within_subset by blast
```
```  3736   next
```
```  3737     have "\<Union>(F ` UNIV) \<subseteq> S"
```
```  3738       using Ceq \<open>C \<union> N = S\<close> by blast
```
```  3739     then show "inj_on g (\<Union>(F ` UNIV))"
```
```  3740       using inj by (meson inj_on_subset)
```
```  3741   qed (use F in auto)
```
```  3742   moreover
```
```  3743   have "?D absolutely_integrable_on C \<and> integral C ?D = b
```
```  3744     \<longleftrightarrow> ?D absolutely_integrable_on S \<and> integral S ?D = b"
```
```  3745   proof (rule conj_cong)
```
```  3746     have neg: "negligible {x \<in> C - S. ?D x \<noteq> 0}" "negligible {x \<in> S - C. ?D x \<noteq> 0}"
```
```  3747       using CNS by (blast intro: negligible_subset [OF \<open>negligible N\<close>])+
```
```  3748     then show "(?D absolutely_integrable_on C) = (?D absolutely_integrable_on S)"
```
```  3749       by (rule absolutely_integrable_spike_set_eq)
```
```  3750     show "(integral C ?D = b) \<longleftrightarrow> (integral S ?D = b)"
```
```  3751       using integral_spike_set [OF neg] by simp
```
```  3752   qed
```
```  3753   moreover
```
```  3754   have "f absolutely_integrable_on (g ` C) \<and> integral (g ` C) f = b
```
```  3755     \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3756   proof (rule conj_cong)
```
```  3757     have "g differentiable_on N"
```
```  3758       by (metis CNS der_g differentiable_def differentiable_on_def differentiable_on_subset sup.cobounded2)
```
```  3759     with \<open>negligible N\<close>
```
```  3760     have neg_gN: "negligible (g ` N)"
```
```  3761       by (blast intro: negligible_differentiable_image_negligible)
```
```  3762     have neg: "negligible {x \<in> g ` C - g ` S. f x \<noteq> 0}"
```
```  3763               "negligible {x \<in> g ` S - g ` C. f x \<noteq> 0}"
```
```  3764       using CNS by (blast intro: negligible_subset [OF neg_gN])+
```
```  3765     then show "(f absolutely_integrable_on g ` C) = (f absolutely_integrable_on g ` S)"
```
```  3766       by (rule absolutely_integrable_spike_set_eq)
```
```  3767     show "(integral (g ` C) f = b) \<longleftrightarrow> (integral (g ` S) f = b)"
```
```  3768       using integral_spike_set [OF neg] by simp
```
```  3769   qed
```
```  3770   ultimately show ?thesis
```
```  3771     by simp
```
```  3772 qed
```
```  3773
```
```  3774
```
```  3775 corollary%important absolutely_integrable_change_of_variables:
```
```  3776   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3777   assumes "S \<in> sets lebesgue"
```
```  3778     and "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3779     and "inj_on g S"
```
```  3780   shows "f absolutely_integrable_on (g ` S)
```
```  3781      \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S"
```
```  3782   using%unimportant assms has_absolute_integral_change_of_variables by%unimportant blast
```
```  3783
```
```  3784 corollary%important integral_change_of_variables:
```
```  3785   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3786   assumes S: "S \<in> sets lebesgue"
```
```  3787     and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
```
```  3788     and inj: "inj_on g S"
```
```  3789     and disj: "(f absolutely_integrable_on (g ` S) \<or>
```
```  3790         (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S)"
```
```  3791   shows "integral (g ` S) f = integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x))"
```
```  3792   using%unimportant has_absolute_integral_change_of_variables [OF S der_g inj] disj
```
```  3793   by%unimportant blast
```
```  3794
```
```  3795 lemma%unimportant has_absolute_integral_change_of_variables_1:
```
```  3796   fixes f :: "real \<Rightarrow> real^'n::{finite,wellorder}" and g :: "real \<Rightarrow> real"
```
```  3797   assumes S: "S \<in> sets lebesgue"
```
```  3798     and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_vector_derivative g' x) (at x within S)"
```
```  3799     and inj: "inj_on g S"
```
```  3800   shows "(\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3801            integral S (\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) = b
```
```  3802      \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3803 proof -
```
```  3804   let ?lift = "vec :: real \<Rightarrow> real^1"
```
```  3805   let ?drop = "(\<lambda>x::real^1. x \$ 1)"
```
```  3806   have S': "?lift ` S \<in> sets lebesgue"
```
```  3807     by (auto intro: differentiable_image_in_sets_lebesgue [OF S] differentiable_vec)
```
```  3808   have "((\<lambda>x. vec (g (x \$ 1))) has_derivative (*\<^sub>R) (g' z)) (at (vec z) within ?lift ` S)"
```
```  3809     if "z \<in> S" for z
```
```  3810     using der_g [OF that]
```
```  3811     by (simp add: has_vector_derivative_def has_derivative_vector_1)
```
```  3812   then have der': "\<And>x. x \<in> ?lift ` S \<Longrightarrow>
```
```  3813         (?lift \<circ> g \<circ> ?drop has_derivative (*\<^sub>R) (g' (?drop x))) (at x within ?lift ` S)"
```
```  3814     by (auto simp: o_def)
```
```  3815   have inj': "inj_on (vec \<circ> g \<circ> ?drop) (vec ` S)"
```
```  3816     using inj by (simp add: inj_on_def)
```
```  3817   let ?fg = "\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)"
```
```  3818   have "((\<lambda>x. ?fg x \$ i) absolutely_integrable_on S \<and> ((\<lambda>x. ?fg x \$ i) has_integral b \$ i) S
```
```  3819     \<longleftrightarrow> (\<lambda>x. f x \$ i) absolutely_integrable_on g ` S \<and> ((\<lambda>x. f x \$ i) has_integral b \$ i) (g ` S))" for i
```
```  3820     using has_absolute_integral_change_of_variables [OF S' der' inj', of "\<lambda>x. ?lift(f (?drop x) \$ i)" "?lift (b\$i)"]
```
```  3821     unfolding integrable_on_1_iff integral_on_1_eq absolutely_integrable_on_1_iff absolutely_integrable_drop absolutely_integrable_on_def
```
```  3822     by (auto simp: image_comp o_def integral_vec1_eq has_integral_iff)
```
```  3823   then have "?fg absolutely_integrable_on S \<and> (?fg has_integral b) S
```
```  3824          \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> (f has_integral b) (g ` S)"
```
```  3825     unfolding has_integral_componentwise_iff [where y=b]
```
```  3826            absolutely_integrable_componentwise_iff [where f=f]
```
```  3827            absolutely_integrable_componentwise_iff [where f = ?fg]
```
```  3828     by (force simp: Basis_vec_def cart_eq_inner_axis)
```
```  3829   then show ?thesis
```
```  3830     using absolutely_integrable_on_def by blast
```
```  3831 qed
```
```  3832
```
```  3833
```
```  3834 corollary%important absolutely_integrable_change_of_variables_1:
```
```  3835   fixes f :: "real \<Rightarrow> real^'n::{finite,wellorder}" and g :: "real \<Rightarrow> real"
```
```  3836   assumes S: "S \<in> sets lebesgue"
```
```  3837     and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_vector_derivative g' x) (at x within S)"
```
```  3838     and inj: "inj_on g S"
```
```  3839   shows "(f absolutely_integrable_on g ` S \<longleftrightarrow>
```
```  3840              (\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S)"
```
```  3841   using%unimportant has_absolute_integral_change_of_variables_1 [OF assms] by%unimportant auto
```
```  3842
```
```  3843
```
```  3844 subsection%important\<open>Change of variables for integrals: special case of linear function\<close>
```
```  3845
```
```  3846 lemma%unimportant has_absolute_integral_change_of_variables_linear:
```
```  3847   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3848   assumes "linear g"
```
```  3849   shows "(\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
```
```  3850            integral S (\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) = b
```
```  3851      \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
```
```  3852 proof (cases "det(matrix g) = 0")
```
```  3853   case True
```
```  3854   then have "negligible(g ` S)"
```
```  3855     using assms det_nz_iff_inj negligible_linear_singular_image by blast
```
```  3856   with True show ?thesis
```
```  3857     by (auto simp: absolutely_integrable_on_def integrable_negligible integral_negligible)
```
```  3858 next
```
```  3859   case False
```
```  3860   then obtain h where h: "\<And>x. x \<in> S \<Longrightarrow> h (g x) = x" "linear h"
```
```  3861     using assms det_nz_iff_inj linear_injective_isomorphism by blast
```
```  3862   show ?thesis
```
```  3863   proof (rule has_absolute_integral_change_of_variables_invertible)
```
```  3864     show "(g has_derivative g) (at x within S)" for x
```
```  3865       by (simp add: assms linear_imp_has_derivative)
```
```  3866     show "continuous_on (g ` S) h"
```
```  3867       using continuous_on_eq_continuous_within has_derivative_continuous linear_imp_has_derivative h by blast
```
```  3868   qed (use h in auto)
```
```  3869 qed
```
```  3870
```
```  3871 lemma%unimportant absolutely_integrable_change_of_variables_linear:
```
```  3872   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3873   assumes "linear g"
```
```  3874   shows "(\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S
```
```  3875      \<longleftrightarrow> f absolutely_integrable_on (g ` S)"
```
```  3876   using assms has_absolute_integral_change_of_variables_linear by blast
```
```  3877
```
```  3878 lemma%unimportant absolutely_integrable_on_linear_image:
```
```  3879   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3880   assumes "linear g"
```
```  3881   shows "f absolutely_integrable_on (g ` S)
```
```  3882      \<longleftrightarrow> (f \<circ> g) absolutely_integrable_on S \<or> det(matrix g) = 0"
```
```  3883   unfolding assms absolutely_integrable_change_of_variables_linear [OF assms, symmetric] absolutely_integrable_on_scaleR_iff
```
```  3884   by (auto simp: set_integrable_def)
```
```  3885
```
```  3886 lemma%important integral_change_of_variables_linear:
```
```  3887   fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
```
```  3888   assumes "linear g"
```
```  3889       and "f absolutely_integrable_on (g ` S) \<or> (f \<circ> g) absolutely_integrable_on S"
```
```  3890     shows "integral (g ` S) f = \<bar>det (matrix g)\<bar> *\<^sub>R integral S (f \<circ> g)"
```
```  3891 proof%unimportant -
```
```  3892   have "((\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f (g x)) absolutely_integrable_on S) \<or> (f absolutely_integrable_on g ` S)"
```
```  3893     using absolutely_integrable_on_linear_image assms by blast
```
```  3894   moreover
```
```  3895   have ?thesis if "((\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f (g x)) absolutely_integrable_on S)" "(f absolutely_integrable_on g ` S)"
```
```  3896     using has_absolute_integral_change_of_variables_linear [OF \<open>linear g\<close>] that
```
```  3897     by (auto simp: o_def)
```
```  3898   ultimately show ?thesis
```
```  3899     using absolutely_integrable_change_of_variables_linear [OF \<open>linear g\<close>]
```
```  3900     by blast
```
```  3901 qed
```
```  3902
```
```  3903 subsection%important\<open>Change of variable for measure\<close>
```
```  3904
```
```  3905 lemma%unimportant has_measure_differentiable_image:
```
```  3906   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  3907   assumes "S \<in> sets lebesgue"
```
```  3908       and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  3909       and "inj_on f S"
```
```  3910   shows "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) = m
```
```  3911      \<longleftrightarrow> ((\<lambda>x. \<bar>det (matrix (f' x))\<bar>) has_integral m) S"
```
```  3912   using%unimportant has_absolute_integral_change_of_variables [OF assms, of "\<lambda>x. (1::real^1)" "vec m"]
```
```  3913   unfolding%unimportant absolutely_integrable_on_1_iff integral_on_1_eq integrable_on_1_iff absolutely_integrable_on_def
```
```  3914   by%unimportant (auto simp: has_integral_iff lmeasurable_iff_integrable_on lmeasure_integral)
```
```  3915
```
```  3916 lemma%unimportant measurable_differentiable_image_eq:
```
```  3917   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  3918   assumes "S \<in> sets lebesgue"
```
```  3919       and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  3920       and "inj_on f S"
```
```  3921   shows "f ` S \<in> lmeasurable \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
```
```  3922   using has_measure_differentiable_image [OF assms]
```
```  3923   by blast
```
```  3924
```
```  3925 lemma%important measurable_differentiable_image_alt:
```
```  3926   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  3927   assumes "S \<in> sets lebesgue"
```
```  3928     and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  3929     and "inj_on f S"
```
```  3930   shows "f ` S \<in> lmeasurable \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
```
```  3931   using%unimportant measurable_differentiable_image_eq [OF assms]
```
```  3932   by%unimportant (simp only: absolutely_integrable_on_iff_nonneg)
```
```  3933
```
```  3934 lemma%important measure_differentiable_image_eq:
```
```  3935   fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
```
```  3936   assumes S: "S \<in> sets lebesgue"
```
```  3937     and der_f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
```
```  3938     and inj: "inj_on f S"
```
```  3939     and intS: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
```
```  3940   shows "measure lebesgue (f ` S) = integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
```
```  3941   using%unimportant measurable_differentiable_image_eq [OF S der_f inj]
```
```  3942         assms has_measure_differentiable_image by%unimportant blast
```
```  3943
```
```  3944 end
```