src/HOL/Analysis/Change_Of_Vars.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (4 weeks ago)
changeset 69981 3dced198b9ec
parent 69922 4a9167f377b0
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
lp15@68017
     1
(*  Title:      HOL/Analysis/Change_Of_Vars.thy
lp15@68017
     2
    Authors:    LC Paulson, based on material from HOL Light
lp15@68017
     3
*)
lp15@68017
     4
lp15@68017
     5
section\<open>Change of Variables Theorems\<close>
lp15@68017
     6
lp15@67998
     7
theory Change_Of_Vars
lp15@67999
     8
  imports Vitali_Covering_Theorem Determinants
lp15@67998
     9
lp15@67998
    10
begin
lp15@67998
    11
ak2110@69720
    12
subsection \<open>Orthogonal Transformation of Balls\<close>
lp15@67998
    13
ak2110@69720
    14
lemma image_orthogonal_transformation_ball:
immler@69675
    15
  fixes f :: "'a::euclidean_space \<Rightarrow> 'a"
immler@69675
    16
  assumes "orthogonal_transformation f"
immler@69675
    17
  shows "f ` ball x r = ball (f x) r"
immler@69675
    18
proof (intro equalityI subsetI)
immler@69675
    19
  fix y assume "y \<in> f ` ball x r"
immler@69675
    20
  with assms show "y \<in> ball (f x) r"
immler@69675
    21
    by (auto simp: orthogonal_transformation_isometry)
immler@69675
    22
next
immler@69675
    23
  fix y assume y: "y \<in> ball (f x) r"
immler@69675
    24
  then obtain z where z: "y = f z"
immler@69675
    25
    using assms orthogonal_transformation_surj by blast
immler@69675
    26
  with y assms show "y \<in> f ` ball x r"
immler@69675
    27
    by (auto simp: orthogonal_transformation_isometry)
lp15@67998
    28
qed
lp15@67998
    29
ak2110@69720
    30
lemma  image_orthogonal_transformation_cball:
immler@69675
    31
  fixes f :: "'a::euclidean_space \<Rightarrow> 'a"
immler@69675
    32
  assumes "orthogonal_transformation f"
immler@69675
    33
  shows "f ` cball x r = cball (f x) r"
immler@69675
    34
proof (intro equalityI subsetI)
immler@69675
    35
  fix y assume "y \<in> f ` cball x r"
immler@69675
    36
  with assms show "y \<in> cball (f x) r"
immler@69675
    37
    by (auto simp: orthogonal_transformation_isometry)
immler@69675
    38
next
immler@69675
    39
  fix y assume y: "y \<in> cball (f x) r"
immler@69675
    40
  then obtain z where z: "y = f z"
immler@69675
    41
    using assms orthogonal_transformation_surj by blast
immler@69675
    42
  with y assms show "y \<in> f ` cball x r"
immler@69675
    43
    by (auto simp: orthogonal_transformation_isometry)
lp15@67998
    44
qed
lp15@67998
    45
immler@68072
    46
immler@69675
    47
subsection \<open>Measurable Shear and Stretch\<close>
lp15@67998
    48
ak2110@69720
    49
proposition
lp15@67998
    50
  fixes a :: "real^'n"
lp15@67998
    51
  assumes "m \<noteq> n" and ab_ne: "cbox a b \<noteq> {}" and an: "0 \<le> a$n"
lp15@67998
    52
  shows measurable_shear_interval: "(\<lambda>x. \<chi> i. if i = m then x$m + x$n else x$i) ` (cbox a b) \<in> lmeasurable"
lp15@67998
    53
       (is  "?f ` _ \<in> _")
lp15@67998
    54
   and measure_shear_interval: "measure lebesgue ((\<lambda>x. \<chi> i. if i = m then x$m + x$n else x$i) ` cbox a b)
lp15@67998
    55
               = measure lebesgue (cbox a b)" (is "?Q")
ak2110@69720
    56
proof -
lp15@67998
    57
  have lin: "linear ?f"
immler@68072
    58
    by (rule linearI) (auto simp: plus_vec_def scaleR_vec_def algebra_simps)
lp15@67998
    59
  show fab: "?f ` cbox a b \<in> lmeasurable"
lp15@67998
    60
    by (simp add: lin measurable_linear_image_interval)
lp15@67998
    61
  let ?c = "\<chi> i. if i = m then b$m + b$n else b$i"
lp15@67998
    62
  let ?mn = "axis m 1 - axis n (1::real)"
lp15@67998
    63
  have eq1: "measure lebesgue (cbox a ?c)
lp15@67998
    64
            = measure lebesgue (?f ` cbox a b)
lp15@67998
    65
            + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a$m})
lp15@67998
    66
            + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b$m})"
lp15@67998
    67
  proof (rule measure_Un3_negligible)
lp15@67998
    68
    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"
lp15@67998
    69
      by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
lp15@67998
    70
    have "negligible {x. ?mn \<bullet> x = a$m}"
lp15@67998
    71
      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
lp15@67998
    72
    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}"
lp15@67998
    73
      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
lp15@67998
    74
    ultimately show "negligible ((?f ` cbox a b) \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a $ m}))"
lp15@67998
    75
      by (rule negligible_subset)
lp15@67998
    76
    have "negligible {x. ?mn \<bullet> x = b$m}"
lp15@67998
    77
      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
lp15@67998
    78
    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}"
lp15@67998
    79
      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
lp15@67998
    80
    ultimately show "negligible (?f ` cbox a b \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b$m}))"
lp15@67998
    81
      by (rule negligible_subset)
lp15@67998
    82
    have "negligible {x. ?mn \<bullet> x = b$m}"
lp15@67998
    83
      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
lp15@67998
    84
    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}"
lp15@67998
    85
      using \<open>m \<noteq> n\<close> ab_ne
lp15@67998
    86
      apply (auto simp: algebra_simps mem_box_cart inner_axis')
lp15@67998
    87
      apply (drule_tac x=m in spec)+
lp15@67998
    88
      apply simp
lp15@67998
    89
      done
lp15@67998
    90
    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}))"
lp15@67998
    91
      by (rule negligible_subset)
lp15@67998
    92
    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 = _")
lp15@67998
    93
    proof
lp15@67998
    94
      show "?lhs \<subseteq> cbox a ?c"
lp15@67998
    95
        by (auto simp: mem_box_cart add_mono) (meson add_increasing2 an order_trans)
lp15@67998
    96
      show "cbox a ?c \<subseteq> ?lhs"
lp15@67998
    97
        apply (auto simp: algebra_simps image_iff inner_axis' lambda_add_Galois [OF \<open>m \<noteq> n\<close>])
lp15@67998
    98
        apply (auto simp: mem_box_cart split: if_split_asm)
lp15@67998
    99
        done
lp15@67998
   100
    qed
lp15@67998
   101
  qed (fact fab)
lp15@67998
   102
  let ?d = "\<chi> i. if i = m then a $ m - b $ m else 0"
lp15@67998
   103
  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})
lp15@67998
   104
           = measure lebesgue (cbox a (\<chi> i. if i = m then a $ m + b $ n else b $ i))"
lp15@67998
   105
  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}"
lp15@67998
   106
     "(\<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)"])
lp15@67998
   107
    show "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a$m}) \<in> lmeasurable"
lp15@67998
   108
      "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b$m} \<in> lmeasurable"
lp15@67998
   109
      by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
lp15@67998
   110
    have "\<And>x. \<lbrakk>x $ n + a $ m \<le> x $ m\<rbrakk>
lp15@67998
   111
         \<Longrightarrow> x \<in> (+) (\<chi> i. if i = m then a $ m - b $ m else 0) ` {x. x $ n + b $ m \<le> x $ m}"
lp15@67998
   112
      using \<open>m \<noteq> n\<close>
lp15@67998
   113
      by (rule_tac x="x - (\<chi> i. if i = m then a$m - b$m else 0)" in image_eqI)
lp15@67998
   114
         (simp_all add: mem_box_cart)
lp15@67998
   115
    then have imeq: "(+) ?d ` {x. b $ m \<le> ?mn \<bullet> x} = {x. a $ m \<le> ?mn \<bullet> x}"
lp15@67998
   116
      using \<open>m \<noteq> n\<close> by (auto simp: mem_box_cart inner_axis' algebra_simps)
lp15@67998
   117
    have "\<And>x. \<lbrakk>0 \<le> a $ n; x $ n + a $ m \<le> x $ m;
lp15@67998
   118
                \<forall>i. i \<noteq> m \<longrightarrow> a $ i \<le> x $ i \<and> x $ i \<le> b $ i\<rbrakk>
lp15@67998
   119
         \<Longrightarrow> a $ m \<le> x $ m"
lp15@67998
   120
      using \<open>m \<noteq> n\<close>  by force
lp15@67998
   121
    then have "(+) ?d ` (cbox a ?c \<inter> {x. b $ m \<le> ?mn \<bullet> x})
lp15@67998
   122
            = cbox a (\<chi> i. if i = m then a $ m + b $ n else b $ i) \<inter> {x. a $ m \<le> ?mn \<bullet> x}"
lp15@67998
   123
      using an ab_ne
lp15@67998
   124
      apply (simp add: cbox_translation [symmetric] translation_Int interval_ne_empty_cart imeq)
lp15@67998
   125
      apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib)
lp15@67998
   126
      by (metis (full_types) add_mono mult_2_right)
lp15@67998
   127
    then show "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a $ m} \<union>
lp15@67998
   128
          (+) ?d ` (cbox a ?c \<inter> {x. b $ m \<le> ?mn \<bullet> x}) =
lp15@67998
   129
          cbox a (\<chi> i. if i = m then a $ m + b $ n else b $ i)"  (is "?lhs = ?rhs")
lp15@67998
   130
      using an \<open>m \<noteq> n\<close>
lp15@67998
   131
      apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib, force)
lp15@67998
   132
        apply (drule_tac x=n in spec)+
lp15@67998
   133
      by (meson ab_ne add_mono_thms_linordered_semiring(3) dual_order.trans interval_ne_empty_cart(1))
lp15@67998
   134
    have "negligible{x. ?mn \<bullet> x = a$m}"
lp15@67998
   135
      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
lp15@67998
   136
    moreover have "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a $ m} \<inter>
lp15@67998
   137
                                 (+) ?d ` (cbox a ?c \<inter> {x. b $ m \<le> ?mn \<bullet> x})) \<subseteq> {x. ?mn \<bullet> x = a$m}"
lp15@67998
   138
      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
lp15@67998
   139
    ultimately show "negligible (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a $ m} \<inter>
lp15@67998
   140
                                 (+) ?d ` (cbox a ?c \<inter> {x. b $ m \<le> ?mn \<bullet> x}))"
lp15@67998
   141
      by (rule negligible_subset)
lp15@67998
   142
  qed
lp15@67998
   143
  have ac_ne: "cbox a ?c \<noteq> {}"
lp15@67998
   144
    using ab_ne an
lp15@67998
   145
    by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
lp15@67998
   146
  have ax_ne: "cbox a (\<chi> i. if i = m then a $ m + b $ n else b $ i) \<noteq> {}"
lp15@67998
   147
    using ab_ne an
lp15@67998
   148
    by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
lp15@67998
   149
  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)"
lp15@67998
   150
    by (simp add: content_cbox_if_cart ab_ne ac_ne ax_ne algebra_simps prod.delta_remove
lp15@67998
   151
             if_distrib [of "\<lambda>u. u - z" for z] prod.remove)
lp15@67998
   152
  show ?Q
lp15@67998
   153
    using eq1 eq2 eq3
lp15@67998
   154
    by (simp add: algebra_simps)
lp15@67998
   155
qed
lp15@67998
   156
lp15@67998
   157
ak2110@69720
   158
proposition
lp15@67998
   159
  fixes S :: "(real^'n) set"
lp15@67998
   160
  assumes "S \<in> lmeasurable"
lp15@67998
   161
  shows measurable_stretch: "((\<lambda>x. \<chi> k. m k * x$k) ` S) \<in> lmeasurable" (is  "?f ` S \<in> _")
lp15@67998
   162
    and measure_stretch: "measure lebesgue ((\<lambda>x. \<chi> k. m k * x$k) ` S) = \<bar>prod m UNIV\<bar> * measure lebesgue S"
lp15@67998
   163
    (is "?MEQ")
ak2110@69720
   164
proof -
lp15@67998
   165
  have "(?f ` S) \<in> lmeasurable \<and> ?MEQ"
lp15@67998
   166
  proof (cases "\<forall>k. m k \<noteq> 0")
lp15@67998
   167
    case True
lp15@67998
   168
    have m0: "0 < \<bar>prod m UNIV\<bar>"
lp15@67998
   169
      using True by simp
lp15@67998
   170
    have "(indicat_real (?f ` S) has_integral \<bar>prod m UNIV\<bar> * measure lebesgue S) UNIV"
lp15@67998
   171
    proof (clarsimp simp add: has_integral_alt [where i=UNIV])
lp15@67998
   172
      fix e :: "real"
lp15@67998
   173
      assume "e > 0"
lp15@67998
   174
      have "(indicat_real S has_integral (measure lebesgue S)) UNIV"
lp15@67998
   175
        using assms lmeasurable_iff_has_integral by blast
lp15@67998
   176
      then obtain B where "B>0"
lp15@67998
   177
        and B: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow>
lp15@67998
   178
                        \<exists>z. (indicat_real S has_integral z) (cbox a b) \<and>
lp15@67998
   179
                            \<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
lp15@67998
   180
        by (simp add: has_integral_alt [where i=UNIV]) (metis (full_types) divide_pos_pos m0  m0 \<open>e > 0\<close>)
lp15@67998
   181
      show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
lp15@67998
   182
                  (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox a b) \<and>
lp15@67998
   183
                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)"
lp15@67998
   184
      proof (intro exI conjI allI)
lp15@67998
   185
        let ?C = "Max (range (\<lambda>k. \<bar>m k\<bar>)) * B"
lp15@67998
   186
        show "?C > 0"
lp15@67998
   187
          using True \<open>B > 0\<close> by (simp add: Max_gr_iff)
lp15@67998
   188
        show "ball 0 ?C \<subseteq> cbox u v \<longrightarrow>
lp15@67998
   189
                  (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
lp15@67998
   190
                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)" for u v
lp15@67998
   191
        proof
lp15@67998
   192
          assume uv: "ball 0 ?C \<subseteq> cbox u v"
lp15@67998
   193
          with \<open>?C > 0\<close> have cbox_ne: "cbox u v \<noteq> {}"
lp15@67998
   194
            using centre_in_ball by blast
lp15@67998
   195
          let ?\<alpha> = "\<lambda>k. u$k / m k"
lp15@67998
   196
          let ?\<beta> = "\<lambda>k. v$k / m k"
lp15@67998
   197
          have invm0: "\<And>k. inverse (m k) \<noteq> 0"
lp15@67998
   198
            using True by auto
lp15@67998
   199
          have "ball 0 B \<subseteq> (\<lambda>x. \<chi> k. x $ k / m k) ` ball 0 ?C"
lp15@67998
   200
          proof clarsimp
lp15@67999
   201
            fix x :: "real^'n"
lp15@67998
   202
            assume x: "norm x < B"
lp15@67998
   203
            have [simp]: "\<bar>Max (range (\<lambda>k. \<bar>m k\<bar>))\<bar> = Max (range (\<lambda>k. \<bar>m k\<bar>))"
lp15@67998
   204
              by (meson Max_ge abs_ge_zero abs_of_nonneg finite finite_imageI order_trans rangeI)
lp15@67998
   205
            have "norm (\<chi> k. m k * x $ k) \<le> norm (Max (range (\<lambda>k. \<bar>m k\<bar>)) *\<^sub>R x)"
lp15@67998
   206
              by (rule norm_le_componentwise_cart) (auto simp: abs_mult intro: mult_right_mono)
lp15@67998
   207
            also have "\<dots> < ?C"
lp15@67998
   208
              using x by simp (metis \<open>B > 0\<close> \<open>?C > 0\<close> mult.commute real_mult_less_iff1 zero_less_mult_pos)
lp15@67998
   209
            finally have "norm (\<chi> k. m k * x $ k) < ?C" .
lp15@67998
   210
            then show "x \<in> (\<lambda>x. \<chi> k. x $ k / m k) ` ball 0 ?C"
lp15@67998
   211
              using stretch_Galois [of "inverse \<circ> m"] True by (auto simp: image_iff field_simps)
lp15@67998
   212
          qed
lp15@67998
   213
          then have Bsub: "ball 0 B \<subseteq> cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k))"
lp15@67998
   214
            using cbox_ne uv image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
lp15@67998
   215
            by (force simp: field_simps)
lp15@67998
   216
          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)))"
lp15@67998
   217
                   and zless: "\<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
lp15@67998
   218
            using B [OF Bsub] by blast
lp15@67998
   219
          have ind: "indicat_real (?f ` S) = (\<lambda>x. indicator S (\<chi> k. x$k / m k))"
lp15@67998
   220
            using True stretch_Galois [of m] by (force simp: indicator_def)
lp15@67998
   221
          show "\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
lp15@67998
   222
                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e"
lp15@67998
   223
          proof (simp add: ind, intro conjI exI)
lp15@67998
   224
            have "((\<lambda>x. indicat_real S (\<chi> k. x $ k/ m k)) has_integral z *\<^sub>R \<bar>prod m UNIV\<bar>)
lp15@67998
   225
                ((\<lambda>x. \<chi> k. x $ k * m k) ` cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k)))"
lp15@67998
   226
              using True has_integral_stretch_cart [OF zint, of "inverse \<circ> m"]
lp15@67998
   227
              by (simp add: field_simps prod_dividef)
lp15@67998
   228
            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"
lp15@67998
   229
              using True image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
lp15@67998
   230
                image_stretch_interval_cart [of "\<lambda>k. 1" u v, symmetric] \<open>cbox u v \<noteq> {}\<close>
lp15@67998
   231
              by (simp add: field_simps image_comp o_def)
lp15@67998
   232
            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)"
lp15@67998
   233
              by simp
lp15@67998
   234
            have "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar>
lp15@67998
   235
                 = \<bar>prod m UNIV\<bar> * \<bar>z - measure lebesgue S\<bar>"
lp15@67998
   236
              by (metis (no_types, hide_lams) abs_abs abs_scaleR mult.commute real_scaleR_def right_diff_distrib')
lp15@67998
   237
            also have "\<dots> < e"
lp15@67998
   238
              using zless True by (simp add: field_simps)
lp15@67998
   239
            finally show "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e" .
lp15@67998
   240
          qed
lp15@67998
   241
        qed
lp15@67998
   242
      qed
lp15@67998
   243
    qed
lp15@67998
   244
    then show ?thesis
lp15@67998
   245
      by (auto simp: has_integral_integrable integral_unique lmeasure_integral_UNIV measurable_integrable)
lp15@67998
   246
  next
lp15@67998
   247
    case False
lp15@67998
   248
    then obtain k where "m k = 0" and prm: "prod m UNIV = 0"
lp15@67998
   249
      by auto
lp15@67998
   250
    have nfS: "negligible (?f ` S)"
lp15@67998
   251
      by (rule negligible_subset [OF negligible_standard_hyperplane_cart]) (use \<open>m k = 0\<close> in auto)
lp15@67998
   252
    then have "(?f ` S) \<in> lmeasurable"
lp15@67998
   253
      by (simp add: negligible_iff_measure)
lp15@67998
   254
    with nfS show ?thesis
lp15@67998
   255
      by (simp add: prm negligible_iff_measure0)
lp15@67998
   256
  qed
lp15@67998
   257
  then show "(?f ` S) \<in> lmeasurable" ?MEQ
lp15@67998
   258
    by metis+
lp15@67998
   259
qed
lp15@67998
   260
lp15@67998
   261
ak2110@69720
   262
proposition
lp15@67998
   263
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
   264
  assumes "linear f" "S \<in> lmeasurable"
lp15@67998
   265
  shows measurable_linear_image: "(f ` S) \<in> lmeasurable"
lp15@67998
   266
    and measure_linear_image: "measure lebesgue (f ` S) = \<bar>det (matrix f)\<bar> * measure lebesgue S" (is "?Q f S")
ak2110@69720
   267
proof -
lp15@67998
   268
  have "\<forall>S \<in> lmeasurable. (f ` S) \<in> lmeasurable \<and> ?Q f S"
lp15@67998
   269
  proof (rule induct_linear_elementary [OF \<open>linear f\<close>]; intro ballI)
lp15@67998
   270
    fix f g and S :: "(real,'n) vec set"
lp15@67998
   271
    assume "linear f" and "linear g"
lp15@67998
   272
      and f [rule_format]: "\<forall>S \<in> lmeasurable. f ` S \<in> lmeasurable \<and> ?Q f S"
lp15@67998
   273
      and g [rule_format]: "\<forall>S \<in> lmeasurable. g ` S \<in> lmeasurable \<and> ?Q g S"
lp15@67998
   274
      and S: "S \<in> lmeasurable"
lp15@67998
   275
    then have gS: "g ` S \<in> lmeasurable"
lp15@67998
   276
      by blast
lp15@67998
   277
    show "(f \<circ> g) ` S \<in> lmeasurable \<and> ?Q (f \<circ> g) S"
lp15@67998
   278
      using f [OF gS] g [OF S] matrix_compose [OF \<open>linear g\<close> \<open>linear f\<close>]
lp15@67998
   279
      by (simp add: o_def image_comp abs_mult det_mul)
lp15@67998
   280
  next
lp15@67999
   281
    fix f :: "real^'n::_ \<Rightarrow> real^'n::_" and i and S :: "(real^'n::_) set"
lp15@67998
   282
    assume "linear f" and 0: "\<And>x. f x $ i = 0" and "S \<in> lmeasurable"
lp15@67998
   283
    then have "\<not> inj f"
lp15@67998
   284
      by (metis (full_types) linear_injective_imp_surjective one_neq_zero surjE vec_component)
lp15@67998
   285
    have detf: "det (matrix f) = 0"
immler@68072
   286
      using \<open>\<not> inj f\<close> det_nz_iff_inj[OF \<open>linear f\<close>] by blast
lp15@67998
   287
    show "f ` S \<in> lmeasurable \<and> ?Q f S"
lp15@67998
   288
    proof
lp15@67998
   289
      show "f ` S \<in> lmeasurable"
lp15@67998
   290
        using lmeasurable_iff_indicator_has_integral \<open>linear f\<close> \<open>\<not> inj f\<close> negligible_UNIV negligible_linear_singular_image by blast
lp15@67998
   291
      have "measure lebesgue (f ` S) = 0"
lp15@67998
   292
        by (meson \<open>\<not> inj f\<close> \<open>linear f\<close> negligible_imp_measure0 negligible_linear_singular_image)
lp15@67998
   293
      also have "\<dots> = \<bar>det (matrix f)\<bar> * measure lebesgue S"
lp15@67998
   294
        by (simp add: detf)
lp15@67998
   295
      finally show "?Q f S" .
lp15@67998
   296
    qed
lp15@67998
   297
  next
lp15@67999
   298
    fix c and S :: "(real^'n::_) set"
lp15@67998
   299
    assume "S \<in> lmeasurable"
lp15@67998
   300
    show "(\<lambda>a. \<chi> i. c i * a $ i) ` S \<in> lmeasurable \<and> ?Q (\<lambda>a. \<chi> i. c i * a $ i) S"
lp15@67998
   301
    proof
lp15@67998
   302
      show "(\<lambda>a. \<chi> i. c i * a $ i) ` S \<in> lmeasurable"
lp15@67998
   303
        by (simp add: \<open>S \<in> lmeasurable\<close> measurable_stretch)
lp15@67998
   304
      show "?Q (\<lambda>a. \<chi> i. c i * a $ i) S"
