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