src/HOL/Hyperreal/NthRoot.thy
author huffman
Mon May 14 18:03:25 2007 +0200 (2007-05-14)
changeset 22968 7134874437ac
parent 22961 e499ded5d0fc
child 23009 01c295dd4a36
permissions -rw-r--r--
tuned
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
huffman@21865
    10
imports SEQ Parity
nipkow@15131
    11
begin
paulson@14324
    12
huffman@22956
    13
subsection {* Existence of Nth Root *}
huffman@20687
    14
wenzelm@14767
    15
text {*
wenzelm@14767
    16
  Various lemmas needed for this result. We follow the proof given by
wenzelm@14767
    17
  John Lindsay Orr (\texttt{jorr@math.unl.edu}) in his Analysis
wenzelm@14767
    18
  Webnotes available at \url{http://www.math.unl.edu/~webnotes}.
wenzelm@14767
    19
wenzelm@14767
    20
  Lemmas about sequences of reals are used to reach the result.
wenzelm@14767
    21
*}
paulson@14324
    22
paulson@14324
    23
lemma lemma_nth_realpow_non_empty:
paulson@14324
    24
     "[| (0::real) < a; 0 < n |] ==> \<exists>s. s : {x. x ^ n <= a & 0 < x}"
paulson@14324
    25
apply (case_tac "1 <= a")
paulson@14477
    26
apply (rule_tac x = 1 in exI)
paulson@14334
    27
apply (drule_tac [2] linorder_not_le [THEN iffD1])
paulson@14477
    28
apply (drule_tac [2] less_not_refl2 [THEN not0_implies_Suc], simp) 
paulson@14348
    29
apply (force intro!: realpow_Suc_le_self simp del: realpow_Suc)
paulson@14324
    30
done
paulson@14324
    31
paulson@14348
    32
text{*Used only just below*}
paulson@14348
    33
lemma realpow_ge_self2: "[| (1::real) \<le> r; 0 < n |] ==> r \<le> r ^ n"
paulson@14348
    34
by (insert power_increasing [of 1 n r], simp)
paulson@14348
    35
paulson@14324
    36
lemma lemma_nth_realpow_isUb_ex:
paulson@14324
    37
     "[| (0::real) < a; 0 < n |]  
paulson@14324
    38
      ==> \<exists>u. isUb (UNIV::real set) {x. x ^ n <= a & 0 < x} u"
paulson@14324
    39
apply (case_tac "1 <= a")
paulson@14477
    40
apply (rule_tac x = a in exI)
paulson@14334
    41
apply (drule_tac [2] linorder_not_le [THEN iffD1])
paulson@14477
    42
apply (rule_tac [2] x = 1 in exI)
paulson@14477
    43
apply (rule_tac [!] setleI [THEN isUbI], safe)
paulson@14324
    44
apply (simp_all (no_asm))
paulson@14324
    45
apply (rule_tac [!] ccontr)
paulson@14334
    46
apply (drule_tac [!] linorder_not_le [THEN iffD1])
paulson@14477
    47
apply (drule realpow_ge_self2, assumption)
paulson@14477
    48
apply (drule_tac n = n in realpow_less)
paulson@14324
    49
apply (assumption+)
paulson@14477
    50
apply (drule real_le_trans, assumption)
paulson@14477
    51
apply (drule_tac y = "y ^ n" in order_less_le_trans, assumption, simp) 
paulson@14477
    52
apply (drule_tac n = n in zero_less_one [THEN realpow_less], auto)
paulson@14324
    53
done
paulson@14324
    54
paulson@14324
    55
lemma nth_realpow_isLub_ex:
paulson@14324
    56
     "[| (0::real) < a; 0 < n |]  
paulson@14324
    57
      ==> \<exists>u. isLub (UNIV::real set) {x. x ^ n <= a & 0 < x} u"
paulson@14365
    58
by (blast intro: lemma_nth_realpow_isUb_ex lemma_nth_realpow_non_empty reals_complete)
paulson@14365
    59
paulson@14324
    60
 
huffman@22956
    61
subsubsection {* First Half -- Lemmas First *}
paulson@14324
    62
paulson@14324
    63
lemma lemma_nth_realpow_seq:
paulson@14324
    64
     "isLub (UNIV::real set) {x. x ^ n <= a & (0::real) < x} u  
paulson@14324
    65
           ==> u + inverse(real (Suc k)) ~: {x. x ^ n <= a & 0 < x}"
paulson@14477
    66
apply (safe, drule isLubD2, blast)
paulson@14365
    67
apply (simp add: linorder_not_less [symmetric])
paulson@14324
    68
done
paulson@14324
    69
paulson@14324
    70
lemma lemma_nth_realpow_isLub_gt_zero:
paulson@14324
    71
     "[| isLub (UNIV::real set) {x. x ^ n <= a & (0::real) < x} u;  
paulson@14324
    72
         0 < a; 0 < n |] ==> 0 < u"
paulson@14477
    73
apply (drule lemma_nth_realpow_non_empty, auto)
paulson@14477
    74