lp15@67998
   305
        by (simp add: measure_stretch [OF \<open>S \<in> lmeasurable\<close>, of c] axis_def matrix_def det_diagonal)
lp15@67998
   306
    qed
lp15@67998
   307
  next
lp15@67998
   308
    fix m :: "'n" and n :: "'n" and S :: "(real, 'n) vec set"
lp15@67998
   309
    assume "m \<noteq> n" and "S \<in> lmeasurable"
lp15@67998
   310
    let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. v $ Fun.swap m n id i"
lp15@67998
   311
    have lin: "linear ?h"
immler@68072
   312
      by (rule linearI) (simp_all add: plus_vec_def scaleR_vec_def)
lp15@67998
   313
    have meq: "measure lebesgue ((\<lambda>v::(real, 'n) vec. \<chi> i. v $ Fun.swap m n id i) ` cbox a b)
lp15@67998
   314
             = measure lebesgue (cbox a b)" for a b
lp15@67998
   315
    proof (cases "cbox a b = {}")
lp15@67998
   316
      case True then show ?thesis
lp15@67998
   317
        by simp
lp15@67998
   318
    next
lp15@67998
   319
      case False
lp15@67998
   320
      then have him: "?h ` (cbox a b) \<noteq> {}"
lp15@67998
   321
        by blast
lp15@67998
   322
      have eq: "?h ` (cbox a b) = cbox (?h a) (?h b)"
lp15@67998
   323
        by (auto simp: image_iff lambda_swap_Galois mem_box_cart) (metis swap_id_eq)+
lp15@67998
   324
      show ?thesis
lp15@67998
   325
        using him prod.permute [OF permutes_swap_id, where S=UNIV and g="\<lambda>i. (b - a)$i", symmetric]
lp15@67998
   326
        by (simp add: eq content_cbox_cart False)
lp15@67998
   327
    qed
lp15@67998
   328
    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))"
lp15@67998
   329
      by (auto intro!: Cart_lambda_cong)
lp15@67998
   330
    then have "matrix ?h = transpose(\<chi> i j. mat 1 $ i $ Fun.swap m n id j)"
lp15@67998
   331
      by (auto simp: matrix_eq transpose_def axis_def mat_def matrix_def)
lp15@67998
   332
    then have 1: "\<bar>det (matrix ?h)\<bar> = 1"
lp15@67998
   333
      by (simp add: det_permute_columns permutes_swap_id sign_swap_id abs_mult)
lp15@67998
   334
    show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
lp15@67998
   335
    proof
lp15@67998
   336
      show "?h ` S \<in> lmeasurable" "?Q ?h S"
lp15@67998
   337
        using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force+
lp15@67998
   338
    qed
lp15@67998
   339
  next
lp15@67999
   340
    fix m n :: "'n" and S :: "(real, 'n) vec set"
lp15@67998
   341
    assume "m \<noteq> n" and "S \<in> lmeasurable"
lp15@67998
   342
    let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. if i = m then v $ m + v $ n else v $ i"
lp15@67998
   343
    have lin: "linear ?h"
immler@68072
   344
      by (rule linearI) (auto simp: algebra_simps plus_vec_def scaleR_vec_def vec_eq_iff)
lp15@67998
   345
    consider "m < n" | " n < m"
lp15@67998
   346
      using \<open>m \<noteq> n\<close> less_linear by blast
lp15@67998
   347
    then have 1: "det(matrix ?h) = 1"
lp15@67998
   348
    proof cases
lp15@67998
   349
      assume "m < n"
lp15@67998
   350
      have *: "matrix ?h $ i $ j = (0::real)" if "j < i" for i j :: 'n
lp15@67998
   351
      proof -
lp15@67998
   352
        have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
lp15@67998
   353
          using axis_def by blast
lp15@67998
   354
        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)"
lp15@67998
   355
          using \<open>j < i\<close> axis_def \<open>m < n\<close> by auto
lp15@67998
   356
        with \<open>m < n\<close> show ?thesis
lp15@67998
   357
          by (auto simp: matrix_def axis_def cong: if_cong)
lp15@67998
   358
      qed
lp15@67998
   359
      show ?thesis
lp15@67998
   360
        using \<open>m \<noteq> n\<close> by (subst det_upperdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
lp15@67998
   361
    next
lp15@67998
   362
      assume "n < m"
lp15@67998
   363
      have *: "matrix ?h $ i $ j = (0::real)" if "j > i" for i j :: 'n
lp15@67998
   364
      proof -
lp15@67998
   365
        have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
lp15@67998
   366
          using axis_def by blast
lp15@67998
   367
        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)"
lp15@67998
   368
          using \<open>j > i\<close> axis_def \<open>m > n\<close> by auto
lp15@67998
   369
        with \<open>m > n\<close> show ?thesis
lp15@67998
   370
          by (auto simp: matrix_def axis_def cong: if_cong)
lp15@67998
   371
      qed
lp15@67998
   372
      show ?thesis
lp15@67998
   373
        using \<open>m \<noteq> n\<close>
lp15@67998
   374
        by (subst det_lowerdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
lp15@67998
   375
    qed
lp15@67998
   376
    have meq: "measure lebesgue (?h ` (cbox a b)) = measure lebesgue (cbox a b)" for a b
lp15@67998
   377
    proof (cases "cbox a b = {}")
lp15@67998
   378
      case True then show ?thesis by simp
lp15@67998
   379
    next
lp15@67998
   380
      case False
lp15@67998
   381
      then have ne: "(+) (\<chi> i. if i = n then - a $ n else 0) ` cbox a b \<noteq> {}"
lp15@67998
   382
        by auto
lp15@67998
   383
      let ?v = "\<chi> i. if i = n then - a $ n else 0"
lp15@67998
   384
      have "?h ` cbox a b
lp15@67998
   385
            = (+) (\<chi> i. if i = m \<or> i = n then a $ n else 0) ` ?h ` (+) ?v ` (cbox a b)"
lp15@67998
   386
        using \<open>m \<noteq> n\<close> unfolding image_comp o_def by (force simp: vec_eq_iff)
lp15@67998
   387
      then have "measure lebesgue (?h ` (cbox a b))
lp15@67998
   388
               = measure lebesgue ((\<lambda>v. \<chi> i. if i = m then v $ m + v $ n else v $ i) `
lp15@67998
   389
                                   (+) ?v ` cbox a b)"
lp15@67998
   390
        by (rule ssubst) (rule measure_translation)
lp15@67998
   391
      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))"
lp15@67998
   392
        by (metis (no_types, lifting) cbox_translation)
lp15@67998
   393
      also have "\<dots> = measure lebesgue ((+) (\<chi> i. if i = n then - a $ n else 0) ` cbox a b)"
lp15@67998
   394
        apply (subst measure_shear_interval)
lp15@67998
   395
        using \<open>m \<noteq> n\<close> ne apply auto
lp15@67998
   396
        apply (simp add: cbox_translation)
lp15@67998
   397
        by (metis cbox_borel cbox_translation measure_completion sets_lborel)
lp15@67998
   398
      also have "\<dots> = measure lebesgue (cbox a b)"
lp15@67998
   399
        by (rule measure_translation)
lp15@67998
   400
        finally show ?thesis .
lp15@67998
   401
      qed
lp15@67998
   402
    show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
lp15@67998
   403
      using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force
lp15@67998
   404
  qed
lp15@67998
   405
  with assms show "(f ` S) \<in> lmeasurable" "?Q f S"
lp15@67998
   406
    by metis+
lp15@67998
   407
qed
lp15@67998
   408
lp15@67998
   409
ak2110@69720
   410
lemma
lp15@67998
   411
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
   412
  assumes f: "orthogonal_transformation f" and S: "S \<in> lmeasurable"
lp15@67998
   413
  shows measurable_orthogonal_image: "f ` S \<in> lmeasurable"
lp15@67998
   414
    and measure_orthogonal_image: "measure lebesgue (f ` S) = measure lebesgue S"
lp15@67998
   415
proof -
lp15@67998
   416
  have "linear f"
lp15@67998
   417
    by (simp add: f orthogonal_transformation_linear)
lp15@67998
   418
  then show "f ` S \<in> lmeasurable"
lp15@67998
   419
    by (metis S measurable_linear_image)
lp15@67998
   420
  show "measure lebesgue (f ` S) = measure lebesgue S"
lp15@67998
   421
    by (simp add: measure_linear_image \<open>linear f\<close> S f)
lp15@67998
   422
qed
lp15@67998
   423
immler@69683
   424
subsection\<open>\<open>F_sigma\<close> and \<open>G_delta\<close> sets.\<close>(*FIX ME mv *)
lp15@67998
   425
lp15@67998
   426
(*https://en.wikipedia.org/wiki/F\<sigma>_set*)
ak2110@68833
   427
inductive%important fsigma :: "'a::topological_space set \<Rightarrow> bool" where
haftmann@69313
   428
  "(\<And>n::nat. closed (F n)) \<Longrightarrow> fsigma (\<Union>(F ` UNIV))"
lp15@67998
   429
ak2110@68833
   430
inductive%important gdelta :: "'a::topological_space set \<Rightarrow> bool" where
haftmann@69313
   431
  "(\<And>n::nat. open (F n)) \<Longrightarrow> gdelta (\<Inter>(F ` UNIV))"
lp15@67998
   432
ak2110@69720
   433
lemma fsigma_Union_compact:
lp15@67998
   434
  fixes S :: "'a::{real_normed_vector,heine_borel} set"
haftmann@69313
   435
  shows "fsigma S \<longleftrightarrow> (\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = \<Union>(F ` UNIV))"
ak2110@69720
   436
proof safe
lp15@67998
   437
  assume "fsigma S"
haftmann@69313
   438
  then obtain F :: "nat \<Rightarrow> 'a set" where F: "range F \<subseteq> Collect closed" "S = \<Union>(F ` UNIV)"
lp15@67998
   439
    by (meson fsigma.cases image_subsetI mem_Collect_eq)
haftmann@69313
   440
  then have "\<exists>D::nat \<Rightarrow> 'a set. range D \<subseteq> Collect compact \<and> \<Union>(D ` UNIV) = F i" for i
lp15@67998
   441
    using closed_Union_compact_subsets [of "F i"]
lp15@67998
   442
    by (metis image_subsetI mem_Collect_eq range_subsetD)
lp15@67998
   443
  then obtain D :: "nat \<Rightarrow> nat \<Rightarrow> 'a set"
haftmann@69313
   444
    where D: "\<And>i. range (D i) \<subseteq> Collect compact \<and> \<Union>((D i) ` UNIV) = F i"
lp15@67998
   445
    by metis
lp15@67998
   446
  let ?DD = "\<lambda>n. (\<lambda>(i,j). D i j) (prod_decode n)"
haftmann@69313
   447
  show "\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = \<Union>(F ` UNIV)"
lp15@67998
   448
  proof (intro exI conjI)
lp15@67998
   449
    show "range ?DD \<subseteq> Collect compact"
lp15@67998
   450
      using D by clarsimp (metis mem_Collect_eq rangeI split_conv subsetCE surj_pair)
haftmann@69325
   451
    show "S = \<Union> (range ?DD)"
lp15@67998
   452
    proof
haftmann@69325
   453
      show "S \<subseteq> \<Union> (range ?DD)"
lp15@67998
   454
        using D F
lp15@67998
   455
        by clarsimp (metis UN_iff old.prod.case prod_decode_inverse prod_encode_eq)
haftmann@69325
   456
      show "\<Union> (range ?DD) \<subseteq> S"
lp15@67998
   457
        using D F  by fastforce
lp15@67998
   458
    qed
lp15@67998
   459
  qed
lp15@67998
   460
next
lp15@67998
   461
  fix F :: "nat \<Rightarrow> 'a set"
haftmann@69313
   462
  assume "range F \<subseteq> Collect compact" and "S = \<Union>(F ` UNIV)"
haftmann@69313
   463
  then show "fsigma (\<Union>(F ` UNIV))"
lp15@67998
   464
    by (simp add: compact_imp_closed fsigma.intros image_subset_iff)
lp15@67998
   465
qed
lp15@67998
   466
ak2110@69720
   467
lemma gdelta_imp_fsigma: "gdelta S \<Longrightarrow> fsigma (- S)"
lp15@67998
   468
proof (induction rule: gdelta.induct)
lp15@67998
   469
  case (1 F)
haftmann@69313
   470
  have "- \<Inter>(F ` UNIV) = (\<Union>i. -(F i))"
lp15@67998
   471
    by auto
lp15@67998
   472
  then show ?case
lp15@67998
   473
    by (simp add: fsigma.intros closed_Compl 1)
lp15@67998
   474
qed
lp15@67998
   475
ak2110@69720
   476
lemma fsigma_imp_gdelta: "fsigma S \<Longrightarrow> gdelta (- S)"
lp15@67998
   477
proof (induction rule: fsigma.induct)
lp15@67998
   478
  case (1 F)
haftmann@69313
   479
  have "- \<Union>(F ` UNIV) = (\<Inter>i. -(F i))"
lp15@67998
   480
    by auto
lp15@67998
   481
  then show ?case
lp15@67998
   482
    by (simp add: 1 gdelta.intros open_closed)
lp15@67998
   483
qed
lp15@67998
   484
ak2110@69720
   485
lemma gdelta_complement: "gdelta(- S) \<longleftrightarrow> fsigma S"
lp15@67998
   486
  using fsigma_imp_gdelta gdelta_imp_fsigma by force
lp15@67998
   487
wenzelm@69272
   488
text\<open>A Lebesgue set is almost an \<open>F_sigma\<close> or \<open>G_delta\<close>.\<close>
ak2110@69720
   489
lemma lebesgue_set_almost_fsigma:
lp15@67998
   490
  assumes "S \<in> sets lebesgue"
lp15@67998
   491
  obtains C T where "fsigma C" "negligible T" "C \<union> T = S" "disjnt C T"
lp15@67998
   492
proof -
lp15@67998
   493
  { fix n::nat
lp15@67998
   494
    have "\<exists>T. closed T \<and> T \<subseteq> S \<and> S - T \<in> lmeasurable \<and> measure lebesgue (S-T) < 1 / Suc n"
lp15@67998
   495
      using sets_lebesgue_inner_closed [OF assms]
lp15@67998
   496
      by (metis divide_pos_pos less_numeral_extra(1) of_nat_0_less_iff zero_less_Suc)
lp15@67998
   497
  }
lp15@67998
   498
  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"
lp15@67998
   499
    by metis
haftmann@69313
   500
  let ?C = "\<Union>(F ` UNIV)"
lp15@67998
   501
  show thesis
lp15@67998
   502
  proof
lp15@67998
   503
    show "fsigma ?C"
lp15@67998
   504
      using F by (simp add: fsigma.intros)
lp15@67998
   505
    show "negligible (S - ?C)"
lp15@67998
   506
    proof (clarsimp simp add: negligible_outer_le)
lp15@67998
   507
      fix e :: "real"
lp15@67998
   508
      assume "0 < e"
lp15@67998
   509
      then obtain n where n: "1 / Suc n < e"
lp15@67998
   510
        using nat_approx_posE by metis
lp15@67998
   511
      show "\<exists>T. S - (\<Union>x. F x) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
lp15@67998
   512
      proof (intro exI conjI)
lp15@67998
   513
        show "measure lebesgue (S - F n) \<le> e"
lp15@67998
   514
          by (meson F n less_trans not_le order.asym)
lp15@67998
   515
      qed (use F in auto)
lp15@67998
   516
    qed
lp15@67998
   517
    show "?C \<union> (S - ?C) = S"
lp15@67998
   518
      using F by blast
lp15@67998
   519
    show "disjnt ?C (S - ?C)"
lp15@67998
   520
      by (auto simp: disjnt_def)
lp15@67998
   521
  qed
lp15@67998
   522
qed
lp15@67998
   523
ak2110@69720
   524
lemma lebesgue_set_almost_gdelta:
lp15@67998
   525
  assumes "S \<in> sets lebesgue"
lp15@67998
   526
  obtains C T where "gdelta C" "negligible T" "S \<union> T = C" "disjnt S T"
lp15@67998
   527
proof -
lp15@67998
   528
  have "-S \<in> sets lebesgue"
lp15@67998
   529
    using assms Compl_in_sets_lebesgue by blast
lp15@67998
   530
  then obtain C T where C: "fsigma C" "negligible T" "C \<union> T = -S" "disjnt C T"
lp15@67998
   531
    using lebesgue_set_almost_fsigma by metis
lp15@67998
   532
  show thesis
lp15@67998
   533
  proof
lp15@67998
   534
    show "gdelta (-C)"
lp15@67998
   535
      by (simp add: \<open>fsigma C\<close> fsigma_imp_gdelta)
lp15@67998
   536
    show "S \<union> T = -C" "disjnt S T"
lp15@67998
   537
      using C by (auto simp: disjnt_def)
lp15@67998
   538
  qed (use C in auto)
lp15@67998
   539
qed
lp15@67998
   540
lp15@67998
   541
ak2110@69720
   542
proposition measure_semicontinuous_with_hausdist_explicit:
lp15@67998
   543
  assumes "bounded S" and neg: "negligible(frontier S)" and "e > 0"
lp15@67998
   544
  obtains d where "d > 0"
lp15@67998
   545
                  "\<And>T. \<lbrakk>T \<in> lmeasurable; \<And>y. y \<in> T \<Longrightarrow> \<exists>x. x \<in> S \<and> dist x y < d\<rbrakk>
lp15@67998
   546
                        \<Longrightarrow> measure lebesgue T < measure lebesgue S + e"
ak2110@69720
   547
proof (cases "S = {}")
lp15@67998
   548
  case True
lp15@67998
   549
  with that \<open>e > 0\<close> show ?thesis by force
lp15@67998
   550
next
lp15@67998
   551
  case False
lp15@67998
   552
  then have frS: "frontier S \<noteq> {}"
lp15@67998
   553
    using \<open>bounded S\<close> frontier_eq_empty not_bounded_UNIV by blast
lp15@67998
   554
  have "S \<in> lmeasurable"
lp15@67998
   555
    by (simp add: \<open>bounded S\<close> measurable_Jordan neg)
lp15@67998
   556
  have null: "(frontier S) \<in> null_sets lebesgue"
lp15@67998
   557
    by (metis neg negligible_iff_null_sets)
lp15@67998
   558
  have "frontier S \<in> lmeasurable" and mS0: "measure lebesgue (frontier S) = 0"
lp15@67998
   559
    using neg negligible_imp_measurable negligible_iff_measure by blast+
lp15@67998
   560
  with \<open>e > 0\<close> lmeasurable_outer_open
lp15@67998
   561
  obtain U where "open U"
lp15@67998
   562
    and U: "frontier S \<subseteq> U" "U - frontier S \<in> lmeasurable" "measure lebesgue (U - frontier S) < e"
lp15@67998
   563
    by (metis fmeasurableD)
lp15@67998
   564
  with null have "U \<in> lmeasurable"
lp15@67998
   565
    by (metis borel_open measurable_Diff_null_set sets_completionI_sets sets_lborel)
lp15@67998
   566
  have "measure lebesgue (U - frontier S) = measure lebesgue U"
lp15@67998
   567
    using mS0 by (simp add: \<open>U \<in> lmeasurable\<close> fmeasurableD measure_Diff_null_set null)
lp15@67998
   568
  with U have mU: "measure lebesgue U < e"
lp15@67998
   569
    by simp
lp15@67998
   570
  show ?thesis
lp15@67998
   571
  proof
lp15@67998
   572
    have "U \<noteq> UNIV"
lp15@67998
   573
      using \<open>U \<in> lmeasurable\<close> by auto
lp15@67998
   574
    then have "- U \<noteq> {}"
lp15@67998
   575
      by blast
lp15@67998
   576
    with \<open>open U\<close> \<open>frontier S \<subseteq> U\<close> show "setdist (frontier S) (- U) > 0"
lp15@67998
   577
      by (auto simp: \<open>bounded S\<close> open_closed compact_frontier_bounded setdist_gt_0_compact_closed frS)
lp15@67998
   578
    fix T
lp15@67998
   579
    assume "T \<in> lmeasurable"
lp15@67998
   580
      and T: "\<And>t. t \<in> T \<Longrightarrow> \<exists>y. y \<in> S \<and> dist y t < setdist (frontier S) (- U)"
lp15@67998
   581
    then have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue (T - S)"
lp15@67998
   582
      by (simp add: \<open>S \<in> lmeasurable\<close> measure_diff_le_measure_setdiff)
lp15@67998
   583
    also have "\<dots>  \<le> measure lebesgue U"
lp15@67998
   584
    proof -
lp15@67998
   585
      have "T - S \<subseteq> U"
lp15@67998
   586
      proof clarify
lp15@67998
   587
        fix x
lp15@67998
   588
        assume "x \<in> T" and "x \<notin> S"
lp15@67998
   589
        then obtain y where "y \<in> S" and y: "dist y x < setdist (frontier S) (- U)"
lp15@67998
   590
          using T by blast
lp15@67998
   591
        have "closed_segment x y \<inter> frontier S \<noteq> {}"
lp15@67998
   592
          using connected_Int_frontier \<open>x \<notin> S\<close> \<open>y \<in> S\<close> by blast
lp15@67998
   593
        then obtain z where z: "z \<in> closed_segment x y" "z \<in> frontier S"
lp15@67998
   594
          by auto
lp15@67998
   595
        with y have "dist z x < setdist(frontier S) (- U)"
lp15@67998
   596
          by (auto simp: dist_commute dest!: dist_in_closed_segment)
lp15@67998
   597
        with z have False if "x \<in> -U"
lp15@67998
   598
          using setdist_le_dist [OF \<open>z \<in> frontier S\<close> that] by auto
lp15@67998
   599
        then show "x \<in> U"
lp15@67998
   600
          by blast
lp15@67998
   601
      qed
lp15@67998
   602
      then show ?thesis
lp15@67998
   603
        by (simp add: \<open>S \<in> lmeasurable\<close> \<open>T \<in> lmeasurable\<close> \<open>U \<in> lmeasurable\<close> fmeasurableD measure_mono_fmeasurable sets.Diff)
lp15@67998
   604
    qed
lp15@67998
   605
    finally have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue U" .
lp15@67998
   606
    with mU show "measure lebesgue T < measure lebesgue S + e"
lp15@67998
   607
      by linarith
lp15@67998
   608
  qed
lp15@67998
   609
qed
lp15@67998
   610
ak2110@69720
   611
proposition lebesgue_regular_inner:
lp15@67998
   612
 assumes "S \<in> sets lebesgue"
lp15@67998
   613
 obtains K C where "negligible K" "\<And>n::nat. compact(C n)" "S = (\<Union>n. C n) \<union> K"
ak2110@69720
   614
proof -
lp15@67998
   615
  have "\<exists>T. closed T \<and> T \<subseteq> S \<and> (S - T) \<in> lmeasurable \<and> measure lebesgue (S - T) < (1/2)^n" for n
lp15@67998
   616
    using sets_lebesgue_inner_closed assms
lp15@67998
   617
    by (metis sets_lebesgue_inner_closed zero_less_divide_1_iff zero_less_numeral zero_less_power)
lp15@67998
   618
  then obtain C where clo: "\<And>n. closed (C n)" and subS: "\<And>n. C n \<subseteq> S"
lp15@67998
   619
    and mea: "\<And>n. (S - C n) \<in> lmeasurable"
lp15@67998
   620
    and less: "\<And>n. measure lebesgue (S - C n) < (1/2)^n"
lp15@67998
   621
    by metis
lp15@67998
   622
  have "\<exists>F. (\<forall>n::nat. compact(F n)) \<and> (\<Union>n. F n) = C m" for m::nat
lp15@67998
   623
    by (metis clo closed_Union_compact_subsets)
lp15@67998
   624
  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"
lp15@67998
   625
    by metis
lp15@67998
   626
  let ?C = "from_nat_into (\<Union>m. range (D m))"
lp15@67998
   627
  have "countable (\<Union>m. range (D m))"
lp15@67998
   628
    by blast
lp15@67998
   629
  have "range (from_nat_into (\<Union>m. range (D m))) = (\<Union>m. range (D m))"
lp15@67998
   630
    using range_from_nat_into by simp
lp15@67998
   631
  then have CD: "\<exists>m n. ?C k = D m n"  for k
lp15@67998
   632
    by (metis (mono_tags, lifting) UN_iff rangeE range_eqI)
lp15@67998
   633
  show thesis
lp15@67998
   634
  proof
lp15@67998
   635
    show "negligible (S - (\<Union>n. C n))"
lp15@67998
   636
    proof (clarsimp simp: negligible_outer_le)
lp15@67998
   637
      fix e :: "real"
lp15@67998
   638
      assume "e > 0"
lp15@67998
   639
      then obtain n where n: "(1/2)^n < e"
lp15@67998
   640
        using real_arch_pow_inv [of e "1/2"] by auto
lp15@67998
   641
      show "\<exists>T. S - (\<Union>n. C n) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
lp15@67998
   642
      proof (intro exI conjI)
lp15@67998
   643
        show "S - (\<Union>n. C n) \<subseteq> S - C n"
lp15@67998
   644
          by blast
lp15@67998
   645
        show "S - C n \<in> lmeasurable"
lp15@67998
   646
          by (simp add: mea)
lp15@67998
   647
        show "measure lebesgue (S - C n) \<le> e"
lp15@67998
   648
          using less [of n] n by simp
lp15@67998
   649
      qed
lp15@67998
   650
    qed
lp15@67998
   651
    show "compact (?C n)" for n
lp15@67998
   652
      using CD D by metis
lp15@67998
   653
    show "S = (\<Union>n. ?C n) \<union> (S - (\<Union>n. C n))" (is "_ = ?rhs")
lp15@67998
   654
    proof
lp15@67998
   655
      show "S \<subseteq> ?rhs"
lp15@67998
   656
        using D by fastforce
lp15@67998
   657
      show "?rhs \<subseteq> S"
lp15@67998
   658
        using subS D CD by auto (metis Sup_upper range_eqI subsetCE)
lp15@67998
   659
    qed
lp15@67998
   660
  qed
lp15@67998
   661
qed
lp15@67998
   662
lp15@67998
   663
ak2110@69720
   664
lemma sets_lebesgue_continuous_image:
lp15@67998
   665
  assumes T: "T \<in> sets lebesgue" and contf: "continuous_on S f"
lp15@67998
   666
    and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)" and "T \<subseteq> S"
