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