src/HOL/NthRoot.thy
author hoelzl
Tue Mar 26 12:20:58 2013 +0100 (2013-03-26)
changeset 51526 155263089e7b
parent 51483 dc39d69774bb
child 53015 a1119cf551e8
permissions -rw-r--r--
move SEQ.thy and Lim.thy to Limits.thy
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
huffman@22956
     7
header {* Nth Roots of Real Numbers *}
paulson@14324
     8
nipkow@15131
     9
theory NthRoot
haftmann@28952
    10
imports Parity Deriv
nipkow@15131
    11
begin
paulson@14324
    12
hoelzl@51483
    13
lemma abs_sgn_eq: "abs (sgn x :: real) = (if x = 0 then 0 else 1)"
hoelzl@51483
    14
  by (simp add: sgn_real_def)
hoelzl@51483
    15
hoelzl@51483
    16
lemma inverse_sgn: "sgn (inverse a) = inverse (sgn a :: real)"
hoelzl@51483
    17
  by (simp add: sgn_real_def)
hoelzl@51483
    18
hoelzl@51483
    19
lemma power_eq_iff_eq_base: 
hoelzl@51483
    20
  fixes a b :: "_ :: linordered_semidom"
hoelzl@51483
    21
  shows "0 < n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a ^ n = b ^ n \<longleftrightarrow> a = b"
hoelzl@51483
    22
  using power_eq_imp_eq_base[of a n b] by auto
hoelzl@51483
    23
huffman@22956
    24
subsection {* Existence of Nth Root *}
huffman@20687
    25
huffman@23009
    26
text {* Existence follows from the Intermediate Value Theorem *}
paulson@14324
    27
huffman@23009
    28
lemma realpow_pos_nth:
huffman@23009
    29
  assumes n: "0 < n"
huffman@23009
    30
  assumes a: "0 < a"
huffman@23009
    31
  shows "\<exists>r>0. r ^ n = (a::real)"
huffman@23009
    32
proof -
huffman@23009
    33
  have "\<exists>r\<ge>0. r \<le> (max 1 a) \<and> r ^ n = a"
huffman@23009
    34
  proof (rule IVT)
huffman@23009
    35
    show "0 ^ n \<le> a" using n a by (simp add: power_0_left)
huffman@23009
    36
    show "0 \<le> max 1 a" by simp
huffman@23009
    37
    from n have n1: "1 \<le> n" by simp
huffman@23009
    38
    have "a \<le> max 1 a ^ 1" by simp
huffman@23009
    39
    also have "max 1 a ^ 1 \<le> max 1 a ^ n"
huffman@23009
    40
      using n1 by (rule power_increasing, simp)
huffman@23009
    41
    finally show "a \<le> max 1 a ^ n" .
huffman@23009
    42
    show "\<forall>r. 0 \<le> r \<and> r \<le> max 1 a \<longrightarrow> isCont (\<lambda>x. x ^ n) r"
huffman@44289
    43
      by simp
huffman@23009
    44
  qed
huffman@23009
    45
  then obtain r where r: "0 \<le> r \<and> r ^ n = a" by fast
huffman@23009
    46
  with n a have "r \<noteq> 0" by (auto simp add: power_0_left)
huffman@23009
    47
  with r have "0 < r \<and> r ^ n = a" by simp
huffman@23009
    48
  thus ?thesis ..
huffman@23009
    49
qed
paulson@14325
    50
huffman@23047
    51
(* Used by Integration/RealRandVar.thy in AFP *)
huffman@23047
    52
lemma realpow_pos_nth2: "(0::real) < a \<Longrightarrow> \<exists>r>0. r ^ Suc n = a"
huffman@23047
    53
by (blast intro: realpow_pos_nth)
huffman@23047
    54
huffman@23009
    55
text {* Uniqueness of nth positive root *}
paulson@14324
    56
hoelzl@51483
    57
lemma realpow_pos_nth_unique: "\<lbrakk>0 < n; 0 < a\<rbrakk> \<Longrightarrow> \<exists>!r. 0 < r \<and> r ^ n = (a::real)"
hoelzl@51483
    58
  by (auto intro!: realpow_pos_nth simp: power_eq_iff_eq_base)
paulson@14324
    59
huffman@20687
    60
subsection {* Nth Root *}
huffman@20687
    61
huffman@22956
    62
text {* We define roots of negative reals such that
huffman@22956
    63
  @{term "root n (- x) = - root n x"}. This allows
huffman@22956
    64
  us to omit side conditions from many theorems. *}
huffman@20687
    65
hoelzl@51483
    66
lemma inj_sgn_power: assumes "0 < n" shows "inj (\<lambda>y. sgn y * \<bar>y\<bar>^n :: real)" (is "inj ?f")
hoelzl@51483
    67
proof (rule injI)
hoelzl@51483
    68
  have x: "\<And>a b :: real. (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b) \<Longrightarrow> a \<noteq> b" by auto
hoelzl@51483
    69
  fix x y assume "?f x = ?f y" with power_eq_iff_eq_base[of n "\<bar>x\<bar>" "\<bar>y\<bar>"] `0<n` show "x = y"
hoelzl@51483
    70
    by (cases rule: linorder_cases[of 0 x, case_product linorder_cases[of 0 y]])
hoelzl@51483
    71
       (simp_all add: x)
hoelzl@51483
    72
qed
hoelzl@51483
    73
hoelzl@51483
    74
lemma sgn_power_injE: "sgn a * \<bar>a\<bar> ^ n = x \<Longrightarrow> x = sgn b * \<bar>b\<bar> ^ n \<Longrightarrow> 0 < n \<Longrightarrow> a = (b::real)"
hoelzl@51483
    75
  using inj_sgn_power[THEN injD, of n a b] by simp
hoelzl@51483
    76
hoelzl@51483
    77
definition root :: "nat \<Rightarrow> real \<Rightarrow> real" where
hoelzl@51483
    78
  "root n x = (if n = 0 then 0 else the_inv (\<lambda>y. sgn y * \<bar>y\<bar>^n) x)"