lp15@67998
   667
 shows "f ` T \<in> sets lebesgue"
lp15@67998
   668
proof -
lp15@67998
   669
  obtain K C where "negligible K" and com: "\<And>n::nat. compact(C n)" and Teq: "T = (\<Union>n. C n) \<union> K"
lp15@67998
   670
    using lebesgue_regular_inner [OF T] by metis
lp15@67998
   671
  then have comf: "\<And>n::nat. compact(f ` C n)"
lp15@67998
   672
    by (metis Un_subset_iff Union_upper \<open>T \<subseteq> S\<close> compact_continuous_image contf continuous_on_subset rangeI)
lp15@67998
   673
  have "((\<Union>n. f ` C n) \<union> f ` K) \<in> sets lebesgue"
lp15@67998
   674
  proof (rule sets.Un)
lp15@67998
   675
    have "K \<subseteq> S"
lp15@67998
   676
      using Teq \<open>T \<subseteq> S\<close> by blast
lp15@67998
   677
    show "(\<Union>n. f ` C n) \<in> sets lebesgue"
lp15@67998
   678
    proof (rule sets.countable_Union)
lp15@67998
   679
      show "range (\<lambda>n. f ` C n) \<subseteq> sets lebesgue"
lp15@67998
   680
        using borel_compact comf by (auto simp: borel_compact)
lp15@67998
   681
    qed auto
lp15@67998
   682
    show "f ` K \<in> sets lebesgue"
lp15@67998
   683
      by (simp add: \<open>K \<subseteq> S\<close> \<open>negligible K\<close> negim negligible_imp_sets)
lp15@67998
   684
  qed
lp15@67998
   685
  then show ?thesis
lp15@67998
   686
    by (simp add: Teq image_Un image_Union)
lp15@67998
   687
qed
lp15@67998
   688
ak2110@69720
   689
lemma differentiable_image_in_sets_lebesgue:
lp15@67998
   690
  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
lp15@67998
   691
  assumes S: "S \<in> sets lebesgue" and dim: "DIM('m) \<le> DIM('n)" and f: "f differentiable_on S"
lp15@67998
   692
  shows "f`S \<in> sets lebesgue"
lp15@67998
   693
proof (rule sets_lebesgue_continuous_image [OF S])
lp15@67998
   694
  show "continuous_on S f"
lp15@67998
   695
    by (meson differentiable_imp_continuous_on f)
lp15@67998
   696
  show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
lp15@67998
   697
    using differentiable_on_subset f
lp15@67998
   698
    by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
lp15@67998
   699
qed auto
lp15@67998
   700
ak2110@69720
   701
lemma sets_lebesgue_on_continuous_image:
lp15@67998
   702
  assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and contf: "continuous_on S f"
lp15@67998
   703
    and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)"
lp15@67998
   704
  shows "f ` X \<in> sets (lebesgue_on (f ` S))"
lp15@67998
   705
proof -
lp15@67998
   706
  have "X \<subseteq> S"
lp15@67998
   707
    by (metis S X sets.Int_space_eq2 sets_restrict_space_iff)
lp15@67998
   708
  moreover have "f ` S \<in> sets lebesgue"
lp15@67998
   709
    using S contf negim sets_lebesgue_continuous_image by blast
lp15@67998
   710
  moreover have "f ` X \<in> sets lebesgue"
lp15@67998
   711
    by (metis S X contf negim sets_lebesgue_continuous_image sets_restrict_space_iff space_restrict_space space_restrict_space2)
lp15@67998
   712
  ultimately show ?thesis
lp15@67998
   713
    by (auto simp: sets_restrict_space_iff)
lp15@67998
   714
qed
lp15@67998
   715
ak2110@69720
   716
lemma differentiable_image_in_sets_lebesgue_on:
lp15@67998
   717
  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
lp15@67998
   718
  assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and dim: "DIM('m) \<le> DIM('n)"
lp15@67998
   719
       and f: "f differentiable_on S"
lp15@67998
   720
     shows "f ` X \<in> sets (lebesgue_on (f`S))"
lp15@67998
   721
proof (rule sets_lebesgue_on_continuous_image [OF S X])
lp15@67998
   722
  show "continuous_on S f"
lp15@67998
   723
    by (meson differentiable_imp_continuous_on f)
lp15@67998
   724
  show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
lp15@67998
   725
    using differentiable_on_subset f
lp15@67998
   726
    by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
lp15@67998
   727
qed
lp15@67998
   728
lp15@67998
   729
ak2110@69720
   730
proposition
lp15@67998
   731
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
   732
  assumes S: "S \<in> lmeasurable"
lp15@67998
   733
  and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
   734
  and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
lp15@67998
   735
  and bounded: "\<And>x. x \<in> S \<Longrightarrow> \<bar>det (matrix (f' x))\<bar> \<le> B"
lp15@67998
   736
  shows measurable_bounded_differentiable_image:
lp15@67998
   737
       "f ` S \<in> lmeasurable"
lp15@67998
   738
    and measure_bounded_differentiable_image:
lp15@67998
   739
       "measure lebesgue (f ` S) \<le> B * measure lebesgue S" (is "?M")
ak2110@69720
   740
proof -
lp15@67998
   741
  have "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> B * measure lebesgue S"
lp15@67998
   742
  proof (cases "B < 0")
lp15@67998
   743
    case True
lp15@67998
   744
    then have "S = {}"
lp15@67998
   745
      by (meson abs_ge_zero bounded empty_iff equalityI less_le_trans linorder_not_less subsetI)
lp15@67998
   746
    then show ?thesis
lp15@67998
   747
      by auto
lp15@67998
   748
  next
lp15@67998
   749
    case False
lp15@67998
   750
    then have "B \<ge> 0"
lp15@67998
   751
      by arith
lp15@67998
   752
    let ?\<mu> = "measure lebesgue"
lp15@67998
   753
    have f_diff: "f differentiable_on S"
lp15@67998
   754
      using deriv by (auto simp: differentiable_on_def differentiable_def)
lp15@67998
   755
    have eps: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> (B+e) * ?\<mu> S" (is "?ME")
lp15@67998
   756
              if "e > 0" for e
lp15@67998
   757
    proof -
lp15@67998
   758
      have eps_d: "f ` S \<in> lmeasurable"  "?\<mu> (f ` S) \<le> (B+e) * (?\<mu> S + d)" (is "?MD")
lp15@67998
   759
                  if "d > 0" for d
lp15@67998
   760
      proof -
lp15@67998
   761
        obtain T where "open T" "S \<subseteq> T" and TS: "(T-S) \<in> lmeasurable" and "?\<mu> (T-S) < d"
lp15@67998
   762
          using S \<open>d > 0\<close> lmeasurable_outer_open by blast
lp15@67998
   763
        with S have "T \<in> lmeasurable" and Tless: "?\<mu> T < ?\<mu> S + d"
lp15@67998
   764
          by (auto simp: measurable_measure_Diff dest!: fmeasurable_Diff_D)
lp15@67998
   765
        have "\<exists>r. 0 < r \<and> r < d \<and> ball x r \<subseteq> T \<and> f ` (S \<inter> ball x r) \<in> lmeasurable \<and>
lp15@67998
   766
                  ?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)"
lp15@67998
   767
          if "x \<in> S" "d > 0" for x d
lp15@67998
   768
        proof -
lp15@67998
   769
          have lin: "linear (f' x)"
lp15@67998
   770
            and lim0: "((\<lambda>y. (f y - (f x + f' x (y - x))) /\<^sub>R norm(y - x)) \<longlongrightarrow> 0) (at x within S)"
lp15@67998
   771
            using deriv \<open>x \<in> S\<close> by (auto simp: has_derivative_within bounded_linear.linear field_simps)
lp15@67998
   772
          have bo: "bounded (f' x ` ball 0 1)"
lp15@67998
   773
            by (simp add: bounded_linear_image linear_linear lin)
lp15@67998
   774
          have neg: "negligible (frontier (f' x ` ball 0 1))"
lp15@67998
   775
            using deriv has_derivative_linear \<open>x \<in> S\<close>
lp15@67998
   776
            by (auto intro!: negligible_convex_frontier [OF convex_linear_image])
lp15@67998
   777
          have 0: "0 < e * unit_ball_vol (real CARD('n))"
lp15@67998
   778
            using  \<open>e > 0\<close> by simp
lp15@67998
   779
          obtain k where "k > 0" and k:
lp15@67998
   780
                  "\<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>
lp15@67998
   781
                        \<Longrightarrow> ?\<mu> U < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
lp15@67998
   782
            using measure_semicontinuous_with_hausdist_explicit [OF bo neg 0] by blast
lp15@67998
   783
          obtain l where "l > 0" and l: "ball x l \<subseteq> T"
lp15@67998
   784
            using \<open>x \<in> S\<close> \<open>open T\<close> \<open>S \<subseteq> T\<close> openE by blast
lp15@67998
   785
          obtain \<zeta> where "0 < \<zeta>"
lp15@67998
   786
            and \<zeta>: "\<And>y. \<lbrakk>y \<in> S; y \<noteq> x; dist y x < \<zeta>\<rbrakk>