apply (drule_tac y = s in isLub_isUb [THEN isUbD])
paulson@14324
    75
apply (auto intro: order_less_le_trans)
paulson@14324
    76
done
paulson@14324
    77
paulson@14324
    78
lemma lemma_nth_realpow_isLub_ge:
paulson@14324
    79
     "[| isLub (UNIV::real set) {x. x ^ n <= a & (0::real) < x} u;  
paulson@14324
    80
         0 < a; 0 < n |] ==> ALL k. a <= (u + inverse(real (Suc k))) ^ n"
paulson@14477
    81
apply safe
paulson@14477
    82
apply (frule lemma_nth_realpow_seq, safe)
paulson@15085
    83
apply (auto elim: order_less_asym simp add: linorder_not_less [symmetric]
paulson@15085
    84
            iff: real_0_less_add_iff) --{*legacy iff rule!*}
paulson@14365
    85
apply (simp add: linorder_not_less)
paulson@14324
    86
apply (rule order_less_trans [of _ 0])
paulson@14325
    87
apply (auto intro: lemma_nth_realpow_isLub_gt_zero)
paulson@14324
    88
done
paulson@14324
    89
paulson@14324
    90
text{*First result we want*}
paulson@14324
    91
lemma realpow_nth_ge:
paulson@14324
    92
     "[| (0::real) < a; 0 < n;  
paulson@14324
    93
     isLub (UNIV::real set)  
paulson@14324
    94
     {x. x ^ n <= a & 0 < x} u |] ==> a <= u ^ n"
paulson@14477
    95
apply (frule lemma_nth_realpow_isLub_ge, safe)
paulson@14324
    96
apply (rule LIMSEQ_inverse_real_of_nat_add [THEN LIMSEQ_pow, THEN LIMSEQ_le_const])
paulson@14334
    97
apply (auto simp add: real_of_nat_def)
paulson@14324
    98
done
paulson@14324
    99
huffman@22956
   100
subsubsection {* Second Half *}
paulson@14324
   101
paulson@14324
   102
lemma less_isLub_not_isUb:
paulson@14324
   103
     "[| isLub (UNIV::real set) S u; x < u |]  
paulson@14324
   104
           ==> ~ isUb (UNIV::real set) S x"
paulson@14477
   105
apply safe
paulson@14477
   106
apply (drule isLub_le_isUb, assumption)
paulson@14477
   107
apply (drule order_less_le_trans, auto)
paulson@14324
   108
done
paulson@14324
   109
paulson@14324
   110
lemma not_isUb_less_ex:
paulson@14324
   111
     "~ isUb (UNIV::real set) S u ==> \<exists>x \<in> S. u < x"
wenzelm@18585
   112
apply (rule ccontr, erule contrapos_np)
paulson@14324
   113
apply (rule setleI [THEN isUbI])
paulson@14365
   114
apply (auto simp add: linorder_not_less [symmetric])
paulson@14324
   115
done
paulson@14324
   116
paulson@14325
   117
lemma real_mult_less_self: "0 < r ==> r * (1 + -inverse(real (Suc n))) < r"
paulson@14334
   118
apply (simp (no_asm) add: right_distrib)
paulson@14334
   119
apply (rule add_less_cancel_left [of "-r", THEN iffD1])
avigad@16775
   120
apply (auto intro: mult_pos_pos
paulson@14334
   121
            simp add: add_assoc [symmetric] neg_less_0_iff_less)
paulson@14325
   122
done
paulson@14325
   123
huffman@22630
   124
lemma real_of_nat_inverse_le_iff:
huffman@22630
   125
  "(inverse (real(Suc n)) \<le> r) = (1 \<le> r * real(Suc n))"
huffman@22630
   126
by (simp add: inverse_eq_divide pos_divide_le_eq)
huffman@22630
   127
paulson@14325
   128
lemma real_mult_add_one_minus_ge_zero:
paulson@14325
   129
     "0 < r ==>  0 <= r*(1 + -inverse(real (Suc n)))"
paulson@15085
   130
by (simp add: zero_le_mult_iff real_of_nat_inverse_le_iff real_0_le_add_iff)
paulson@14325
   131
paulson@14324
   132
lemma lemma_nth_realpow_isLub_le:
paulson@14324
   133
     "[| isLub (UNIV::real set) {x. x ^ n <= a & (0::real) < x} u;  
paulson@14325
   134
       0 < a; 0 < n |] ==> ALL k. (u*(1 + -inverse(real (Suc k)))) ^ n <= a"
paulson@14477
   135
apply safe
paulson@14324
   136
apply (frule less_isLub_not_isUb [THEN not_isUb_less_ex])
paulson@14477
   137
apply (rule_tac n = k in real_mult_less_self)
paulson@14477
   138
apply (blast intro: lemma_nth_realpow_isLub_gt_zero, safe)
paulson@14477
   139
apply (drule_tac n = k in
paulson@14477
   140
        lemma_nth_realpow_isLub_gt_zero [THEN real_mult_add_one_minus_ge_zero], assumption+)
paulson@14348
   141