hoelzl@51483
    79
hoelzl@51483
    80
lemma root_0 [simp]: "root 0 x = 0"
hoelzl@51483
    81
  by (simp add: root_def)
hoelzl@51483
    82
hoelzl@51483
    83
lemma root_sgn_power: "0 < n \<Longrightarrow> root n (sgn y * \<bar>y\<bar>^n) = y"
hoelzl@51483
    84
  using the_inv_f_f[OF inj_sgn_power] by (simp add: root_def)
hoelzl@51483
    85
hoelzl@51483
    86
lemma sgn_power_root:
hoelzl@51483
    87
  assumes "0 < n" shows "sgn (root n x) * \<bar>(root n x)\<bar>^n = x" (is "?f (root n x) = x")
hoelzl@51483
    88
proof cases
hoelzl@51483
    89
  assume "x \<noteq> 0"
hoelzl@51483
    90
  with realpow_pos_nth[OF `0 < n`, of "\<bar>x\<bar>"] obtain r where "0 < r" "r ^ n = \<bar>x\<bar>" by auto
hoelzl@51483
    91
  with `x \<noteq> 0` have S: "x \<in> range ?f"
hoelzl@51483
    92
    by (intro image_eqI[of _ _ "sgn x * r"])
hoelzl@51483
    93
       (auto simp: abs_mult sgn_mult power_mult_distrib abs_sgn_eq mult_sgn_abs)
hoelzl@51483
    94
  from `0 < n` f_the_inv_into_f[OF inj_sgn_power[OF `0 < n`] this]  show ?thesis
hoelzl@51483
    95
    by (simp add: root_def)
hoelzl@51483
    96
qed (insert `0 < n` root_sgn_power[of n 0], simp)
hoelzl@51483
    97
hoelzl@51483
    98
lemma split_root: "P (root n x) \<longleftrightarrow> (n = 0 \<longrightarrow> P 0) \<and> (0 < n \<longrightarrow> (\<forall>y. sgn y * \<bar>y\<bar>^n = x \<longrightarrow> P y))"
hoelzl@51483
    99
  apply (cases "n = 0")
hoelzl@51483
   100
  apply simp_all
hoelzl@51483
   101
  apply (metis root_sgn_power sgn_power_root)
hoelzl@51483
   102
  done
huffman@20687
   103
huffman@22956
   104
lemma real_root_zero [simp]: "root n 0 = 0"
hoelzl@51483
   105
  by (simp split: split_root add: sgn_zero_iff)
hoelzl@51483
   106
hoelzl@51483
   107
lemma real_root_minus: "root n (- x) = - root n x"
hoelzl@51483
   108
  by (clarsimp split: split_root elim!: sgn_power_injE simp: sgn_minus)
huffman@22956
   109
hoelzl@51483
   110
lemma real_root_less_mono: "\<lbrakk>0 < n; x < y\<rbrakk> \<Longrightarrow> root n x < root n y"
hoelzl@51483
   111
proof (clarsimp split: split_root)
hoelzl@51483
   112
  have x: "\<And>a b :: real. (0 < b \<and> a < 0) \<Longrightarrow> \<not> a > b" by auto
hoelzl@51483
   113
  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
   114
    using power_less_imp_less_base[of a n b]  power_less_imp_less_base[of "-b" n "-a"]
hoelzl@51483
   115
    by (simp add: sgn_real_def power_less_zero_eq x[of "a ^ n" "- ((- b) ^ n)"] split: split_if_asm)
hoelzl@51483
   116
qed
huffman@22956
   117
huffman@22956
   118
lemma real_root_gt_zero: "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> 0 < root n x"
hoelzl@51483
   119
  using real_root_less_mono[of n 0 x] by simp
hoelzl@51483
   120
hoelzl@51483
   121
lemma real_root_ge_zero: "0 \<le> x \<Longrightarrow> 0 \<le> root n x"
hoelzl@51483
   122
  using real_root_gt_zero[of n x] by (cases "n = 0") (auto simp add: le_less)
huffman@20687
   123
huffman@22956
   124
lemma real_root_pow_pos: (* TODO: rename *)
huffman@22956
   125
  "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
hoelzl@51483
   126
  using sgn_power_root[of n x] real_root_gt_zero[of n x] by simp
huffman@20687
   127
huffman@22956
   128
lemma real_root_pow_pos2 [simp]: (* TODO: rename *)
huffman@22956
   129
  "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
huffman@22956
   130
by (auto simp add: order_le_less real_root_pow_pos)
huffman@22956
   131
hoelzl@51483
   132
lemma sgn_root: "0 < n \<Longrightarrow> sgn (root n x) = sgn x"
hoelzl@51483
   133
  by (auto split: split_root simp: sgn_real_def power_less_zero_eq)
hoelzl@51483
   134
huffman@23046
   135
lemma odd_real_root_pow: "odd n \<Longrightarrow> root n x ^ n = x"
hoelzl@51483
   136
  using sgn_power_root[of n x] by (simp add: odd_pos sgn_real_def split: split_if_asm)
huffman@20687
   137
huffman@22956
   138
lemma real_root_power_cancel: "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n (x ^ n) = x"
hoelzl@51483
   139
  using root_sgn_power[of n x] by (auto simp add: le_less power_0_left)
huffman@20687
   140
huffman@23046
   141
lemma odd_real_root_power_cancel: "odd n \<Longrightarrow> root n (x ^ n) = x"
hoelzl@51483
   142
  using root_sgn_power[of n x] by (simp add: odd_pos sgn_real_def power_0_left split: split_if_asm)
huffman@23046
   143
hoelzl@51483
   144
lemma real_root_pos_unique: "\<lbrakk>0 < n; 0 \<le> y; y ^ n = x\<rbrakk> \<Longrightarrow> root n x = y"
hoelzl@51483
   145
  using root_sgn_power[of n y] by (auto simp add: le_less power_0_left)