lp15@67998
   787
                        \<Longrightarrow> norm (f y - (f x + f' x (y - x))) / norm (y - x) < k"
lp15@67998
   788
            using lim0 \<open>k > 0\<close> by (force simp: Lim_within field_simps)
lp15@67998
   789
          define r where "r \<equiv> min (min l (\<zeta>/2)) (min 1 (d/2))"
lp15@67998
   790
          show ?thesis
lp15@67998
   791
          proof (intro exI conjI)
lp15@67998
   792
            show "r > 0" "r < d"
lp15@67998
   793
              using \<open>l > 0\<close> \<open>\<zeta> > 0\<close> \<open>d > 0\<close> by (auto simp: r_def)
lp15@67998
   794
            have "r \<le> l"
lp15@67998
   795
              by (auto simp: r_def)
lp15@67998
   796
            with l show "ball x r \<subseteq> T"
lp15@67998
   797
              by auto
lp15@67998
   798
            have ex_lessK: "\<exists>x' \<in> ball 0 1. dist (f' x x') ((f y - f x) /\<^sub>R r) < k"
lp15@67998
   799
              if "y \<in> S" and "dist x y < r" for y
lp15@67998
   800
            proof (cases "y = x")
lp15@67998
   801
              case True
lp15@67998
   802
              with lin linear_0 \<open>k > 0\<close> that show ?thesis
lp15@67998
   803
                by (rule_tac x=0 in bexI) (auto simp: linear_0)
lp15@67998
   804
            next
lp15@67998
   805
              case False
lp15@67998
   806
              then show ?thesis
lp15@67998
   807
              proof (rule_tac x="(y - x) /\<^sub>R r" in bexI)
lp15@67998
   808
                have "f' x ((y - x) /\<^sub>R r) = f' x (y - x) /\<^sub>R r"
immler@68072
   809
                  by (simp add: lin linear_scale)
lp15@67998
   810
                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)"
lp15@67998
   811
                  by (simp add: dist_norm)
lp15@67998
   812
                also have "\<dots> = norm (f' x (y - x) - (f y - f x)) / r"
immler@68072
   813
                  using \<open>r > 0\<close> by (simp add: scale_right_diff_distrib [symmetric] divide_simps)
lp15@67998
   814
                also have "\<dots> \<le> norm (f y - (f x + f' x (y - x))) / norm (y - x)"
lp15@67998
   815
                  using that \<open>r > 0\<close> False by (simp add: algebra_simps divide_simps dist_norm norm_minus_commute mult_right_mono)
lp15@67998
   816
                also have "\<dots> < k"
lp15@67998
   817
                  using that \<open>0 < \<zeta>\<close> by (simp add: dist_commute r_def  \<zeta> [OF \<open>y \<in> S\<close> False])
lp15@67998
   818
                finally show "dist (f' x ((y - x) /\<^sub>R r)) ((f y - f x) /\<^sub>R r) < k" .
lp15@67998
   819
                show "(y - x) /\<^sub>R r \<in> ball 0 1"
lp15@67998
   820
                  using that \<open>r > 0\<close> by (simp add: dist_norm divide_simps norm_minus_commute)
lp15@67998
   821
              qed
lp15@67998
   822
            qed
lp15@67998
   823
            let ?rfs = "(\<lambda>x. x /\<^sub>R r) ` (+) (- f x) ` f ` (S \<inter> ball x r)"
lp15@67998
   824
            have rfs_mble: "?rfs \<in> lmeasurable"
lp15@67998
   825
            proof (rule bounded_set_imp_lmeasurable)
lp15@67998
   826
              have "f differentiable_on S \<inter> ball x r"
lp15@67998
   827
                using f_diff by (auto simp: fmeasurableD differentiable_on_subset)
lp15@67998
   828
              with S show "?rfs \<in> sets lebesgue"
lp15@67998
   829
                by (auto simp: sets.Int intro!: lebesgue_sets_translation differentiable_image_in_sets_lebesgue)
lp15@67998
   830
              let ?B = "(\<lambda>(x, y). x + y) ` (f' x ` ball 0 1 \<times> ball 0 k)"
lp15@67998
   831
              have "bounded ?B"
lp15@67998
   832
                by (simp add: bounded_plus [OF bo])
lp15@67998
   833
              moreover have "?rfs \<subseteq> ?B"
lp15@67998
   834
                apply (auto simp: dist_norm image_iff dest!: ex_lessK)
lp15@67998
   835
                by (metis (no_types, hide_lams) add.commute diff_add_cancel dist_0_norm dist_commute dist_norm mem_ball)
lp15@67998
   836
              ultimately show "bounded (?rfs)"
lp15@67998
   837
                by (rule bounded_subset)
lp15@67998
   838
            qed
lp15@67998
   839
            then have "(\<lambda>x. r *\<^sub>R x) ` ?rfs \<in> lmeasurable"
lp15@67998
   840
              by (simp add: measurable_linear_image)
lp15@67998
   841
            with \<open>r > 0\<close> have "(+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
lp15@67998
   842
              by (simp add: image_comp o_def)
lp15@67998
   843
            then have "(+) (f x) ` (+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
lp15@67998
   844
              using  measurable_translation by blast
lp15@67998
   845
            then show fsb: "f ` (S \<inter> ball x r) \<in> lmeasurable"
lp15@67998
   846
              by (simp add: image_comp o_def)
lp15@67998
   847
            have "?\<mu> (f ` (S \<inter> ball x r)) = ?\<mu> (?rfs) * r ^ CARD('n)"
haftmann@69661
   848
              using \<open>r > 0\<close> fsb
haftmann@69661
   849
              by (simp add: measure_linear_image measure_translation_subtract measurable_translation_subtract field_simps cong: image_cong_simp)
lp15@67998
   850
            also have "\<dots> \<le> (\<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))) * r ^ CARD('n)"
lp15@67998
   851
            proof -
lp15@67998
   852
              have "?\<mu> (?rfs) < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
lp15@67998
   853
                using rfs_mble by (force intro: k dest!: ex_lessK)
lp15@67998
   854
              then have "?\<mu> (?rfs) < \<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))"
lp15@67998
   855
                by (simp add: lin measure_linear_image [of "f' x"] content_ball)
lp15@67998
   856
              with \<open>r > 0\<close> show ?thesis
lp15@67998
   857
                by auto
lp15@67998
   858
            qed
lp15@67998
   859
            also have "\<dots> \<le> (B + e) * ?\<mu> (ball x r)"
lp15@67998
   860
              using bounded [OF \<open>x \<in> S\<close>] \<open>r > 0\<close> by (simp add: content_ball algebra_simps)
lp15@67998
   861
            finally show "?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)" .
lp15@67998
   862
          qed
lp15@67998
   863
        qed
lp15@67998
   864
        then obtain r where
lp15@67998
   865
          r0d: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> 0 < r x d \<and> r x d < d"
lp15@67998
   866
          and rT: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> ball x (r x d) \<subseteq> T"
lp15@67998
   867
          and r: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow>
lp15@67998
   868
                  (f ` (S \<inter> ball x (r x d))) \<in> lmeasurable \<and>
lp15@67998
   869
                  ?\<mu> (f ` (S \<inter> ball x (r x d))) \<le> (B + e) * ?\<mu> (ball x (r x d))"
lp15@67998
   870
          by metis
lp15@67998
   871
        obtain C where "countable C" and Csub: "C \<subseteq> {(x,r x t) |x t. x \<in> S \<and> 0 < t}"
lp15@67998
   872
          and pwC: "pairwise (\<lambda>i j. disjnt (ball (fst i) (snd i)) (ball (fst j) (snd j))) C"
lp15@67998
   873
          and negC: "negligible(S - (\<Union>i \<in> C. ball (fst i) (snd i)))"
lp15@67998
   874
          apply (rule Vitali_covering_theorem_balls [of S "{(x,r x t) |x t. x \<in> S \<and> 0 < t}" fst snd])
lp15@67998
   875
           apply auto
lp15@67998
   876
          by (metis dist_eq_0_iff r0d)
lp15@67998
   877
        let ?UB = "(\<Union>(x,s) \<in> C. ball x s)"
lp15@67998
   878
        have eq: "f ` (S \<inter> ?UB) = (\<Union>(x,s) \<in> C. f ` (S \<inter> ball x s))"
lp15@67998
   879
          by auto
lp15@67998
   880
        have mle: "?\<mu> (\<Union>(x,s) \<in> K. f ` (S \<inter> ball x s)) \<le> (B + e) * (?\<mu> S + d)"  (is "?l \<le> ?r")
lp15@67998
   881
          if "K \<subseteq> C" and "finite K" for K
lp15@67998
   882
        proof -
lp15@67998
   883
          have gt0: "b > 0" if "(a, b) \<in> K" for a b
lp15@67998
   884
            using Csub that \<open>K \<subseteq> C\<close> r0d by auto
lp15@67998
   885
          have inj: "inj_on (\<lambda>(x, y). ball x y) K"
lp15@67998
   886
            by (force simp: inj_on_def ball_eq_ball_iff dest: gt0)
lp15@67998
   887
          have disjnt: "disjoint ((\<lambda>(x, y). ball x y) ` K)"
lp15@67998
   888
            using pwC that
lp15@67998
   889
            apply (clarsimp simp: pairwise_def case_prod_unfold ball_eq_ball_iff)
lp15@67998
   890
            by (metis subsetD fst_conv snd_conv)
lp15@67998
   891
          have "?l \<le> (\<Sum>i\<in>K. ?\<mu> (case i of (x, s) \<Rightarrow> f ` (S \<inter> ball x s)))"
lp15@67998
   892
          proof (rule measure_UNION_le [OF \<open>finite K\<close>], clarify)
lp15@67998
   893
            fix x r
lp15@67998
   894
            assume "(x,r) \<in> K"
lp15@67998
   895
            then have "x \<in> S"
lp15@67998
   896
              using Csub \<open>K \<subseteq> C\<close> by auto
lp15@67998
   897
            show "f ` (S \<inter> ball x r) \<in> sets lebesgue"
lp15@67998
   898
              by (meson Int_lower1 S differentiable_on_subset f_diff fmeasurableD lmeasurable_ball order_refl sets.Int differentiable_image_in_sets_lebesgue)
lp15@67998
   899
          qed
lp15@67998
   900
          also have "\<dots> \<le> (\<Sum>(x,s) \<in> K. (B + e) * ?\<mu> (ball x s))"
lp15@67998
   901
            apply (rule sum_mono)
lp15@67998
   902
            using Csub r \<open>K \<subseteq> C\<close> by auto
lp15@67998
   903
          also have "\<dots> = (B + e) * (\<Sum>(x,s) \<in> K. ?\<mu> (ball x s))"
lp15@67998
   904
            by (simp add: prod.case_distrib sum_distrib_left)
lp15@67998
   905
          also have "\<dots> = (B + e) * sum ?\<mu> ((\<lambda>(x, y). ball x y) ` K)"
lp15@67998
   906
            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> by (simp add: inj sum.reindex prod.case_distrib)
lp15@67998
   907
          also have "\<dots> = (B + e) * ?\<mu> (\<Union>(x,s) \<in> K. ball x s)"
lp15@67998
   908
            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that
lp15@67998
   909
            by (subst measure_Union') (auto simp: disjnt measure_Union')
lp15@67998
   910
          also have "\<dots> \<le> (B + e) * ?\<mu> T"
lp15@67998
   911
            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that apply simp
lp15@67998
   912
            apply (rule measure_mono_fmeasurable [OF _ _ \<open>T \<in> lmeasurable\<close>])
lp15@67998
   913
            using Csub rT by force+
lp15@67998
   914
          also have "\<dots> \<le> (B + e) * (?\<mu> S + d)"
lp15@67998
   915
            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> Tless by simp
lp15@67998
   916
          finally show ?thesis .
lp15@67998
   917
        qed
lp15@67998
   918
        have fSUB_mble: "(f ` (S \<inter> ?UB)) \<in> lmeasurable"
lp15@67998
   919
          unfolding eq using Csub r False \<open>e > 0\<close> that
lp15@67998
   920
          by (auto simp: intro!: fmeasurable_UN_bound [OF \<open>countable C\<close> _ mle])
lp15@67998
   921
        have fSUB_meas: "?\<mu> (f ` (S \<inter> ?UB)) \<le> (B + e) * (?\<mu> S + d)"  (is "?MUB")
lp15@67998
   922
          unfolding eq using Csub r False \<open>e > 0\<close> that
lp15@67998
   923
          by (auto simp: intro!: measure_UN_bound [OF \<open>countable C\<close> _ mle])
lp15@67998
   924
        have neg: "negligible ((f ` (S \<inter> ?UB) - f ` S) \<union> (f ` S - f ` (S \<inter> ?UB)))"
lp15@67998
   925
        proof (rule negligible_subset [OF negligible_differentiable_image_negligible [OF order_refl negC, where f=f]])
lp15@67998
   926
          show "f differentiable_on S - (\<Union>i\<in>C. ball (fst i) (snd i))"
lp15@67998
   927
            by (meson DiffE differentiable_on_subset subsetI f_diff)
lp15@67998
   928
        qed force
lp15@67998
   929
        show "f ` S \<in> lmeasurable"
lp15@67998
   930
          by (rule lmeasurable_negligible_symdiff [OF fSUB_mble neg])
lp15@67998
   931
        show ?MD
lp15@67998
   932
          using fSUB_meas measure_negligible_symdiff [OF fSUB_mble neg] by simp
lp15@67998
   933
      qed
lp15@67998
   934
      show "f ` S \<in> lmeasurable"
lp15@67998
   935
        using eps_d [of 1] by simp
lp15@67998
   936
      show ?ME
lp15@67998
   937
      proof (rule field_le_epsilon)
lp15@67998
   938
        fix \<delta> :: real
lp15@67998
   939
        assume "0 < \<delta>"
lp15@67998
   940
        then show "?\<mu> (f ` S) \<le> (B + e) * ?\<mu> S + \<delta>"
lp15@67998
   941
          using eps_d [of "\<delta> / (B+e)"] \<open>e > 0\<close> \<open>B \<ge> 0\<close> by (auto simp: divide_simps mult_ac)
lp15@67998
   942
      qed
lp15@67998
   943
    qed
lp15@67998
   944
    show ?thesis
lp15@67998
   945
    proof (cases "?\<mu> S = 0")
lp15@67998
   946
      case True
lp15@67998
   947
      with eps have "?\<mu> (f ` S) = 0"
lp15@67998
   948
        by (metis mult_zero_right not_le zero_less_measure_iff)
lp15@67998
   949
      then show ?thesis
lp15@67998
   950
        using eps [of 1] by (simp add: True)
lp15@67998
   951
    next
lp15@67998
   952
      case False
lp15@67998
   953
      have "?\<mu> (f ` S) \<le> B * ?\<mu> S"
lp15@67998
   954
      proof (rule field_le_epsilon)
lp15@67998
   955
        fix e :: real
lp15@67998
   956
        assume "e > 0"
lp15@67998
   957
        then show "?\<mu> (f ` S) \<le> B * ?\<mu> S + e"
lp15@67998
   958
          using eps [of "e / ?\<mu> S"] False by (auto simp: algebra_simps zero_less_measure_iff)
lp15@67998
   959
      qed
lp15@67998
   960
      with eps [of 1] show ?thesis by auto
lp15@67998
   961
    qed
lp15@67998
   962
  qed
lp15@67998
   963
  then show "f ` S \<in> lmeasurable" ?M by blast+
lp15@67998
   964
qed
lp15@67998
   965
nipkow@69739
   966
lemma m_diff_image_weak:
lp15@67998
   967
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
   968
  assumes S: "S \<in> lmeasurable"
lp15@67998
   969
    and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
   970
    and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
nipkow@69739
   971
  shows "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
ak2110@69720
   972
proof -
lp15@67998
   973
  let ?\<mu> = "measure lebesgue"
lp15@67998
   974
  have aint_S: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
lp15@67998
   975
    using int unfolding absolutely_integrable_on_def by auto
lp15@67998
   976
  define m where "m \<equiv> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
   977
  have *: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
lp15@67998
   978
    if "e > 0" for e
lp15@67998
   979
  proof -
lp15@67998
   980
    define T where "T \<equiv> \<lambda>n. {x \<in> S. n * e \<le> \<bar>det (matrix (f' x))\<bar> \<and>
lp15@67998
   981
                                     \<bar>det (matrix (f' x))\<bar> < (Suc n) * e}"
lp15@67998
   982
    have meas_t: "T n \<in> lmeasurable" for n
lp15@67998
   983
    proof -
lp15@67998
   984
      have *: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<in> borel_measurable (lebesgue_on S)"
lp15@67998
   985
        using aint_S by (simp add: S borel_measurable_restrict_space_iff fmeasurableD set_integrable_def)
lp15@67998
   986
      have [intro]: "x \<in> sets (lebesgue_on S) \<Longrightarrow> x \<in> sets lebesgue" for x
lp15@67998
   987
        using S sets_restrict_space_subset by blast
lp15@67998
   988
      have "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> sets lebesgue"
lp15@67998
   989
        using * by (auto simp: borel_measurable_iff_halfspace_ge space_restrict_space)
lp15@67998
   990
      then have 1: "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> lmeasurable"
lp15@67998
   991
        using S by (simp add: fmeasurableI2)
lp15@67998
   992
      have "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> sets lebesgue"
lp15@67998
   993
        using * by (auto simp: borel_measurable_iff_halfspace_less space_restrict_space)
lp15@67998
   994
      then have 2: "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> lmeasurable"
lp15@67998
   995
        using S by (simp add: fmeasurableI2)
lp15@67998
   996
      show ?thesis
lp15@67998
   997
        using fmeasurable.Int [OF 1 2] by (simp add: T_def Int_def cong: conj_cong)
lp15@67998
   998
    qed
lp15@67998
   999
    have aint_T: "\<And>k. (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on T k"
lp15@67998
  1000
      using set_integrable_subset [OF aint_S] meas_t T_def by blast
lp15@67998
  1001
    have Seq: "S = (\<Union>n. T n)"
lp15@67998
  1002
      apply (auto simp: T_def)
lp15@67998
  1003
      apply (rule_tac x="nat(floor(abs(det(matrix(f' x))) / e))" in exI)
lp15@67998
  1004
      using that apply auto
lp15@67998
  1005
      using of_int_floor_le pos_le_divide_eq apply blast
lp15@67998
  1006
      by (metis add.commute pos_divide_less_eq real_of_int_floor_add_one_gt)
lp15@67998
  1007
    have meas_ft: "f ` T n \<in> lmeasurable" for n
lp15@67998
  1008
    proof (rule measurable_bounded_differentiable_image)
lp15@67998
  1009
      show "T n \<in> lmeasurable"
lp15@67998
  1010
        by (simp add: meas_t)
lp15@67998
  1011
    next
lp15@67998
  1012
      fix x :: "(real,'n) vec"
lp15@67998
  1013
      assume "x \<in> T n"
lp15@67998
  1014
      show "(f has_derivative f' x) (at x within T n)"
lp15@67998
  1015
        by (metis (no_types, lifting) \<open>x \<in> T n\<close> deriv has_derivative_within_subset mem_Collect_eq subsetI T_def)
lp15@67998
  1016
      show "\<bar>det (matrix (f' x))\<bar> \<le> (Suc n) * e"
lp15@67998
  1017
        using \<open>x \<in> T n\<close> T_def by auto
lp15@67998
  1018
    next
lp15@67998
  1019
      show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T n"
lp15@67998
  1020
        using aint_T absolutely_integrable_on_def by blast
lp15@67998
  1021
    qed
lp15@67998
  1022
    have disT: "disjoint (range T)"
lp15@67998
  1023
      unfolding disjoint_def
lp15@67998
  1024
    proof clarsimp
lp15@67998
  1025
      show "T m \<inter> T n = {}" if "T m \<noteq> T n" for m n
lp15@67998
  1026
        using that
lp15@67998
  1027
      proof (induction m n rule: linorder_less_wlog)
lp15@67998
  1028
        case (less m n)
lp15@67998
  1029
        with \<open>e > 0\<close> show ?case
lp15@67998
  1030
          unfolding T_def
lp15@67998
  1031
          proof (clarsimp simp add: Collect_conj_eq [symmetric])
lp15@67998
  1032
            fix x
lp15@67998
  1033
            assume "e > 0"  "m < n"  "n * e \<le> \<bar>det (matrix (f' x))\<bar>"  "\<bar>det (matrix (f' x))\<bar> < (1 + real m) * e"
lp15@67998
  1034
            then have "n < 1 + real m"
lp15@67998
  1035
              by (metis (no_types, hide_lams) less_le_trans mult.commute not_le real_mult_le_cancel_iff2)
lp15@67998
  1036
            then show "False"
lp15@67998
  1037
              using less.hyps by linarith
lp15@67998
  1038
          qed
lp15@67998
  1039
      qed auto
lp15@67998
  1040
    qed
lp15@67998
  1041
    have injT: "inj_on T ({n. T n \<noteq> {}})"
lp15@67998
  1042
      unfolding inj_on_def
lp15@67998
  1043
    proof clarsimp
lp15@67998
  1044
      show "m = n" if "T m = T n" "T n \<noteq> {}" for m n
lp15@67998
  1045
        using that
lp15@67998
  1046
      proof (induction m n rule: linorder_less_wlog)
lp15@67998
  1047
        case (less m n)
lp15@67998
  1048
        have False if "T n \<subseteq> T m" "x \<in> T n" for x
lp15@67998
  1049
          using \<open>e > 0\<close> \<open>m < n\<close> that
lp15@67998
  1050
          apply (auto simp: T_def  mult.commute intro: less_le_trans dest!: subsetD)
lp15@67998
  1051
          by (metis add.commute less_le_trans nat_less_real_le not_le real_mult_le_cancel_iff2)
lp15@67998
  1052
        then show ?case
lp15@67998
  1053
          using less.prems by blast
lp15@67998
  1054
      qed auto
lp15@67998
  1055
    qed
lp15@67998
  1056
    have sum_eq_Tim: "(\<Sum>k\<le>n. f (T k)) = sum f (T ` {..n})" if "f {} = 0" for f :: "_ \<Rightarrow> real" and n
lp15@67998
  1057
    proof (subst sum.reindex_nontrivial)
lp15@67998
  1058
      fix i j  assume "i \<in> {..n}" "j \<in> {..n}" "i \<noteq> j" "T i = T j"
lp15@67998
  1059
      with that  injT [unfolded inj_on_def] show "f (T i) = 0"
lp15@67998
  1060
        by simp metis
lp15@67998
  1061
    qed (use atMost_atLeast0 in auto)
lp15@67998
  1062
    let ?B = "m + e * ?\<mu> S"
lp15@67998
  1063
    have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" for n
lp15@67998
  1064
    proof -
lp15@67998
  1065
      have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> (\<Sum>k\<le>n. ((k+1) * e) * ?\<mu>(T k))"
lp15@67998
  1066
      proof (rule sum_mono [OF measure_bounded_differentiable_image])
lp15@67998
  1067
        show "(f has_derivative f' x) (at x within T k)" if "x \<in> T k" for k x
lp15@67998
  1068
          using that unfolding T_def by (blast intro: deriv has_derivative_within_subset)
lp15@67998
  1069
        show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k" for k
lp15@67998
  1070
          using absolutely_integrable_on_def aint_T by blast
lp15@67998
  1071
        show "\<bar>det (matrix (f' x))\<bar> \<le> real (k + 1) * e" if "x \<in> T k" for k x
lp15@67998
  1072
          using T_def that by auto
lp15@67998
  1073
      qed (use meas_t in auto)
lp15@67998
  1074
      also have "\<dots> \<le> (\<Sum>k\<le>n. (k * e) * ?\<mu>(T k)) + (\<Sum>k\<le>n. e * ?\<mu>(T k))"
lp15@67998
  1075
        by (simp add: algebra_simps sum.distrib)
lp15@67998
  1076
      also have "\<dots> \<le> ?B"
lp15@67998
  1077
      proof (rule add_mono)
lp15@67998
  1078
        have "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) = (\<Sum>k\<le>n. integral (T k) (\<lambda>x. k * e))"
lp15@67998
  1079
          by (simp add: lmeasure_integral [OF meas_t]
nipkow@68403
  1080
                   flip: integral_mult_right integral_mult_left)
lp15@67998
  1081
        also have "\<dots> \<le> (\<Sum>k\<le>n. integral (T k) (\<lambda>x.  (abs (det (matrix (f' x))))))"
lp15@67998
  1082
        proof (rule sum_mono)
lp15@67998
  1083
          fix k
lp15@67998
  1084
          assume "k \<in> {..n}"
lp15@67998
  1085
          show "integral (T k) (\<lambda>x. k * e) \<le> integral (T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1086
          proof (rule integral_le [OF integrable_on_const [OF meas_t]])
lp15@67998
  1087
            show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k"
lp15@67998
  1088
              using absolutely_integrable_on_def aint_T by blast
lp15@67998
  1089
          next
lp15@67998
  1090
            fix x assume "x \<in> T k"
lp15@67998
  1091
            show "k * e \<le> \<bar>det (matrix (f' x))\<bar>"
lp15@67998
  1092
              using \<open>x \<in> T k\<close> T_def by blast
lp15@67998
  1093
          qed
lp15@67998
  1094
        qed
lp15@67998
  1095
        also have "\<dots> = sum (\<lambda>T. integral T (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) (T ` {..n})"
lp15@67998
  1096
          by (auto intro: sum_eq_Tim)
lp15@67998
  1097
        also have "\<dots> = integral (\<Union>k\<le>n. T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1098
        proof (rule integral_unique [OF has_integral_Union, symmetric])
lp15@67998
  1099
          fix S  assume "S \<in> T ` {..n}"
lp15@67998
  1100
          then show "((\<lambda>x. \<bar>det (matrix (f' x))\<bar>) has_integral integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) S"
lp15@67998
  1101
          using absolutely_integrable_on_def aint_T by blast
lp15@67998
  1102
        next
lp15@67998
  1103
          show "pairwise (\<lambda>S S'. negligible (S \<inter> S')) (T ` {..n})"
lp15@67998
  1104
            using disT unfolding disjnt_iff by (auto simp: pairwise_def intro!: empty_imp_negligible)
lp15@67998
  1105
        qed auto
lp15@67998
  1106
        also have "\<dots> \<le> m"
lp15@67998
  1107
          unfolding m_def
lp15@67998
  1108
        proof (rule integral_subset_le)
lp15@67998
  1109
          have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on (\<Union>k\<le>n. T k)"
lp15@67998
  1110
            apply (rule set_integrable_subset [OF aint_S])
lp15@67998
  1111
             apply (intro measurable meas_t fmeasurableD)
lp15@67998
  1112
            apply (force simp: Seq)
lp15@67998
  1113
            done
lp15@67998
  1114
          then show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on (\<Union>k\<le>n. T k)"
lp15@67998
  1115
            using absolutely_integrable_on_def by blast
lp15@67998
  1116
        qed (use Seq int in auto)
lp15@67998
  1117
        finally show "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) \<le> m" .
lp15@67998
  1118
      next
lp15@67998
  1119
        have "(\<Sum>k\<le>n. ?\<mu> (T k)) = sum ?\<mu> (T ` {..n})"
lp15@67998
  1120
          by (auto intro: sum_eq_Tim)
lp15@67998
  1121
        also have "\<dots> = ?\<mu> (\<Union>k\<le>n. T k)"
lp15@67998
  1122
          using S disT by (auto simp: pairwise_def meas_t intro: measure_Union' [symmetric])
lp15@67998
  1123
        also have "\<dots> \<le> ?\<mu> S"
lp15@67998
  1124
          using S by (auto simp: Seq intro: meas_t fmeasurableD measure_mono_fmeasurable)
lp15@67998
  1125
        finally have "(\<Sum>k\<le>n. ?\<mu> (T k)) \<le> ?\<mu> S" .
lp15@67998
  1126
        then show "(\<Sum>k\<le>n. e * ?\<mu> (T k)) \<le> e * ?\<mu> S"
lp15@67998
  1127
          by (metis less_eq_real_def ordered_comm_semiring_class.comm_mult_left_mono sum_distrib_left that)
lp15@67998
  1128
      qed
lp15@67998
  1129
      finally show "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" .
lp15@67998
  1130
    qed
lp15@67998
  1131
    moreover have "measure lebesgue (\<Union>k\<le>n. f ` T k) \<le> (\<Sum>k\<le>n. ?\<mu> (f ` T k))" for n
lp15@67998
  1132
      by (simp add: fmeasurableD meas_ft measure_UNION_le)
lp15@67998
  1133
    ultimately have B_ge_m: "?\<mu> (\<Union>k\<le>n. (f ` T k)) \<le> ?B" for n
lp15@67998
  1134
      by (meson order_trans)
lp15@67998
  1135
    have "(\<Union>n. f ` T n) \<in> lmeasurable"
lp15@67998
  1136
      by (rule fmeasurable_countable_Union [OF meas_ft B_ge_m])
lp15@67998
  1137
    moreover have "?\<mu> (\<Union>n. f ` T n) \<le> m + e * ?\<mu> S"
lp15@67998
  1138
      by (rule measure_countable_Union_le [OF meas_ft B_ge_m])
lp15@67998
  1139
    ultimately show "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
lp15@67998
  1140
      by (auto simp: Seq image_Union)
lp15@67998
  1141
  qed
lp15@67998
  1142
  show ?thesis
lp15@67998
  1143
  proof
lp15@67998
  1144
    show "f ` S \<in> lmeasurable"
lp15@67998
  1145
      using * linordered_field_no_ub by blast
lp15@67998
  1146
    let ?x = "m - ?\<mu> (f ` S)"
lp15@67998
  1147
    have False if "?\<mu> (f ` S) > integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1148
    proof -
lp15@67998
  1149
      have ml: "m < ?\<mu> (f ` S)"
lp15@67998
  1150
        using m_def that by blast
lp15@67998
  1151
      then have "?\<mu> S \<noteq> 0"
lp15@67998
  1152
        using "*"(2) bgauge_existence_lemma by fastforce
lp15@67998
  1153
      with ml have 0: "0 < - (m - ?\<mu> (f ` S))/2 / ?\<mu> S"
lp15@67998
  1154
        using that zero_less_measure_iff by force
lp15@67998
  1155
      then show ?thesis
lp15@67998
  1156
        using * [OF 0] that by (auto simp: divide_simps m_def split: if_split_asm)
lp15@67998
  1157
    qed
lp15@67998
  1158
    then show "?\<mu> (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1159
      by fastforce
lp15@67998
  1160
  qed
lp15@67998
  1161
qed
lp15@67998
  1162
lp15@67998
  1163
nipkow@69739
  1164
theorem
lp15@67998
  1165
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
  1166
  assumes S: "S \<in> sets lebesgue"
lp15@67998
  1167
    and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
  1168
    and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
lp15@67998
  1169
  shows measurable_differentiable_image: "f ` S \<in> lmeasurable"
lp15@67998
  1170
    and measure_differentiable_image:
lp15@67998
  1171
       "measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is "?M")
ak2110@69720
  1172
proof -
lp15@67998
  1173
  let ?I = "\<lambda>n::nat. cbox (vec (-n)) (vec n) \<inter> S"
lp15@67998
  1174
  let ?\<mu> = "measure lebesgue"
lp15@67998
  1175
  have "x \<in> cbox (vec (- real (nat \<lceil>norm x\<rceil>))) (vec (real (nat \<lceil>norm x\<rceil>)))" for x :: "real^'n::_"
lp15@67998
  1176
    apply (auto simp: mem_box_cart)
lp15@67998
  1177
    apply (metis abs_le_iff component_le_norm_cart minus_le_iff of_nat_ceiling order.trans)
lp15@67998
  1178
    by (meson abs_le_D1 norm_bound_component_le_cart real_nat_ceiling_ge)
lp15@67998
  1179
  then have Seq: "S = (\<Union>n. ?I n)"
lp15@67998
  1180
    by auto
lp15@67998
  1181
  have fIn: "f ` ?I n \<in> lmeasurable"
lp15@67998
  1182
       and mfIn: "?\<mu> (f ` ?I n) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is ?MN) for n
lp15@67998
  1183
  proof -
lp15@67998
  1184
    have In: "?I n \<in> lmeasurable"
lp15@67998
  1185
      by (simp add: S bounded_Int bounded_set_imp_lmeasurable sets.Int)
lp15@67998
  1186
    moreover have "\<And>x. x \<in> ?I n \<Longrightarrow> (f has_derivative f' x) (at x within ?I n)"
lp15@67998
  1187
      by (meson Int_iff deriv has_derivative_within_subset subsetI)
lp15@67998
  1188
    moreover have int_In: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on ?I n"
lp15@67998
  1189
    proof -
lp15@67998
  1190
      have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
lp15@67998
  1191
        using int absolutely_integrable_integrable_bound by force
lp15@67998
  1192
      then have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on ?I n"
lp15@67998
  1193
        by (metis (no_types) Int_lower1 In fmeasurableD inf_commute set_integrable_subset)
lp15@67998
  1194
      then show ?thesis
lp15@67998
  1195
        using absolutely_integrable_on_def by blast
lp15@67998
  1196
    qed
lp15@67998
  1197
    ultimately have "f ` ?I n \<in> lmeasurable" "?\<mu> (f ` ?I n) \<le> integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1198
      using m_diff_image_weak by metis+
lp15@67998
  1199
    moreover have "integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
lp15@67998
  1200
      by (simp add: int_In int integral_subset_le)
lp15@67998
  1201
    ultimately show "f ` ?I n \<in> lmeasurable" ?MN
lp15@67998
  1202
      by auto
lp15@67998
  1203
  qed
lp15@67998
  1204
  have "?I k \<subseteq> ?I n" if "k \<le> n" for k n
lp15@67998
  1205
    by (rule Int_mono) (use that in \<open>auto simp: subset_interval_imp_cart\<close>)
lp15@67998
  1206
  then have "(\<Union>k\<le>n. f ` ?I k) = f ` ?I n" for n
lp15@67998
  1207
    by (fastforce simp add:)
lp15@67998
  1208
  with mfIn have "?\<mu> (\<Union>k\<le>n. f ` ?I k) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" for n
lp15@67998
  1209
    by simp
lp15@67998
  1210
  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>)"
lp15@67998
  1211
    by (rule fmeasurable_countable_Union [OF fIn] measure_countable_Union_le [OF fIn])+
lp15@67998
  1212
  then show "f ` S \<in> lmeasurable" ?M
lp15@67998
  1213
    by (metis Seq image_UN)+
lp15@67998
  1214
qed
lp15@67998
  1215
lp15@67998
  1216
ak2110@69720
  1217
lemma borel_measurable_simple_function_limit_increasing:
lp15@67998
  1218
  fixes f :: "'a::euclidean_space \<Rightarrow> real"
lp15@67998
  1219
  shows "(f \<in> borel_measurable lebesgue \<and> (\<forall>x. 0 \<le> f x)) \<longleftrightarrow>
lp15@67998
  1220
         (\<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>
lp15@67998
  1221
              (\<forall>n. g n \<in> borel_measurable lebesgue) \<and> (\<forall>n. finite(range (g n))) \<and>
lp15@67998
  1222
              (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x))"
lp15@67998
  1223
         (is "?lhs = ?rhs")
lp15@67998
  1224
proof
lp15@67998
  1225
  assume f: ?lhs
lp15@67998
  1226
  have leb_f: "{x. a \<le> f x \<and> f x < b} \<in> sets lebesgue" for a b
lp15@67998
  1227
  proof -
lp15@67998
  1228
    have "{x. a \<le> f x \<and> f x < b} = {x. f x < b} - {x. f x < a}"
lp15@67998
  1229
      by auto
lp15@67998
  1230
    also have "\<dots> \<in> sets lebesgue"
lp15@67998
  1231
      using borel_measurable_vimage_halfspace_component_lt [of f UNIV] f by auto
lp15@67998
  1232
    finally show ?thesis .
lp15@67998
  1233
  qed
lp15@67998
  1234
  have "g n x \<le> f x"
lp15@67998
  1235
        if inc_g: "\<And>n x. 0 \<le> g n x \<and> g n x \<le> g (Suc n) x"
lp15@67998
  1236
           and meas_g: "\<And>n. g n \<in> borel_measurable lebesgue"
lp15@67998
  1237
           and fin: "\<And>n. finite(range (g n))" and lim: "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x" for g n x
lp15@67998
  1238
  proof -
lp15@67998
  1239
    have "\<exists>r>0. \<forall>N. \<exists>n\<ge>N. dist (g n x) (f x) \<ge> r" if "g n x > f x"
lp15@67998
  1240
    proof -
lp15@67998
  1241
      have g: "g n x \<le> g (N + n) x" for N
lp15@67998
  1242
        by (rule transitive_stepwise_le) (use inc_g in auto)
lp15@67998
  1243
      have "\<exists>na\<ge>N. g n x - f x \<le> dist (g na x) (f x)" for N
lp15@67998
  1244
        apply (rule_tac x="N+n" in exI)
lp15@67998
  1245
        using g [of N] by (auto simp: dist_norm)
lp15@67998
  1246
      with that show ?thesis
lp15@67998
  1247
        using diff_gt_0_iff_gt by blast
lp15@67998
  1248
    qed
lp15@67998
  1249
    with lim show ?thesis
lp15@67998
  1250
      apply (auto simp: lim_sequentially)
lp15@67998
  1251
      by (meson less_le_not_le not_le_imp_less)
lp15@67998
  1252
  qed
lp15@67998
  1253
  moreover
lp15@67998
  1254
  let ?\<Omega> = "\<lambda>n k. indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n}"
lp15@67998
  1255
  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)"
lp15@67998
  1256
  have "\<exists>g. (\<forall>n x. 0 \<le> g n x \<and> g n x \<le> (g(Suc n) x)) \<and>
lp15@67998
  1257
             (\<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)"
lp15@67998
  1258
  proof (intro exI allI conjI)
lp15@67998
  1259
    show "0 \<le> ?g n x" for n x
lp15@67998
  1260
    proof (clarify intro!: ordered_comm_monoid_add_class.sum_nonneg)
lp15@67998
  1261
      fix k::real
lp15@67998
  1262
      assume "k \<in> \<int>" and k: "\<bar>k\<bar> \<le> 2 ^ (2*n)"
lp15@67998
  1263
      show "0 \<le> k/2^n * ?\<Omega> n k x"
lp15@67998
  1264
        using f \<open>k \<in> \<int>\<close> apply (auto simp: indicator_def divide_simps Ints_def)
lp15@67998
  1265
        apply (drule spec [where x=x])
lp15@67998
  1266
        using zero_le_power [of "2::real" n] mult_nonneg_nonneg [of "f x" "2^n"]
lp15@67998
  1267
        by linarith
lp15@67998
  1268
    qed
lp15@67998
  1269
    show "?g n x \<le> ?g (Suc n) x" for n x
lp15@67998
  1270
    proof -
lp15@67998
  1271
      have "?g n x =
lp15@67998
  1272
            (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
lp15@67998
  1273
              k/2^n * (indicator {y. k/2^n \<le> f y \<and> f y < (k+1/2)/2^n} x +
lp15@67998
  1274
              indicator {y. (k+1/2)/2^n \<le> f y \<and> f y < (k+1)/2^n} x))"
lp15@67998
  1275
        by (rule sum.cong [OF refl]) (simp add: indicator_def divide_simps)
lp15@67998
  1276
      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) +
lp15@67998
  1277
                       (\<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)"
lp15@67998
  1278
        by (simp add:  comm_monoid_add_class.sum.distrib algebra_simps)
lp15@67998
  1279
      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) +