apply (blast intro: order_trans order_less_imp_le power_mono) 
paulson@14324
   142
done
paulson@14324
   143
paulson@14324
   144
text{*Second result we want*}
paulson@14324
   145
lemma realpow_nth_le:
paulson@14324
   146
     "[| (0::real) < a; 0 < n;  
paulson@14324
   147
     isLub (UNIV::real set)  
paulson@14324
   148
     {x. x ^ n <= a & 0 < x} u |] ==> u ^ n <= a"
paulson@14477
   149
apply (frule lemma_nth_realpow_isLub_le, safe)
paulson@14348
   150
apply (rule LIMSEQ_inverse_real_of_nat_add_minus_mult
paulson@14348
   151
                [THEN LIMSEQ_pow, THEN LIMSEQ_le_const2])
paulson@14334
   152
apply (auto simp add: real_of_nat_def)
paulson@14324
   153
done
paulson@14324
   154
paulson@14348
   155
text{*The theorem at last!*}
paulson@14324
   156
lemma realpow_nth: "[| (0::real) < a; 0 < n |] ==> \<exists>r. r ^ n = a"
paulson@14477
   157
apply (frule nth_realpow_isLub_ex, auto)
paulson@14477
   158
apply (auto intro: realpow_nth_le realpow_nth_ge order_antisym)
paulson@14324
   159
done
paulson@14324
   160
huffman@22956
   161
text {* positive only *}
paulson@14324
   162
lemma realpow_pos_nth: "[| (0::real) < a; 0 < n |] ==> \<exists>r. 0 < r & r ^ n = a"
paulson@14477
   163
apply (frule nth_realpow_isLub_ex, auto)
paulson@14477
   164
apply (auto intro: realpow_nth_le realpow_nth_ge order_antisym lemma_nth_realpow_isLub_gt_zero)
paulson@14324
   165
done
paulson@14324
   166
paulson@14324
   167
lemma realpow_pos_nth2: "(0::real) < a  ==> \<exists>r. 0 < r & r ^ Suc n = a"
paulson@14477
   168
by (blast intro: realpow_pos_nth)
paulson@14324
   169
huffman@22956
   170
text {* uniqueness of nth positive root *}
paulson@14324
   171
lemma realpow_pos_nth_unique:
paulson@14324
   172
     "[| (0::real) < a; 0 < n |] ==> EX! r. 0 < r & r ^ n = a"
paulson@14324
   173
apply (auto intro!: realpow_pos_nth)
paulson@14477
   174
apply (cut_tac x = r and y = y in linorder_less_linear, auto)
paulson@14477
   175
apply (drule_tac x = r in realpow_less)
paulson@14477
   176
apply (drule_tac [4] x = y in realpow_less, auto)
paulson@14324
   177
done
paulson@14324
   178
huffman@20687
   179
subsection {* Nth Root *}
huffman@20687
   180
huffman@22956
   181
text {* We define roots of negative reals such that
huffman@22956
   182
  @{term "root n (- x) = - root n x"}. This allows
huffman@22956
   183
  us to omit side conditions from many theorems. *}
huffman@20687
   184
huffman@22956
   185
definition
huffman@22956
   186
  root :: "[nat, real] \<Rightarrow> real" where
huffman@22956
   187
  "root n x = (if 0 < x then (THE u. 0 < u \<and> u ^ n = x) else
huffman@22956
   188
               if x < 0 then - (THE u. 0 < u \<and> u ^ n = - x) else 0)"
huffman@20687
   189
huffman@22956
   190
lemma real_root_zero [simp]: "root n 0 = 0"
huffman@22956
   191
unfolding root_def by simp
huffman@22956
   192
huffman@22956
   193
lemma real_root_minus: "0 < n \<Longrightarrow> root n (- x) = - root n x"
huffman@22956
   194
unfolding root_def by simp
huffman@22956
   195
huffman@22956
   196
lemma real_root_gt_zero: "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> 0 < root n x"
huffman@20687
   197
apply (simp add: root_def)
huffman@22956
   198
apply (drule (1) realpow_pos_nth_unique)
huffman@20687
   199