huffman@22956
   146
huffman@23046
   147
lemma odd_real_root_unique:
huffman@23046
   148
  "\<lbrakk>odd n; y ^ n = x\<rbrakk> \<Longrightarrow> root n x = y"
huffman@23046
   149
by (erule subst, rule odd_real_root_power_cancel)
huffman@23046
   150
huffman@22956
   151
lemma real_root_one [simp]: "0 < n \<Longrightarrow> root n 1 = 1"
huffman@22956
   152
by (simp add: real_root_pos_unique)
huffman@22956
   153
huffman@22956
   154
text {* Root function is strictly monotonic, hence injective *}
huffman@22956
   155
huffman@22956
   156
lemma real_root_le_mono: "\<lbrakk>0 < n; x \<le> y\<rbrakk> \<Longrightarrow> root n x \<le> root n y"
hoelzl@51483
   157
  by (auto simp add: order_le_less real_root_less_mono)
huffman@22956
   158
huffman@22721
   159
lemma real_root_less_iff [simp]:
huffman@22956
   160
  "0 < n \<Longrightarrow> (root n x < root n y) = (x < y)"
huffman@22956
   161
apply (cases "x < y")
huffman@22956
   162
apply (simp add: real_root_less_mono)
huffman@22956
   163
apply (simp add: linorder_not_less real_root_le_mono)
huffman@22721
   164
done
huffman@22721
   165
huffman@22721
   166
lemma real_root_le_iff [simp]:
huffman@22956
   167
  "0 < n \<Longrightarrow> (root n x \<le> root n y) = (x \<le> y)"
huffman@22956
   168
apply (cases "x \<le> y")
huffman@22956
   169
apply (simp add: real_root_le_mono)
huffman@22956
   170
apply (simp add: linorder_not_le real_root_less_mono)
huffman@22721
   171
done
huffman@22721
   172
huffman@22721
   173
lemma real_root_eq_iff [simp]:
huffman@22956
   174
  "0 < n \<Longrightarrow> (root n x = root n y) = (x = y)"
huffman@22956
   175
by (simp add: order_eq_iff)
huffman@22956
   176
huffman@22956
   177
lemmas real_root_gt_0_iff [simp] = real_root_less_iff [where x=0, simplified]
huffman@22956
   178
lemmas real_root_lt_0_iff [simp] = real_root_less_iff [where y=0, simplified]
huffman@22956
   179
lemmas real_root_ge_0_iff [simp] = real_root_le_iff [where x=0, simplified]
huffman@22956
   180
lemmas real_root_le_0_iff [simp] = real_root_le_iff [where y=0, simplified]
huffman@22956
   181
lemmas real_root_eq_0_iff [simp] = real_root_eq_iff [where y=0, simplified]
huffman@22721
   182
huffman@23257
   183
lemma real_root_gt_1_iff [simp]: "0 < n \<Longrightarrow> (1 < root n y) = (1 < y)"
huffman@23257
   184
by (insert real_root_less_iff [where x=1], simp)
huffman@23257
   185
huffman@23257
   186
lemma real_root_lt_1_iff [simp]: "0 < n \<Longrightarrow> (root n x < 1) = (x < 1)"
huffman@23257
   187
by (insert real_root_less_iff [where y=1], simp)
huffman@23257
   188
huffman@23257
   189
lemma real_root_ge_1_iff [simp]: "0 < n \<Longrightarrow> (1 \<le> root n y) = (1 \<le> y)"
huffman@23257
   190
by (insert real_root_le_iff [where x=1], simp)
huffman@23257
   191
huffman@23257
   192
lemma real_root_le_1_iff [simp]: "0 < n \<Longrightarrow> (root n x \<le> 1) = (x \<le> 1)"
huffman@23257
   193
by (insert real_root_le_iff [where y=1], simp)
huffman@23257
   194
huffman@23257
   195
lemma real_root_eq_1_iff [simp]: "0 < n \<Longrightarrow> (root n x = 1) = (x = 1)"
huffman@23257
   196
by (insert real_root_eq_iff [where y=1], simp)
huffman@23257
   197
hoelzl@51483
   198
text {* Roots of multiplication and division *}
hoelzl@51483
   199
hoelzl@51483
   200
lemma real_root_mult: "root n (x * y) = root n x * root n y"
hoelzl@51483
   201
  by (auto split: split_root elim!: sgn_power_injE simp: sgn_mult abs_mult power_mult_distrib)
hoelzl@51483
   202
hoelzl@51483
   203
lemma real_root_inverse: "root n (inverse x) = inverse (root n x)"
hoelzl@51483
   204
  by (auto split: split_root elim!: sgn_power_injE simp: inverse_sgn power_inverse)
hoelzl@51483
   205
hoelzl@51483
   206
lemma real_root_divide: "root n (x / y) = root n x / root n y"
hoelzl@51483
   207
  by (simp add: divide_inverse real_root_mult real_root_inverse)
hoelzl@51483
   208
hoelzl@51483
   209
lemma real_root_abs: "0 < n \<Longrightarrow> root n \<bar>x\<bar> = \<bar>root n x\<bar>"
hoelzl@51483
   210
  by (simp add: abs_if real_root_minus)
hoelzl@51483
   211
hoelzl@51483
   212
lemma real_root_power: "0 < n \<Longrightarrow> root n (x ^ k) = root n x ^ k"
hoelzl@51483
   213
  by (induct k) (simp_all add: real_root_mult)
hoelzl@51483
   214
huffman@23257
   215
text {* Roots of roots *}
huffman@23257
   216
huffman@23257
   217
lemma real_root_Suc_0 [simp]: "root (Suc 0) x = x"
huffman@23257
   218
by (simp add: odd_real_root_unique)
huffman@23257
   219
hoelzl@51483
   220
lemma real_root_mult_exp: "root (m * n) x = root m (root n x)"
hoelzl@51483
   221
  by (auto split: split_root elim!: sgn_power_injE
hoelzl@51483
   222
           simp: sgn_zero_iff sgn_mult power_mult[symmetric] abs_mult power_mult_distrib abs_sgn_eq)