lp15@67998
  1280
                       (\<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)"
lp15@67998
  1281
        by (force simp: field_simps indicator_def intro: sum.cong)
lp15@67998
  1282
      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))"
lp15@67998
  1283
                (is "?a + _ \<le> ?b")
lp15@67998
  1284
      proof -
lp15@67998
  1285
        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"
lp15@67998
  1286
          by linarith
lp15@67998
  1287
        let ?h = "\<lambda>k. (2*k+1)/2 ^ Suc n *
lp15@67998
  1288
                      (indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < ((2*k+1) + 1)/2 ^ Suc n} x)"
lp15@67998
  1289
        show ?thesis
lp15@67998
  1290
        proof (rule *)
lp15@67998
  1291
          show "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
lp15@67998
  1292
                  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)
lp15@67998
  1293
                \<le> sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
lp15@67998
  1294
            by (rule sum_mono) (simp add: indicator_def divide_simps)
lp15@67998
  1295
        next
nipkow@69064
  1296
          have \<alpha>: "?a = (\<Sum>k \<in> (*)2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
lp15@67998
  1297
                         k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
lp15@67998
  1298
            by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
lp15@67998
  1299
          have \<beta>: "sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
lp15@67998
  1300
                   = (\<Sum>k \<in> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
lp15@67998
  1301
                      k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
lp15@67998
  1302
            by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
nipkow@69064
  1303
          have 0: "(*) 2 ` {k \<in> \<int>. P k} \<inter> (\<lambda>x. 2 * x + 1) ` {k \<in> \<int>. P k} = {}" for P :: "real \<Rightarrow> bool"
lp15@67998
  1304
          proof -
lp15@67998
  1305
            have "2 * i \<noteq> 2 * j + 1" for i j :: int by arith
lp15@67998
  1306
            thus ?thesis
lp15@67998
  1307
              unfolding Ints_def by auto (use of_int_eq_iff in fastforce)
lp15@67998
  1308
          qed
lp15@67998
  1309
          have "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
nipkow@69064
  1310
                = (\<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)}.
lp15@67998
  1311
                  k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
lp15@67998
  1312
            unfolding \<alpha> \<beta>
lp15@67998
  1313
            using finite_abs_int_segment [of "2 ^ (2*n)"]
lp15@67998
  1314
            by (subst sum_Un) (auto simp: 0)
lp15@67998
  1315
          also have "\<dots> \<le> ?b"
lp15@67998
  1316
          proof (rule sum_mono2)
lp15@67998
  1317
            show "finite {k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)}"
lp15@67998
  1318
              by (rule finite_abs_int_segment)
nipkow@69064
  1319
            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)}"
lp15@67998
  1320
              apply auto
lp15@67998
  1321
              using one_le_power [of "2::real" "2*n"]  by linarith
lp15@67998
  1322
            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
lp15@67998
  1323
              by blast
lp15@67998
  1324
            have "0 \<le> b" if "b \<in> \<int>" "f x * (2 * 2^n) < b + 1" for b
lp15@67998
  1325
            proof -
lp15@67998
  1326
              have "0 \<le> f x * (2 * 2^n)"
lp15@67998
  1327
                by (simp add: f)
lp15@67998
  1328
              also have "\<dots> < b+1"
lp15@67998
  1329
                by (simp add: that)
lp15@67998
  1330
              finally show "0 \<le> b"
lp15@67998
  1331
                using \<open>b \<in> \<int>\<close> by (auto simp: elim!: Ints_cases)
lp15@67998
  1332
            qed
lp15@67998
  1333
            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"
lp15@67998
  1334
                  if "b \<in> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)} -
nipkow@69064
  1335
                          ((*) 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
lp15@67998
  1336
              using that by (simp add: indicator_def divide_simps)
lp15@67998
  1337
          qed
lp15@67998
  1338
          finally show "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<le> ?b" .
lp15@67998
  1339
        qed
lp15@67998
  1340
      qed
lp15@67998
  1341
      finally show ?thesis .
lp15@67998
  1342
    qed
lp15@67998
  1343
    show "?g n \<in> borel_measurable lebesgue" for n
lp15@67998
  1344
      apply (intro borel_measurable_indicator borel_measurable_times borel_measurable_sum)
lp15@67998
  1345
      using leb_f sets_restrict_UNIV by auto
lp15@67998
  1346
    show "finite (range (?g n))" for n
lp15@67998
  1347
    proof -
lp15@67998
  1348
      have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x)
lp15@67998
  1349
              \<in> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}" for x
lp15@67998
  1350
      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")
lp15@67998
  1351
        case True
lp15@67998
  1352
        then show ?thesis
lp15@67998
  1353
          by (blast intro: indicator_sum_eq)
lp15@67998
  1354
      next
lp15@67998
  1355
        case False
lp15@67998
  1356
        then have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) = 0"
lp15@67998
  1357
          by auto
lp15@67998
  1358
        then show ?thesis by force
lp15@67998
  1359
      qed
lp15@67998
  1360
      then have "range (?g n) \<subseteq> ((\<lambda>k. (k/2^n)) ` {k. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n)})"
lp15@67998
  1361
        by auto
lp15@67998
  1362
      moreover have "finite ((\<lambda>k::real. (k/2^n)) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})"
lp15@67998
  1363
        by (intro finite_imageI finite_abs_int_segment)
lp15@67998
  1364
      ultimately show ?thesis
lp15@67998
  1365
        by (rule finite_subset)
lp15@67998
  1366
    qed
lp15@67998
  1367
    show "(\<lambda>n. ?g n x) \<longlonglongrightarrow> f x" for x
lp15@67998
  1368
    proof (clarsimp simp add: lim_sequentially)
lp15@67998
  1369
      fix e::real
lp15@67998
  1370
      assume "e > 0"
lp15@67998
  1371
      obtain N1 where N1: "2 ^ N1 > abs(f x)"
lp15@67998
  1372
        using real_arch_pow by fastforce
lp15@67998
  1373
      obtain N2 where N2: "(1/2) ^ N2 < e"
lp15@67998
  1374
        using real_arch_pow_inv \<open>e > 0\<close> by fastforce
lp15@67998
  1375
      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
lp15@67998
  1376
      proof -
lp15@67998
  1377
        let ?m = "real_of_int \<lfloor>2^n * f x\<rfloor>"
lp15@67998
  1378
        have "\<bar>?m\<bar> \<le> 2^n * 2^N1"
lp15@67998
  1379
          using N1 apply (simp add: f)
lp15@67998
  1380
          by (meson floor_mono le_floor_iff less_le_not_le mult_le_cancel_left_pos zero_less_numeral zero_less_power)
lp15@67998
  1381
        also have "\<dots> \<le> 2 ^ (2*n)"
lp15@67998
  1382
          by (metis that add_leD1 add_le_cancel_left mult.commute mult_2_right one_less_numeral_iff
lp15@67998
  1383
                    power_add power_increasing_iff semiring_norm(76))
lp15@67998
  1384
        finally have m_le: "\<bar>?m\<bar> \<le> 2 ^ (2*n)" .
lp15@67998
  1385
        have "?m/2^n \<le> f x" "f x < (?m + 1)/2^n"
lp15@67998
  1386
          by (auto simp: mult.commute pos_divide_le_eq mult_imp_less_div_pos)
lp15@67998
  1387
        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)
lp15@67998
  1388
                     = dist (?m/2^n) (f x)"
lp15@67998
  1389
          by (subst indicator_sum_eq [of ?m]) (auto simp: m_le)
lp15@67998
  1390
        have "\<bar>2^n\<bar> * \<bar>?m/2^n - f x\<bar> = \<bar>2^n * (?m/2^n - f x)\<bar>"
lp15@67998
  1391
          by (simp add: abs_mult)
lp15@67998
  1392
        also have "\<dots> < 2 ^ N2 * e"
lp15@67998
  1393
          using N2 by (simp add: divide_simps mult.commute) linarith
lp15@67998
  1394
        also have "\<dots> \<le> \<bar>2^n\<bar> * e"
lp15@67998
  1395
          using that \<open>e > 0\<close> by auto
lp15@67998
  1396
        finally have "dist (?m/2^n) (f x) < e"
lp15@67998
  1397
          by (simp add: dist_norm)
lp15@67998
  1398
        then show ?thesis
lp15@67998
  1399
          using eq by linarith
lp15@67998
  1400
      qed
lp15@67998
  1401
      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"
lp15@67998
  1402
        by force
lp15@67998
  1403
    qed
lp15@67998
  1404
  qed
lp15@67998
  1405
  ultimately show ?rhs
lp15@67998
  1406
    by metis
lp15@67998
  1407
next
lp15@67998
  1408
  assume RHS: ?rhs
lp15@67998
  1409
  with borel_measurable_simple_function_limit [of f UNIV, unfolded borel_measurable_UNIV_eq]
lp15@67998
  1410
  show ?lhs
lp15@67998
  1411
    by (blast intro: order_trans)
lp15@67998
  1412
qed
lp15@67998
  1413
immler@69683
  1414
subsection\<open>Borel measurable Jacobian determinant\<close>
lp15@67998
  1415
ak2110@69720
  1416
lemma lemma_partial_derivatives0:
lp15@67998
  1417
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
lp15@67998
  1418
  assumes "linear f" and lim0: "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within S)"
lp15@67998
  1419
    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>)"
lp15@67998
  1420
  shows "f x = 0"
lp15@67998
  1421
proof -
immler@68072
  1422
  interpret linear f by fact
lp15@67998
  1423
  have "dim {x. f x = 0} \<le> DIM('a)"
immler@68072
  1424
    by (rule dim_subset_UNIV)
lp15@67998
  1425
  moreover have False if less: "dim {x. f x = 0} < DIM('a)"
lp15@67998
  1426
  proof -
lp15@67998
  1427
    obtain d where "d \<noteq> 0" and d: "\<And>y. f y = 0 \<Longrightarrow> d \<bullet> y = 0"
lp15@67998
  1428
      using orthogonal_to_subspace_exists [OF less] orthogonal_def
immler@68074
  1429
      by (metis (mono_tags, lifting) mem_Collect_eq span_base)
lp15@67998
  1430
    then obtain k where "k > 0"
lp15@67998
  1431
      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>"
lp15@67998
  1432
      using lb by blast
lp15@67998
  1433
    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>
lp15@67998
  1434
               norm (h (Suc n)) < norm (h n)"
lp15@67998
  1435
    proof (rule dependent_nat_choice)
lp15@67998
  1436
      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)"
lp15@67998
  1437
        by simp (metis DiffE insertCI k not_less not_one_le_zero)
lp15@67998
  1438
    qed (use k [of "min (norm x) (1/(Suc n + 1))" for x n] in auto)
lp15@67998
  1439
    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>"
lp15@67998
  1440
         and norm_lt: "\<And>n. norm(\<alpha> n) < 1/(Suc n)"
lp15@67998
  1441
      by force
lp15@67998
  1442
    let ?\<beta> = "\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)"
lp15@67998
  1443
    have com: "\<And>g. (\<forall>n. g n \<in> sphere (0::'a) 1)
lp15@67998
  1444
              \<Longrightarrow> \<exists>l \<in> sphere 0 1. \<exists>\<rho>::nat\<Rightarrow>nat. strict_mono \<rho> \<and> (g \<circ> \<rho>) \<longlonglongrightarrow> l"
lp15@67998
  1445
      using compact_sphere compact_def by metis
lp15@67998
  1446
    moreover have "\<forall>n. ?\<beta> n \<in> sphere 0 1"
lp15@67998
  1447
      using \<alpha> by auto
lp15@67998
  1448
    ultimately obtain l::'a and \<rho>::"nat\<Rightarrow>nat"
lp15@67998
  1449
       where l: "l \<in> sphere 0 1" and "strict_mono \<rho>" and to_l: "(?\<beta> \<circ> \<rho>) \<longlonglongrightarrow> l"
lp15@67998
  1450
      by meson
lp15@67998
  1451
    moreover have "continuous (at l) (\<lambda>x. (\<bar>d \<bullet> x\<bar> - k))"
lp15@67998
  1452
      by (intro continuous_intros)
lp15@67998
  1453
    ultimately have lim_dl: "((\<lambda>x. (\<bar>d \<bullet> x\<bar> - k)) \<circ> (?\<beta> \<circ> \<rho>)) \<longlonglongrightarrow> (\<bar>d \<bullet> l\<bar> - k)"
lp15@67998
  1454
      by (meson continuous_imp_tendsto)
lp15@67998
  1455
    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"
lp15@67998
  1456
      using \<alpha> kd by (auto simp: divide_simps)
lp15@67998
  1457
    then have "k \<le> \<bar>d \<bullet> l\<bar>"
lp15@67998
  1458
      using tendsto_lowerbound [OF lim_dl, of 0] by auto
lp15@67998
  1459
    moreover have "d \<bullet> l = 0"
lp15@67998
  1460
    proof (rule d)
lp15@67998
  1461
      show "f l = 0"
lp15@67998
  1462
      proof (rule LIMSEQ_unique [of "f \<circ> ?\<beta> \<circ> \<rho>"])
lp15@67998
  1463
        have "isCont f l"
lp15@67998
  1464
          using \<open>linear f\<close> linear_continuous_at linear_conv_bounded_linear by blast
lp15@67998
  1465
        then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> f l"
lp15@67998
  1466
          unfolding comp_assoc
lp15@67998
  1467
          using to_l continuous_imp_tendsto by blast
lp15@67998
  1468
        have "\<alpha> \<longlonglongrightarrow> 0"
lp15@67998
  1469
          using norm_lt LIMSEQ_norm_0 by metis
lp15@67998
  1470
        with \<open>strict_mono \<rho>\<close> have "(\<alpha> \<circ> \<rho>) \<longlonglongrightarrow> 0"
lp15@67998
  1471
          by (metis LIMSEQ_subseq_LIMSEQ)
lp15@67998
  1472
        with lim0 \<alpha> have "((\<lambda>x. f x /\<^sub>R norm x) \<circ> (\<alpha> \<circ> \<rho>)) \<longlonglongrightarrow> 0"
lp15@67998
  1473
          by (force simp: tendsto_at_iff_sequentially)
lp15@67998
  1474
        then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> 0"
immler@68072
  1475
          by (simp add: o_def scale)
lp15@67998
  1476
      qed
lp15@67998
  1477
    qed
lp15@67998
  1478
    ultimately show False
lp15@67998
  1479
      using \<open>k > 0\<close> by auto
lp15@67998
  1480
  qed
lp15@67998
  1481
  ultimately have dim: "dim {x. f x = 0} = DIM('a)"
lp15@67998
  1482
    by force
lp15@67998
  1483
  then show ?thesis
immler@68072
  1484
    using dim_eq_full
immler@68072
  1485
    by (metis (mono_tags, lifting) eq_0_on_span eucl.span_Basis linear_axioms linear_eq_stdbasis
immler@68072
  1486
        mem_Collect_eq module_hom_zero span_base span_raw_def)
lp15@67998
  1487
qed
lp15@67998
  1488
ak2110@69720
  1489
lemma lemma_partial_derivatives:
lp15@67998
  1490
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
lp15@67998
  1491
  assumes "linear f" and lim: "((\<lambda>x. f (x - a) /\<^sub>R norm (x - a)) \<longlongrightarrow> 0) (at a within S)"
lp15@67998
  1492
    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>)"
lp15@67998
  1493
  shows "f x = 0"
lp15@67998
  1494
proof -
lp15@67998
  1495
  have "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within (\<lambda>x. x-a) ` S)"
lp15@67998
  1496
    using lim by (simp add: Lim_within dist_norm)
lp15@67998
  1497
  then show ?thesis
lp15@67998
  1498
  proof (rule lemma_partial_derivatives0 [OF \<open>linear f\<close>])
lp15@67998
  1499
    fix v :: "'a"
lp15@67998
  1500
    assume v: "v \<noteq> 0"
lp15@67998
  1501
    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>"
lp15@67998
  1502
      using lb [OF v] by (force simp:  norm_minus_commute)
lp15@67998
  1503
  qed
lp15@67998
  1504
qed
lp15@67998
  1505
lp15@67998
  1506
ak2110@69720
  1507
proposition borel_measurable_partial_derivatives:
lp15@67998
  1508
  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n"
lp15@67998
  1509
  assumes S: "S \<in> sets lebesgue"
lp15@67998
  1510
    and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
  1511
  shows "(\<lambda>x. (matrix(f' x)$m$n)) \<in> borel_measurable (lebesgue_on S)"
ak2110@69720
  1512
proof -
lp15@67998
  1513
  have contf: "continuous_on S f"
lp15@67998
  1514
    using continuous_on_eq_continuous_within f has_derivative_continuous by blast
lp15@67998
  1515
  have "{x \<in> S.  (matrix (f' x)$m$n) \<le> b} \<in> sets lebesgue" for b
lp15@67998
  1516
  proof (rule sets_negligible_symdiff)
lp15@67998
  1517
    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>
lp15@67998
  1518
                       (\<forall>y \<in> S. norm(y - x) < d \<longrightarrow> norm(f y - f x - A *v (y - x)) \<le> e * norm(y - x))}"
lp15@67998
  1519
    let ?U = "S \<inter>
lp15@67998
  1520
              (\<Inter>e \<in> {e \<in> \<rat>. e > 0}.
lp15@67998
  1521
                \<Union>A \<in> {A. A$m$n < b \<and> (\<forall>i j. A$i$j \<in> \<rat>)}.
lp15@67998
  1522
                  \<Union>d \<in> {d \<in> \<rat>. 0 < d}.
lp15@67998
  1523
                     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)}))"
lp15@67998
  1524
    have "?T = ?U"
lp15@67998
  1525
    proof (intro set_eqI iffI)
lp15@67998
  1526
      fix x
lp15@67998
  1527
      assume xT: "x \<in> ?T"
lp15@67998
  1528
      then show "x \<in> ?U"
lp15@67998
  1529
      proof (clarsimp simp add:)
lp15@67998
  1530
        fix q :: real
lp15@67998
  1531
        assume "q \<in> \<rat>" "q > 0"
lp15@67998
  1532
        then obtain d A where "d > 0" and A: "A $ m $ n < b" "\<And>i j. A $ i $ j \<in> \<rat>"
lp15@67998
  1533
          "\<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)"
lp15@67998
  1534
          using xT by auto
lp15@67998
  1535
        then obtain \<delta> where "d > \<delta>" "\<delta> > 0" "\<delta> \<in> \<rat>"
lp15@67998
  1536
          using Rats_dense_in_real by blast
lp15@67998
  1537
        with A show "\<exists>A. A $ m $ n < b \<and> (\<forall>i j. A $ i $ j \<in> \<rat>) \<and>
lp15@67998
  1538
                         (\<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)))"
lp15@67998
  1539
          by force
lp15@67998
  1540
      qed
lp15@67998
  1541
    next
lp15@67998
  1542
      fix x
lp15@67998
  1543
      assume xU: "x \<in> ?U"
lp15@67998
  1544
      then show "x \<in> ?T"
lp15@67998
  1545
      proof clarsimp
lp15@67998
  1546
        fix e :: "real"
lp15@67998
  1547
        assume "e > 0"
lp15@67998
  1548
        then obtain \<epsilon> where \<epsilon>: "e > \<epsilon>" "\<epsilon> > 0" "\<epsilon> \<in> \<rat>"
lp15@67998
  1549
          using Rats_dense_in_real by blast
lp15@67998
  1550
        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"
lp15@67998
  1551
          and "\<forall>y\<in>S. norm (y - x) < r \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> \<epsilon> * norm (y - x)"
lp15@67998
  1552
          by (auto simp: split: if_split_asm)
lp15@67998
  1553
        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)"
