src/HOL/NthRoot.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62347 2230b7047376
child 62381 a6479cb85944
child 62390 842917225d56
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     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: split_if_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: split_if_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: split_if_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_le_lsqrt: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y\<^sup>2 \<Longrightarrow> sqrt x \<le> y"
   409   using real_sqrt_le_iff[of x "y\<^sup>2"] by simp
   410 
   411 lemma real_le_rsqrt: "x\<^sup>2 \<le> y \<Longrightarrow> x \<le> sqrt y"
   412   using real_sqrt_le_mono[of "x\<^sup>2" y] by simp
   413 
   414 lemma real_less_rsqrt: "x\<^sup>2 < y \<Longrightarrow> x < sqrt y"
   415   using real_sqrt_less_mono[of "x\<^sup>2" y] by simp
   416 
   417 lemma sqrt_le_D: "sqrt x \<le> y \<Longrightarrow> x \<le> y^2"
   418   by (meson not_le real_less_rsqrt)
   419 
   420 lemma sqrt_even_pow2:
   421   assumes n: "even n"
   422   shows "sqrt (2 ^ n) = 2 ^ (n div 2)"
   423 proof -
   424   from n obtain m where m: "n = 2 * m" ..
   425   from m have "sqrt (2 ^ n) = sqrt ((2 ^ m)\<^sup>2)"
   426     by (simp only: power_mult[symmetric] mult.commute)
   427   then show ?thesis
   428     using m by simp
   429 qed
   430 
   431 lemmas real_sqrt_gt_0_iff [simp] = real_sqrt_less_iff [where x=0, unfolded real_sqrt_zero]
   432 lemmas real_sqrt_lt_0_iff [simp] = real_sqrt_less_iff [where y=0, unfolded real_sqrt_zero]
   433 lemmas real_sqrt_ge_0_iff [simp] = real_sqrt_le_iff [where x=0, unfolded real_sqrt_zero]
   434 lemmas real_sqrt_le_0_iff [simp] = real_sqrt_le_iff [where y=0, unfolded real_sqrt_zero]
   435 lemmas real_sqrt_eq_0_iff [simp] = real_sqrt_eq_iff [where y=0, unfolded real_sqrt_zero]
   436 
   437 lemmas real_sqrt_gt_1_iff [simp] = real_sqrt_less_iff [where x=1, unfolded real_sqrt_one]
   438 lemmas real_sqrt_lt_1_iff [simp] = real_sqrt_less_iff [where y=1, unfolded real_sqrt_one]
   439 lemmas real_sqrt_ge_1_iff [simp] = real_sqrt_le_iff [where x=1, unfolded real_sqrt_one]
   440 lemmas real_sqrt_le_1_iff [simp] = real_sqrt_le_iff [where y=1, unfolded real_sqrt_one]
   441 lemmas real_sqrt_eq_1_iff [simp] = real_sqrt_eq_iff [where y=1, unfolded real_sqrt_one]
   442 
   443 lemma sqrt_add_le_add_sqrt:
   444   assumes "0 \<le> x" "0 \<le> y"
   445   shows "sqrt (x + y) \<le> sqrt x + sqrt y"
   446 by (rule power2_le_imp_le) (simp_all add: power2_sum assms)
   447 
   448 lemma isCont_real_sqrt: "isCont sqrt x"
   449 unfolding sqrt_def by (rule isCont_real_root)
   450 
   451 lemma tendsto_real_sqrt[tendsto_intros]:
   452   "(f \<longlongrightarrow> x) F \<Longrightarrow> ((\<lambda>x. sqrt (f x)) \<longlongrightarrow> sqrt x) F"
   453   unfolding sqrt_def by (rule tendsto_real_root)
   454 
   455 lemma continuous_real_sqrt[continuous_intros]:
   456   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sqrt (f x))"
   457   unfolding sqrt_def by (rule continuous_real_root)
   458 
   459 lemma continuous_on_real_sqrt[continuous_intros]:
   460   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sqrt (f x))"
   461   unfolding sqrt_def by (rule continuous_on_real_root)
   462 
   463 lemma DERIV_real_sqrt_generic:
   464   assumes "x \<noteq> 0"
   465   assumes "x > 0 \<Longrightarrow> D = inverse (sqrt x) / 2"
   466   assumes "x < 0 \<Longrightarrow> D = - inverse (sqrt x) / 2"
   467   shows "DERIV sqrt x :> D"
   468   using assms unfolding sqrt_def
   469   by (auto intro!: DERIV_real_root_generic)
   470 
   471 lemma DERIV_real_sqrt:
   472   "0 < x \<Longrightarrow> DERIV sqrt x :> inverse (sqrt x) / 2"
   473   using DERIV_real_sqrt_generic by simp
   474 
   475 declare
   476   DERIV_real_sqrt_generic[THEN DERIV_chain2, derivative_intros]
   477   DERIV_real_root_generic[THEN DERIV_chain2, derivative_intros]
   478 
   479 lemma not_real_square_gt_zero [simp]: "(~ (0::real) < x*x) = (x = 0)"
   480 apply auto
   481 apply (cut_tac x = x and y = 0 in linorder_less_linear)
   482 apply (simp add: zero_less_mult_iff)
   483 done
   484 
   485 lemma real_sqrt_abs2 [simp]: "sqrt(x*x) = \<bar>x\<bar>"
   486 apply (subst power2_eq_square [symmetric])
   487 apply (rule real_sqrt_abs)
   488 done
   489 
   490 lemma real_inv_sqrt_pow2: "0 < x ==> (inverse (sqrt x))\<^sup>2 = inverse x"
   491 by (simp add: power_inverse)
   492 
   493 lemma real_sqrt_eq_zero_cancel: "[| 0 \<le> x; sqrt(x) = 0|] ==> x = 0"
   494 by simp
   495 
   496 lemma real_sqrt_ge_one: "1 \<le> x ==> 1 \<le> sqrt x"
   497 by simp
   498 
   499 lemma sqrt_divide_self_eq:
   500   assumes nneg: "0 \<le> x"
   501   shows "sqrt x / x = inverse (sqrt x)"
   502 proof cases
   503   assume "x=0" thus ?thesis by simp
   504 next
   505   assume nz: "x\<noteq>0"
   506   hence pos: "0<x" using nneg by arith
   507   show ?thesis
   508   proof (rule right_inverse_eq [THEN iffD1, THEN sym])
   509     show "sqrt x / x \<noteq> 0" by (simp add: divide_inverse nneg nz)
   510     show "inverse (sqrt x) / (sqrt x / x) = 1"
   511       by (simp add: divide_inverse mult.assoc [symmetric]
   512                   power2_eq_square [symmetric] real_inv_sqrt_pow2 pos nz)
   513   qed
   514 qed
   515 
   516 lemma real_div_sqrt: "0 \<le> x \<Longrightarrow> x / sqrt x = sqrt x"
   517   apply (cases "x = 0")
   518   apply simp_all
   519   using sqrt_divide_self_eq[of x]
   520   apply (simp add: field_simps)
   521   done
   522 
   523 lemma real_divide_square_eq [simp]: "(((r::real) * a) / (r * r)) = a / r"
   524 apply (simp add: divide_inverse)
   525 apply (case_tac "r=0")
   526 apply (auto simp add: ac_simps)
   527 done
   528 
   529 lemma lemma_real_divide_sqrt_less: "0 < u ==> u / sqrt 2 < u"
   530 by (simp add: divide_less_eq)
   531 
   532 lemma four_x_squared:
   533   fixes x::real
   534   shows "4 * x\<^sup>2 = (2 * x)\<^sup>2"
   535 by (simp add: power2_eq_square)
   536 
   537 lemma sqrt_at_top: "LIM x at_top. sqrt x :: real :> at_top"
   538   by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g="power2"])
   539      (auto intro: eventually_gt_at_top)
   540 
   541 subsection \<open>Square Root of Sum of Squares\<close>
   542 
   543 lemma sum_squares_bound:
   544   fixes x:: "'a::linordered_field"
   545   shows "2*x*y \<le> x^2 + y^2"
   546 proof -
   547   have "(x-y)^2 = x*x - 2*x*y + y*y"
   548     by algebra
   549   then have "0 \<le> x^2 - 2*x*y + y^2"
   550     by (metis sum_power2_ge_zero zero_le_double_add_iff_zero_le_single_add power2_eq_square)
   551   then show ?thesis
   552     by arith
   553 qed
   554 
   555 lemma arith_geo_mean:
   556   fixes u:: "'a::linordered_field" assumes "u\<^sup>2 = x*y" "x\<ge>0" "y\<ge>0" shows "u \<le> (x + y)/2"
   557     apply (rule power2_le_imp_le)
   558     using sum_squares_bound assms
   559     apply (auto simp: zero_le_mult_iff)
   560     by (auto simp: algebra_simps power2_eq_square)
   561 
   562 lemma arith_geo_mean_sqrt:
   563   fixes x::real assumes "x\<ge>0" "y\<ge>0" shows "sqrt(x*y) \<le> (x + y)/2"
   564   apply (rule arith_geo_mean)
   565   using assms
   566   apply (auto simp: zero_le_mult_iff)
   567   done
   568 
   569 lemma real_sqrt_sum_squares_mult_ge_zero [simp]:
   570      "0 \<le> sqrt ((x\<^sup>2 + y\<^sup>2)*(xa\<^sup>2 + ya\<^sup>2))"
   571   by (metis real_sqrt_ge_0_iff split_mult_pos_le sum_power2_ge_zero)
   572 
   573 lemma real_sqrt_sum_squares_mult_squared_eq [simp]:
   574      "(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)"
   575   by (simp add: zero_le_mult_iff)
   576 
   577 lemma real_sqrt_sum_squares_eq_cancel: "sqrt (x\<^sup>2 + y\<^sup>2) = x \<Longrightarrow> y = 0"
   578 by (drule_tac f = "%x. x\<^sup>2" in arg_cong, simp)
   579 
   580 lemma real_sqrt_sum_squares_eq_cancel2: "sqrt (x\<^sup>2 + y\<^sup>2) = y \<Longrightarrow> x = 0"
   581 by (drule_tac f = "%x. x\<^sup>2" in arg_cong, simp)
   582 
   583 lemma real_sqrt_sum_squares_ge1 [simp]: "x \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   584 by (rule power2_le_imp_le, simp_all)
   585 
   586 lemma real_sqrt_sum_squares_ge2 [simp]: "y \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   587 by (rule power2_le_imp_le, simp_all)
   588 
   589 lemma real_sqrt_ge_abs1 [simp]: "\<bar>x\<bar> \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   590 by (rule power2_le_imp_le, simp_all)
   591 
   592 lemma real_sqrt_ge_abs2 [simp]: "\<bar>y\<bar> \<le> sqrt (x\<^sup>2 + y\<^sup>2)"
   593 by (rule power2_le_imp_le, simp_all)
   594 
   595 lemma le_real_sqrt_sumsq [simp]: "x \<le> sqrt (x * x + y * y)"
   596 by (simp add: power2_eq_square [symmetric])
   597 
   598 lemma real_sqrt_sum_squares_triangle_ineq:
   599   "sqrt ((a + c)\<^sup>2 + (b + d)\<^sup>2) \<le> sqrt (a\<^sup>2 + b\<^sup>2) + sqrt (c\<^sup>2 + d\<^sup>2)"
   600 apply (rule power2_le_imp_le, simp)
   601 apply (simp add: power2_sum)
   602 apply (simp only: mult.assoc distrib_left [symmetric])
   603 apply (rule mult_left_mono)
   604 apply (rule power2_le_imp_le)
   605 apply (simp add: power2_sum power_mult_distrib)
   606 apply (simp add: ring_distribs)
   607 apply (subgoal_tac "0 \<le> b\<^sup>2 * c\<^sup>2 + a\<^sup>2 * d\<^sup>2 - 2 * (a * c) * (b * d)", simp)
   608 apply (rule_tac b="(a * d - b * c)\<^sup>2" in ord_le_eq_trans)
   609 apply (rule zero_le_power2)
   610 apply (simp add: power2_diff power_mult_distrib)
   611 apply (simp)
   612 apply simp
   613 apply (simp add: add_increasing)
   614 done
   615 
   616 lemma real_sqrt_sum_squares_less:
   617   "\<lbrakk>\<bar>x\<bar> < u / sqrt 2; \<bar>y\<bar> < u / sqrt 2\<rbrakk> \<Longrightarrow> sqrt (x\<^sup>2 + y\<^sup>2) < u"
   618 apply (rule power2_less_imp_less, simp)
   619 apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
   620 apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
   621 apply (simp add: power_divide)
   622 apply (drule order_le_less_trans [OF abs_ge_zero])
   623 apply (simp add: zero_less_divide_iff)
   624 done
   625 
   626 lemma sqrt2_less_2: "sqrt 2 < (2::real)"
   627   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))
   628 
   629 
   630 text\<open>Needed for the infinitely close relation over the nonstandard
   631     complex numbers\<close>
   632 lemma lemma_sqrt_hcomplex_capprox:
   633      "[| 0 < u; x < u/2; y < u/2; 0 \<le> x; 0 \<le> y |] ==> sqrt (x\<^sup>2 + y\<^sup>2) < u"
   634   apply (rule real_sqrt_sum_squares_less)
   635   apply (auto simp add: abs_if field_simps)
   636   apply (rule le_less_trans [where y = "x*2"])
   637   using less_eq_real_def sqrt2_less_2 apply force
   638   apply assumption
   639   apply (rule le_less_trans [where y = "y*2"])
   640   using less_eq_real_def sqrt2_less_2 mult_le_cancel_left
   641   apply auto
   642   done
   643 
   644 lemma LIMSEQ_root: "(\<lambda>n. root n n) \<longlonglongrightarrow> 1"
   645 proof -
   646   def x \<equiv> "\<lambda>n. root n n - 1"
   647   have "x \<longlonglongrightarrow> sqrt 0"
   648   proof (rule tendsto_sandwich[OF _ _ tendsto_const])
   649     show "(\<lambda>x. sqrt (2 / x)) \<longlonglongrightarrow> sqrt 0"
   650       by (intro tendsto_intros tendsto_divide_0[OF tendsto_const] filterlim_mono[OF filterlim_real_sequentially])
   651          (simp_all add: at_infinity_eq_at_top_bot)
   652     { fix n :: nat assume "2 < n"
   653       have "1 + (real (n - 1) * n) / 2 * x n^2 = 1 + of_nat (n choose 2) * x n^2"
   654         using \<open>2 < n\<close> unfolding gbinomial_def binomial_gbinomial
   655         by (simp add: atLeast0AtMost atMost_Suc field_simps of_nat_diff numeral_2_eq_2)
   656       also have "\<dots> \<le> (\<Sum>k\<in>{0, 2}. of_nat (n choose k) * x n^k)"
   657         by (simp add: x_def)
   658       also have "\<dots> \<le> (\<Sum>k=0..n. of_nat (n choose k) * x n^k)"
   659         using \<open>2 < n\<close> by (intro setsum_mono2) (auto intro!: mult_nonneg_nonneg zero_le_power simp: x_def le_diff_eq)
   660       also have "\<dots> = (x n + 1) ^ n"
   661         by (simp add: binomial_ring)
   662       also have "\<dots> = n"
   663         using \<open>2 < n\<close> by (simp add: x_def)
   664       finally have "real (n - 1) * (real n / 2 * (x n)\<^sup>2) \<le> real (n - 1) * 1"
   665         by simp
   666       then have "(x n)\<^sup>2 \<le> 2 / real n"
   667         using \<open>2 < n\<close> unfolding mult_le_cancel_left by (simp add: field_simps)
   668       from real_sqrt_le_mono[OF this] have "x n \<le> sqrt (2 / real n)"
   669         by simp }
   670     then show "eventually (\<lambda>n. x n \<le> sqrt (2 / real n)) sequentially"
   671       by (auto intro!: exI[of _ 3] simp: eventually_sequentially)
   672     show "eventually (\<lambda>n. sqrt 0 \<le> x n) sequentially"
   673       by (auto intro!: exI[of _ 1] simp: eventually_sequentially le_diff_eq x_def)
   674   qed
   675   from tendsto_add[OF this tendsto_const[of 1]] show ?thesis
   676     by (simp add: x_def)
   677 qed
   678 
   679 lemma LIMSEQ_root_const:
   680   assumes "0 < c"
   681   shows "(\<lambda>n. root n c) \<longlonglongrightarrow> 1"
   682 proof -
   683   { fix c :: real assume "1 \<le> c"
   684     def x \<equiv> "\<lambda>n. root n c - 1"
   685     have "x \<longlonglongrightarrow> 0"
   686     proof (rule tendsto_sandwich[OF _ _ tendsto_const])
   687       show "(\<lambda>n. c / n) \<longlonglongrightarrow> 0"
   688         by (intro tendsto_divide_0[OF tendsto_const] filterlim_mono[OF filterlim_real_sequentially])
   689            (simp_all add: at_infinity_eq_at_top_bot)
   690       { fix n :: nat assume "1 < n"
   691         have "1 + x n * n = 1 + of_nat (n choose 1) * x n^1"
   692           using \<open>1 < n\<close> unfolding gbinomial_def binomial_gbinomial by simp
   693         also have "\<dots> \<le> (\<Sum>k\<in>{0, 1}. of_nat (n choose k) * x n^k)"
   694           by (simp add: x_def)
   695         also have "\<dots> \<le> (\<Sum>k=0..n. of_nat (n choose k) * x n^k)"
   696           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)
   697         also have "\<dots> = (x n + 1) ^ n"
   698           by (simp add: binomial_ring)
   699         also have "\<dots> = c"
   700           using \<open>1 < n\<close> \<open>1 \<le> c\<close> by (simp add: x_def)
   701         finally have "x n \<le> c / n"
   702           using \<open>1 \<le> c\<close> \<open>1 < n\<close> by (simp add: field_simps) }
   703       then show "eventually (\<lambda>n. x n \<le> c / n) sequentially"
   704         by (auto intro!: exI[of _ 3] simp: eventually_sequentially)
   705       show "eventually (\<lambda>n. 0 \<le> x n) sequentially"
   706         using \<open>1 \<le> c\<close> by (auto intro!: exI[of _ 1] simp: eventually_sequentially le_diff_eq x_def)
   707     qed
   708     from tendsto_add[OF this tendsto_const[of 1]] have "(\<lambda>n. root n c) \<longlonglongrightarrow> 1"
   709       by (simp add: x_def) }
   710   note ge_1 = this
   711 
   712   show ?thesis
   713   proof cases
   714     assume "1 \<le> c" with ge_1 show ?thesis by blast
   715   next
   716     assume "\<not> 1 \<le> c"
   717     with \<open>0 < c\<close> have "1 \<le> 1 / c"
   718       by simp
   719     then have "(\<lambda>n. 1 / root n (1 / c)) \<longlonglongrightarrow> 1 / 1"
   720       by (intro tendsto_divide tendsto_const ge_1 \<open>1 \<le> 1 / c\<close> one_neq_zero)
   721     then show ?thesis
   722       by (rule filterlim_cong[THEN iffD1, rotated 3])
   723          (auto intro!: exI[of _ 1] simp: eventually_sequentially real_root_divide)
   724   qed
   725 qed
   726 
   727 
   728 text "Legacy theorem names:"
   729 lemmas real_root_pos2 = real_root_power_cancel
   730 lemmas real_root_pos_pos = real_root_gt_zero [THEN order_less_imp_le]
   731 lemmas real_root_pos_pos_le = real_root_ge_zero
   732 lemmas real_sqrt_mult_distrib = real_sqrt_mult
   733 lemmas real_sqrt_mult_distrib2 = real_sqrt_mult
   734 lemmas real_sqrt_eq_zero_cancel_iff = real_sqrt_eq_0_iff
   735 
   736 end