huffman@23257
   223
hoelzl@51483
   224
lemma real_root_commute: "root m (root n x) = root n (root m x)"
hoelzl@51483
   225
  by (simp add: real_root_mult_exp [symmetric] mult_commute)
huffman@23257
   226
huffman@23257
   227
text {* Monotonicity in first argument *}
huffman@23257
   228
huffman@23257
   229
lemma real_root_strict_decreasing:
huffman@23257
   230
  "\<lbrakk>0 < n; n < N; 1 < x\<rbrakk> \<Longrightarrow> root N x < root n x"
huffman@23257
   231
apply (subgoal_tac "root n (root N x) ^ n < root N (root n x) ^ N", simp)
huffman@23257
   232
apply (simp add: real_root_commute power_strict_increasing
huffman@23257
   233
            del: real_root_pow_pos2)
huffman@23257
   234
done
huffman@23257
   235
huffman@23257
   236
lemma real_root_strict_increasing:
huffman@23257
   237
  "\<lbrakk>0 < n; n < N; 0 < x; x < 1\<rbrakk> \<Longrightarrow> root n x < root N x"
huffman@23257
   238
apply (subgoal_tac "root N (root n x) ^ N < root n (root N x) ^ n", simp)
huffman@23257
   239
apply (simp add: real_root_commute power_strict_decreasing
huffman@23257
   240
            del: real_root_pow_pos2)
huffman@23257
   241
done
huffman@23257
   242
huffman@23257
   243
lemma real_root_decreasing:
huffman@23257
   244
  "\<lbrakk>0 < n; n < N; 1 \<le> x\<rbrakk> \<Longrightarrow> root N x \<le> root n x"
huffman@23257
   245
by (auto simp add: order_le_less real_root_strict_decreasing)
huffman@23257
   246
huffman@23257
   247
lemma real_root_increasing:
huffman@23257
   248
  "\<lbrakk>0 < n; n < N; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> root n x \<le> root N x"
huffman@23257
   249
by (auto simp add: order_le_less real_root_strict_increasing)
huffman@23257
   250
huffman@23042
   251
text {* Continuity and derivatives *}
huffman@23042
   252
hoelzl@51483
   253
lemma isCont_real_root: "isCont (root n) x"
hoelzl@51483
   254
proof cases
hoelzl@51483
   255
  assume n: "0 < n"
hoelzl@51483
   256
  let ?f = "\<lambda>y::real. sgn y * \<bar>y\<bar>^n"
hoelzl@51483
   257
  have "continuous_on ({0..} \<union> {.. 0}) (\<lambda>x. if 0 < x then x ^ n else - ((-x) ^ n) :: real)"
hoelzl@51483
   258
    using n by (intro continuous_on_If continuous_on_intros) auto
hoelzl@51483
   259
  then have "continuous_on UNIV ?f"
hoelzl@51483
   260
    by (rule continuous_on_cong[THEN iffD1, rotated 2]) (auto simp: not_less real_sgn_neg le_less n)
hoelzl@51483
   261
  then have [simp]: "\<And>x. isCont ?f x"
hoelzl@51483
   262
    by (simp add: continuous_on_eq_continuous_at)
huffman@23042
   263
hoelzl@51483
   264
  have "isCont (root n) (?f (root n x))"
hoelzl@51483
   265
    by (rule isCont_inverse_function [where f="?f" and d=1]) (auto simp: root_sgn_power n)
hoelzl@51483
   266
  then show ?thesis
hoelzl@51483
   267
    by (simp add: sgn_power_root n)
hoelzl@51483
   268
qed (simp add: root_def[abs_def])
huffman@23042
   269
hoelzl@51478
   270
lemma tendsto_real_root[tendsto_intros]:
hoelzl@51483
   271
  "(f ---> x) F \<Longrightarrow> ((\<lambda>x. root n (f x)) ---> root n x) F"
hoelzl@51483
   272
  using isCont_tendsto_compose[OF isCont_real_root, of f x F] .
hoelzl@51478
   273
hoelzl@51478
   274
lemma continuous_real_root[continuous_intros]:
hoelzl@51483
   275
  "continuous F f \<Longrightarrow> continuous F (\<lambda>x. root n (f x))"
hoelzl@51478
   276
  unfolding continuous_def by (rule tendsto_real_root)
hoelzl@51478
   277
  
hoelzl@51478
   278
lemma continuous_on_real_root[continuous_on_intros]:
hoelzl@51483
   279
  "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. root n (f x))"
hoelzl@51478
   280
  unfolding continuous_on_def by (auto intro: tendsto_real_root)
hoelzl@51478
   281
huffman@23042
   282
lemma DERIV_real_root:
huffman@23042
   283
  assumes n: "0 < n"
huffman@23042
   284
  assumes x: "0 < x"
huffman@23042
   285
  shows "DERIV (root n) x :> inverse (real n * root n x ^ (n - Suc 0))"
huffman@23042
   286
proof (rule DERIV_inverse_function)
huffman@23044
   287
  show "0 < x" using x .
huffman@23044
   288
  show "x < x + 1" by simp
huffman@23044
   289
  show "\<forall>y. 0 < y \<and> y < x + 1 \<longrightarrow> root n y ^ n = y"
huffman@23042
   290
    using n by simp
huffman@23042
   291
  show "DERIV (\<lambda>x. x ^ n) (root n x) :> real n * root n x ^ (n - Suc 0)"
huffman@23042
   292
    by (rule DERIV_pow)
huffman@23042
   293
  show "real n * root n x ^ (n - Suc 0) \<noteq> 0"
huffman@23042
   294
    using n x by simp
hoelzl@51483
   295
qed (rule isCont_real_root)
huffman@23042
   296
huffman@23046
   297
lemma DERIV_odd_real_root:
huffman@23046
   298
  assumes n: "odd n"