lp15@67998
  1554
          by (meson \<open>e > \<epsilon>\<close> less_eq_real_def mult_right_mono norm_ge_zero order_trans)
lp15@67998
  1555
        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))"
lp15@67998
  1556
          using \<open>x \<in> S\<close> Ar by blast
lp15@67998
  1557
      qed
lp15@67998
  1558
    qed
lp15@67998
  1559
    moreover have "?U \<in> sets lebesgue"
lp15@67998
  1560
    proof -
lp15@67998
  1561
      have coQ: "countable {e \<in> \<rat>. 0 < e}"
lp15@67998
  1562
        using countable_Collect countable_rat by blast
lp15@67998
  1563
      have ne: "{e \<in> \<rat>. (0::real) < e} \<noteq> {}"
lp15@67998
  1564
        using zero_less_one Rats_1 by blast
lp15@67998
  1565
      have coA: "countable {A. A $ m $ n < b \<and> (\<forall>i j. A $ i $ j \<in> \<rat>)}"
lp15@67998
  1566
      proof (rule countable_subset)
lp15@67998
  1567
        show "countable {A. \<forall>i j. A $ i $ j \<in> \<rat>}"
lp15@67998
  1568
          using countable_vector [OF countable_vector, of "\<lambda>i j. \<rat>"] by (simp add: countable_rat)
lp15@67998
  1569
      qed blast
lp15@69922
  1570
      have *: "\<lbrakk>U \<noteq> {} \<Longrightarrow> closedin (top_of_set S) (S \<inter> \<Inter> U)\<rbrakk>
lp15@69922
  1571
               \<Longrightarrow> closedin (top_of_set S) (S \<inter> \<Inter> U)" for U
lp15@67998
  1572
        by fastforce
lp15@67998
  1573
      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
lp15@67998
  1574
        by auto
lp15@67998
  1575
      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)})
lp15@67998
  1576
                  \<in> sets lebesgue" for e A d
lp15@67998
  1577
      proof -
lp15@69922
  1578
        have clo: "closedin (top_of_set S)
lp15@67998
  1579
                     {x \<in> S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)}"
lp15@67998
  1580
          for y
lp15@67998
  1581
        proof -
lp15@67998
  1582
          have cont1: "continuous_on S (\<lambda>x. norm (y - x))"
lp15@67998
  1583
          and  cont2: "continuous_on S (\<lambda>x. e * norm (y - x) - norm (f y - f x - (A *v y - A *v x)))"
lp15@67998
  1584
            by (force intro: contf continuous_intros)+
lp15@69922
  1585
          have clo1: "closedin (top_of_set S) {x \<in> S. d \<le> norm (y - x)}"
lp15@67998
  1586
            using continuous_closedin_preimage [OF cont1, of "{d..}"] by (simp add: vimage_def Int_def)
lp15@69922
  1587
          have clo2: "closedin (top_of_set S)
lp15@67998
  1588
                       {x \<in> S. norm (f y - f x - (A *v y - A *v x)) \<le> e * norm (y - x)}"
lp15@67998
  1589
            using continuous_closedin_preimage [OF cont2, of "{0..}"] by (simp add: vimage_def Int_def)
lp15@67998
  1590
          show ?thesis
lp15@67998
  1591
            by (auto simp: eq not_less matrix_vector_mult_diff_distrib intro: clo1 clo2)
lp15@67998
  1592
        qed
lp15@67998
  1593
        show ?thesis
lp15@67998
  1594
          by (rule lebesgue_closedin [of S]) (force intro: * S clo)+
lp15@67998
  1595
      qed
lp15@67998
  1596
      show ?thesis
lp15@67998
  1597
        by (intro sets sets.Int S sets.countable_UN'' sets.countable_INT'' coQ coA) auto
lp15@67998
  1598
    qed
lp15@67998
  1599
    ultimately show "?T \<in> sets lebesgue"
lp15@67998
  1600
      by simp
lp15@67998
  1601
    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))"
lp15@67998
  1602
    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)"
lp15@67998
  1603
    have nN: "negligible {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
lp15@67998
  1604
      unfolding negligible_eq_zero_density
lp15@67998
  1605
    proof clarsimp
lp15@67998
  1606
      fix x v and r e :: "real"
lp15@67998
  1607
      assume "x \<in> S" "v \<noteq> 0" "r > 0" "e > 0"
lp15@67998
  1608
      and Theta [rule_format]: "?\<Theta> x v"
lp15@67998
  1609
      moreover have "(norm v * e / 2) / CARD('m) ^ CARD('m) > 0"
lp15@67998
  1610
        by (simp add: \<open>v \<noteq> 0\<close> \<open>e > 0\<close>)
lp15@67998
  1611
      ultimately obtain d where "d > 0"
lp15@67998
  1612
         and dless: "\<And>y. \<lbrakk>y \<in> S - {x}; norm (x - y) < d\<rbrakk> \<Longrightarrow>
lp15@67998
  1613
                        \<bar>v \<bullet> (y - x)\<bar> < ((norm v * e / 2) / CARD('m) ^ CARD('m)) * norm (x - y)"
lp15@67998
  1614
        by metis
lp15@67998
  1615
      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)}"
lp15@67998
  1616
      have "open {x. \<bar>v \<bullet> (x - a)\<bar> < b}" for a b
lp15@67998
  1617
        by (intro open_Collect_less continuous_intros)
lp15@67998
  1618
      show "\<exists>d>0. d \<le> r \<and>
lp15@67998
  1619
            (\<exists>U. {x' \<in> S. \<exists>v\<noteq>0. ?\<Theta> x' v} \<inter> ball x d \<subseteq> U \<and>
lp15@67998
  1620
                 U \<in> lmeasurable \<and> measure lebesgue U < e * content (ball x d))"
lp15@67998
  1621
      proof (intro exI conjI)
lp15@67998
  1622
        show "0 < min d r" "min d r \<le> r"
lp15@67998
  1623
          using \<open>r > 0\<close> \<open>d > 0\<close> by auto
lp15@67998
  1624
        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"
lp15@67998
  1625
          proof (clarsimp simp: dist_norm norm_minus_commute)
lp15@68001
  1626
            fix y w
lp15@67998
  1627
            assume "y \<in> S" "w \<noteq> 0"
lp15@67998
  1628
              and less [rule_format]:
lp15@67998
  1629
                    "\<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)"
lp15@67998
  1630
              and d: "norm (y - x) < d" and r: "norm (y - x) < r"
lp15@67998
  1631
            show "\<bar>v \<bullet> (y - x)\<bar> < norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
lp15@67998
  1632
            proof (cases "y = x")
lp15@67998
  1633
              case True
lp15@67998
  1634
              with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> \<open>v \<noteq> 0\<close> show ?thesis
lp15@67998
  1635
                by simp
lp15@67998
  1636
            next
lp15@67998
  1637
              case False
lp15@67998
  1638
              have "\<bar>v \<bullet> (y - x)\<bar> < norm v * e / 2 / real (CARD('m) ^ CARD('m)) * norm (x - y)"
lp15@67998
  1639
                apply (rule dless)
lp15@67998
  1640
                using False \<open>y \<in> S\<close> d by (auto simp: norm_minus_commute)
lp15@67998
  1641
              also have "\<dots> \<le> norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
lp15@67998
  1642
                using d r \<open>e > 0\<close> by (simp add: field_simps norm_minus_commute mult_left_mono)
lp15@67998
  1643
              finally show ?thesis .
lp15@67998
  1644
            qed
lp15@67998
  1645
          qed
lp15@67998
  1646
          show "?W \<in> lmeasurable"
lp15@67998
  1647
            by (simp add: fmeasurable_Int_fmeasurable borel_open)
lp15@67998
  1648
          obtain k::'m where True
lp15@67998
  1649
            by metis
lp15@67998
  1650
          obtain T where T: "orthogonal_transformation T" and v: "v = T(norm v *\<^sub>R axis k (1::real))"
lp15@67998
  1651
            using rotation_rightward_line by metis
lp15@67998
  1652
          define b where "b \<equiv> norm v"
lp15@67998
  1653
          have "b > 0"
lp15@67998
  1654
            using \<open>v \<noteq> 0\<close> by (auto simp: b_def)
lp15@67998
  1655
          obtain eqb: "inv T v = b *\<^sub>R axis k (1::real)" and "inj T" "bij T" and invT: "orthogonal_transformation (inv T)"
lp15@67998
  1656
            by (metis UNIV_I b_def  T v bij_betw_inv_into_left orthogonal_transformation_inj orthogonal_transformation_bij orthogonal_transformation_inv)
lp15@67998
  1657
          let ?v = "\<chi> i. min d r / CARD('m)"
lp15@67998
  1658
          let ?v' = "\<chi> i. if i = k then (e/2 * min d r) / CARD('m) ^ CARD('m) else min d r"
lp15@67998
  1659
          let ?x' = "inv T x"
lp15@67998
  1660
          let ?W' = "(ball ?x' (min d r) \<inter> {y. \<bar>(y - ?x')$k\<bar> < e * min d r / (2 * CARD('m) ^ CARD('m))})"
lp15@67998
  1661
          have abs: "x - e \<le> y \<and> y \<le> x + e \<longleftrightarrow> abs(y - x) \<le> e" for x y e::real
lp15@67998
  1662
            by auto
lp15@67998
  1663
          have "?W = T ` ?W'"
lp15@67998
  1664
          proof -
lp15@67998
  1665
            have 1: "T ` (ball (inv T x) (min d r)) = ball x (min d r)"
lp15@67998
  1666
              by (simp add: T image_orthogonal_transformation_ball orthogonal_transformation_surj surj_f_inv_f)
lp15@67998
  1667
            have 2: "{y. \<bar>v \<bullet> (y - x)\<bar> < b * e * min d r / (2 * real CARD('m) ^ CARD('m))} =
lp15@67998
  1668
                      T ` {y. \<bar>y $ k - ?x' $ k\<bar> < e * min d r / (2 * real CARD('m) ^ CARD('m))}"
lp15@67998
  1669
            proof -
lp15@67998
  1670
              have *: "\<bar>T (b *\<^sub>R axis k 1) \<bullet> (y - x)\<bar> = b * \<bar>inv T y $ k - ?x' $ k\<bar>" for y
lp15@67998
  1671
              proof -
lp15@67998
  1672
                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>"
lp15@67998
  1673
                  by (metis (no_types, hide_lams) b_def eqb invT orthogonal_transformation_def v)
lp15@67998
  1674
                also have "\<dots> = b * \<bar>(axis k 1) \<bullet> inv T (y - x)\<bar>"
lp15@67998
  1675
                  using \<open>b > 0\<close> by (simp add: abs_mult)
lp15@67998
  1676
                also have "\<dots> = b * \<bar>inv T y $ k - ?x' $ k\<bar>"
lp15@67998
  1677
                  using orthogonal_transformation_linear [OF invT]
lp15@67998
  1678
                  by (simp add: inner_axis' linear_diff)
lp15@67998
  1679
                finally show ?thesis
lp15@67998
  1680
                  by simp
lp15@67998
  1681
              qed
lp15@67998
  1682
              show ?thesis
lp15@67998
  1683
                using v b_def [symmetric]
lp15@67998
  1684
                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)
lp15@67998
  1685
            qed
lp15@67998
  1686
            show ?thesis
lp15@67998
  1687
              using \<open>b > 0\<close> by (simp add: image_Int \<open>inj T\<close> 1 2 b_def [symmetric])
lp15@67998
  1688
          qed
lp15@67998
  1689
          moreover have "?W' \<in> lmeasurable"
lp15@67998
  1690
            by (auto intro: fmeasurable_Int_fmeasurable)
lp15@67998
  1691
          ultimately have "measure lebesgue ?W = measure lebesgue ?W'"
lp15@67998
  1692
            by (metis measure_orthogonal_image T)
lp15@67998
  1693
          also have "\<dots> \<le> measure lebesgue (cbox (?x' - ?v') (?x' + ?v'))"
lp15@67998
  1694
          proof (rule measure_mono_fmeasurable)
lp15@67998
  1695
            show "?W' \<subseteq> cbox (?x' - ?v') (?x' + ?v')"
lp15@67998
  1696
              apply (clarsimp simp add: mem_box_cart abs dist_norm norm_minus_commute simp del: min_less_iff_conj min.bounded_iff)
lp15@67998
  1697
              by (metis component_le_norm_cart less_eq_real_def le_less_trans vector_minus_component)
lp15@67998
  1698
          qed auto
lp15@67998
  1699
          also have "\<dots> \<le> e/2 * measure lebesgue (cbox (?x' - ?v) (?x' + ?v))"
lp15@67998
  1700
          proof -
lp15@67998
  1701
            have "cbox (?x' - ?v) (?x' + ?v) \<noteq> {}"
lp15@67998
  1702
              using \<open>r > 0\<close> \<open>d > 0\<close> by (auto simp: interval_eq_empty_cart divide_less_0_iff)
lp15@67998
  1703
            with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> show ?thesis
lp15@67998
  1704
              apply (simp add: content_cbox_if_cart mem_box_cart)
lp15@67998
  1705
              apply (auto simp: prod_nonneg)
lp15@67998
  1706
              apply (simp add: abs if_distrib prod.delta_remove prod_constant field_simps power_diff split: if_split_asm)
lp15@67998
  1707
              done
lp15@67998
  1708
          qed
lp15@67998
  1709
          also have "\<dots> \<le> e/2 * measure lebesgue (cball ?x' (min d r))"
lp15@67998
  1710
          proof (rule mult_left_mono [OF measure_mono_fmeasurable])
lp15@67998
  1711
            have *: "norm (?x' - y) \<le> min d r"
lp15@67998
  1712
              if y: "\<And>i. \<bar>?x' $ i - y $ i\<bar> \<le> min d r / real CARD('m)" for y
lp15@67998
  1713
            proof -
lp15@67998
  1714
              have "norm (?x' - y) \<le> (\<Sum>i\<in>UNIV. \<bar>(?x' - y) $ i\<bar>)"
lp15@67998
  1715
                by (rule norm_le_l1_cart)
lp15@67998
  1716
              also have "\<dots> \<le> real CARD('m) * (min d r / real CARD('m))"
lp15@67998
  1717
                by (rule sum_bounded_above) (use y in auto)
lp15@67998
  1718
              finally show ?thesis
lp15@67998
  1719
                by simp
lp15@67998
  1720
            qed
lp15@67998
  1721
            show "cbox (?x' - ?v) (?x' + ?v) \<subseteq> cball ?x' (min d r)"
lp15@67998
  1722
              apply (clarsimp simp only: mem_box_cart dist_norm mem_cball intro!: *)
lp15@67998
  1723
              by (simp add: abs_diff_le_iff abs_minus_commute)
lp15@67998
  1724
          qed (use \<open>e > 0\<close> in auto)
lp15@67998
  1725
          also have "\<dots> < e * content (cball ?x' (min d r))"
lp15@67998
  1726
            using \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> by auto
lp15@67998
  1727
          also have "\<dots> = e * content (ball x (min d r))"
lp15@67998
  1728
            using \<open>r > 0\<close> \<open>d > 0\<close> by (simp add: content_cball content_ball)
lp15@67998
  1729
          finally show "measure lebesgue ?W < e * content (ball x (min d r))" .
lp15@67998
  1730
      qed
lp15@67998
  1731
    qed
lp15@67998
  1732
    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"
lp15@67998
  1733
      by blast
lp15@67998
  1734
    have MN: "?M \<subseteq> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
lp15@67998
  1735
    proof (rule *)
lp15@67998
  1736
      fix x
lp15@67998
  1737
      assume x: "x \<notin> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
lp15@67998
  1738
      show "(x \<in> ?T) \<longleftrightarrow> (x \<in> {x \<in> S. matrix (f' x) $ m $ n \<le> b})"
lp15@67998
  1739
      proof (cases "x \<in> S")
lp15@67998
  1740
        case True
lp15@67998
  1741
        then have x: "\<not> ?\<Theta> x v" if "v \<noteq> 0" for v
lp15@67998
  1742
          using x that by force
lp15@67998
  1743
        show ?thesis
lp15@67998
  1744
        proof (rule iffI; clarsimp)
lp15@67998
  1745
          assume b: "\<forall>e>0. \<exists>d>0. \<exists>A. A $ m $ n < b \<and> (\<forall>i j. A $ i $ j \<in> \<rat>) \<and>
lp15@67998
  1746
                                    (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
lp15@67998
  1747
                     (is "\<forall>e>0. \<exists>d>0. \<exists>A. ?\<Phi> e d A")
lp15@67998
  1748
          then have "\<forall>k. \<exists>d>0. \<exists>A. ?\<Phi> (1 / Suc k) d A"
lp15@67998
  1749
            by (metis (no_types, hide_lams) less_Suc_eq_0_disj of_nat_0_less_iff zero_less_divide_1_iff)
lp15@67998
  1750
          then obtain \<delta> A where \<delta>: "\<And>k. \<delta> k > 0"
lp15@67998
  1751
                           and Ab: "\<And>k. A k $ m $ n < b"
lp15@67998
  1752
                           and A: "\<And>k y. \<lbrakk>y \<in> S; norm (y - x) < \<delta> k\<rbrakk> \<Longrightarrow>
lp15@67998
  1753
                                          norm (f y - f x - A k *v (y - x)) \<le> 1/(Suc k) * norm (y - x)"
lp15@67998
  1754
            by metis
lp15@67998
  1755
          have "\<forall>i j. \<exists>a. (\<lambda>n. A n $ i $ j) \<longlonglongrightarrow> a"
lp15@67998
  1756
          proof (intro allI)
lp15@67998
  1757
            fix i j
lp15@67998
  1758
            have vax: "(A n *v axis j 1) $ i = A n $ i $ j" for n
lp15@67998
  1759
              by (metis cart_eq_inner_axis matrix_vector_mul_component)
lp15@67998
  1760
            let ?CA = "{x. Cauchy (\<lambda>n. (A n) *v x)}"
lp15@67998
  1761
            have "subspace ?CA"
lp15@67998
  1762
              unfolding subspace_def convergent_eq_Cauchy [symmetric]
lp15@67998
  1763
                by (force simp: algebra_simps intro: tendsto_intros)
lp15@67998
  1764
            then have CA_eq: "?CA = span ?CA"
immler@68072
  1765
              by (metis span_eq_iff)
lp15@67998
  1766
            also have "\<dots> = UNIV"
lp15@67998
  1767
            proof -
lp15@67998
  1768
              have "dim ?CA \<le> CARD('m)"
immler@68072
  1769
                using dim_subset_UNIV[of ?CA]
immler@68072
  1770
                by auto
lp15@67998
  1771
              moreover have "False" if less: "dim ?CA < CARD('m)"
lp15@67998
  1772
              proof -
lp15@67998
  1773
                obtain d where "d \<noteq> 0" and d: "\<And>y. y \<in> span ?CA \<Longrightarrow> orthogonal d y"
lp15@67998
  1774
                  using less by (force intro: orthogonal_to_subspace_exists [of ?CA])
lp15@67998
  1775
                with x [OF \<open>d \<noteq> 0\<close>] obtain \<xi> where "\<xi> > 0"
lp15@67998
  1776
                  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>"
lp15@67998
  1777
                  by (fastforce simp: not_le Bex_def)
lp15@67998
  1778
                obtain \<gamma> z where \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
lp15@67998
  1779
                           and \<gamma>le:   "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
lp15@67998
  1780
                           and \<gamma>x:    "\<gamma> \<longlonglongrightarrow> x"
lp15@67998
  1781
                           and z:     "(\<lambda>n. (\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x)) \<longlonglongrightarrow> z"
lp15@67998
  1782
                proof -
lp15@67998
  1783
                  have "\<exists>\<gamma>. (\<forall>i. (\<gamma> i \<in> S - {x} \<and>
lp15@67998
  1784
                                  \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar> \<and> norm(\<gamma> i - x) < 1/Suc i) \<and>
lp15@67998
  1785
                                 norm(\<gamma>(Suc i) - x) < norm(\<gamma> i - x))"
lp15@67998
  1786
                  proof (rule dependent_nat_choice)
lp15@67998
  1787
                    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"
lp15@67998
  1788
                      using \<xi> [of 1] by (auto simp: dist_norm norm_minus_commute)
lp15@67998
  1789
                  next
lp15@67998
  1790
                    fix y i
lp15@67998
  1791
                    assume "y \<in> S - {x} \<and> \<xi> * norm (y - x) \<le> \<bar>d \<bullet> (y - x)\<bar> \<and> norm (y - x) < 1/Suc i"
lp15@67998
  1792
                    then have "min (norm(y - x)) (1/((Suc i) + 1)) > 0"
lp15@67998
  1793
                      by auto
lp15@67998
  1794
                    then obtain y' where "y' \<in> S - {x}" and y': "norm (x - y') < min (norm (y - x)) (1/((Suc i) + 1))"
lp15@67998
  1795
                                         "\<xi> * norm (x - y') \<le> \<bar>d \<bullet> (y' - x)\<bar>"
lp15@67998
  1796
                      using \<xi> by metis
lp15@67998
  1797
                    with \<xi> show "\<exists>y'. (y' \<in> S - {x} \<and> \<xi> * norm (y' - x) \<le> \<bar>d \<bullet> (y' - x)\<bar> \<and>
lp15@67998
  1798
                              norm (y' - x) < 1/(Suc (Suc i))) \<and> norm (y' - x) < norm (y - x)"
lp15@67998
  1799
                      by (auto simp: dist_norm norm_minus_commute)
lp15@67998
  1800
                  qed
lp15@67998
  1801
                  then obtain \<gamma> where
lp15@67998
  1802
                        \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
lp15@67998
  1803
                        and \<gamma>le: "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
lp15@67998
  1804
                        and \<gamma>conv: "\<And>i. norm(\<gamma> i - x) < 1/(Suc i)"
lp15@67998
  1805
                    by blast
lp15@67998
  1806
                  let ?f = "\<lambda>i. (\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x)"
lp15@67998
  1807
                  have "?f i \<in> sphere 0 1" for i
lp15@67998
  1808
                    using \<gamma>Sx by auto
lp15@67998
  1809
                  then obtain l \<rho> where "l \<in> sphere 0 1" "strict_mono \<rho>" and l: "(?f \<circ> \<rho>) \<longlonglongrightarrow> l"
lp15@67998
  1810
                    using compact_sphere [of "0::(real,'m) vec" 1]  unfolding compact_def by meson
lp15@67998
  1811
                  show thesis
lp15@67998
  1812
                  proof
lp15@67998
  1813
                    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
lp15@67998
  1814
                      using \<gamma>Sx \<gamma>le by auto
lp15@67998
  1815
                    have "\<gamma> \<longlonglongrightarrow> x"
lp15@67998
  1816
                    proof (clarsimp simp add: LIMSEQ_def dist_norm)
lp15@67998
  1817
                      fix r :: "real"
lp15@67998
  1818
                      assume "r > 0"
lp15@67998
  1819
                      with real_arch_invD obtain no where "no \<noteq> 0" "real no > 1/r"
lp15@67998
  1820
                        by (metis divide_less_0_1_iff not_less_iff_gr_or_eq of_nat_0_eq_iff reals_Archimedean2)
lp15@67998
  1821
                      with \<gamma>conv show "\<exists>no. \<forall>n\<ge>no. norm (\<gamma> n - x) < r"
lp15@67998
  1822
                        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)
lp15@67998
  1823
                    qed
lp15@67998
  1824
                    with \<open>strict_mono \<rho>\<close> show "(\<gamma> \<circ> \<rho>) \<longlonglongrightarrow> x"
lp15@67998
  1825
                      by (metis LIMSEQ_subseq_LIMSEQ)
lp15@67998
  1826
                    show "(\<lambda>n. ((\<gamma> \<circ> \<rho>) n - x) /\<^sub>R norm ((\<gamma> \<circ> \<rho>) n - x)) \<longlonglongrightarrow> l"
lp15@67998
  1827
                      using l by (auto simp: o_def)
lp15@67998
  1828
                  qed
lp15@67998
  1829
                qed
lp15@67998
  1830
                have "isCont (\<lambda>x. (\<bar>d \<bullet> x\<bar> - \<xi>)) z"
lp15@67998
  1831
                  by (intro continuous_intros)
lp15@67998
  1832
                from isCont_tendsto_compose [OF this z]
lp15@67998
  1833
                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>"
lp15@67998
  1834
                  by auto
lp15@67998
  1835
                moreover have "\<forall>\<^sub>F i in sequentially. 0 \<le> \<bar>d \<bullet> ((\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x))\<bar> - \<xi>"
lp15@67998
  1836
                proof (rule eventuallyI)
lp15@67998
  1837
                  fix n
lp15@67998
  1838
                  show "0 \<le> \<bar>d \<bullet> ((\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x))\<bar> - \<xi>"
lp15@67998
  1839
                  using \<gamma>le [of n] \<gamma>Sx by (auto simp: abs_mult divide_simps)
lp15@67998
  1840
                qed
lp15@67998
  1841
                ultimately have "\<xi> \<le> \<bar>d \<bullet> z\<bar>"
lp15@67998
  1842
                  using tendsto_lowerbound [where a=0] by fastforce
lp15@67998
  1843
                have "Cauchy (\<lambda>n. (A n) *v z)"
lp15@67998
  1844
                proof (clarsimp simp add: Cauchy_def)
lp15@67998
  1845
                  fix \<epsilon> :: "real"
lp15@67998
  1846
                  assume "0 < \<epsilon>"
lp15@67998
  1847
                  then obtain N::nat where "N > 0" and N: "\<epsilon>/2 > 1/N"
lp15@67998
  1848
                    by (metis half_gt_zero inverse_eq_divide neq0_conv real_arch_inverse)
lp15@67998
  1849
                  show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (A m *v z) (A n *v z) < \<epsilon>"
lp15@67998
  1850
                  proof (intro exI allI impI)
lp15@67998
  1851
                    fix i j
lp15@67998
  1852
                    assume ij: "N \<le> i" "N \<le> j"
lp15@67998
  1853
                    let ?V = "\<lambda>i k. A i *v ((\<gamma> k - x) /\<^sub>R norm (\<gamma> k - x))"
lp15@67998
  1854
                    have "\<forall>\<^sub>F k in sequentially. dist (\<gamma> k) x < min (\<delta> i) (\<delta> j)"
lp15@67998
  1855
                      using \<gamma>x [unfolded tendsto_iff] by (meson min_less_iff_conj \<delta>)
lp15@67998
  1856
                    then have even: "\<forall>\<^sub>F k in sequentially. norm (?V i k - ?V j k) - 2 / N \<le> 0"
lp15@67998
  1857
                    proof (rule eventually_mono, clarsimp)
lp15@67998
  1858
                      fix p
lp15@67998
  1859
                      assume p: "dist (\<gamma> p) x < \<delta> i" "dist (\<gamma> p) x < \<delta> j"
lp15@67998
  1860
                      let ?C = "\<lambda>k. f (\<gamma> p) - f x - A k *v (\<gamma> p - x)"
lp15@67998
  1861
                      have "norm ((A i - A j) *v (\<gamma> p - x)) = norm (?C j - ?C i)"
lp15@67998
  1862
                        by (simp add: algebra_simps)
lp15@67998
  1863
                      also have "\<dots> \<le> norm (?C j) + norm (?C i)"
lp15@67998
  1864
                        using norm_triangle_ineq4 by blast
lp15@67998
  1865
                      also have "\<dots> \<le> 1/(Suc j) * norm (\<gamma> p - x) + 1/(Suc i) * norm (\<gamma> p - x)"
lp15@67998
  1866
                        by (metis A Diff_iff \<gamma>Sx dist_norm p add_mono)
lp15@67998
  1867
                      also have "\<dots> \<le> 1/N * norm (\<gamma> p - x) + 1/N * norm (\<gamma> p - x)"
lp15@67998
  1868
                        apply (intro add_mono mult_right_mono)
lp15@67998
  1869
                        using ij \<open>N > 0\<close> by (auto simp: field_simps)
lp15@67998
  1870
                      also have "\<dots> = 2 / N * norm (\<gamma> p - x)"
lp15@67998
  1871
                        by simp
lp15@67998
  1872
                      finally have no_le: "norm ((A i - A j) *v (\<gamma> p - x)) \<le> 2 / N * norm (\<gamma> p - x)" .
lp15@67998
  1873
                      have "norm (?V i p - ?V j p) =
lp15@67998
  1874
                            norm ((A i - A j) *v ((\<gamma> p - x) /\<^sub>R norm (\<gamma> p - x)))"
lp15@67998
  1875
                        by (simp add: algebra_simps)
lp15@67998
  1876
                      also have "\<dots> = norm ((A i - A j) *v (\<gamma> p - x)) / norm (\<gamma> p - x)"
lp15@67998
  1877
                        by (simp add: divide_inverse matrix_vector_mult_scaleR)
lp15@67998
  1878
                      also have "\<dots> \<le> 2 / N"
lp15@67998
  1879
                        using no_le by (auto simp: divide_simps)
lp15@67998
  1880
                      finally show "norm (?V i p - ?V j p) \<le> 2 / N" .
lp15@67998
  1881
                    qed
lp15@67998
  1882
                    have "isCont (\<lambda>w. (norm(A i *v w - A j *v w) - 2 / N)) z"
lp15@67998
  1883
                      by (intro continuous_intros)
lp15@67998
  1884
                    from isCont_tendsto_compose [OF this z]
lp15@67998
  1885
                    have lim: "(\<lambda>w. norm (A i *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x)) -
lp15@67998
  1886
                                    A j *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x))) - 2 / N)
lp15@67998
  1887
                               \<longlonglongrightarrow> norm (A i *v z - A j *v z) - 2 / N"
lp15@67998
  1888
                      by auto
lp15@67998
  1889
                    have "dist (A i *v z) (A j *v z) \<le> 2 / N"
lp15@67998
  1890
                      using tendsto_upperbound [OF lim even] by (auto simp: dist_norm)
lp15@67998
  1891
                    with N show "dist (A i *v z) (A j *v z) < \<epsilon>"
lp15@67998
  1892
                      by linarith
lp15@67998
  1893
                  qed
lp15@67998
  1894
                qed
lp15@67998
  1895
                then have "d \<bullet> z = 0"
lp15@67998
  1896
                  using CA_eq d orthogonal_def by auto
lp15@67998
  1897
                then show False
lp15@67998
  1898
                  using \<open>0 < \<xi>\<close> \<open>\<xi> \<le> \<bar>d \<bullet> z\<bar>\<close> by auto
lp15@67998
  1899
              qed
lp15@67998
  1900
              ultimately show ?thesis
lp15@67998
  1901
                using dim_eq_full by fastforce
lp15@67998
  1902
            qed
lp15@67998
  1903
            finally have "?CA = UNIV" .
lp15@67998
  1904
            then have "Cauchy (\<lambda>n. (A n) *v axis j 1)"
lp15@67998
  1905
              by auto
lp15@67998
  1906
            then obtain L where "(\<lambda>n. A n *v axis j 1) \<longlonglongrightarrow> L"
lp15@67998
  1907
              by (auto simp: Cauchy_convergent_iff convergent_def)
lp15@67998
  1908
            then have "(\<lambda>x. (A x *v axis j 1) $ i) \<longlonglongrightarrow> L $ i"
lp15@67998
  1909
              by (rule tendsto_vec_nth)
lp15@67998
  1910
            then show "\<exists>a. (\<lambda>n. A n $ i $ j) \<longlonglongrightarrow> a"
lp15@67998
  1911
              by (force simp: vax)
lp15@67998
  1912
          qed
lp15@67998
  1913
          then obtain B where B: "\<And>i j. (\<lambda>n. A n $ i $ j) \<longlonglongrightarrow> B $ i $ j"
lp15@67998
  1914
            by (auto simp: lambda_skolem)
lp15@67998
  1915
          have lin_df: "linear (f' x)"
lp15@67998
  1916
               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)"
lp15@67998
  1917
            using \<open>x \<in> S\<close> assms by (auto simp: has_derivative_within linear_linear)
immler@68072
  1918
          moreover
immler@68072
  1919
          interpret linear "f' x" by fact
immler@68072
  1920
          have "(matrix (f' x) - B) *v w = 0" for w
nipkow@69064
  1921
          proof (rule lemma_partial_derivatives [of "(*v) (matrix (f' x) - B)"])
nipkow@69064
  1922
            show "linear ((*v) (matrix (f' x) - B))"
lp15@67998
  1923
              by (rule matrix_vector_mul_linear)
lp15@67998
  1924
            have "((\<lambda>y. ((f x + f' x (y - x)) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
lp15@67998
  1925
              using tendsto_minus [OF lim_df] by (simp add: algebra_simps divide_simps)
lp15@67998
  1926
            then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
lp15@67998
  1927
            proof (rule Lim_transform)
lp15@67998
  1928
              have "((\<lambda>y. ((f y + B *v x - (f x + B *v y)) /\<^sub>R norm (y - x))) \<longlongrightarrow> 0) (at x within S)"
lp15@67998
  1929
              proof (clarsimp simp add: Lim_within dist_norm)
lp15@67998
  1930
                fix e :: "real"
lp15@67998
  1931
                assume "e > 0"
lp15@67998
  1932
                then obtain q::nat where "q \<noteq> 0" and qe2: "1/q < e/2"
lp15@67998
  1933
                  by (metis divide_pos_pos inverse_eq_divide real_arch_inverse zero_less_numeral)
lp15@67998
  1934
                let ?g = "\<lambda>p. sum  (\<lambda>i. sum (\<lambda>j. abs((A p - B)$i$j)) UNIV) UNIV"
lp15@67998
  1935
                have "(\<lambda>k. onorm (\<lambda>y. (A k - B) *v y)) \<longlonglongrightarrow> 0"
lp15@67998
  1936
                proof (rule Lim_null_comparison)
lp15@67998
  1937
                  show "\<forall>\<^sub>F k in sequentially. norm (onorm (\<lambda>y. (A k - B) *v y)) \<le> ?g k"
lp15@67998
  1938
                  proof (rule eventually_sequentiallyI)
lp15@67998
  1939
                    fix k :: "nat"
lp15@67998
  1940
                    assume "0 \<le> k"
nipkow@69064
  1941
                    have "0 \<le> onorm ((*v) (A k - B))"
immler@68072
  1942
                      using matrix_vector_mul_bounded_linear
immler@68072
  1943
                      by (rule onorm_pos_le)
nipkow@69064
  1944
                    then show "norm (onorm ((*v) (A k - B))) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>(A k - B) $ i $ j\<bar>)"
lp15@67998
  1945
                      by (simp add: onorm_le_matrix_component_sum del: vector_minus_component)
lp15@67998
  1946
                  qed
lp15@67998
  1947
                next
lp15@67998
  1948
                  show "?g \<longlonglongrightarrow> 0"
lp15@67998
  1949
                    using B Lim_null tendsto_rabs_zero_iff by (fastforce intro!: tendsto_null_sum)
lp15@67998
  1950
                qed
nipkow@69064
  1951
                with \<open>e > 0\<close> obtain p where "\<And>n. n \<ge> p \<Longrightarrow> \<bar>onorm ((*v) (A n - B))\<bar> < e/2"
lp15@67998
  1952
                  unfolding lim_sequentially by (metis diff_zero dist_real_def divide_pos_pos zero_less_numeral)
nipkow@69064
  1953
                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`]*)
lp15@67998
  1954
                  using le_add1 by blast
lp15@67998
  1955
                show "\<exists>d>0. \<forall>y\<in>S. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow>
lp15@67998
  1956
                           inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
lp15@67998
  1957
                proof (intro exI, safe)
lp15@67998
  1958
                  show "0 < \<delta>(p + q)"
lp15@67998
  1959
                    by (simp add: \<delta>)
lp15@67998
  1960
                next
lp15@67998
  1961
                  fix y
lp15@67998
  1962
                  assume y: "y \<in> S" "norm (y - x) < \<delta>(p + q)" and "y \<noteq> x"
lp15@67998
  1963
                  have *: "\<lbrakk>norm(b - c) < e - d; norm(y - x - b) \<le> d\<rbrakk> \<Longrightarrow> norm(y - x - c) < e"
lp15@67998
  1964
                    for b c d e x and y:: "real^'n"
lp15@67998
  1965
                    using norm_triangle_ineq2 [of "y - x - c" "y - x - b"] by simp
lp15@67998
  1966
                  have "norm (f y - f x - B *v (y - x)) < e * norm (y - x)"
lp15@67998
  1967
                  proof (rule *)
lp15@67998
  1968
                    show "norm (f y - f x - A (p + q) *v (y - x)) \<le> norm (y - x) / (Suc (p + q))"
lp15@67998
  1969
                      using A [OF y] by simp
lp15@67998
  1970
                    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)"
