src/HOL/NthRoot.thy
author paulson <lp15@cam.ac.uk>
Tue Jun 30 13:56:16 2015 +0100 (2015-06-30)
changeset 60615 e5fa1d5d3952
parent 60141 833adf7db7d8
child 60758 d8d85a8172b5
permissions -rw-r--r--
Useful lemmas. The theorem concerning swapping the variables in a double integral.
     1 (*  Title       : NthRoot.thy
     2     Author      : Jacques D. Fleuriot
     3     Copyright   : 1998  University of Cambridge
     4     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
     5 *)
     6 
     7 section {* Nth Roots of Real Numbers *}
     8 
     9 theory NthRoot
    10 imports Deriv Binomial
    11 begin
    12 
    13 lemma abs_sgn_eq: "abs (sgn x :: real) = (if x = 0 then 0 else 1)"
    14   by (simp add: sgn_real_def)
    15 
    16 lemma inverse_sgn: "sgn (inverse a) = inverse (sgn a :: real)"
    17   by (simp add: sgn_real_def)
    18 
    19 lemma power_eq_iff_eq_base: 
    20   fixes a b :: "_ :: linordered_semidom"
    21   shows "0 < n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a ^ n = b ^ n \<longleftrightarrow> a = b"
    22   using power_eq_imp_eq_base[of a n b] by auto
    23 
    24 subsection {* Existence of Nth Root *}
    25 
    26 text {* Existence follows from the Intermediate Value Theorem *}
    27 
    28 lemma realpow_pos_nth:
    29   assumes n: "0 < n"
    30   assumes a: "0 < a"
    31   shows "\<exists>r>0. r ^ n = (a::real)"
    32 proof -
    33   have "\<exists>r\<ge>0. r \<le> (max 1 a) \<and> r ^ n = a"
    34   proof (rule IVT)
    35     show "0 ^ n \<le> a" using n a by (simp add: power_0_left)
    36     show "0 \<le> max 1 a" by simp
    37     from n have n1: "1 \<le> n" by simp
    38     have "a \<le> max 1 a ^ 1" by simp
    39     also have "max 1 a ^ 1 \<le> max 1 a ^ n"
    40       using n1 by (rule power_increasing, simp)
    41     finally show "a \<le> max 1 a ^ n" .
    42     show "\<forall>r. 0 \<le> r \<and> r \<le> max 1 a \<longrightarrow> isCont (\<lambda>x. x ^ n) r"
    43       by simp
    44   qed
    45   then obtain r where r: "0 \<le> r \<and> r ^ n = a" by fast
    46   with n a have "r \<noteq> 0" by (auto simp add: power_0_left)
    47   with r have "0 < r \<and> r ^ n = a" by simp
    48   thus ?thesis ..
    49 qed
    50 
    51 (* Used by Integration/RealRandVar.thy in AFP *)
    52 lemma realpow_pos_nth2: "(0::real) < a \<Longrightarrow> \<exists>r>0. r ^ Suc n = a"
    53 by (blast intro: realpow_pos_nth)
    54 
    55 text {* Uniqueness of nth positive root *}
    56 
    57 lemma realpow_pos_nth_unique: "\<lbrakk>0 < n; 0 < a\<rbrakk> \<Longrightarrow> \<exists>!r. 0 < r \<and> r ^ n = (a::real)"
    58   by (auto intro!: realpow_pos_nth simp: power_eq_iff_eq_base)
    59 
    60 subsection {* Nth Root *}
    61 
    62 text {* We define roots of negative reals such that
    63   @{term "root n (- x) = - root n x"}. This allows
    64   us to omit side conditions from many theorems. *}
    65 
    66 lemma inj_sgn_power: assumes "0 < n" shows "inj (\<lambda>y. sgn y * \<bar>y\<bar>^n :: real)" (is "inj ?f")
    67 proof (rule injI)
    68   have x: "\<And>a b :: real. (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b) \<Longrightarrow> a \<noteq> b" by auto
    69   fix x y assume "?f x = ?f y" with power_eq_iff_eq_base[of n "\<bar>x\<bar>" "\<bar>y\<bar>"] `0<n` show "x = y"
    70     by (cases rule: linorder_cases[of 0 x, case_product linorder_cases[of 0 y]])
    71        (simp_all add: x)
    72 qed
    73 
    74 lemma sgn_power_injE: "sgn a * \<bar>a\<bar> ^ n = x \<Longrightarrow> x = sgn b * \<bar>b\<bar> ^ n \<Longrightarrow> 0 < n \<Longrightarrow> a = (b::real)"
    75   using inj_sgn_power[THEN injD, of n a b] by simp
    76 
    77 definition root :: "nat \<Rightarrow> real \<Rightarrow> real" where
    78   "root n x = (if n = 0 then 0 else the_inv (\<lambda>y. sgn y * \<bar>y\<bar>^n) x)"
    79 
    80 lemma root_0 [simp]: "root 0 x = 0"
    81   by (simp add: root_def)
    82 
    83 lemma root_sgn_power: "0 < n \<Longrightarrow> root n (sgn y * \<bar>y\<bar>^n) = y"
    84   using the_inv_f_f[OF inj_sgn_power] by (simp add: root_def)
    85 
    86 lemma sgn_power_root:
    87   assumes "0 < n" shows "sgn (root n x) * \<bar>(root n x)\<bar>^n = x" (is "?f (root n x) = x")
    88 proof cases
    89   assume "x \<noteq> 0"
    90   with realpow_pos_nth[OF `0 < n`, of "\<bar>x\<bar>"] obtain r where "0 < r" "r ^ n = \<bar>x\<bar>" by auto
    91   with `x \<noteq> 0` have S: "x \<in> range ?f"
    92     by (intro image_eqI[of _ _ "sgn x * r"])
    93        (auto simp: abs_mult sgn_mult power_mult_distrib abs_sgn_eq mult_sgn_abs)
    94   from `0 < n` f_the_inv_into_f[OF inj_sgn_power[OF `0 < n`] this]  show ?thesis
    95     by (simp add: root_def)
    96 qed (insert `0 < n` root_sgn_power[of n 0], simp)
    97 
    98 lemma split_root: "P (root n x) \<longleftrightarrow> (n = 0 \<longrightarrow> P 0) \<and> (0 < n \<longrightarrow> (\<forall>y. sgn y * \<bar>y\<bar>^n = x \<longrightarrow> P y))"
    99   apply (cases "n = 0")
   100   apply simp_all
   101   apply (metis root_sgn_power sgn_power_root)
   102   done
   103 
   104 lemma real_root_zero [simp]: "root n 0 = 0"
   105   by (simp split: split_root add: sgn_zero_iff)
   106 
   107 lemma real_root_minus: "root n (- x) = - root n x"
   108   by (clarsimp split: split_root elim!: sgn_power_injE simp: sgn_minus)
   109 
   110 lemma real_root_less_mono: "\<lbrakk>0 < n; x < y\<rbrakk> \<Longrightarrow> root n x < root n y"
   111 proof (clarsimp split: split_root)
   112   have x: "\<And>a b :: real. (0 < b \<and> a < 0) \<Longrightarrow> \<not> a > b" by auto
   113   fix a b :: real assume "0 < n" "sgn a * \<bar>a\<bar> ^ n < sgn b * \<bar>b\<bar> ^ n" then show "a < b"
   114     using power_less_imp_less_base[of a n b]  power_less_imp_less_base[of "-b" n "-a"]
   115     by (simp add: sgn_real_def power_less_zero_eq x[of "a ^ n" "- ((- b) ^ n)"] split: split_if_asm)
   116 qed
   117 
   118 lemma real_root_gt_zero: "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> 0 < root n x"
   119   using real_root_less_mono[of n 0 x] by simp
   120 
   121 lemma real_root_ge_zero: "0 \<le> x \<Longrightarrow> 0 \<le> root n x"
   122   using real_root_gt_zero[of n x] by (cases "n = 0") (auto simp add: le_less)
   123 
   124 lemma real_root_pow_pos: (* TODO: rename *)
   125   "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
   126   using sgn_power_root[of n x] real_root_gt_zero[of n x] by simp
   127 
   128 lemma real_root_pow_pos2 [simp]: (* TODO: rename *)
   129   "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
   130 by (auto simp add: order_le_less real_root_pow_pos)
   131 
   132 lemma sgn_root: "0 < n \<Longrightarrow> sgn (root n x) = sgn x"
   133   by (auto split: split_root simp: sgn_real_def power_less_zero_eq)
   134 
   135 lemma odd_real_root_pow: "odd n \<Longrightarrow> root n x ^ n = x"
   136   using sgn_power_root[of n x] by (simp add: odd_pos sgn_real_def split: split_if_asm)
   137 
   138 lemma real_root_power_cancel: "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n (x ^ n) = x"
   139   using root_sgn_power[of n x] by (auto simp add: le_less power_0_left)
   140 
   141 lemma odd_real_root_power_cancel: "odd n \<Longrightarrow> root n (x ^ n) = x"
   142   using root_sgn_power[of n x] by (simp add: odd_pos sgn_real_def power_0_left split: split_if_asm)
   143 
   144 lemma real_root_pos_unique: "\<lbrakk>0 < n; 0 \<le> y; y ^ n = x\<rbrakk> \<Longrightarrow> root n x = y"
   145   using root_sgn_power[of n y] by (auto simp add: le_less power_0_left)
   146 
   147 lemma odd_real_root_unique:
   148   "\<lbrakk>odd n; y ^ n = x\<rbrakk> \<Longrightarrow> root n x = y"
   149 by (erule subst, rule odd_real_root_power_cancel)
   150 
   151 lemma real_root_one [simp]: "0 < n \<Longrightarrow> root n 1 = 1"
   152 by (simp add: real_root_pos_unique)
   153 
   154 text {* Root function is strictly monotonic, hence injective *}
   155 
   156 lemma real_root_le_mono: "\<lbrakk>0 < n; x \<le> y\<rbrakk> \<Longrightarrow> root n x \<le> root n y"
   157   by (auto simp add: order_le_less real_root_less_mono)
   158 
   159 lemma real_root_less_iff [simp]:
   160   "0 < n \<Longrightarrow> (root n x < root n y) = (x < y)"
   161 apply (cases "x < y")
   162 apply (simp add: real_root_less_mono)
   163 apply (simp add: linorder_not_less real_root_le_mono)
   164 done
   165 
   166 lemma real_root_le_iff [simp]:
   167   "0 < n \<Longrightarrow> (root n x \<le> root n y) = (x \<le> y)"
   168 apply (cases "x \<le> y")
   169 apply (simp add: real_root_le_mono)
   170 apply (simp add: linorder_not_le real_root_less_mono)
   171 done
   172 
   173 lemma real_root_eq_iff [simp]:
   174   "0 < n \<Longrightarrow> (root n x = root n y) = (x = y)"
   175 by (simp add: order_eq_iff)
   176 
   177 lemmas real_root_gt_0_iff [simp] = real_root_less_iff [where x=0, simplified]
   178 lemmas real_root_lt_0_iff [simp] = real_root_less_iff [where y=0, simplified]
   179 lemmas real_root_ge_0_iff [simp] = real_root_le_iff [where x=0, simplified]
   180 lemmas real_root_le_0_iff [simp] = real_root_le_iff [where y=0, simplified]
   181 lemmas real_root_eq_0_iff [simp] = real_root_eq_iff [where y=0, simplified]
   182 
   183 lemma real_root_gt_1_iff [simp]: "0 < n \<Longrightarrow> (1 < root n y) = (1 < y)"
   184 by (insert real_root_less_iff [where x=1], simp)
   185 
   186 lemma real_root_lt_1_iff [simp]: "0 < n \<Longrightarrow> (root n x < 1) = (x < 1)"
   187 by (insert real_root_less_iff [where y=1], simp)
   188 
   189 lemma real_root_ge_1_iff [simp]: "0 < n \<Longrightarrow> (1 \<le> root n y) = (1 \<le> y)"
   190 by (insert real_root_le_iff [where x=1], simp)
   191 
   192 lemma real_root_le_1_iff [simp]: "0 < n \<Longrightarrow> (root n x \<le> 1) = (x \<le> 1)"
   193 by (insert real_root_le_iff [where y=1], simp)
   194 
   195 lemma real_root_eq_1_iff [simp]: "0 < n \<Longrightarrow> (root n x = 1) = (x = 1)"
   196 by (insert real_root_eq_iff [where y=1], simp)
   197 
   198 text {* Roots of multiplication and division *}
   199 
   200 lemma real_root_mult: "root n (x * y) = root n x * root n y"
   201   by (auto split: split_root elim!: sgn_power_injE simp: sgn_mult abs_mult power_mult_distrib)
   202 
   203 lemma real_root_inverse: "root n (inverse x) = inverse (root n x)"
   204   by (auto split: split_root elim!: sgn_power_injE simp: inverse_sgn power_inverse)
   205 
   206 lemma real_root_divide: "root n (x / y) = root n x / root n y"
   207   by (simp add: divide_inverse real_root_mult real_root_inverse)
   208 
   209 lemma real_root_abs: "0 < n \<Longrightarrow> root n \<bar>x\<bar> = \<bar>root n x\<bar>"
   210   by (simp add: abs_if real_root_minus)
   211 
   212 lemma real_root_power: "0 < n \<Longrightarrow> root n (x ^ k) = root n x ^ k"
   213   by (induct k) (simp_all add: real_root_mult)
   214 
   215 text {* Roots of roots *}
   216 
   217 lemma real_root_Suc_0 [simp]: "root (Suc 0) x = x"
   218 by (simp add: odd_real_root_unique)
   219 
   220 lemma real_root_mult_exp: "root (m * n) x = root m (root n x)"
   221   by (auto split: split_root elim!: sgn_power_injE
   222            simp: sgn_zero_iff sgn_mult power_mult[symmetric] abs_mult power_mult_distrib abs_sgn_eq)
   223 
   224 lemma real_root_commute: "root m (root n x) = root n (root m x)"
   225   by (simp add: real_root_mult_exp [symmetric] mult.commute)
   226 
   227 text {* Monotonicity in first argument *}
   228 
   229 lemma real_root_strict_decreasing:
   230   "\<lbrakk>0 < n; n < N; 1 < x\<rbrakk> \<Longrightarrow> root N x < root n x"
   231 apply (subgoal_tac "root n (root N x) ^ n < root N (root n x) ^ N", simp)
   232 apply (simp add: real_root_commute power_strict_increasing
   233             del: real_root_pow_pos2)
   234 done
   235 
   236 lemma real_root_strict_increasing:
   237   "\<lbrakk>0 < n; n < N; 0 < x; x < 1\<rbrakk> \<Longrightarrow> root n x < root N x"
   238 apply (subgoal_tac "root N (root n x) ^ N < root n (root N x) ^ n", simp)
   239 apply (simp add: real_root_commute power_strict_decreasing
   240             del: real_root_pow_pos2)
   241 done
   242 
   243 lemma real_root_decreasing:
   244   "\<lbrakk>0 < n; n < N; 1 \<le> x\<rbrakk> \<Longrightarrow> root N x \<le> root n x"
   245 by (auto simp add: order_le_less real_root_strict_decreasing)
   246 
   247 lemma real_root_increasing:
   248   "\<lbrakk>0 < n; n < N; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> root n x \<le> root N x"
   249 by (auto simp add: order_le_less real_root_strict_increasing)
   250 
   251 text {* Continuity and derivatives *}
   252 
   253 lemma isCont_real_root: "isCont (root n) x"
   254 proof cases
   255   assume n: "0 < n"
   256   let ?f = "\<lambda>y::real. sgn y * \<bar>y\<bar>^n"
   257   have "continuous_on ({0..} \<union> {.. 0}) (\<lambda>x. if 0 < x then x ^ n else - ((-x) ^ n) :: real)"
   258     using n by (intro continuous_on_If continuous_intros) auto
   259   then have "continuous_on UNIV ?f"
   260     by (rule continuous_on_cong[THEN iffD1, rotated 2]) (auto simp: not_less real_sgn_neg le_less n)
   261   then have [simp]: "\<And>x. isCont ?f x"
   262     by (simp add: continuous_on_eq_continuous_at)
   263 
   264   have "isCont (root n) (?f (root n x))"
   265     by (rule isCont_inverse_function [where f="?f" and d=1]) (auto simp: root_sgn_power n)
   266   then show ?thesis
   267     by (simp add: sgn_power_root n)
   268 qed (simp add: root_def[abs_def])
   269 
   270 lemma tendsto_real_root[tendsto_intros]:
   271   "(f ---> x) F \<Longrightarrow> ((\<lambda>x. root n (f x)) ---> root n x) F"
   272   using isCont_tendsto_compose[OF isCont_real_root, of f x F] .
   273 
   274 lemma continuous_real_root[continuous_intros]:
   275   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. root n (f x))"
   276   unfolding continuous_def by (rule tendsto_real_root)
   277   
   278 lemma continuous_on_real_root[continuous_intros]:
   279   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. root n (f x))"
   280   unfolding continuous_on_def by (auto intro: tendsto_real_root)
   281 
   282 lemma DERIV_real_root:
   283   assumes n: "0 < n"
   284   assumes x: "0 < x"
   285   shows "DERIV (root n) x :> inverse (real n * root n x ^ (n - Suc 0))"
   286 proof (rule DERIV_inverse_function)
   287   show "0 < x" using x .
   288   show "x < x + 1" by simp
   289   show "\<forall>y. 0 < y \<and> y < x + 1 \<longrightarrow> root n y ^ n = y"
   290     using n by simp
   291   show "DERIV (\<lambda>x. x ^ n) (root n x) :> real n * root n x ^ (n - Suc 0)"
   292     by (rule DERIV_pow)
   293   show "real n * root n x ^ (n - Suc 0) \<noteq> 0"
   294     using n x by simp
   295 qed (rule isCont_real_root)
   296 
   297 lemma DERIV_odd_real_root:
   298   assumes n: "odd n"
   299   assumes x: "x \<noteq> 0"
   300   shows "DERIV (root n) x :> inverse (real n * root n x ^ (n - Suc 0))"
   301 proof (rule DERIV_inverse_function)
   302   show "x - 1 < x" by simp
   303   show "x < x + 1" by simp
   304   show "\<forall>y. x - 1 < y \<and> y < x + 1 \<longrightarrow> root n y ^ n = y"
   305     using n by (simp add: odd_real_root_pow)
   306   show "DERIV (\<lambda>x. x ^ n) (root n x) :> real n * root n x ^ (n - Suc 0)"
   307     by (rule DERIV_pow)
   308   show "real n * root n x ^ (n - Suc 0) \<noteq> 0"
   309     using odd_pos [OF n] x by simp
   310 qed (rule isCont_real_root)
   311 
   312 lemma DERIV_even_real_root:
   313   assumes n: "0 < n" and "even n"
   314   assumes x: "x < 0"
   315   shows "DERIV (root n) x :> inverse (- real n * root n x ^ (n - Suc 0))"
   316 proof (rule DERIV_inverse_function)
   317   show "x - 1 < x" by simp
   318   show "x < 0" using x .
   319 next
   320   show "\<forall>y. x - 1 < y \<and> y < 0 \<longrightarrow> - (root n y ^ n) = y"
   321   proof (rule allI, rule impI, erule conjE)
   322     fix y assume "x - 1 < y" and "y < 0"
   323     hence "root n (-y) ^ n = -y" using `0 < n` by simp
   324     with real_root_minus and `even n`
   325     show "- (root n y ^ n) = y" by simp
   326   qed
   327 next
   328   show "DERIV (\<lambda>x. - (x ^ n)) (root n x) :> - real n * root n x ^ (n - Suc 0)"
   329     by  (auto intro!: derivative_eq_intros simp: real_of_nat_def)
   330   show "- real n * root n x ^ (n - Suc 0) \<noteq> 0"
   331     using n x by simp
   332 qed (rule isCont_real_root)
   333 
   334 lemma DERIV_real_root_generic:
   335   assumes "0 < n" and "x \<noteq> 0"
   336     and "\<lbrakk> even n ; 0 < x \<rbrakk> \<Longrightarrow> D = inverse (real n * root n x ^ (n - Suc 0))"
   337     and "\<lbrakk> even n ; x < 0 \<rbrakk> \<Longrightarrow> D = - inverse (real n * root n x ^ (n - Suc 0))"
   338     and "odd n \<Longrightarrow> D = inverse (real n * root n x ^ (n - Suc 0))"
   339   shows "DERIV (root n) x :> D"
   340 using assms by (cases "even n", cases "0 < x",
   341   auto intro: DERIV_real_root[THEN DERIV_cong]
   342               DERIV_odd_real_root[THEN DERIV_cong]
   343               DERIV_even_real_root[THEN DERIV_cong])
   344 
   345 subsection {* Square Root *}
   346 
   347 definition sqrt :: "real \<Rightarrow> real" where
   348   "sqrt = root 2"
   349 
   350 lemma pos2: "0 < (2::nat)" by simp
   351 
   352 lemma real_sqrt_unique: "\<lbrakk>y\<^sup>2 = x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt x = y"
   353 unfolding sqrt_def by (rule real_root_pos_unique [OF pos2])
   354 
   355 lemma real_sqrt_abs [simp]: "sqrt (x\<^sup>2) = \<bar>x\<bar>"
   356 apply (rule real_sqrt_unique)
   357 apply (rule power2_abs)
   358 apply (rule abs_ge_zero)
   359 done
   360 
   361 lemma real_sqrt_pow2 [simp]: "0 \<le> x \<Longrightarrow> (sqrt x)\<^sup>2 = x"
   362 unfolding sqrt_def by (rule real_root_pow_pos2 [OF pos2])
   363 
   364 lemma real_sqrt_pow2_iff [simp]: "((sqrt x)\<^sup>2 = x) = (0 \<le> x)"
   365 apply (rule iffI)
   366 apply (erule subst)
   367 apply (rule zero_le_power2)
   368 apply (erule real_sqrt_pow2)
   369 done
   370 
   371 lemma real_sqrt_zero [simp]: "sqrt 0 = 0"
   372 unfolding sqrt_def by (rule real_root_zero)
   373 
   374 lemma real_sqrt_one [simp]: "sqrt 1 = 1"
   375 unfolding sqrt_def by (rule real_root_one [OF pos2])
   376 
   377 lemma real_sqrt_four [simp]: "sqrt 4 = 2"
   378   using real_sqrt_abs[of 2] by simp
   379 
   380 lemma real_sqrt_minus: "sqrt (- x) = - sqrt x"
   381 unfolding sqrt_def by (rule real_root_minus)
   382 
   383 lemma real_sqrt_mult: "sqrt (x * y) = sqrt x * sqrt y"
   384 unfolding sqrt_def by (rule real_root_mult)
   385 
   386 lemma real_sqrt_mult_self[simp]: "sqrt a * sqrt a = \<bar>a\<bar>"
   387   using real_sqrt_abs[of a] unfolding power2_eq_square real_sqrt_mult .
   388 
   389 lemma real_sqrt_inverse: "sqrt (inverse x) = inverse (sqrt x)"
   390 unfolding sqrt_def by (rule real_root_inverse)
   391 
   392 lemma real_sqrt_divide: "sqrt (x / y) = sqrt x / sqrt y"
   393 unfolding sqrt_def by (rule real_root_divide)
   394 
   395 lemma real_sqrt_power: "sqrt (x ^ k) = sqrt x ^ k"
   396 unfolding sqrt_def by (rule real_root_power [OF pos2])
   397 
   398 lemma real_sqrt_gt_zero: "0 < x \<Longrightarrow> 0 < sqrt x"
   399 unfolding sqrt_def by (rule real_root_gt_zero [OF pos2])
   400 
   401 lemma real_sqrt_ge_zero: "0 \<le> x \<Longrightarrow> 0 \<le> sqrt x"
   402 unfolding sqrt_def by (rule real_root_ge_zero)
   403 
   404 lemma real_sqrt_less_mono: "x < y \<Longrightarrow> sqrt x < sqrt y"
   405 unfolding sqrt_def by (rule real_root_less_mono [OF pos2])
   406 
   407 lemma real_sqrt_le_mono: "x \<le> y \<Longrightarrow> sqrt x \<le> sqrt y"
   408 unfolding sqrt_def by (rule real_root_le_mono [OF pos2])
   409 
   410 lemma real_sqrt_less_iff [simp]: "(sqrt x < sqrt y) = (x < y)"
   411 unfolding sqrt_def by (rule real_root_less_iff [OF pos2])
   412 
   413 lemma real_sqrt_le_iff [simp]: "(sqrt x \<le> sqrt y) = (x \<le> y)"
   414 unfolding sqrt_def by (rule real_root_le_iff [OF pos2])
   415 
   416 lemma real_sqrt_eq_iff [simp]: "(sqrt x = sqrt y) = (x = y)"
   417 unfolding sqrt_def by (rule real_root_eq_iff [OF pos2])
   418 
   419 lemma real_le_lsqrt: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y\<^sup>2 \<Longrightarrow> sqrt x \<le> y"
   420   using real_sqrt_le_iff[of x "y\<^sup>2"] by simp
   421 
   422 lemma real_le_rsqrt: "x\<^sup>2 \<le> y \<Longrightarrow> x \<le> sqrt y"
   423   using real_sqrt_le_mono[of "x\<^sup>2" y] by simp
   424 
   425 lemma real_less_rsqrt: "x\<^sup>2 < y \<Longrightarrow> x < sqrt y"
   426   using real_sqrt_less_mono[of "x\<^sup>2" y] by simp
   427 
   428 lemma sqrt_even_pow2:
   429   assumes n: "even n"
   430   shows "sqrt (2 ^ n) = 2 ^ (n div 2)"
   431 proof -
   432   from n obtain m where m: "n = 2 * m" ..
   433   from m have "sqrt (2 ^ n) = sqrt ((2 ^ m)\<^sup>2)"
   434     by (simp only: power_mult[symmetric] mult.commute)
   435   then show ?thesis
   436     using m by simp
   437 qed
   438 
   439 lemmas real_sqrt_gt_0_iff [simp] = real_sqrt_less_iff [where x=0, unfolded real_sqrt_zero]
   440 lemmas real_sqrt_lt_0_iff [simp] = real_sqrt_less_iff [where y=0, unfolded real_sqrt_zero]
   441 lemmas real_sqrt_ge_0_iff [simp] = real_sqrt_le_iff [where x=0, unfolded real_sqrt_zero]
   442 lemmas real_sqrt_le_0_iff [simp] = real_sqrt_le_iff [where y=0, unfolded real_sqrt_zero]
   443 lemmas real_sqrt_eq_0_iff [simp] = real_sqrt_eq_iff [where y=0, unfolded real_sqrt_zero]
   444 
   445 lemmas real_sqrt_gt_1_iff [simp] = real_sqrt_less_iff [where x=1, unfolded real_sqrt_one]
   446 lemmas real_sqrt_lt_1_iff [simp] = real_sqrt_less_iff [where y=1, unfolded real_sqrt_one]
   447 lemmas real_sqrt_ge_1_iff [simp] = real_sqrt_le_iff [where x=1, unfolded real_sqrt_one]
   448 lemmas real_sqrt_le_1_iff [simp] = real_sqrt_le_iff [where y=1, unfolded real_sqrt_one]
   449 lemmas real_sqrt_eq_1_iff [simp] = real_sqrt_eq_iff [where y=1, unfolded real_sqrt_one]
   450 
   451 lemma sqrt_add_le_add_sqrt:
   452   assumes "0 \<le> x" "0 \<le> y"
   453   shows "sqrt (x + y) \<le> sqrt x + sqrt y"
   454 by (rule power2_le_imp_le) (simp_all add: power2_sum assms)
   455 
   456 lemma isCont_real_sqrt: "isCont sqrt x"
   457 unfolding sqrt_def by (rule isCont_real_root)
   458 
   459 lemma tendsto_real_sqrt[tendsto_intros]:
   460   "(f ---> x) F \<Longrightarrow> ((\<lambda>x. sqrt (f x)) ---> sqrt x) F"
   461   unfolding sqrt_def by (rule tendsto_real_root)
   462 
   463 lemma continuous_real_sqrt[continuous_intros]:
   464   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sqrt (f x))"
   465   unfolding sqrt_def by (rule continuous_real_root)
   466   
   467 lemma continuous_on_real_sqrt[continuous_intros]:
   468   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sqrt (f x))"
   469   unfolding sqrt_def by (rule continuous_on_real_root)
   470 
   471 lemma DERIV_real_sqrt_generic:
   472   assumes "x \<noteq> 0"
   473   assumes "x > 0 \<Longrightarrow> D = inverse (sqrt x) / 2"
   474   assumes "x < 0 \<Longrightarrow> D = - inverse (sqrt x) / 2"
   475   shows "DERIV sqrt x :> D"
   476   using assms unfolding sqrt_def
   477   by (auto intro!: DERIV_real_root_generic)
   478 
   479 lemma DERIV_real_sqrt:
   480   "0 < x \<Longrightarrow> DERIV sqrt x :> inverse (sqrt x) / 2"
   481   using DERIV_real_sqrt_generic by simp
   482 
   483 declare
   484   DERIV_real_sqrt_generic[THEN DERIV_chain2, derivative_intros]
   485   DERIV_real_root_generic[THEN DERIV_chain2, derivative_intros]
   486 
   487 lemma not_real_square_gt_zero [simp]: "(~ (0::real) < x*x) = (x = 0)"
   488 apply auto
   489 apply (cut_tac x = x and y = 0 in linorder_less_linear)
   490 apply (simp add: zero_less_mult_iff)
   491 done
   492 
   493 lemma real_sqrt_abs2 [simp]: "sqrt(x*x) = \<bar>x\<bar>"
   494 apply (subst power2_eq_square [symmetric])
   495 apply (rule real_sqrt_abs)
   496 done
   497 
   498 lemma real_inv_sqrt_pow2: "0 < x ==> (inverse (sqrt x))\<^sup>2 = inverse x"
   499 by (simp add: power_inverse [symmetric])
   500 
   501 lemma real_sqrt_eq_zero_cancel: "[| 0 \<le> x; sqrt(x) = 0|] ==> x = 0"
   502 by simp
   503 
   504 lemma real_sqrt_ge_one: "1 \<le> x ==> 1 \<le> sqrt x"
   505 by simp
   506 
   507 lemma sqrt_divide_self_eq:
   508   assumes nneg: "0 \<le> x"
   509   shows "sqrt x / x = inverse (sqrt x)"
   510 proof cases
   511   assume "x=0" thus ?thesis by simp
   512 next
   513   assume nz: "x\<noteq>0" 
   514   hence pos: "0<x" using nneg by arith
   515   show ?thesis
   516   proof (rule right_inverse_eq [THEN iffD1, THEN sym]) 
   517     show "sqrt x / x \<noteq> 0" by (simp add: divide_inverse nneg nz) 
   518     show "inverse (sqrt x) / (sqrt x / x) = 1"
   519       by (simp add: divide_inverse mult.assoc [symmetric] 
   520                   power2_eq_square [symmetric] real_inv_sqrt_pow2 pos nz) 
   521   qed
   522 qed
   523 
   524 lemma real_div_sqrt: "0 \<le> x \<Longrightarrow> x / sqrt x = sqrt x"
   525   apply (cases "x = 0")
   526   apply simp_all
   527   using sqrt_divide_self_eq[of x]
   528   apply (simp add: inverse_eq_divide field_simps)
   529   done
   530 
   531 lemma real_divide_square_eq [simp]: "(((r::real) * a) / (r * r)) = a / r"
   532 apply (simp add: divide_inverse)
   533 apply (case_tac "r=0")
   534 apply (auto simp add: ac_simps)
   535 done
   536 
   537 lemma lemma_real_divide_sqrt_less: "0 < u ==> u / sqrt 2 < u"
   538 by (simp add: divide_less_eq)
   539 
   540 lemma four_x_squared: 
   541   fixes x::real
   542   shows "4 * x\<^sup>2 = (2 * x)\<^sup>2"
   543 by (simp add: power2_eq_square)
   544 
   545 lemma sqrt_at_top: "LIM x at_top. sqrt x :: real :> at_top"
   546   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="power2"])
   547      (auto intro: eventually_gt_at_top)
   548 
   549 subsection {* Square Root of Sum of Squares *}
   550 
   551 lemma sum_squares_bound: 
   552   fixes x:: "'a::linordered_field"
   553   shows "2*x*y \<le> x^2 + y^2"
   554 proof -
   555   have "(x-y)^2 = x*x - 2*x*y + y*y"
   556     by algebra
   557   then have "0 \<le> x^2 - 2*x*y + y^2"
   558     by (metis sum_power2_ge_zero zero_le_double_add_iff_zero_le_single_add power2_eq_square)
   559   then show ?thesis
   560     by arith
   561 qed
   562 
   563 lemma arith_geo_mean: 
   564   fixes u:: "'a::linordered_field" assumes "u\<^sup>2 = x*y" "x\<ge>0" "y\<ge>0" shows "u \<le> (x + y)/2"
   565     apply (rule power2_le_imp_le)
   566     using sum_squares_bound assms
   567     apply (auto simp: zero_le_mult_iff)
   568     by (auto simp: algebra_simps power2_eq_square)
   569 
   570 lemma arith_geo_mean_sqrt: 
   571   fixes x::real assumes "x\<ge>0" "y\<ge>0" shows "sqrt(x*y) \<le> (x + y)/2"
   572   apply (rule arith_geo_mean)
   573   using assms
   574   apply (auto simp: zero_le_mult_iff)
   575   done
   576 
   577 lemma real_sqrt_sum_squares_mult_ge_zero [simp]:
   578      "0 \<le> sqrt ((x\<^sup>2 + y\<^sup>2)*(xa\<^sup>2 + ya\<^sup>2))"
   579   by (metis real_sqrt_ge_0_iff split_mult_pos_le sum_power2_ge_zero)
   580 
   581 lemma real_sqrt_sum_squares_mult_squared_eq [simp]:
   582      "(sqrt ((x\<^sup>2 + y\<^sup>2) * (xa\<^sup>2 + ya\<^sup>2)))\<^sup>2 = (x\<^sup>2 + y\<^sup>2) * (xa\<^sup>2 + ya\<^sup>2)"
   583   by (simp add: zero_le_mult_iff)
   584 
   585 lemma real_sqrt_sum_squares_eq_cancel: "sqrt (x\<^sup>2 + y\<^sup>2) = x \<Longrightarrow> y = 0"
   586 by (drule_tac f = "%x. x\<^sup>2" in arg_cong, simp)
   587 
   588 lemma real_sqrt_sum_squares_eq_cancel2: "sqrt (x\<^sup>2 + y\<^sup>2) = y \<Longrightarrow> x = 0"
   589 by (drule_tac f = "%x. x\<^sup>2" in arg_cong, simp)
   590 
   591 lemma real_sqrt_sum_squares_ge1 [simp]: "x \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   592 by (rule power2_le_imp_le, simp_all)
   593 
   594 lemma real_sqrt_sum_squares_ge2 [simp]: "y \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   595 by (rule power2_le_imp_le, simp_all)
   596 
   597 lemma real_sqrt_ge_abs1 [simp]: "\<bar>x\<bar> \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   598 by (rule power2_le_imp_le, simp_all)
   599 
   600 lemma real_sqrt_ge_abs2 [simp]: "\<bar>y\<bar> \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   601 by (rule power2_le_imp_le, simp_all)
   602 
   603 lemma le_real_sqrt_sumsq [simp]: "x \<le> sqrt (x * x + y * y)"
   604 by (simp add: power2_eq_square [symmetric])
   605 
   606 lemma real_sqrt_sum_squares_triangle_ineq:
   607   "sqrt ((a + c)\<^sup>2 + (b + d)\<^sup>2) \<le> sqrt (a\<^sup>2 + b\<^sup>2) + sqrt (c\<^sup>2 + d\<^sup>2)"
   608 apply (rule power2_le_imp_le, simp)
   609 apply (simp add: power2_sum)
   610 apply (simp only: mult.assoc distrib_left [symmetric])
   611 apply (rule mult_left_mono)
   612 apply (rule power2_le_imp_le)
   613 apply (simp add: power2_sum power_mult_distrib)
   614 apply (simp add: ring_distribs)
   615 apply (subgoal_tac "0 \<le> b\<^sup>2 * c\<^sup>2 + a\<^sup>2 * d\<^sup>2 - 2 * (a * c) * (b * d)", simp)
   616 apply (rule_tac b="(a * d - b * c)\<^sup>2" in ord_le_eq_trans)
   617 apply (rule zero_le_power2)
   618 apply (simp add: power2_diff power_mult_distrib)
   619 apply (simp)
   620 apply simp
   621 apply (simp add: add_increasing)
   622 done
   623 
   624 lemma real_sqrt_sum_squares_less:
   625   "\<lbrakk>\<bar>x\<bar> < u / sqrt 2; \<bar>y\<bar> < u / sqrt 2\<rbrakk> \<Longrightarrow> sqrt (x\<^sup>2 + y\<^sup>2) < u"
   626 apply (rule power2_less_imp_less, simp)
   627 apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
   628 apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
   629 apply (simp add: power_divide)
   630 apply (drule order_le_less_trans [OF abs_ge_zero])
   631 apply (simp add: zero_less_divide_iff)
   632 done
   633 
   634 lemma sqrt2_less_2: "sqrt 2 < (2::real)"
   635   by (metis not_less not_less_iff_gr_or_eq numeral_less_iff real_sqrt_four real_sqrt_le_iff semiring_norm(75) semiring_norm(78) semiring_norm(85))
   636 
   637 
   638 text{*Needed for the infinitely close relation over the nonstandard
   639     complex numbers*}
   640 lemma lemma_sqrt_hcomplex_capprox:
   641      "[| 0 < u; x < u/2; y < u/2; 0 \<le> x; 0 \<le> y |] ==> sqrt (x\<^sup>2 + y\<^sup>2) < u"
   642   apply (rule real_sqrt_sum_squares_less)
   643   apply (auto simp add: abs_if field_simps)
   644   apply (rule le_less_trans [where y = "x*2"])
   645   using less_eq_real_def sqrt2_less_2 apply force
   646   apply assumption
   647   apply (rule le_less_trans [where y = "y*2"])
   648   using less_eq_real_def sqrt2_less_2 mult_le_cancel_left 
   649   apply auto 
   650   done
   651   
   652 lemma LIMSEQ_root: "(\<lambda>n. root n n) ----> 1"
   653 proof -
   654   def x \<equiv> "\<lambda>n. root n n - 1"
   655   have "x ----> sqrt 0"
   656   proof (rule tendsto_sandwich[OF _ _ tendsto_const])
   657     show "(\<lambda>x. sqrt (2 / x)) ----> sqrt 0"
   658       by (intro tendsto_intros tendsto_divide_0[OF tendsto_const] filterlim_mono[OF filterlim_real_sequentially])
   659          (simp_all add: at_infinity_eq_at_top_bot)
   660     { fix n :: nat assume "2 < n"
   661       have "1 + (real (n - 1) * n) / 2 * x n^2 = 1 + of_nat (n choose 2) * x n^2"
   662         using `2 < n` unfolding gbinomial_def binomial_gbinomial
   663         by (simp add: atLeast0AtMost atMost_Suc field_simps real_of_nat_diff numeral_2_eq_2 real_eq_of_nat[symmetric])
   664       also have "\<dots> \<le> (\<Sum>k\<in>{0, 2}. of_nat (n choose k) * x n^k)"
   665         by (simp add: x_def)
   666       also have "\<dots> \<le> (\<Sum>k=0..n. of_nat (n choose k) * x n^k)"
   667         using `2 < n` by (intro setsum_mono2) (auto intro!: mult_nonneg_nonneg zero_le_power simp: x_def le_diff_eq)
   668       also have "\<dots> = (x n + 1) ^ n"
   669         by (simp add: binomial_ring)
   670       also have "\<dots> = n"
   671         using `2 < n` by (simp add: x_def)
   672       finally have "real (n - 1) * (real n / 2 * (x n)\<^sup>2) \<le> real (n - 1) * 1"
   673         by simp
   674       then have "(x n)\<^sup>2 \<le> 2 / real n"
   675         using `2 < n` unfolding mult_le_cancel_left by (simp add: field_simps)
   676       from real_sqrt_le_mono[OF this] have "x n \<le> sqrt (2 / real n)"
   677         by simp }
   678     then show "eventually (\<lambda>n. x n \<le> sqrt (2 / real n)) sequentially"
   679       by (auto intro!: exI[of _ 3] simp: eventually_sequentially)
   680     show "eventually (\<lambda>n. sqrt 0 \<le> x n) sequentially"
   681       by (auto intro!: exI[of _ 1] simp: eventually_sequentially le_diff_eq x_def)
   682   qed
   683   from tendsto_add[OF this tendsto_const[of 1]] show ?thesis
   684     by (simp add: x_def)
   685 qed
   686 
   687 lemma LIMSEQ_root_const:
   688   assumes "0 < c"
   689   shows "(\<lambda>n. root n c) ----> 1"
   690 proof -
   691   { fix c :: real assume "1 \<le> c"
   692     def x \<equiv> "\<lambda>n. root n c - 1"
   693     have "x ----> 0"
   694     proof (rule tendsto_sandwich[OF _ _ tendsto_const])
   695       show "(\<lambda>n. c / n) ----> 0"
   696         by (intro tendsto_divide_0[OF tendsto_const] filterlim_mono[OF filterlim_real_sequentially])
   697            (simp_all add: at_infinity_eq_at_top_bot)
   698       { fix n :: nat assume "1 < n"
   699         have "1 + x n * n = 1 + of_nat (n choose 1) * x n^1"
   700           using `1 < n` unfolding gbinomial_def binomial_gbinomial by (simp add: real_eq_of_nat[symmetric])
   701         also have "\<dots> \<le> (\<Sum>k\<in>{0, 1}. of_nat (n choose k) * x n^k)"
   702           by (simp add: x_def)
   703         also have "\<dots> \<le> (\<Sum>k=0..n. of_nat (n choose k) * x n^k)"
   704           using `1 < n` `1 \<le> c` by (intro setsum_mono2) (auto intro!: mult_nonneg_nonneg zero_le_power simp: x_def le_diff_eq)
   705         also have "\<dots> = (x n + 1) ^ n"
   706           by (simp add: binomial_ring)
   707         also have "\<dots> = c"
   708           using `1 < n` `1 \<le> c` by (simp add: x_def)
   709         finally have "x n \<le> c / n"
   710           using `1 \<le> c` `1 < n` by (simp add: field_simps) }
   711       then show "eventually (\<lambda>n. x n \<le> c / n) sequentially"
   712         by (auto intro!: exI[of _ 3] simp: eventually_sequentially)
   713       show "eventually (\<lambda>n. 0 \<le> x n) sequentially"
   714         using `1 \<le> c` by (auto intro!: exI[of _ 1] simp: eventually_sequentially le_diff_eq x_def)
   715     qed
   716     from tendsto_add[OF this tendsto_const[of 1]] have "(\<lambda>n. root n c) ----> 1"
   717       by (simp add: x_def) }
   718   note ge_1 = this
   719 
   720   show ?thesis
   721   proof cases
   722     assume "1 \<le> c" with ge_1 show ?thesis by blast
   723   next
   724     assume "\<not> 1 \<le> c"
   725     with `0 < c` have "1 \<le> 1 / c"
   726       by simp
   727     then have "(\<lambda>n. 1 / root n (1 / c)) ----> 1 / 1"
   728       by (intro tendsto_divide tendsto_const ge_1 `1 \<le> 1 / c` one_neq_zero)
   729     then show ?thesis
   730       by (rule filterlim_cong[THEN iffD1, rotated 3])
   731          (auto intro!: exI[of _ 1] simp: eventually_sequentially real_root_divide)
   732   qed
   733 qed
   734 
   735 
   736 text "Legacy theorem names:"
   737 lemmas real_root_pos2 = real_root_power_cancel
   738 lemmas real_root_pos_pos = real_root_gt_zero [THEN order_less_imp_le]
   739 lemmas real_root_pos_pos_le = real_root_ge_zero
   740 lemmas real_sqrt_mult_distrib = real_sqrt_mult
   741 lemmas real_sqrt_mult_distrib2 = real_sqrt_mult
   742 lemmas real_sqrt_eq_zero_cancel_iff = real_sqrt_eq_0_iff
   743 
   744 (* needed for CauchysMeanTheorem.het_base from AFP *)
   745 lemma real_root_pos: "0 < x \<Longrightarrow> root (Suc n) (x ^ (Suc n)) = x"
   746 by (rule real_root_power_cancel [OF zero_less_Suc order_less_imp_le])
   747 
   748 end