huffman@23046
   299
  assumes x: "x \<noteq> 0"
huffman@23046
   300
  shows "DERIV (root n) x :> inverse (real n * root n x ^ (n - Suc 0))"
huffman@23046
   301
proof (rule DERIV_inverse_function)
huffman@23046
   302
  show "x - 1 < x" by simp
huffman@23046
   303
  show "x < x + 1" by simp
huffman@23046
   304
  show "\<forall>y. x - 1 < y \<and> y < x + 1 \<longrightarrow> root n y ^ n = y"
huffman@23046
   305
    using n by (simp add: odd_real_root_pow)
huffman@23046
   306
  show "DERIV (\<lambda>x. x ^ n) (root n x) :> real n * root n x ^ (n - Suc 0)"
huffman@23046
   307
    by (rule DERIV_pow)
huffman@23046
   308
  show "real n * root n x ^ (n - Suc 0) \<noteq> 0"
huffman@23046
   309
    using odd_pos [OF n] x by simp
hoelzl@51483
   310
qed (rule isCont_real_root)
huffman@23046
   311
hoelzl@31880
   312
lemma DERIV_even_real_root:
hoelzl@31880
   313
  assumes n: "0 < n" and "even n"
hoelzl@31880
   314
  assumes x: "x < 0"
hoelzl@31880
   315
  shows "DERIV (root n) x :> inverse (- real n * root n x ^ (n - Suc 0))"
hoelzl@31880
   316
proof (rule DERIV_inverse_function)
hoelzl@31880
   317
  show "x - 1 < x" by simp
hoelzl@31880
   318
  show "x < 0" using x .
hoelzl@31880
   319
next
hoelzl@31880
   320
  show "\<forall>y. x - 1 < y \<and> y < 0 \<longrightarrow> - (root n y ^ n) = y"
hoelzl@31880
   321
  proof (rule allI, rule impI, erule conjE)
hoelzl@31880
   322
    fix y assume "x - 1 < y" and "y < 0"
hoelzl@31880
   323
    hence "root n (-y) ^ n = -y" using `0 < n` by simp
hoelzl@51483
   324
    with real_root_minus and `even n`
hoelzl@31880
   325
    show "- (root n y ^ n) = y" by simp
hoelzl@31880
   326
  qed
hoelzl@31880
   327
next
hoelzl@31880
   328
  show "DERIV (\<lambda>x. - (x ^ n)) (root n x) :> - real n * root n x ^ (n - Suc 0)"
hoelzl@31880
   329
    by  (auto intro!: DERIV_intros)
hoelzl@31880
   330
  show "- real n * root n x ^ (n - Suc 0) \<noteq> 0"
hoelzl@31880
   331
    using n x by simp
hoelzl@51483
   332
qed (rule isCont_real_root)
hoelzl@31880
   333
hoelzl@31880
   334
lemma DERIV_real_root_generic:
hoelzl@31880
   335
  assumes "0 < n" and "x \<noteq> 0"
wenzelm@49753
   336
    and "\<lbrakk> even n ; 0 < x \<rbrakk> \<Longrightarrow> D = inverse (real n * root n x ^ (n - Suc 0))"
wenzelm@49753
   337
    and "\<lbrakk> even n ; x < 0 \<rbrakk> \<Longrightarrow> D = - inverse (real n * root n x ^ (n - Suc 0))"
wenzelm@49753
   338
    and "odd n \<Longrightarrow> D = inverse (real n * root n x ^ (n - Suc 0))"
hoelzl@31880
   339
  shows "DERIV (root n) x :> D"
hoelzl@31880
   340
using assms by (cases "even n", cases "0 < x",
hoelzl@31880
   341
  auto intro: DERIV_real_root[THEN DERIV_cong]
hoelzl@31880
   342
              DERIV_odd_real_root[THEN DERIV_cong]
hoelzl@31880
   343
              DERIV_even_real_root[THEN DERIV_cong])
hoelzl@31880
   344
huffman@22956
   345
subsection {* Square Root *}
huffman@20687
   346
hoelzl@51483
   347
definition sqrt :: "real \<Rightarrow> real" where
huffman@22956
   348
  "sqrt = root 2"
huffman@20687
   349
huffman@22956
   350
lemma pos2: "0 < (2::nat)" by simp
huffman@22956
   351
huffman@22956
   352
lemma real_sqrt_unique: "\<lbrakk>y\<twosuperior> = x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt x = y"
huffman@22956
   353
unfolding sqrt_def by (rule real_root_pos_unique [OF pos2])
huffman@20687
   354
huffman@22956
   355
lemma real_sqrt_abs [simp]: "sqrt (x\<twosuperior>) = \<bar>x\<bar>"
huffman@22956
   356
apply (rule real_sqrt_unique)
huffman@22956
   357
apply (rule power2_abs)
huffman@22956
   358
apply (rule abs_ge_zero)
huffman@22956
   359
done
huffman@20687
   360
huffman@22956
   361
lemma real_sqrt_pow2 [simp]: "0 \<le> x \<Longrightarrow> (sqrt x)\<twosuperior> = x"
huffman@22956
   362
unfolding sqrt_def by (rule real_root_pow_pos2 [OF pos2])
huffman@22856
   363
huffman@22956
   364
lemma real_sqrt_pow2_iff [simp]: "((sqrt x)\<twosuperior> = x) = (0 \<le> x)"
huffman@22856
   365
apply (rule iffI)
huffman@22856
   366
apply (erule subst)
huffman@22856
   367
apply (rule zero_le_power2)
huffman@22856
   368
apply (erule real_sqrt_pow2)
huffman@20687
   369
done
huffman@20687
   370
huffman@22956
   371
lemma real_sqrt_zero [simp]: "sqrt 0 = 0"
huffman@22956
   372
unfolding sqrt_def by (rule real_root_zero)
huffman@22956
   373
huffman@22956
   374
lemma real_sqrt_one [simp]: "sqrt 1 = 1"
huffman@22956
   375