lp15@67998
  1971
                      by (metis linear_linear matrix_vector_mul_linear matrix_vector_mult_diff_rdistrib onorm)
lp15@67998
  1972
                    also have "\<dots> < (e/2) * norm (y - x)"
lp15@67998
  1973
                      using \<open>y \<noteq> x\<close> pqe2 by auto
lp15@67998
  1974
                    also have "\<dots> \<le> (e - 1 / (Suc (p + q))) * norm (y - x)"
lp15@67998
  1975
                    proof (rule mult_right_mono)
lp15@67998
  1976
                      have "1 / Suc (p + q) \<le> 1 / q"
lp15@67998
  1977
                        using \<open>q \<noteq> 0\<close> by (auto simp: divide_simps)
lp15@67998
  1978
                      also have "\<dots> < e/2"
lp15@67998
  1979
                        using qe2 by auto
lp15@67998
  1980
                      finally show "e / 2 \<le> e - 1 / real (Suc (p + q))"
lp15@67998
  1981
                        by linarith
lp15@67998
  1982
                    qed auto
lp15@67998
  1983
                    finally show "norm (A (p + q) *v (y - x) - B *v (y - x)) < e * norm (y - x) - norm (y - x) / real (Suc (p + q))"
lp15@67998
  1984
                      by (simp add: algebra_simps)
lp15@67998
  1985
                  qed
lp15@67998
  1986
                  then show "inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
lp15@67998
  1987
                    using \<open>y \<noteq> x\<close> by (simp add: divide_simps algebra_simps)
lp15@67998
  1988
                qed
lp15@67998
  1989
              qed
lp15@67998
  1990
              then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R
lp15@67998
  1991
                           norm (y - x) - (f x + f' x (y - x) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0)
lp15@67998
  1992
                          (at x within S)"
immler@68072
  1993
                by (simp add: algebra_simps diff lin_df matrix_vector_mul_linear scalar_mult_eq_scaleR)
lp15@67998
  1994
            qed
lp15@67998
  1995
          qed (use x in \<open>simp; auto simp: not_less\<close>)
nipkow@69064
  1996
          ultimately have "f' x = (*v) B"
immler@68072
  1997
            by (force simp: algebra_simps scalar_mult_eq_scaleR)
lp15@67998
  1998
          show "matrix (f' x) $ m $ n \<le> b"
lp15@67998
  1999
          proof (rule tendsto_upperbound [of "\<lambda>i. (A i $ m $ n)" _ sequentially])
lp15@67998
  2000
            show "(\<lambda>i. A i $ m $ n) \<longlonglongrightarrow> matrix (f' x) $ m $ n"
nipkow@69064
  2001
              by (simp add: B \<open>f' x = (*v) B\<close>)
lp15@67998
  2002
            show "\<forall>\<^sub>F i in sequentially. A i $ m $ n \<le> b"
lp15@67998
  2003
              by (simp add: Ab less_eq_real_def)
lp15@67998
  2004
          qed auto
lp15@67998
  2005
        next
lp15@67998
  2006
          fix e :: "real"
lp15@67998
  2007
          assume "x \<in> S" and b: "matrix (f' x) $ m $ n \<le> b" and "e > 0"
lp15@67998
  2008
          then obtain d where "d>0"
lp15@67998
  2009
            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))
lp15@67998
  2010
                  < e/2"
lp15@67998
  2011
            using f [OF \<open>x \<in> S\<close>] unfolding Deriv.has_derivative_at_within Lim_within
lp15@67998
  2012
            by (auto simp: field_simps dest: spec [of _ "e/2"])
lp15@67998
  2013
          let ?A = "matrix(f' x) - (\<chi> i j. if i = m \<and> j = n then e / 4 else 0)"
nipkow@69064
  2014
          obtain B where BRats: "\<And>i j. B$i$j \<in> \<rat>" and Bo_e6: "onorm((*v) (?A - B)) < e/6"
lp15@67998
  2015
            using matrix_rational_approximation \<open>e > 0\<close>
lp15@67998
  2016
            by (metis zero_less_divide_iff zero_less_numeral)
lp15@67998
  2017
          show "\<exists>d>0. \<exists>A. A $ m $ n < b \<and> (\<forall>i j. A $ i $ j \<in> \<rat>) \<and>
lp15@67998
  2018
                (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
lp15@67998
  2019
          proof (intro exI conjI ballI allI impI)
lp15@67998
  2020
            show "d>0"
lp15@67998
  2021
              by (rule \<open>d>0\<close>)
lp15@67998
  2022
            show "B $ m $ n < b"
lp15@67998
  2023
            proof -
nipkow@69064
  2024
              have "\<bar>matrix ((*v) (?A - B)) $ m $ n\<bar> \<le> onorm ((*v) (?A - B))"
lp15@67998
  2025
                using component_le_onorm [OF matrix_vector_mul_linear, of _ m n] by metis
lp15@67998
  2026
              then show ?thesis
lp15@67998
  2027
                using b Bo_e6 by simp
lp15@67998
  2028
            qed
lp15@67998
  2029
            show "B $ i $ j \<in> \<rat>" for i j
lp15@67998
  2030
              using BRats by auto
lp15@67998
  2031
            show "norm (f y - f x - B *v (y - x)) \<le> e * norm (y - x)"
lp15@67998
  2032
              if "y \<in> S" and y: "norm (y - x) < d" for y
lp15@67998
  2033
            proof (cases "y = x")
lp15@67998
  2034
              case True then show ?thesis
lp15@67998
  2035
                by simp
lp15@67998
  2036
            next
lp15@67998
  2037
              case False
lp15@67998
  2038
              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"
lp15@67998
  2039
                using norm_triangle_le [of "d' - d" "y - (x + d')"] by simp
lp15@67998
  2040
              show ?thesis
lp15@67998
  2041
              proof (rule *)
lp15@67998
  2042
                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"
lp15@67998
  2043
                  using norm_triangle_le [of y "x-y" "e/2"] \<open>e > 0\<close> by simp
lp15@67998
  2044
                have "linear (f' x)"
lp15@67998
  2045
                  using True f has_derivative_linear by blast
lp15@67998
  2046
                then have "norm (f' x (y - x) - B *v (y - x)) = norm ((matrix (f' x) - B) *v (y - x))"
immler@68072
  2047
                  by (simp add: matrix_vector_mult_diff_rdistrib)
lp15@67998
  2048
                also have "\<dots> \<le> (e * norm (y - x)) / 2"
lp15@67998
  2049
                proof (rule split246)
lp15@67998
  2050
                  have "norm ((?A - B) *v (y - x)) / norm (y - x) \<le> onorm(\<lambda>x. (?A - B) *v x)"
immler@68072
  2051
                    by (rule le_onorm) auto
lp15@67998
  2052
                  also have  "\<dots> < e/6"
lp15@67998
  2053
                    by (rule Bo_e6)
lp15@67998
  2054
                  finally have "norm ((?A - B) *v (y - x)) / norm (y - x) < e / 6" .
lp15@67998
  2055
                  then show "norm ((?A - B) *v (y - x)) \<le> e * norm (y - x) / 6"
lp15@67998
  2056
                    by (simp add: divide_simps False)
lp15@67998
  2057
                  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))"
lp15@67998
  2058
                    by (simp add: algebra_simps)
lp15@67998
  2059
                  also have "\<dots> = norm((e/4) *\<^sub>R (y - x)$n *\<^sub>R axis m (1::real))"
lp15@67998
  2060
                  proof -
lp15@67998
  2061
                    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
lp15@67998
  2062
                    proof (cases "i=m")
lp15@67998
  2063
                      case True then show ?thesis
lp15@67998
  2064
                        by (auto simp: if_distrib [of "\<lambda>z. z * _"] cong: if_cong)
lp15@67998
  2065
                    next
lp15@67998
  2066
                      case False then show ?thesis
lp15@67998
  2067
                        by (simp add: axis_def)
lp15@67998
  2068
                    qed
lp15@67998
  2069
                    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)"
lp15@67998
  2070
                      by (auto simp: vec_eq_iff matrix_vector_mult_def)
lp15@67998
  2071
                    then show ?thesis
lp15@67998
  2072
                      by metis
lp15@67998
  2073
                  qed
lp15@67998
  2074
                  also have "\<dots> \<le> e * norm (y - x) / 4"
lp15@67998
  2075
                    using \<open>e > 0\<close> apply (simp add: norm_mult abs_mult)
lp15@67998
  2076
                    by (metis component_le_norm_cart vector_minus_component)
lp15@67998
  2077
                  finally show "norm ((matrix (f' x) - B) *v (y - x) - ((?A - B) *v (y - x))) \<le> e * norm (y - x) / 4" .
lp15@67998
  2078
                  show "0 < e * norm (y - x)"
lp15@67998
  2079
                    by (simp add: False \<open>e > 0\<close>)
lp15@67998
  2080
                qed
lp15@67998
  2081
                finally show "norm (f' x (y - x) - B *v (y - x)) \<le> (e * norm (y - x)) / 2" .
lp15@67998
  2082
                show "norm (f y - (f x + f' x (y - x))) < (e * norm (y - x)) / 2"
lp15@67998
  2083
                  using False d [OF \<open>y \<in> S\<close>] y by (simp add: dist_norm field_simps)
lp15@67998
  2084
              qed
lp15@67998
  2085
            qed
lp15@67998
  2086
          qed
lp15@67998
  2087
        qed
lp15@67998
  2088
      qed auto
lp15@67998
  2089
    qed
lp15@67998
  2090
    show "negligible ?M"
lp15@67998
  2091
      using negligible_subset [OF nN MN] .
lp15@67998
  2092
  qed
lp15@67998
  2093
  then show ?thesis
lp15@67998
  2094
    by (simp add: borel_measurable_vimage_halfspace_component_le sets_restrict_space_iff assms)
lp15@67998
  2095
qed
lp15@67998
  2096
lp15@67998
  2097
ak2110@69720
  2098
theorem borel_measurable_det_Jacobian:
lp15@67998
  2099
 fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
lp15@67998
  2100
  assumes S: "S \<in> sets lebesgue" and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
  2101
  shows "(\<lambda>x. det(matrix(f' x))) \<in> borel_measurable (lebesgue_on S)"
lp15@67998
  2102
  unfolding det_def
ak2110@68833
  2103
  by%unimportant (intro measurable) (auto intro: f borel_measurable_partial_derivatives [OF S])
lp15@67998
  2104
lp15@68001
  2105
text\<open>The localisation wrt S uses the same argument for many similar results.\<close>
lp15@68001
  2106