apply (erule theI' [THEN conjunct1])
huffman@20687
   200
done
huffman@20687
   201
huffman@22956
   202
lemma real_root_pow_pos: (* TODO: rename *)
huffman@22956
   203
  "\<lbrakk>0 < n; 0 < x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
huffman@22956
   204
apply (simp add: root_def)
huffman@22956
   205
apply (drule (1) realpow_pos_nth_unique)
huffman@22956
   206
apply (erule theI' [THEN conjunct2])
huffman@22956
   207
done
huffman@20687
   208
huffman@22956
   209
lemma real_root_pow_pos2 [simp]: (* TODO: rename *)
huffman@22956
   210
  "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n x ^ n = x"
huffman@22956
   211
by (auto simp add: order_le_less real_root_pow_pos)
huffman@22956
   212
huffman@22956
   213
lemma real_root_ge_zero: "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> 0 \<le> root n x"
huffman@20687
   214
by (auto simp add: order_le_less real_root_gt_zero)
huffman@20687
   215
huffman@22956
   216
lemma real_root_power_cancel: "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n (x ^ n) = x"
huffman@22956
   217
apply (subgoal_tac "0 \<le> x ^ n")
huffman@22956
   218
apply (subgoal_tac "0 \<le> root n (x ^ n)")
huffman@22956
   219
apply (subgoal_tac "root n (x ^ n) ^ n = x ^ n")
huffman@22956
   220
apply (erule (3) power_eq_imp_eq_base)
huffman@22956
   221
apply (erule (1) real_root_pow_pos2)
huffman@22956
   222
apply (erule (1) real_root_ge_zero)
huffman@22956
   223
apply (erule zero_le_power)
huffman@20687
   224
done
huffman@20687
   225
huffman@22956
   226
lemma real_root_pos_unique:
huffman@22956
   227
  "\<lbrakk>0 < n; 0 \<le> y; y ^ n = x\<rbrakk> \<Longrightarrow> root n x = y"
huffman@22956
   228
by (erule subst, rule real_root_power_cancel)
huffman@22956
   229
huffman@22956
   230
lemma real_root_one [simp]: "0 < n \<Longrightarrow> root n 1 = 1"
huffman@22956
   231
by (simp add: real_root_pos_unique)
huffman@22956
   232
huffman@22956
   233
text {* Root function is strictly monotonic, hence injective *}
huffman@22956
   234
huffman@22956
   235
lemma real_root_less_mono_lemma:
huffman@22956
   236
  "\<lbrakk>0 < n; 0 \<le> x; x < y\<rbrakk> \<Longrightarrow> root n x < root n y"
huffman@22856
   237
apply (subgoal_tac "0 \<le> y")
huffman@22956
   238
apply (subgoal_tac "root n x ^ n < root n y ^ n")
huffman@22956
   239
apply (erule power_less_imp_less_base)
huffman@22956
   240
apply (erule (1) real_root_ge_zero)
huffman@22956
   241
apply simp
huffman@22956
   242
apply simp
huffman@22721
   243
done
huffman@22721
   244
huffman@22956
   245
lemma real_root_less_mono: "\<lbrakk>0 < n; x < y\<rbrakk> \<Longrightarrow> root n x < root n y"
huffman@22956
   246
apply (cases "0 \<le> x")
huffman@22956
   247
apply (erule (2) real_root_less_mono_lemma)
huffman@22956
   248
apply (cases "0 \<le> y")
huffman@22956
   249
apply (rule_tac y=0 in order_less_le_trans)
huffman@22956
   250
apply (subgoal_tac "0 < root n (- x)")
huffman@22956
   251
apply (simp add: real_root_minus)
huffman@22956
   252
apply (simp add: real_root_gt_zero)
huffman@22956
   253
apply (simp add: real_root_ge_zero)
huffman@22956
   254
apply (subgoal_tac "root n (- y) < root n (- x)")
huffman@22956
   255
apply (simp add: real_root_minus)
huffman@22956
   256
apply (simp add: real_root_less_mono_lemma)
huffman@22721
   257
done
huffman@22721
   258
huffman@22956
   259
lemma real_root_le_mono: "\<lbrakk>0 < n; x \<le> y\<rbrakk> \<Longrightarrow> root n x \<le> root n y"
huffman@22956
   260
by (auto simp add: order_le_less real_root_less_mono)
huffman@22956
   261
huffman@22721
   262
lemma real_root_less_iff [simp]:
huffman@22956
   263
  "0 < n \<Longrightarrow> (root n x < root n y) = (x < y)"
huffman@22956
   264
apply (cases "x < y")
huffman@22956
   265
apply (simp add: real_root_less_mono)
huffman@22956
   266
apply (simp add: linorder_not_less real_root_le_mono)
huffman@22721
   267
done
huffman@22721
   268
huffman@22721
   269
lemma real_root_le_iff [simp]:
huffman@22956
   270
  "0 < n \<Longrightarrow> (root n x \<le> root n y) = (x \<le> y)"
huffman@22956
   271
apply (cases "x \<le> y")
huffman@22956
   272
apply (simp add: real_root_le_mono)
huffman@22956
   273
apply (simp add: linorder_not_le real_root_less_mono)
huffman@22721
   274
done
huffman@22721
   275
huffman@22721
   276
lemma real_root_eq_iff [simp]:
huffman@22956
   277
  "0 < n \<Longrightarrow> (root n x = root n y) = (x = y)"
huffman@22956
   278
by (simp add: order_eq_iff)
huffman@22956
   279
huffman@22956
   280
lemmas real_root_gt_0_iff [simp] = real_root_less_iff [where x=0, simplified]
huffman@22956
   281
lemmas real_root_lt_0_iff [simp] = real_root_less_iff [where y=0, simplified]
huffman@22956
   282
lemmas real_root_ge_0_iff [simp] = real_root_le_iff [where x=0, simplified]
huffman@22956
   283
lemmas real_root_le_0_iff [simp] = real_root_le_iff [where y=0, simplified]
huffman@22956
   284
lemmas real_root_eq_0_iff [simp] = real_root_eq_iff [where y=0, simplified]
huffman@22721
   285
huffman@22956
   286
text {* Roots of multiplication and division *}
huffman@22956
   287
huffman@22956
   288
lemma real_root_mult_lemma:
huffman@22956
   289
  "\<lbrakk>0 < n; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> root n (x * y) = root n x * root n y"
huffman@22956
   290
by (simp add: real_root_pos_unique mult_nonneg_nonneg power_mult_distrib)
huffman@22956
   291
huffman@22956
   292
lemma real_root_inverse_lemma:
huffman@22956
   293
  "\<lbrakk>0 < n; 0 \<le> x\<rbrakk> \<Longrightarrow> root n (inverse x) = inverse (root n x)"
huffman@22956
   294
by (simp add: real_root_pos_unique power_inverse [symmetric])
huffman@22721
   295
huffman@22721
   296
lemma real_root_mult:
huffman@22956
   297
  assumes n: "0 < n"
huffman@22956
   298
  shows "root n (x * y) = root n x * root n y"
huffman@22956
   299
proof (rule linorder_le_cases, rule_tac [!] linorder_le_cases)
huffman@22956
   300
  assume "0 \<le> x" and "0 \<le> y"
huffman@22956
   301
  thus ?thesis by (rule real_root_mult_lemma [OF n])
huffman@22956
   302
next
huffman@22956
   303
  assume "0 \<le> x" and "y \<le> 0"
huffman@22956
   304
  hence "0 \<le> x" and "0 \<le> - y" by simp_all
huffman@22956
   305
  hence "root n (x * - y) = root n x * root n (- y)"
huffman@22956
   306
    by (rule real_root_mult_lemma [OF n])
huffman@22956
   307
  thus ?thesis by (simp add: real_root_minus [OF n])
huffman@22956
   308
next
huffman@22956
   309
  assume "x \<le> 0" and "0 \<le> y"
huffman@22956
   310
  hence "0 \<le> - x" and "0 \<le> y" by simp_all
huffman@22956
   311
  hence "root n (- x * y) = root n (- x) * root n y"
huffman@22956
   312
    by (rule real_root_mult_lemma [OF n])
huffman@22956
   313
  thus ?thesis by (simp add: real_root_minus [OF n])
huffman@22956
   314
next
huffman@22956
   315
  assume "x \<le> 0" and "y \<le> 0"
huffman@22956
   316
  hence "0 \<le> - x" and "0 \<le> - y" by simp_all
huffman@22956
   317
  hence "root n (- x * - y) = root n (- x) * root n (- y)"
huffman@22956
   318
    by (rule real_root_mult_lemma [OF n])
huffman@22956
   319
  thus ?thesis by (simp add: real_root_minus [OF n])
huffman@22956
   320
qed
huffman@22721
   321
huffman@22721
   322
lemma real_root_inverse:
huffman@22956
   323
  assumes n: "0 < n"
huffman@22956
   324
  shows "root n (inverse x) = inverse (root n x)"
huffman@22956
   325
proof (rule linorder_le_cases)
huffman@22956
   326
  assume "0 \<le> x"
huffman@22956
   327
  thus ?thesis by (rule real_root_inverse_lemma [OF n])
huffman@22956
   328
next
huffman@22956
   329
  assume "x \<le> 0"
huffman@22956
   330
  hence "0 \<le> - x" by simp
huffman@22956
   331
  hence "root n (inverse (- x)) = inverse (root n (- x))"
huffman@22956
   332
    by (rule real_root_inverse_lemma [OF n])
huffman@22956
   333
  thus ?thesis by (simp add: real_root_minus [OF n])
huffman@22956
   334
qed
huffman@22721
   335
huffman@22956
   336
lemma real_root_divide:
huffman@22956
   337
  "0 < n \<Longrightarrow> root n (x / y) = root n x / root n y"
huffman@22956
   338
by (simp add: divide_inverse real_root_mult real_root_inverse)
huffman@22956
   339
huffman@22956
   340
lemma real_root_power:
huffman@22956
   341
  "0 < n \<Longrightarrow> root n (x ^ k) = root n x ^ k"
huffman@22956
   342
by (induct k, simp_all add: real_root_mult)
huffman@22721
   343
huffman@20687
   344
huffman@22956
   345
subsection {* Square Root *}
huffman@20687
   346
huffman@22956
   347
definition
huffman@22956
   348
  sqrt :: "real \<Rightarrow> real" where
huffman@22956
   349
  "sqrt = root 2"
huffman@20687
   350
huffman@22956
   351
lemma pos2: "0 < (2::nat)" by simp
huffman@22956
   352
huffman@22956
   353
lemma real_sqrt_unique: "\<lbrakk>y\<twosuperior> = x; 0 \<le> y\<rbrakk> \<Longrightarrow> sqrt x = y"
huffman@22956
   354
unfolding sqrt_def by (rule real_root_pos_unique [OF pos2])
huffman@20687
   355
huffman@22956
   356
lemma real_sqrt_abs [simp]: "sqrt (x\<twosuperior>) = \<bar>x\<bar>"
huffman@22956
   357
apply (rule real_sqrt_unique)
huffman@22956
   358
apply (rule power2_abs)
huffman@22956
   359
apply (rule abs_ge_zero)
huffman@22956
   360
done
huffman@20687
   361
huffman@22956
   362
lemma real_sqrt_pow2 [simp]: "0 \<le> x \<Longrightarrow> (sqrt x)\<twosuperior> = x"
huffman@22956
   363
unfolding sqrt_def by (rule real_root_pow_pos2 [OF pos2])
huffman@22856
   364
huffman@22956
   365
lemma real_sqrt_pow2_iff [simp]: "((sqrt x)\<twosuperior> = x) = (0 \<le> x)"
huffman@22856
   366
apply (rule iffI)
huffman@22856
   367
apply (erule subst)
huffman@22856
   368
apply (rule zero_le_power2)
huffman@22856
   369
apply (erule real_sqrt_pow2)
huffman@20687
   370
done
huffman@20687
   371
huffman@22956
   372
lemma real_sqrt_zero [simp]: "sqrt 0 = 0"
huffman@22956
   373
unfolding sqrt_def by (rule real_root_zero)
huffman@22956
   374
huffman@22956
   375
lemma real_sqrt_one [simp]: "sqrt 1 = 1"
huffman@22956
   376
unfolding sqrt_def by (rule real_root_one [OF pos2])
huffman@22956
   377
huffman@22956
   378
lemma real_sqrt_minus: "sqrt (- x) = - sqrt x"
huffman@22956
   379
unfolding sqrt_def by (rule real_root_minus [OF pos2])
huffman@22956
   380
huffman@22956
   381
lemma real_sqrt_mult: "sqrt (x * y) = sqrt x * sqrt y"
huffman@22956
   382
unfolding sqrt_def by (rule real_root_mult [OF pos2])
huffman@22956
   383
huffman@22956
   384
lemma real_sqrt_inverse: "sqrt (inverse x) = inverse (sqrt x)"
huffman@22956
   385
unfolding sqrt_def by (rule real_root_inverse [OF pos2])
huffman@22956
   386
huffman@22956
   387
lemma real_sqrt_divide: "sqrt (x / y) = sqrt x / sqrt y"
huffman@22956
   388
unfolding sqrt_def by (rule real_root_divide [OF pos2])
huffman@22956
   389
huffman@22956
   390
lemma real_sqrt_power: "sqrt (x ^ k) = sqrt x ^ k"
huffman@22956
   391
unfolding sqrt_def by (rule real_root_power [OF pos2])
huffman@22956
   392
huffman@22956
   393
lemma real_sqrt_gt_zero: "0 < x \<Longrightarrow> 0 < sqrt x"
huffman@22956
   394
unfolding sqrt_def by (rule real_root_gt_zero [OF pos2])
huffman@22956
   395
huffman@22956
   396
lemma real_sqrt_ge_zero: "0 \<le> x \<Longrightarrow> 0 \<le> sqrt x"
huffman@22956
   397
unfolding sqrt_def by (rule real_root_ge_zero [OF pos2])
huffman@20687
   398
huffman@22956
   399
lemma real_sqrt_less_mono: "x < y \<Longrightarrow> sqrt x < sqrt y"
huffman@22956
   400
unfolding sqrt_def by (rule real_root_less_mono [OF pos2])
huffman@22956
   401
huffman@22956
   402
lemma real_sqrt_le_mono: "x \<le> y \<Longrightarrow> sqrt x \<le> sqrt y"
huffman@22956
   403
unfolding sqrt_def by (rule real_root_le_mono [OF pos2])
huffman@22956
   404
huffman@22956
   405
lemma real_sqrt_less_iff [simp]: "(sqrt x < sqrt y) = (x < y)"
huffman@22956
   406
unfolding sqrt_def by (rule real_root_less_iff [OF pos2])
huffman@22956
   407
huffman@22956
   408
lemma real_sqrt_le_iff [simp]: "(sqrt x \<le> sqrt y) = (x \<le> y)"
huffman@22956
   409
unfolding sqrt_def by (rule real_root_le_iff [OF pos2])
huffman@22956
   410
huffman@22956
   411
lemma real_sqrt_eq_iff [simp]: "(sqrt x = sqrt y) = (x = y)"
huffman@22956
   412
unfolding sqrt_def by (rule real_root_eq_iff [OF pos2])
huffman@22956
   413
huffman@22956
   414
lemmas real_sqrt_gt_0_iff [simp] = real_sqrt_less_iff [where x=0, simplified]
huffman@22956
   415
lemmas real_sqrt_lt_0_iff [simp] = real_sqrt_less_iff [where y=0, simplified]
huffman@22956
   416
lemmas real_sqrt_ge_0_iff [simp] = real_sqrt_le_iff [where x=0, simplified]
huffman@22956
   417
lemmas real_sqrt_le_0_iff [simp] = real_sqrt_le_iff [where y=0, simplified]
huffman@22956
   418
lemmas real_sqrt_eq_0_iff [simp] = real_sqrt_eq_iff [where y=0, simplified]
huffman@22956
   419
huffman@22956
   420
lemmas real_sqrt_gt_1_iff [simp] = real_sqrt_less_iff [where x=1, simplified]
huffman@22956
   421
lemmas real_sqrt_lt_1_iff [simp] = real_sqrt_less_iff [where y=1, simplified]
huffman@22956
   422
lemmas real_sqrt_ge_1_iff [simp] = real_sqrt_le_iff [where x=1, simplified]
huffman@22956
   423
lemmas real_sqrt_le_1_iff [simp] = real_sqrt_le_iff [where y=1, simplified]
huffman@22956
   424
lemmas real_sqrt_eq_1_iff [simp] = real_sqrt_eq_iff [where y=1, simplified]
huffman@20687
   425
huffman@20687
   426
lemma not_real_square_gt_zero [simp]: "(~ (0::real) < x*x) = (x = 0)"
huffman@20687
   427
apply auto
huffman@20687
   428
apply (cut_tac x = x and y = 0 in linorder_less_linear)
huffman@20687
   429
apply (simp add: zero_less_mult_iff)
huffman@20687
   430
done
huffman@20687
   431
huffman@20687
   432
lemma real_sqrt_abs2 [simp]: "sqrt(x*x) = \<bar>x\<bar>"
huffman@22856
   433
apply (subst power2_eq_square [symmetric])
huffman@20687
   434
apply (rule real_sqrt_abs)
huffman@20687
   435
done
huffman@20687
   436
huffman@20687
   437
lemma real_sqrt_pow2_gt_zero: "0 < x ==> 0 < (sqrt x)\<twosuperior>"
huffman@22956
   438
by simp (* TODO: delete *)
huffman@20687
   439
huffman@20687
   440
lemma real_sqrt_not_eq_zero: "0 < x ==> sqrt x \<noteq> 0"
huffman@22956
   441
by simp (* TODO: delete *)
huffman@20687
   442
huffman@20687
   443
lemma real_inv_sqrt_pow2: "0 < x ==> inverse (sqrt(x)) ^ 2 = inverse x"
huffman@22856
   444
by (simp add: power_inverse [symmetric])
huffman@20687
   445
huffman@20687
   446
lemma real_sqrt_eq_zero_cancel: "[| 0 \<le> x; sqrt(x) = 0|] ==> x = 0"
huffman@22956
   447
by simp
huffman@20687
   448
huffman@20687
   449
lemma real_sqrt_ge_one: "1 \<le> x ==> 1 \<le> sqrt x"
huffman@22956
   450
by simp
huffman@20687
   451
huffman@22443
   452
lemma sqrt_divide_self_eq:
huffman@22443
   453
  assumes nneg: "0 \<le> x"
huffman@22443
   454
  shows "sqrt x / x = inverse (sqrt x)"
huffman@22443
   455
proof cases
huffman@22443
   456
  assume "x=0" thus ?thesis by simp
huffman@22443
   457
next
huffman@22443
   458
  assume nz: "x\<noteq>0" 
huffman@22443
   459
  hence pos: "0<x" using nneg by arith
huffman@22443
   460
  show ?thesis
huffman@22443
   461
  proof (rule right_inverse_eq [THEN iffD1, THEN sym]) 
huffman@22443
   462
    show "sqrt x / x \<noteq> 0" by (simp add: divide_inverse nneg nz) 
huffman@22443
   463
    show "inverse (sqrt x) / (sqrt x / x) = 1"
huffman@22443
   464
      by (simp add: divide_inverse mult_assoc [symmetric] 
huffman@22443
   465
                  power2_eq_square [symmetric] real_inv_sqrt_pow2 pos nz) 
huffman@22443
   466
  qed
huffman@22443
   467
qed
huffman@22443
   468
huffman@22721
   469
lemma real_divide_square_eq [simp]: "(((r::real) * a) / (r * r)) = a / r"
huffman@22721
   470
apply (simp add: divide_inverse)
huffman@22721
   471
apply (case_tac "r=0")
huffman@22721
   472
apply (auto simp add: mult_ac)
huffman@22721
   473
done
huffman@22721
   474
huffman@22856
   475
subsection {* Square Root of Sum of Squares *}
huffman@22856
   476
huffman@22856
   477
lemma real_sqrt_mult_self_sum_ge_zero [simp]: "0 \<le> sqrt(x*x + y*y)"
huffman@22968
   478
by (rule real_sqrt_ge_zero [OF sum_squares_ge_zero])
huffman@22856
   479
huffman@22856
   480
lemma real_sqrt_sum_squares_ge_zero [simp]: "0 \<le> sqrt (x\<twosuperior> + y\<twosuperior>)"
huffman@22961
   481
by simp
huffman@22856
   482
huffman@22856
   483
lemma real_sqrt_sum_squares_mult_ge_zero [simp]:
huffman@22856
   484
     "0 \<le> sqrt ((x\<twosuperior> + y\<twosuperior>)*(xa\<twosuperior> + ya\<twosuperior>))"
huffman@22856
   485
by (auto intro!: real_sqrt_ge_zero simp add: zero_le_mult_iff)
huffman@22856
   486
huffman@22856
   487
lemma real_sqrt_sum_squares_mult_squared_eq [simp]:
huffman@22856
   488
     "sqrt ((x\<twosuperior> + y\<twosuperior>) * (xa\<twosuperior> + ya\<twosuperior>)) ^ 2 = (x\<twosuperior> + y\<twosuperior>) * (xa\<twosuperior> + ya\<twosuperior>)"
huffman@22956
   489
by (auto simp add: zero_le_mult_iff)
huffman@22856
   490
huffman@22856
   491
lemma real_sqrt_sum_squares_ge1 [simp]: "x \<le> sqrt(x\<twosuperior> + y\<twosuperior>)"
huffman@22856
   492
by (rule power2_le_imp_le, simp_all)
huffman@22856
   493
huffman@22856
   494
lemma real_sqrt_sum_squares_ge2 [simp]: "y \<le> sqrt(x\<twosuperior> + y\<twosuperior>)"
huffman@22856
   495
by (rule power2_le_imp_le, simp_all)
huffman@22856
   496
huffman@22858
   497
lemma power2_sum:
huffman@22858
   498
  fixes x y :: "'a::{number_ring,recpower}"
huffman@22858
   499
  shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
huffman@22858
   500
by (simp add: left_distrib right_distrib power2_eq_square)
huffman@22858
   501
huffman@22858
   502
lemma power2_diff:
huffman@22858
   503
  fixes x y :: "'a::{number_ring,recpower}"
huffman@22858
   504
  shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
huffman@22858
   505
by (simp add: left_diff_distrib right_diff_distrib power2_eq_square)
huffman@22858
   506
huffman@22858
   507
lemma real_sqrt_sum_squares_triangle_ineq:
huffman@22858
   508
  "sqrt ((a + c)\<twosuperior> + (b + d)\<twosuperior>) \<le> sqrt (a\<twosuperior> + b\<twosuperior>) + sqrt (c\<twosuperior> + d\<twosuperior>)"
huffman@22858
   509
apply (rule power2_le_imp_le, simp)
huffman@22858
   510
apply (simp add: power2_sum)
huffman@22858
   511
apply (simp only: mult_assoc right_distrib [symmetric])
huffman@22858
   512
apply (rule mult_left_mono)
huffman@22858
   513
apply (rule power2_le_imp_le)
huffman@22858
   514
apply (simp add: power2_sum power_mult_distrib)
huffman@22858
   515
apply (simp add: ring_distrib)
huffman@22858
   516
apply (subgoal_tac "0 \<le> b\<twosuperior> * c\<twosuperior> + a\<twosuperior> * d\<twosuperior> - 2 * (a * c) * (b * d)", simp)
huffman@22858
   517
apply (rule_tac b="(a * d - b * c)\<twosuperior>" in ord_le_eq_trans)
huffman@22858
   518
apply (rule zero_le_power2)
huffman@22858
   519
apply (simp add: power2_diff power_mult_distrib)
huffman@22858
   520
apply (simp add: mult_nonneg_nonneg)
huffman@22858
   521
apply simp
huffman@22858
   522
apply (simp add: add_increasing)
huffman@22858
   523
done
huffman@22858
   524
huffman@22956
   525
text "Legacy theorem names:"
huffman@22956
   526
lemmas real_root_pos2 = real_root_power_cancel
huffman@22956
   527
lemmas real_root_pos_pos = real_root_gt_zero [THEN order_less_imp_le]
huffman@22956
   528
lemmas real_root_pos_pos_le = real_root_ge_zero
huffman@22956
   529
lemmas real_sqrt_mult_distrib = real_sqrt_mult
huffman@22956
   530
lemmas real_sqrt_mult_distrib2 = real_sqrt_mult
huffman@22956
   531
lemmas real_sqrt_eq_zero_cancel_iff = real_sqrt_eq_0_iff
huffman@22956
   532
huffman@22956
   533
(* needed for CauchysMeanTheorem.het_base from AFP *)
huffman@22956
   534
lemma real_root_pos: "0 < x \<Longrightarrow> root (Suc n) (x ^ (Suc n)) = x"
huffman@22956
   535
by (rule real_root_power_cancel [OF zero_less_Suc order_less_imp_le])
huffman@22956
   536
huffman@22956
   537
(* FIXME: the stronger version of real_root_less_iff
huffman@22956
   538
 breaks CauchysMeanTheorem.list_gmean_gt_iff from AFP. *)
huffman@22956
   539
huffman@22956
   540
declare real_root_less_iff [simp del]
huffman@22956
   541
lemma real_root_less_iff_nonneg [simp]:
huffman@22956
   542
  "\<lbrakk>0 < n; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> (root n x < root n y) = (x < y)"
huffman@22956
   543
by (rule real_root_less_iff)
huffman@22956
   544
paulson@14324
   545
end