unfolding sqrt_def by (rule real_root_one [OF pos2])
huffman@22956
   376
huffman@22956
   377
lemma real_sqrt_minus: "sqrt (- x) = - sqrt x"
hoelzl@51483
   378
unfolding sqrt_def by (rule real_root_minus)
huffman@22956
   379
huffman@22956
   380
lemma real_sqrt_mult: "sqrt (x * y) = sqrt x * sqrt y"
hoelzl@51483
   381
unfolding sqrt_def by (rule real_root_mult)
huffman@22956
   382
huffman@22956
   383
lemma real_sqrt_inverse: "sqrt (inverse x) = inverse (sqrt x)"
hoelzl@51483
   384
unfolding sqrt_def by (rule real_root_inverse)
huffman@22956
   385
huffman@22956
   386
lemma real_sqrt_divide: "sqrt (x / y) = sqrt x / sqrt y"
hoelzl@51483
   387
unfolding sqrt_def by (rule real_root_divide)
huffman@22956
   388
huffman@22956
   389
lemma real_sqrt_power: "sqrt (x ^ k) = sqrt x ^ k"
huffman@22956
   390
unfolding sqrt_def by (rule real_root_power [OF pos2])
huffman@22956
   391
huffman@22956
   392
lemma real_sqrt_gt_zero: "0 < x \<Longrightarrow> 0 < sqrt x"
huffman@22956
   393
unfolding sqrt_def by (rule real_root_gt_zero [OF pos2])
huffman@22956
   394
huffman@22956
   395
lemma real_sqrt_ge_zero: "0 \<le> x \<Longrightarrow> 0 \<le> sqrt x"
hoelzl@51483
   396
unfolding sqrt_def by (rule real_root_ge_zero)
huffman@20687
   397
huffman@22956
   398
lemma real_sqrt_less_mono: "x < y \<Longrightarrow> sqrt x < sqrt y"
huffman@22956
   399
unfolding sqrt_def by (rule real_root_less_mono [OF pos2])
huffman@22956
   400
huffman@22956
   401
lemma real_sqrt_le_mono: "x \<le> y \<Longrightarrow> sqrt x \<le> sqrt y"
huffman@22956
   402
unfolding sqrt_def by (rule real_root_le_mono [OF pos2])
huffman@22956
   403
huffman@22956
   404
lemma real_sqrt_less_iff [simp]: "(sqrt x < sqrt y) = (x < y)"
huffman@22956
   405
unfolding sqrt_def by (rule real_root_less_iff [OF pos2])
huffman@22956
   406
huffman@22956
   407
lemma real_sqrt_le_iff [simp]: "(sqrt x \<le> sqrt y) = (x \<le> y)"
huffman@22956
   408
unfolding sqrt_def by (rule real_root_le_iff [OF pos2])
huffman@22956
   409
huffman@22956
   410
lemma real_sqrt_eq_iff [simp]: "(sqrt x = sqrt y) = (x = y)"
huffman@22956
   411
unfolding sqrt_def by (rule real_root_eq_iff [OF pos2])
huffman@22956
   412
huffman@22956
   413
lemmas real_sqrt_gt_0_iff [simp] = real_sqrt_less_iff [where x=0, simplified]
huffman@22956
   414
lemmas real_sqrt_lt_0_iff [simp] = real_sqrt_less_iff [where y=0, simplified]
huffman@22956
   415
lemmas real_sqrt_ge_0_iff [simp] = real_sqrt_le_iff [where x=0, simplified]
huffman@22956
   416
lemmas real_sqrt_le_0_iff [simp] = real_sqrt_le_iff [where y=0, simplified]
huffman@22956
   417
lemmas real_sqrt_eq_0_iff [simp] = real_sqrt_eq_iff [where y=0, simplified]
huffman@22956
   418
huffman@22956
   419
lemmas real_sqrt_gt_1_iff [simp] = real_sqrt_less_iff [where x=1, simplified]
huffman@22956
   420
lemmas real_sqrt_lt_1_iff [simp] = real_sqrt_less_iff [where y=1, simplified]
huffman@22956
   421
lemmas real_sqrt_ge_1_iff [simp] = real_sqrt_le_iff [where x=1, simplified]
huffman@22956
   422
lemmas real_sqrt_le_1_iff [simp] = real_sqrt_le_iff [where y=1, simplified]
huffman@22956
   423
lemmas real_sqrt_eq_1_iff [simp] = real_sqrt_eq_iff [where y=1, simplified]
huffman@20687
   424
huffman@23042
   425
lemma isCont_real_sqrt: "isCont sqrt x"
hoelzl@51483
   426
unfolding sqrt_def by (rule isCont_real_root)
huffman@23042
   427
hoelzl@51478
   428
lemma tendsto_real_sqrt[tendsto_intros]:
hoelzl@51478
   429
  "(f ---> x) F \<Longrightarrow> ((\<lambda>x. sqrt (f x)) ---> sqrt x) F"
hoelzl@51483
   430
  unfolding sqrt_def by (rule tendsto_real_root)
hoelzl@51478
   431
hoelzl@51478
   432
lemma continuous_real_sqrt[continuous_intros]:
hoelzl@51478
   433
  "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sqrt (f x))"
hoelzl@51483
   434
  unfolding sqrt_def by (rule continuous_real_root)
hoelzl@51478
   435
  
hoelzl@51478
   436
lemma continuous_on_real_sqrt[continuous_on_intros]:
hoelzl@51478
   437
  "continuous_on s f \<Longrightarrow> 0 < n \<Longrightarrow> continuous_on s (\<lambda>x. sqrt (f x))"
hoelzl@51483
   438
  unfolding sqrt_def by (rule continuous_on_real_root)
hoelzl@51478
   439
hoelzl@31880
   440
lemma DERIV_real_sqrt_generic:
hoelzl@31880
   441
  assumes "x \<noteq> 0"
hoelzl@31880
   442
  assumes "x > 0 \<Longrightarrow> D = inverse (sqrt x) / 2"