(*See HOL Light's MEASURABLE_ON_LEBESGUE_MEASURABLE_PREIMAGE_BOREL, etc.*)
ak2110@69720
  2107
theorem borel_measurable_lebesgue_on_preimage_borel:
lp15@67998
  2108
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
lp15@67998
  2109
  assumes "S \<in> sets lebesgue"
lp15@67998
  2110
  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
lp15@67998
  2111
         (\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
ak2110@69720
  2112
proof -
lp15@67998
  2113
  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"
lp15@67998
  2114
         if "T \<in> sets borel" for T
lp15@67998
  2115
    proof (cases "0 \<in> T")
lp15@67998
  2116
      case True
lp15@67998
  2117
      then have "{x \<in> S. f x \<in> T} = {x. (if x \<in> S then f x else 0) \<in> T} \<inter> S"
lp15@67998
  2118
                "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T} \<union> -S"
lp15@67998
  2119
        by auto
lp15@67998
  2120
      then show ?thesis
lp15@67998
  2121
        by (metis (no_types, lifting) Compl_in_sets_lebesgue assms sets.Int sets.Un)
lp15@67998
  2122
    next
lp15@67998
  2123
      case False
lp15@67998
  2124
      then have "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T}"
lp15@67998
  2125
        by auto
lp15@67998
  2126
      then show ?thesis
lp15@67998
  2127
        by auto
lp15@67998
  2128
    qed
lp15@67998
  2129
    then show ?thesis
lp15@67998
  2130
      unfolding borel_measurable_lebesgue_preimage_borel borel_measurable_UNIV [OF assms, symmetric]
lp15@67998
  2131
      by blast
lp15@67998
  2132
qed
lp15@67998
  2133
ak2110@69720
  2134
lemma sets_lebesgue_almost_borel:
lp15@67998
  2135
  assumes "S \<in> sets lebesgue"
lp15@67998
  2136
  obtains B N where "B \<in> sets borel" "negligible N" "B \<union> N = S"
lp15@67998
  2137
proof -
lp15@67998
  2138
  obtain T N N' where "S = T \<union> N" "N \<subseteq> N'" "N' \<in> null_sets lborel" "T \<in> sets borel"
lp15@67998
  2139
    using sets_completionE [OF assms] by auto
lp15@67998
  2140
  then show thesis
lp15@67998
  2141
    by (metis negligible_iff_null_sets negligible_subset null_sets_completionI that)
lp15@67998
  2142
qed
lp15@67998
  2143
ak2110@69720
  2144
lemma double_lebesgue_sets:
lp15@67998
  2145
 assumes S: "S \<in> sets lebesgue" and T: "T \<in> sets lebesgue" and fim: "f ` S \<subseteq> T"
lp15@67998
  2146
 shows "(\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue) \<longleftrightarrow>
lp15@67998
  2147
          f \<in> borel_measurable (lebesgue_on S) \<and>
lp15@67998
  2148
          (\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue)"
lp15@67998
  2149
         (is "?lhs \<longleftrightarrow> _ \<and> ?rhs")
lp15@67998
  2150
  unfolding borel_measurable_lebesgue_on_preimage_borel [OF S]
lp15@67998
  2151
proof (intro iffI allI conjI impI, safe)
lp15@67998
  2152
  fix V :: "'b set"
lp15@67998
  2153
  assume *: "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
lp15@67998
  2154
    and "V \<in> sets borel"
lp15@67998
  2155
  then have V: "V \<in> sets lebesgue"
lp15@67998
  2156
    by simp
lp15@67998
  2157
  have "{x \<in> S. f x \<in> V} = {x \<in> S. f x \<in> T \<inter> V}"
lp15@67998
  2158
    using fim by blast
lp15@67998
  2159
  also have "{x \<in> S. f x \<in> T \<inter> V} \<in> sets lebesgue"
lp15@67998
  2160
    using T V * le_inf_iff by blast
lp15@67998
  2161
  finally show "{x \<in> S. f x \<in> V} \<in> sets lebesgue" .
lp15@67998
  2162
next
lp15@67998
  2163
  fix U :: "'b set"
lp15@67998
  2164
  assume "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
lp15@67998
  2165
         "negligible U" "U \<subseteq> T"
lp15@67998
  2166
  then show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
lp15@67998
  2167
    using negligible_imp_sets by blast
lp15@67998
  2168
next
lp15@67998
  2169
  fix U :: "'b set"
lp15@67998
  2170
  assume 1 [rule_format]: "(\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
lp15@67998
  2171
     and 2 [rule_format]: "\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
lp15@67998
  2172
     and "U \<in> sets lebesgue" "U \<subseteq> T"
lp15@67998
  2173
  then obtain C N where C: "C \<in> sets borel \<and> negligible N \<and> C \<union> N = U"
lp15@67998
  2174
    using sets_lebesgue_almost_borel
lp15@67998
  2175
    by metis
lp15@67998
  2176
  then have "{x \<in> S. f x \<in> C} \<in> sets lebesgue"
lp15@67998
  2177
    by (blast intro: 1)
lp15@67998
  2178
  moreover have "{x \<in> S. f x \<in> N} \<in> sets lebesgue"
lp15@67998
  2179
    using C \<open>U \<subseteq> T\<close> by (blast intro: 2)
lp15@67998
  2180
  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}"
lp15@67998
  2181
    by auto
lp15@67998
  2182
  ultimately show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
lp15@67998
  2183
    using C by auto
lp15@67998
  2184
qed
lp15@67998
  2185
lp15@67998
  2186
immler@69683
  2187
subsection\<open>Simplest case of Sard's theorem (we don't need continuity of derivative)\<close>
lp15@67998
  2188
ak2110@69720
  2189
lemma Sard_lemma00:
lp15@67998
  2190
  fixes P :: "'b::euclidean_space set"
lp15@67998
  2191
  assumes "a \<ge> 0" and a: "a *\<^sub>R i \<noteq> 0" and i: "i \<in> Basis"
lp15@67998
  2192
    and P: "P \<subseteq> {x. a *\<^sub>R i \<bullet> x = 0}"
lp15@67998
  2193
    and "0 \<le> m" "0 \<le> e"
lp15@67998
  2194
 obtains S where "S \<in> lmeasurable"
lp15@67998
  2195
            and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
lp15@67998
  2196
            and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (DIM('b) - 1)"
ak2110@69720
  2197
proof -
lp15@67998
  2198
  have "a > 0"
lp15@67998
  2199
    using assms by simp
lp15@67998
  2200
  let ?v = "(\<Sum>j\<in>Basis. (if j = i then e else m) *\<^sub>R j)"
lp15@67998
  2201
  show thesis
lp15@67998
  2202
  proof
lp15@67998
  2203
    have "- e \<le> x \<bullet> i" "x \<bullet> i \<le> e"
lp15@67998
  2204
      if "t \<in> P" "norm (x - t) \<le> e" for x t
lp15@67998
  2205
      using \<open>a > 0\<close> that Basis_le_norm [of i "x-t"] P i
lp15@67998
  2206
      by (auto simp: inner_commute algebra_simps)
lp15@67998
  2207
    moreover have "- m \<le> x \<bullet> j" "x \<bullet> j \<le> m"
lp15@67998
  2208
      if "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e" "j \<in> Basis" and "j \<noteq> i"
lp15@67998
  2209
      for x t j
lp15@67998
  2210
      using that Basis_le_norm [of j x] by auto
lp15@67998
  2211
    ultimately
lp15@67998
  2212
    show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> cbox (-?v) ?v"
lp15@67998
  2213
      by (auto simp: mem_box)
lp15@67998
  2214
    have *: "\<forall>k\<in>Basis. - ?v \<bullet> k \<le> ?v \<bullet> k"
lp15@67998
  2215
      using \<open>0 \<le> m\<close> \<open>0 \<le> e\<close> by (auto simp: inner_Basis)
lp15@67998
  2216
    have 2: "2 ^ DIM('b) = 2 * 2 ^ (DIM('b) - Suc 0)"
lp15@67998
  2217
      by (metis DIM_positive Suc_pred power_Suc)
lp15@67998
  2218
    show "measure lebesgue (cbox (-?v) ?v) \<le> 2 * e * (2 * m) ^ (DIM('b) - 1)"
lp15@67998
  2219
      using \<open>i \<in> Basis\<close>
lp15@67998
  2220
      by (simp add: content_cbox [OF *] prod.distrib prod.If_cases Diff_eq [symmetric] 2)
lp15@67998
  2221
  qed blast
lp15@67998
  2222
qed
lp15@67998
  2223
lp15@68001
  2224
text\<open>As above, but reorienting the vector (HOL Light's @text{GEOM\_BASIS\_MULTIPLE\_TAC})\<close>
ak2110@69720
  2225
lemma Sard_lemma0:
lp15@67998
  2226
  fixes P :: "(real^'n::{finite,wellorder}) set"
lp15@67998
  2227
  assumes "a \<noteq> 0"
lp15@67998
  2228
    and P: "P \<subseteq> {x. a \<bullet> x = 0}" and "0 \<le> m" "0 \<le> e"
lp15@67998
  2229
  obtains S where "S \<in> lmeasurable"
lp15@67998
  2230
    and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
lp15@67998
  2231
    and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
lp15@67998
  2232
proof -
lp15@67998
  2233
  obtain T and k::'n where T: "orthogonal_transformation T" and a: "a = T (norm a *\<^sub>R axis k (1::real))"
lp15@67998
  2234
    using rotation_rightward_line by metis
lp15@67998
  2235
  have Tinv [simp]: "T (inv T x) = x" for x
lp15@67998
  2236
    by (simp add: T orthogonal_transformation_surj surj_f_inv_f)
lp15@67998
  2237
  obtain S where S: "S \<in> lmeasurable"
lp15@67998
  2238
    and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> T-`P. norm(z - t) \<le> e)} \<subseteq> S"
lp15@67998
  2239
    and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
lp15@67998
  2240
  proof (rule Sard_lemma00 [of "norm a" "axis k (1::real)" "T-`P" m e])
lp15@67998
  2241
    have "norm a *\<^sub>R axis k 1 \<bullet> x = 0" if "T x \<in> P" for x
lp15@67998
  2242
    proof -
lp15@67998
  2243
      have "a \<bullet> T x = 0"
lp15@67998
  2244
        using P that by blast
lp15@67998
  2245
      then show ?thesis
lp15@67998
  2246
        by (metis (no_types, lifting) T a orthogonal_orthogonal_transformation orthogonal_def)
lp15@67998
  2247
    qed
lp15@67998
  2248
    then show "T -` P \<subseteq> {x. norm a *\<^sub>R axis k 1 \<bullet> x = 0}"
lp15@67998
  2249
      by auto
lp15@67998
  2250
  qed (use assms T in auto)
lp15@67998
  2251
  show thesis
lp15@67998
  2252
  proof
lp15@67998
  2253
    show "T ` S \<in> lmeasurable"
lp15@67998
  2254
      using S measurable_orthogonal_image T by blast
lp15@67998
  2255
    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)}"
lp15@67998
  2256
    proof clarsimp
lp15@67998
  2257
      fix x t
lp15@67998
  2258
      assume "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e"
lp15@67998
  2259
      then have "norm (inv T x) \<le> m"
lp15@67998
  2260
        using orthogonal_transformation_inv [OF T] by (simp add: orthogonal_transformation_norm)
lp15@67998
  2261
      moreover have "\<exists>t\<in>T -` P. norm (inv T x - t) \<le> e"
lp15@67998
  2262
      proof
lp15@67998
  2263
        have "T (inv T x - inv T t) = x - t"
immler@68072
  2264
          using T linear_diff orthogonal_transformation_def
immler@68072
  2265
          by (metis (no_types, hide_lams) Tinv)
lp15@67998
  2266
        then have "norm (inv T x - inv T t) = norm (x - t)"
lp15@67998
  2267
          by (metis T orthogonal_transformation_norm)
lp15@67998
  2268
        then show "norm (inv T x - inv T t) \<le> e"
lp15@67998
  2269
          using \<open>norm (x - t) \<le> e\<close> by linarith
lp15@67998
  2270
       next
lp15@67998
  2271
         show "inv T t \<in> T -` P"
lp15@67998
  2272
           using \<open>t \<in> P\<close> by force
lp15@67998
  2273
      qed
lp15@67998
  2274
      ultimately show "x \<in> T ` {z. norm z \<le> m \<and> (\<exists>t\<in>T -` P. norm (z - t) \<le> e)}"
lp15@67998
  2275
        by force
lp15@67998
  2276
    qed
lp15@67998
  2277
    then show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> T ` S"
lp15@67998
  2278
      using image_mono [OF subS] by (rule order_trans)
lp15@67998
  2279
    show "measure lebesgue (T ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
lp15@67998
  2280
      using mS T by (simp add: S measure_orthogonal_image)
lp15@67998
  2281
  qed
lp15@67998
  2282
qed
lp15@67998
  2283
lp15@68001
  2284
text\<open>As above, but translating the sets (HOL Light's @text{GEN\_GEOM\_ORIGIN\_TAC})\<close>
ak2110@69720
  2285
lemma Sard_lemma1:
lp15@67998
  2286
  fixes P :: "(real^'n::{finite,wellorder}) set"
lp15@67998
  2287
   assumes P: "dim P < CARD('n)" and "0 \<le> m" "0 \<le> e"
lp15@67998
  2288
 obtains S where "S \<in> lmeasurable"
lp15@67998
  2289
            and "{z. norm(z - w) \<le> m \<and> (\<exists>t \<in> P. norm(z - w - t) \<le> e)} \<subseteq> S"
lp15@67998
  2290
            and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
ak2110@69720
  2291
proof -
lp15@67998
  2292
  obtain a where "a \<noteq> 0" "P \<subseteq> {x. a \<bullet> x = 0}"
immler@68072
  2293
    using lowdim_subset_hyperplane [of P] P span_base by auto
lp15@67998
  2294
  then obtain S where S: "S \<in> lmeasurable"
lp15@67998
  2295
    and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
lp15@67998
  2296
    and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
lp15@67998
  2297
    by (rule Sard_lemma0 [OF _ _ \<open>0 \<le> m\<close> \<open>0 \<le> e\<close>])
lp15@67998
  2298
  show thesis
lp15@67998
  2299
  proof
lp15@67998
  2300
    show "(+)w ` S \<in> lmeasurable"
lp15@67998
  2301
      by (metis measurable_translation S)
lp15@67998
  2302
    show "{z. norm (z - w) \<le> m \<and> (\<exists>t\<in>P. norm (z - w - t) \<le> e)} \<subseteq> (+)w ` S"
lp15@67998
  2303
      using subS by force
lp15@67998
  2304
    show "measure lebesgue ((+)w ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
lp15@67998
  2305
      by (metis measure_translation mS)
lp15@67998
  2306
  qed
lp15@67998
  2307
qed
lp15@67998
  2308
ak2110@69720
  2309
lemma Sard_lemma2:
lp15@67998
  2310
  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n::{finite,wellorder}"
lp15@67998
  2311
  assumes mlen: "CARD('m) \<le> CARD('n)" (is "?m \<le> ?n")
lp15@67998
  2312
    and "B > 0" "bounded S"
lp15@67998
  2313
    and derS: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
lp15@67998
  2314
    and rank: "\<And>x. x \<in> S \<Longrightarrow> rank(matrix(f' x)) < CARD('n)"
lp15@67998
  2315
    and B: "\<And>x. x \<in> S \<Longrightarrow> onorm(f' x) \<le> B"
lp15@67998
  2316
  shows "negligible(f ` S)"
ak2110@69720
  2317
proof -
lp15@67998
  2318
  have lin_f': "\<And>x. x \<in> S \<Longrightarrow> linear(f' x)"
lp15@67998
  2319
    using derS has_derivative_linear by blast
lp15@67998
  2320
  show ?thesis
lp15@67998
  2321
  proof (clarsimp simp add: negligible_outer_le)
lp15@67998
  2322
    fix e :: "real"
lp15@67998
  2323
    assume "e > 0"
lp15@67998
  2324
    obtain c where csub: "S \<subseteq> cbox (- (vec c)) (vec c)" and "c > 0"
lp15@67998
  2325
    proof -
lp15@67998
  2326
      obtain b where b: "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> b"
lp15@67998
  2327
        using \<open>bounded S\<close> by (auto simp: bounded_iff)
lp15@67998
  2328
      show thesis
lp15@67998
  2329
      proof
lp15@67998
  2330
        have "- \<bar>b\<bar> - 1 \<le> x $ i \<and> x $ i \<le> \<bar>b\<bar> + 1" if "x \<in> S" for x i
lp15@67998
  2331
          using component_le_norm_cart [of x i] b [OF that] by auto
lp15@67998
  2332
        then show "S \<subseteq> cbox (- vec (\<bar>b\<bar> + 1)) (vec (\<bar>b\<bar> + 1))"
lp15@67998
  2333
          by (auto simp: mem_box_cart)
lp15@67998
  2334
      qed auto
lp15@67998
  2335
    qed
lp15@67998
  2336
    then have box_cc: "box (- (vec c)) (vec c) \<noteq> {}" and cbox_cc: "cbox (- (vec c)) (vec c) \<noteq> {}"
lp15@67998
  2337
      by (auto simp: interval_eq_empty_cart)
lp15@67998
  2338
    obtain d where "d > 0" "d \<le> B"
lp15@67998
  2339
             and d: "(d * 2) * (4 * B) ^ (?n - 1) \<le> e / (2*c) ^ ?m / ?m ^ ?m"
lp15@67998
  2340
      apply (rule that [of "min B (e / (2*c) ^ ?m / ?m ^ ?m / (4 * B) ^ (?n - 1) / 2)"])
lp15@67998
  2341
      using \<open>B > 0\<close> \<open>c > 0\<close> \<open>e > 0\<close>
lp15@67998
  2342
      by (simp_all add: divide_simps min_mult_distrib_right)
lp15@67998
  2343
    have "\<exists>r. 0 < r \<and> r \<le> 1/2 \<and>
lp15@67998
  2344
              (x \<in> S
lp15@67998
  2345
               \<longrightarrow> (\<forall>y. y \<in> S \<and> norm(y - x) < r
lp15@67998
  2346
                       \<longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)))" for x
lp15@67998
  2347
    proof (cases "x \<in> S")
lp15@67998
  2348
      case True
lp15@67998
  2349
      then obtain r where "r > 0"
lp15@67998
  2350
              and "\<And>y. \<lbrakk>y \<in> S; norm (y - x) < r\<rbrakk>
lp15@67998
  2351
                       \<Longrightarrow> norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
lp15@67998
  2352
        using derS \<open>d > 0\<close> by (force simp: has_derivative_within_alt)
lp15@67998
  2353
      then show ?thesis
lp15@67998
  2354
        by (rule_tac x="min r (1/2)" in exI) simp
lp15@67998
  2355
    next
lp15@67998
  2356
      case False
lp15@67998
  2357
      then show ?thesis
lp15@67998
  2358
        by (rule_tac x="1/2" in exI) simp
lp15@67998
  2359
    qed
lp15@67998
  2360
    then obtain r where r12: "\<And>x. 0 < r x \<and> r x \<le> 1/2"
lp15@67998
  2361
            and r: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S; norm(y - x) < r x\<rbrakk>
lp15@67998
  2362
                          \<Longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)"
lp15@67998
  2363
      by metis
lp15@67998
  2364
    then have ga: "gauge (\<lambda>x. ball x (r x))"
lp15@67998
  2365
      by (auto simp: gauge_def)
lp15@67998
  2366
    obtain \<D> where \<D>: "countable \<D>" and sub_cc: "\<Union>\<D> \<subseteq> cbox (- vec c) (vec c)"
lp15@67998
  2367
      and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>u v. K = cbox u v)"
lp15@67998
  2368
      and djointish: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
lp15@67998
  2369
      and covered: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> ball x (r x)"
lp15@67998
  2370
      and close: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i::'m. v $ i - u $ i = 2*c / 2^n"
lp15@67998
  2371
      and covers: "S \<subseteq> \<Union>\<D>"
lp15@67998
  2372
      apply (rule covering_lemma [OF csub box_cc ga])
lp15@67998
  2373
      apply (auto simp: Basis_vec_def cart_eq_inner_axis [symmetric])
lp15@67998
  2374
      done
lp15@67998
  2375
    let ?\<mu> = "measure lebesgue"
lp15@67998
  2376
    have "\<exists>T. T \<in> lmeasurable \<and> f ` (K \<inter> S) \<subseteq> T \<and> ?\<mu> T \<le> e / (2*c) ^ ?m * ?\<mu> K"
lp15@67998
  2377
      if "K \<in> \<D>" for K
lp15@67998
  2378
    proof -
lp15@67998
  2379
      obtain u v where uv: "K = cbox u v"
lp15@67998
  2380
        using cbox \<open>K \<in> \<D>\<close> by blast
lp15@67998
  2381
      then have uv_ne: "cbox u v \<noteq> {}"
lp15@67998
  2382
        using cbox that by fastforce
lp15@67998
  2383
      obtain x where x: "x \<in> S \<inter> cbox u v" "cbox u v \<subseteq> ball x (r x)"
lp15@67998
  2384
        using \<open>K \<in> \<D>\<close> covered uv by blast
lp15@67998
  2385
      then have "dim (range (f' x)) < ?n"
immler@68072
  2386
        using rank_dim_range [of "matrix (f' x)"] x rank[of x]
immler@68072
  2387
        by (auto simp: matrix_works scalar_mult_eq_scaleR lin_f')
lp15@67998
  2388
      then obtain T where T: "T \<in> lmeasurable"
lp15@67998
  2389
            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"
lp15@67998
  2390
            and measT: "?\<mu> T \<le> (2 * (d * norm(v - u))) * (2 * ((2 * B) * norm(v - u))) ^ (?n - 1)"
lp15@67998
  2391
                        (is "_ \<le> ?DVU")
lp15@67998
  2392
        apply (rule Sard_lemma1 [of "range (f' x)" "(2 * B) * norm(v - u)" "d * norm(v - u)" "f x"])
lp15@67998
  2393
        using \<open>B > 0\<close> \<open>d > 0\<close> by simp_all
lp15@67998
  2394
      show ?thesis
lp15@67998
  2395
      proof (intro exI conjI)
lp15@67998
  2396
        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))}"
lp15@67998
  2397
          unfolding uv
lp15@67998
  2398
        proof (clarsimp simp: mult.assoc, intro conjI)
lp15@67998
  2399
          fix y
lp15@67998
  2400
          assume y: "y \<in> cbox u v" and "y \<in> S"
lp15@67998
  2401
          then have "norm (y - x) < r x"
lp15@67998
  2402
            by (metis dist_norm mem_ball norm_minus_commute subsetCE x(2))
lp15@67998
  2403
          then have le_dyx: "norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
lp15@67998
  2404
            using r [of x y] x \<open>y \<in> S\<close> by blast
lp15@67998
  2405
          have yx_le: "norm (y - x) \<le> norm (v - u)"
lp15@67998
  2406
          proof (rule norm_le_componentwise_cart)
immler@68072
  2407
            show "norm ((y - x) $ i) \<le> norm ((v - u) $ i)" for i
immler@68072
  2408
              using x y by (force simp: mem_box_cart dest!: spec [where x=i])
lp15@67998
  2409
          qed
lp15@67998
  2410
          have *: "\<lbrakk>norm(y - x - z) \<le> d; norm z \<le> B; d \<le> B\<rbrakk> \<Longrightarrow> norm(y - x) \<le> 2 * B"
lp15@67998
  2411
            for x y z :: "real^'n::_" and d B
lp15@67998
  2412
            using norm_triangle_ineq2 [of "y - x" z] by auto
lp15@67998
  2413
          show "norm (f y - f x) \<le> 2 * (B * norm (v - u))"
lp15@67998
  2414
          proof (rule * [OF le_dyx])
lp15@67998
  2415
            have "norm (f' x (y - x)) \<le> onorm (f' x) * norm (y - x)"