hoelzl@31880
   443
  assumes "x < 0 \<Longrightarrow> D = - inverse (sqrt x) / 2"
hoelzl@31880
   444
  shows "DERIV sqrt x :> D"
hoelzl@31880
   445
  using assms unfolding sqrt_def
hoelzl@31880
   446
  by (auto intro!: DERIV_real_root_generic)
hoelzl@31880
   447
huffman@23042
   448
lemma DERIV_real_sqrt:
huffman@23042
   449
  "0 < x \<Longrightarrow> DERIV sqrt x :> inverse (sqrt x) / 2"
hoelzl@31880
   450
  using DERIV_real_sqrt_generic by simp
hoelzl@31880
   451
hoelzl@31880
   452
declare
hoelzl@31880
   453
  DERIV_real_sqrt_generic[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
hoelzl@31880
   454
  DERIV_real_root_generic[THEN DERIV_chain2, THEN DERIV_cong, DERIV_intros]
huffman@23042
   455
huffman@20687
   456
lemma not_real_square_gt_zero [simp]: "(~ (0::real) < x*x) = (x = 0)"
huffman@20687
   457
apply auto
huffman@20687
   458
apply (cut_tac x = x and y = 0 in linorder_less_linear)
huffman@20687
   459
apply (simp add: zero_less_mult_iff)
huffman@20687
   460
done
huffman@20687
   461
huffman@20687
   462
lemma real_sqrt_abs2 [simp]: "sqrt(x*x) = \<bar>x\<bar>"
huffman@22856
   463
apply (subst power2_eq_square [symmetric])
huffman@20687
   464
apply (rule real_sqrt_abs)
huffman@20687
   465
done
huffman@20687
   466
huffman@20687
   467
lemma real_inv_sqrt_pow2: "0 < x ==> inverse (sqrt(x)) ^ 2 = inverse x"
huffman@22856
   468
by (simp add: power_inverse [symmetric])
huffman@20687
   469
huffman@20687
   470
lemma real_sqrt_eq_zero_cancel: "[| 0 \<le> x; sqrt(x) = 0|] ==> x = 0"
huffman@22956
   471
by simp
huffman@20687
   472
huffman@20687
   473
lemma real_sqrt_ge_one: "1 \<le> x ==> 1 \<le> sqrt x"
huffman@22956
   474
by simp
huffman@20687
   475
huffman@22443
   476
lemma sqrt_divide_self_eq:
huffman@22443
   477
  assumes nneg: "0 \<le> x"
huffman@22443
   478
  shows "sqrt x / x = inverse (sqrt x)"
huffman@22443
   479
proof cases
huffman@22443
   480
  assume "x=0" thus ?thesis by simp
huffman@22443
   481
next
huffman@22443
   482
  assume nz: "x\<noteq>0" 
huffman@22443
   483
  hence pos: "0<x" using nneg by arith
huffman@22443
   484
  show ?thesis
huffman@22443
   485
  proof (rule right_inverse_eq [THEN iffD1, THEN sym]) 
huffman@22443
   486
    show "sqrt x / x \<noteq> 0" by (simp add: divide_inverse nneg nz) 
huffman@22443
   487
    show "inverse (sqrt x) / (sqrt x / x) = 1"
huffman@22443
   488
      by (simp add: divide_inverse mult_assoc [symmetric] 
huffman@22443
   489
                  power2_eq_square [symmetric] real_inv_sqrt_pow2 pos nz) 
huffman@22443
   490
  qed
huffman@22443
   491
qed
huffman@22443
   492
huffman@22721
   493
lemma real_divide_square_eq [simp]: "(((r::real) * a) / (r * r)) = a / r"
huffman@22721
   494
apply (simp add: divide_inverse)
huffman@22721
   495
apply (case_tac "r=0")
huffman@22721
   496
apply (auto simp add: mult_ac)
huffman@22721
   497
done
huffman@22721
   498
huffman@23049
   499
lemma lemma_real_divide_sqrt_less: "0 < u ==> u / sqrt 2 < u"
huffman@35216
   500
by (simp add: divide_less_eq)
huffman@23049
   501
huffman@23049
   502
lemma four_x_squared: 
huffman@23049
   503
  fixes x::real
huffman@23049
   504
  shows "4 * x\<twosuperior> = (2 * x)\<twosuperior>"
huffman@23049
   505
by (simp add: power2_eq_square)
huffman@23049
   506
huffman@22856
   507
subsection {* Square Root of Sum of Squares *}
huffman@22856
   508
huffman@44320
   509
lemma real_sqrt_sum_squares_ge_zero: "0 \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@44320
   510
  by simp (* TODO: delete *)
huffman@22856
   511
huffman@23049
   512
declare real_sqrt_sum_squares_ge_zero [THEN abs_of_nonneg, simp]
huffman@23049
   513
huffman@22856
   514
lemma real_sqrt_sum_squares_mult_ge_zero [simp]:
huffman@22856
   515
     "0 \<le> sqrt ((x\<twosuperior> + y\<twosuperior>)*(xa\<twosuperior> + ya\<twosuperior>))"
huffman@44320
   516
  by (simp add: zero_le_mult_iff)
huffman@22856
   517
huffman@22856
   518
lemma real_sqrt_sum_squares_mult_squared_eq [simp]:
huffman@22856
   519
     "sqrt ((x\<twosuperior> + y\<twosuperior>) * (xa\<twosuperior> + ya\<twosuperior>)) ^ 2 = (x\<twosuperior> + y\<twosuperior>) * (xa\<twosuperior> + ya\<twosuperior>)"
huffman@44320
   520
  by (simp add: zero_le_mult_iff)
huffman@22856
   521
huffman@23049
   522
lemma real_sqrt_sum_squares_eq_cancel: "sqrt (x\<twosuperior> + y\<twosuperior>) = x \<Longrightarrow> y = 0"
huffman@23049
   523
by (drule_tac f = "%x. x\<twosuperior>" in arg_cong, simp)
huffman@23049
   524
huffman@23049
   525
lemma real_sqrt_sum_squares_eq_cancel2: "sqrt (x\<twosuperior> + y\<twosuperior>) = y \<Longrightarrow> x = 0"
huffman@23049
   526
by (drule_tac f = "%x. x\<twosuperior>" in arg_cong, simp)
huffman@23049
   527
huffman@23049
   528
lemma real_sqrt_sum_squares_ge1 [simp]: "x \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@22856
   529
by (rule power2_le_imp_le, simp_all)
huffman@22856
   530
huffman@23049
   531
lemma real_sqrt_sum_squares_ge2 [simp]: "y \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@23049
   532
by (rule power2_le_imp_le, simp_all)
huffman@23049
   533
huffman@23049
   534
lemma real_sqrt_ge_abs1 [simp]: "\<bar>x\<bar> \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@22856
   535
by (rule power2_le_imp_le, simp_all)
huffman@22856
   536
huffman@23049
   537
lemma real_sqrt_ge_abs2 [simp]: "\<bar>y\<bar> \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@23049
   538
by (rule power2_le_imp_le, simp_all)
huffman@23049
   539
huffman@23049
   540
lemma le_real_sqrt_sumsq [simp]: "x \<le> sqrt (x * x + y * y)"
huffman@23049
   541
by (simp add: power2_eq_square [symmetric])
huffman@23049
   542
huffman@22858
   543
lemma real_sqrt_sum_squares_triangle_ineq:
huffman@22858
   544
  "sqrt ((a + c)\<twosuperior> + (b + d)\<twosuperior>) \<le> sqrt (a\<twosuperior> + b\<twosuperior>) + sqrt (c\<twosuperior> + d\<twosuperior>)"
huffman@22858
   545
apply (rule power2_le_imp_le, simp)
huffman@22858
   546
apply (simp add: power2_sum)
webertj@49962
   547
apply (simp only: mult_assoc distrib_left [symmetric])
huffman@22858
   548
apply (rule mult_left_mono)
huffman@22858
   549
apply (rule power2_le_imp_le)
huffman@22858
   550
apply (simp add: power2_sum power_mult_distrib)
nipkow@23477
   551
apply (simp add: ring_distribs)
huffman@22858
   552
apply (subgoal_tac "0 \<le> b\<twosuperior> * c\<twosuperior> + a\<twosuperior> * d\<twosuperior> - 2 * (a * c) * (b * d)", simp)
huffman@22858
   553
apply (rule_tac b="(a * d - b * c)\<twosuperior>" in ord_le_eq_trans)
huffman@22858
   554
apply (rule zero_le_power2)
huffman@22858
   555
apply (simp add: power2_diff power_mult_distrib)
huffman@22858
   556
apply (simp add: mult_nonneg_nonneg)
huffman@22858
   557
apply simp
huffman@22858
   558
apply (simp add: add_increasing)
huffman@22858
   559
done
huffman@22858
   560
huffman@23122
   561
lemma real_sqrt_sum_squares_less:
huffman@23122
   562
  "\<lbrakk>\<bar>x\<bar> < u / sqrt 2; \<bar>y\<bar> < u / sqrt 2\<rbrakk> \<Longrightarrow> sqrt (x\<twosuperior> + y\<twosuperior>) < u"
huffman@23122
   563
apply (rule power2_less_imp_less, simp)
huffman@23122
   564
apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
huffman@23122
   565
apply (drule power_strict_mono [OF _ abs_ge_zero pos2])
huffman@23122
   566
apply (simp add: power_divide)
huffman@23122
   567
apply (drule order_le_less_trans [OF abs_ge_zero])
huffman@23122
   568
apply (simp add: zero_less_divide_iff)
huffman@23122
   569
done
huffman@23122
   570
huffman@23049
   571
text{*Needed for the infinitely close relation over the nonstandard
huffman@23049
   572
    complex numbers*}
huffman@23049
   573
lemma lemma_sqrt_hcomplex_capprox:
huffman@23049
   574
     "[| 0 < u; x < u/2; y < u/2; 0 \<le> x; 0 \<le> y |] ==> sqrt (x\<twosuperior> + y\<twosuperior>) < u"
huffman@23049
   575
apply (rule_tac y = "u/sqrt 2" in order_le_less_trans)
huffman@23049
   576
apply (erule_tac [2] lemma_real_divide_sqrt_less)
huffman@23049
   577
apply (rule power2_le_imp_le)
huffman@44349
   578
apply (auto simp add: zero_le_divide_iff power_divide)
huffman@23049
   579
apply (rule_tac t = "u\<twosuperior>" in real_sum_of_halves [THEN subst])
huffman@23049
   580
apply (rule add_mono)
huffman@30273
   581
apply (auto simp add: four_x_squared intro: power_mono)
huffman@23049
   582
done
huffman@23049
   583
huffman@22956
   584
text "Legacy theorem names:"
huffman@22956
   585
lemmas real_root_pos2 = real_root_power_cancel
huffman@22956
   586
lemmas real_root_pos_pos = real_root_gt_zero [THEN order_less_imp_le]
huffman@22956
   587
lemmas real_root_pos_pos_le = real_root_ge_zero
huffman@22956
   588
lemmas real_sqrt_mult_distrib = real_sqrt_mult
huffman@22956
   589
lemmas real_sqrt_mult_distrib2 = real_sqrt_mult
huffman@22956
   590
lemmas real_sqrt_eq_zero_cancel_iff = real_sqrt_eq_0_iff
huffman@22956
   591
huffman@22956
   592
(* needed for CauchysMeanTheorem.het_base from AFP *)
huffman@22956
   593
lemma real_root_pos: "0 < x \<Longrightarrow> root (Suc n) (x ^ (Suc n)) = x"
huffman@22956
   594
by (rule real_root_power_cancel [OF zero_less_Suc order_less_imp_le])
huffman@22956
   595
paulson@14324
   596
end