src/HOL/Library/BigO.thy
author haftmann
Fri Aug 20 17:48:30 2010 +0200 (2010-08-20)
changeset 38622 86fc906dcd86
parent 35028 108662d50512
child 41528 276078f01ada
permissions -rw-r--r--
split and enriched theory SetsAndFunctions
wenzelm@16932
     1
(*  Title:      HOL/Library/BigO.thy
avigad@16908
     2
    Authors:    Jeremy Avigad and Kevin Donnelly
avigad@16908
     3
*)
avigad@16908
     4
avigad@16908
     5
header {* Big O notation *}
avigad@16908
     6
avigad@16908
     7
theory BigO
haftmann@38622
     8
imports Complex_Main Function_Algebras Set_Algebras
avigad@16908
     9
begin
avigad@16908
    10
avigad@16908
    11
text {*
avigad@16908
    12
This library is designed to support asymptotic ``big O'' calculations,
wenzelm@17199
    13
i.e.~reasoning with expressions of the form $f = O(g)$ and $f = g +
wenzelm@17199
    14
O(h)$.  An earlier version of this library is described in detail in
wenzelm@17199
    15
\cite{Avigad-Donnelly}.
wenzelm@17199
    16
avigad@16908
    17
The main changes in this version are as follows:
avigad@16908
    18
\begin{itemize}
wenzelm@17199
    19
\item We have eliminated the @{text O} operator on sets. (Most uses of this seem
avigad@16908
    20
  to be inessential.)
wenzelm@17199
    21
\item We no longer use @{text "+"} as output syntax for @{text "+o"}
wenzelm@17199
    22
\item Lemmas involving @{text "sumr"} have been replaced by more general lemmas 
wenzelm@17199
    23
  involving `@{text "setsum"}.
avigad@16908
    24
\item The library has been expanded, with e.g.~support for expressions of
wenzelm@17199
    25
  the form @{text "f < g + O(h)"}.
avigad@16908
    26
\end{itemize}
wenzelm@17199
    27
wenzelm@17199
    28
See \verb,Complex/ex/BigO_Complex.thy, for additional lemmas that
wenzelm@17199
    29
require the \verb,HOL-Complex, logic image.
avigad@16908
    30
wenzelm@17199
    31
Note also since the Big O library includes rules that demonstrate set
wenzelm@17199
    32
inclusion, to use the automated reasoners effectively with the library
wenzelm@17199
    33
one should redeclare the theorem @{text "subsetI"} as an intro rule,
wenzelm@17199
    34
rather than as an @{text "intro!"} rule, for example, using
wenzelm@17199
    35
\isa{\isakeyword{declare}}~@{text "subsetI [del, intro]"}.
avigad@16908
    36
*}
avigad@16908
    37
avigad@16908
    38
subsection {* Definitions *}
avigad@16908
    39
wenzelm@19736
    40
definition
haftmann@35028
    41
  bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set"  ("(1O'(_'))") where
wenzelm@19736
    42
  "O(f::('a => 'b)) =
avigad@16908
    43
      {h. EX c. ALL x. abs (h x) <= c * abs (f x)}"
avigad@16908
    44
haftmann@35028
    45
lemma bigo_pos_const: "(EX (c::'a::linordered_idom). 
avigad@16908
    46
    ALL x. (abs (h x)) <= (c * (abs (f x))))
avigad@16908
    47
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
avigad@16908
    48
  apply auto
avigad@16908
    49
  apply (case_tac "c = 0")
avigad@16908
    50
  apply simp
avigad@16908
    51
  apply (rule_tac x = "1" in exI)
avigad@16908
    52
  apply simp
avigad@16908
    53
  apply (rule_tac x = "abs c" in exI)
avigad@16908
    54
  apply auto
avigad@16908
    55
  apply (subgoal_tac "c * abs(f x) <= abs c * abs (f x)")
avigad@16908
    56
  apply (erule_tac x = x in allE)
avigad@16908
    57
  apply force
avigad@16908
    58
  apply (rule mult_right_mono)
avigad@16908
    59
  apply (rule abs_ge_self)
avigad@16908
    60
  apply (rule abs_ge_zero)
wenzelm@22665
    61
  done
avigad@16908
    62
avigad@16908
    63
lemma bigo_alt_def: "O(f) = 
avigad@16908
    64
    {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
wenzelm@22665
    65
  by (auto simp add: bigo_def bigo_pos_const)
avigad@16908
    66
avigad@16908
    67
lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
avigad@16908
    68
  apply (auto simp add: bigo_alt_def)
avigad@16908
    69
  apply (rule_tac x = "ca * c" in exI)
avigad@16908
    70
  apply (rule conjI)
avigad@16908
    71
  apply (rule mult_pos_pos)
avigad@16908
    72
  apply (assumption)+
avigad@16908
    73
  apply (rule allI)
avigad@16908
    74
  apply (drule_tac x = "xa" in spec)+
avigad@16908
    75
  apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))")
avigad@16908
    76
  apply (erule order_trans)
avigad@16908
    77
  apply (simp add: mult_ac)
avigad@16908
    78
  apply (rule mult_left_mono, assumption)
avigad@16908
    79
  apply (rule order_less_imp_le, assumption)
wenzelm@22665
    80
  done
avigad@16908
    81
avigad@16908
    82
lemma bigo_refl [intro]: "f : O(f)"
avigad@16908
    83
  apply(auto simp add: bigo_def)
avigad@16908
    84
  apply(rule_tac x = 1 in exI)
avigad@16908
    85
  apply simp
wenzelm@22665
    86
  done
avigad@16908
    87
avigad@16908
    88
lemma bigo_zero: "0 : O(g)"
avigad@16908
    89
  apply (auto simp add: bigo_def func_zero)
avigad@16908
    90
  apply (rule_tac x = 0 in exI)
avigad@16908
    91
  apply auto
wenzelm@22665
    92
  done
avigad@16908
    93
avigad@16908
    94
lemma bigo_zero2: "O(%x.0) = {%x.0}"
avigad@16908
    95
  apply (auto simp add: bigo_def) 
avigad@16908
    96
  apply (rule ext)
avigad@16908
    97
  apply auto
wenzelm@22665
    98
  done
avigad@16908
    99
avigad@16908
   100
lemma bigo_plus_self_subset [intro]: 
berghofe@26814
   101
  "O(f) \<oplus> O(f) <= O(f)"
berghofe@26814
   102
  apply (auto simp add: bigo_alt_def set_plus_def)
avigad@16908
   103
  apply (rule_tac x = "c + ca" in exI)
avigad@16908
   104
  apply auto
nipkow@23477
   105
  apply (simp add: ring_distribs func_plus)
avigad@16908
   106
  apply (rule order_trans)
avigad@16908
   107
  apply (rule abs_triangle_ineq)
avigad@16908
   108
  apply (rule add_mono)
avigad@16908
   109
  apply force
avigad@16908
   110
  apply force
avigad@16908
   111
done
avigad@16908
   112
berghofe@26814
   113
lemma bigo_plus_idemp [simp]: "O(f) \<oplus> O(f) = O(f)"
avigad@16908
   114
  apply (rule equalityI)
avigad@16908
   115
  apply (rule bigo_plus_self_subset)
avigad@16908
   116
  apply (rule set_zero_plus2) 
avigad@16908
   117
  apply (rule bigo_zero)
wenzelm@22665
   118
  done
avigad@16908
   119
berghofe@26814
   120
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) \<oplus> O(g)"
avigad@16908
   121
  apply (rule subsetI)
berghofe@26814
   122
  apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
avigad@16908
   123
  apply (subst bigo_pos_const [symmetric])+
avigad@16908
   124
  apply (rule_tac x = 
avigad@16908
   125
    "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
avigad@16908
   126
  apply (rule conjI)
avigad@16908
   127
  apply (rule_tac x = "c + c" in exI)
avigad@16908
   128
  apply (clarsimp)
avigad@16908
   129
  apply (auto)
avigad@16908
   130
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
avigad@16908
   131
  apply (erule_tac x = xa in allE)
avigad@16908
   132
  apply (erule order_trans)
avigad@16908
   133
  apply (simp)
avigad@16908
   134
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
avigad@16908
   135
  apply (erule order_trans)
nipkow@23477
   136
  apply (simp add: ring_distribs)
avigad@16908
   137
  apply (rule mult_left_mono)
avigad@16908
   138
  apply assumption
avigad@16908
   139
  apply (simp add: order_less_le)
avigad@16908
   140
  apply (rule mult_left_mono)
avigad@16908
   141
  apply (simp add: abs_triangle_ineq)
avigad@16908
   142
  apply (simp add: order_less_le)
avigad@16908
   143
  apply (rule mult_nonneg_nonneg)
avigad@16908
   144
  apply (rule add_nonneg_nonneg)
avigad@16908
   145
  apply auto
avigad@16908
   146
  apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
avigad@16908
   147
     in exI)
avigad@16908
   148
  apply (rule conjI)
avigad@16908
   149
  apply (rule_tac x = "c + c" in exI)
avigad@16908
   150
  apply auto
avigad@16908
   151
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
avigad@16908
   152
  apply (erule_tac x = xa in allE)
avigad@16908
   153
  apply (erule order_trans)
avigad@16908
   154
  apply (simp)
avigad@16908
   155
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
avigad@16908
   156
  apply (erule order_trans)
nipkow@23477
   157
  apply (simp add: ring_distribs)
avigad@16908
   158
  apply (rule mult_left_mono)
avigad@16908
   159
  apply (simp add: order_less_le)
avigad@16908
   160
  apply (simp add: order_less_le)
avigad@16908
   161
  apply (rule mult_left_mono)
avigad@16908
   162
  apply (rule abs_triangle_ineq)
avigad@16908
   163
  apply (simp add: order_less_le)
avigad@16908
   164
  apply (rule mult_nonneg_nonneg)
avigad@16908
   165
  apply (rule add_nonneg_nonneg)
avigad@16908
   166
  apply (erule order_less_imp_le)+
avigad@16908
   167
  apply simp
avigad@16908
   168
  apply (rule ext)
avigad@16908
   169
  apply (auto simp add: if_splits linorder_not_le)
wenzelm@22665
   170
  done
avigad@16908
   171
berghofe@26814
   172
lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A \<oplus> B <= O(f)"
berghofe@26814
   173
  apply (subgoal_tac "A \<oplus> B <= O(f) \<oplus> O(f)")
avigad@16908
   174
  apply (erule order_trans)
avigad@16908
   175
  apply simp
avigad@16908
   176
  apply (auto del: subsetI simp del: bigo_plus_idemp)
wenzelm@22665
   177
  done
avigad@16908
   178
avigad@16908
   179
lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==> 
berghofe@26814
   180
    O(f + g) = O(f) \<oplus> O(g)"
avigad@16908
   181
  apply (rule equalityI)
avigad@16908
   182
  apply (rule bigo_plus_subset)
berghofe@26814
   183
  apply (simp add: bigo_alt_def set_plus_def func_plus)
avigad@16908
   184
  apply clarify
avigad@16908
   185
  apply (rule_tac x = "max c ca" in exI)
avigad@16908
   186
  apply (rule conjI)
avigad@16908
   187
  apply (subgoal_tac "c <= max c ca")
avigad@16908
   188
  apply (erule order_less_le_trans)
avigad@16908
   189
  apply assumption
avigad@16908
   190
  apply (rule le_maxI1)
avigad@16908
   191
  apply clarify
avigad@16908
   192
  apply (drule_tac x = "xa" in spec)+
avigad@16908
   193
  apply (subgoal_tac "0 <= f xa + g xa")
nipkow@23477
   194
  apply (simp add: ring_distribs)
avigad@16908
   195
  apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
avigad@16908
   196
  apply (subgoal_tac "abs(a xa) + abs(b xa) <= 
avigad@16908
   197
      max c ca * f xa + max c ca * g xa")
avigad@16908
   198
  apply (force)
avigad@16908
   199
  apply (rule add_mono)
avigad@16908
   200
  apply (subgoal_tac "c * f xa <= max c ca * f xa")
avigad@16908
   201
  apply (force)
avigad@16908
   202
  apply (rule mult_right_mono)
avigad@16908
   203
  apply (rule le_maxI1)
avigad@16908
   204
  apply assumption
avigad@16908
   205
  apply (subgoal_tac "ca * g xa <= max c ca * g xa")
avigad@16908
   206
  apply (force)
avigad@16908
   207
  apply (rule mult_right_mono)
avigad@16908
   208
  apply (rule le_maxI2)
avigad@16908
   209
  apply assumption
avigad@16908
   210
  apply (rule abs_triangle_ineq)
avigad@16908
   211
  apply (rule add_nonneg_nonneg)
avigad@16908
   212
  apply assumption+
wenzelm@22665
   213
  done
avigad@16908
   214
avigad@16908
   215
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
avigad@16908
   216
    f : O(g)" 
avigad@16908
   217
  apply (auto simp add: bigo_def)
avigad@16908
   218
  apply (rule_tac x = "abs c" in exI)
avigad@16908
   219
  apply auto
avigad@16908
   220
  apply (drule_tac x = x in spec)+
avigad@16908
   221
  apply (simp add: abs_mult [symmetric])
wenzelm@22665
   222
  done
avigad@16908
   223
avigad@16908
   224
lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==> 
avigad@16908
   225
    f : O(g)" 
avigad@16908
   226
  apply (erule bigo_bounded_alt [of f 1 g])
avigad@16908
   227
  apply simp
wenzelm@22665
   228
  done
avigad@16908
   229
avigad@16908
   230
lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
avigad@16908
   231
    f : lb +o O(g)"
avigad@16908
   232
  apply (rule set_minus_imp_plus)
avigad@16908
   233
  apply (rule bigo_bounded)
berghofe@26814
   234
  apply (auto simp add: diff_minus fun_Compl_def func_plus)
avigad@16908
   235
  apply (drule_tac x = x in spec)+
avigad@16908
   236
  apply force
avigad@16908
   237
  apply (drule_tac x = x in spec)+
avigad@16908
   238
  apply force
wenzelm@22665
   239
  done
avigad@16908
   240
avigad@16908
   241
lemma bigo_abs: "(%x. abs(f x)) =o O(f)" 
avigad@16908
   242
  apply (unfold bigo_def)
avigad@16908
   243
  apply auto
avigad@16908
   244
  apply (rule_tac x = 1 in exI)
avigad@16908
   245
  apply auto
wenzelm@22665
   246
  done
avigad@16908
   247
avigad@16908
   248
lemma bigo_abs2: "f =o O(%x. abs(f x))"
avigad@16908
   249
  apply (unfold bigo_def)
avigad@16908
   250
  apply auto
avigad@16908
   251
  apply (rule_tac x = 1 in exI)
avigad@16908
   252
  apply auto
wenzelm@22665
   253
  done
avigad@16908
   254
avigad@16908
   255
lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
avigad@16908
   256
  apply (rule equalityI)
avigad@16908
   257
  apply (rule bigo_elt_subset)
avigad@16908
   258
  apply (rule bigo_abs2)
avigad@16908
   259
  apply (rule bigo_elt_subset)
avigad@16908
   260
  apply (rule bigo_abs)
wenzelm@22665
   261
  done
avigad@16908
   262
avigad@16908
   263
lemma bigo_abs4: "f =o g +o O(h) ==> 
avigad@16908
   264
    (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
avigad@16908
   265
  apply (drule set_plus_imp_minus)
avigad@16908
   266
  apply (rule set_minus_imp_plus)
berghofe@26814
   267
  apply (subst fun_diff_def)
avigad@16908
   268
proof -
avigad@16908
   269
  assume a: "f - g : O(h)"
avigad@16908
   270
  have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
avigad@16908
   271
    by (rule bigo_abs2)
avigad@16908
   272
  also have "... <= O(%x. abs (f x - g x))"
avigad@16908
   273
    apply (rule bigo_elt_subset)
avigad@16908
   274
    apply (rule bigo_bounded)
avigad@16908
   275
    apply force
avigad@16908
   276
    apply (rule allI)
avigad@16908
   277
    apply (rule abs_triangle_ineq3)
avigad@16908
   278
    done
avigad@16908
   279
  also have "... <= O(f - g)"
avigad@16908
   280
    apply (rule bigo_elt_subset)
berghofe@26814
   281
    apply (subst fun_diff_def)
avigad@16908
   282
    apply (rule bigo_abs)
avigad@16908
   283
    done
wenzelm@23373
   284
  also from a have "... <= O(h)"
avigad@16908
   285
    by (rule bigo_elt_subset)
avigad@16908
   286
  finally show "(%x. abs (f x) - abs (g x)) : O(h)".
avigad@16908
   287
qed
avigad@16908
   288
avigad@16908
   289
lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)" 
wenzelm@22665
   290
  by (unfold bigo_def, auto)
avigad@16908
   291
berghofe@26814
   292
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) \<oplus> O(h)"
avigad@16908
   293
proof -
avigad@16908
   294
  assume "f : g +o O(h)"
berghofe@26814
   295
  also have "... <= O(g) \<oplus> O(h)"
avigad@16908
   296
    by (auto del: subsetI)
berghofe@26814
   297
  also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
avigad@16908
   298
    apply (subst bigo_abs3 [symmetric])+
avigad@16908
   299
    apply (rule refl)
avigad@16908
   300
    done
avigad@16908
   301
  also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
avigad@16908
   302
    by (rule bigo_plus_eq [symmetric], auto)
avigad@16908
   303
  finally have "f : ...".
avigad@16908
   304
  then have "O(f) <= ..."
avigad@16908
   305
    by (elim bigo_elt_subset)
berghofe@26814
   306
  also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
avigad@16908
   307
    by (rule bigo_plus_eq, auto)
avigad@16908
   308
  finally show ?thesis
avigad@16908
   309
    by (simp add: bigo_abs3 [symmetric])
avigad@16908
   310
qed
avigad@16908
   311
berghofe@26814
   312
lemma bigo_mult [intro]: "O(f)\<otimes>O(g) <= O(f * g)"
avigad@16908
   313
  apply (rule subsetI)
avigad@16908
   314
  apply (subst bigo_def)
berghofe@26814
   315
  apply (auto simp add: bigo_alt_def set_times_def func_times)
avigad@16908
   316
  apply (rule_tac x = "c * ca" in exI)
avigad@16908
   317
  apply(rule allI)
avigad@16908
   318
  apply(erule_tac x = x in allE)+
avigad@16908
   319
  apply(subgoal_tac "c * ca * abs(f x * g x) = 
avigad@16908
   320
      (c * abs(f x)) * (ca * abs(g x))")
avigad@16908
   321
  apply(erule ssubst)
avigad@16908
   322
  apply (subst abs_mult)
avigad@16908
   323
  apply (rule mult_mono)
avigad@16908
   324
  apply assumption+
avigad@16908
   325
  apply (rule mult_nonneg_nonneg)
avigad@16908
   326
  apply auto
avigad@16908
   327
  apply (simp add: mult_ac abs_mult)
wenzelm@22665
   328
  done
avigad@16908
   329
avigad@16908
   330
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
avigad@16908
   331
  apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
avigad@16908
   332
  apply (rule_tac x = c in exI)
avigad@16908
   333
  apply auto
avigad@16908
   334
  apply (drule_tac x = x in spec)
avigad@16908
   335
  apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
avigad@16908
   336
  apply (force simp add: mult_ac)
avigad@16908
   337
  apply (rule mult_left_mono, assumption)
avigad@16908
   338
  apply (rule abs_ge_zero)
wenzelm@22665
   339
  done
avigad@16908
   340
avigad@16908
   341
lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
avigad@16908
   342
  apply (rule subsetD)
avigad@16908
   343
  apply (rule bigo_mult)
avigad@16908
   344
  apply (erule set_times_intro, assumption)
wenzelm@22665
   345
  done
avigad@16908
   346
avigad@16908
   347
lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
avigad@16908
   348
  apply (drule set_plus_imp_minus)
avigad@16908
   349
  apply (rule set_minus_imp_plus)
avigad@16908
   350
  apply (drule bigo_mult3 [where g = g and j = g])
nipkow@29667
   351
  apply (auto simp add: algebra_simps)
wenzelm@22665
   352
  done
avigad@16908
   353
avigad@16908
   354
lemma bigo_mult5: "ALL x. f x ~= 0 ==>
haftmann@35028
   355
    O(f * g) <= (f::'a => ('b::linordered_field)) *o O(g)"
avigad@16908
   356
proof -
avigad@16908
   357
  assume "ALL x. f x ~= 0"
avigad@16908
   358
  show "O(f * g) <= f *o O(g)"
avigad@16908
   359
  proof
avigad@16908
   360
    fix h
avigad@16908
   361
    assume "h : O(f * g)"
avigad@16908
   362
    then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
avigad@16908
   363
      by auto
avigad@16908
   364
    also have "... <= O((%x. 1 / f x) * (f * g))"
avigad@16908
   365
      by (rule bigo_mult2)
avigad@16908
   366
    also have "(%x. 1 / f x) * (f * g) = g"
avigad@16908
   367
      apply (simp add: func_times) 
avigad@16908
   368
      apply (rule ext)
avigad@16908
   369
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
avigad@16908
   370
      done
avigad@16908
   371
    finally have "(%x. (1::'b) / f x) * h : O(g)".
avigad@16908
   372
    then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
avigad@16908
   373
      by auto
avigad@16908
   374
    also have "f * ((%x. (1::'b) / f x) * h) = h"
avigad@16908
   375
      apply (simp add: func_times) 
avigad@16908
   376
      apply (rule ext)
avigad@16908
   377
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
avigad@16908
   378
      done
avigad@16908
   379
    finally show "h : f *o O(g)".
avigad@16908
   380
  qed
avigad@16908
   381
qed
avigad@16908
   382
avigad@16908
   383
lemma bigo_mult6: "ALL x. f x ~= 0 ==>
haftmann@35028
   384
    O(f * g) = (f::'a => ('b::linordered_field)) *o O(g)"
avigad@16908
   385
  apply (rule equalityI)
avigad@16908
   386
  apply (erule bigo_mult5)
avigad@16908
   387
  apply (rule bigo_mult2)
wenzelm@22665
   388
  done
avigad@16908
   389
avigad@16908
   390
lemma bigo_mult7: "ALL x. f x ~= 0 ==>
haftmann@35028
   391
    O(f * g) <= O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
avigad@16908
   392
  apply (subst bigo_mult6)
avigad@16908
   393
  apply assumption
avigad@16908
   394
  apply (rule set_times_mono3)
avigad@16908
   395
  apply (rule bigo_refl)
wenzelm@22665
   396
  done
avigad@16908
   397
avigad@16908
   398
lemma bigo_mult8: "ALL x. f x ~= 0 ==>
haftmann@35028
   399
    O(f * g) = O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
avigad@16908
   400
  apply (rule equalityI)
avigad@16908
   401
  apply (erule bigo_mult7)
avigad@16908
   402
  apply (rule bigo_mult)
wenzelm@22665
   403
  done
avigad@16908
   404
avigad@16908
   405
lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
berghofe@26814
   406
  by (auto simp add: bigo_def fun_Compl_def)
avigad@16908
   407
avigad@16908
   408
lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
avigad@16908
   409
  apply (rule set_minus_imp_plus)
avigad@16908
   410
  apply (drule set_plus_imp_minus)
avigad@16908
   411
  apply (drule bigo_minus)
avigad@16908
   412
  apply (simp add: diff_minus)
wenzelm@22665
   413
  done
avigad@16908
   414
avigad@16908
   415
lemma bigo_minus3: "O(-f) = O(f)"
berghofe@26814
   416
  by (auto simp add: bigo_def fun_Compl_def abs_minus_cancel)
avigad@16908
   417
avigad@16908
   418
lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
avigad@16908
   419
proof -
avigad@16908
   420
  assume a: "f : O(g)"
avigad@16908
   421
  show "f +o O(g) <= O(g)"
avigad@16908
   422
  proof -
avigad@16908
   423
    have "f : O(f)" by auto
berghofe@26814
   424
    then have "f +o O(g) <= O(f) \<oplus> O(g)"
avigad@16908
   425
      by (auto del: subsetI)
berghofe@26814
   426
    also have "... <= O(g) \<oplus> O(g)"
avigad@16908
   427
    proof -
avigad@16908
   428
      from a have "O(f) <= O(g)" by (auto del: subsetI)
avigad@16908
   429
      thus ?thesis by (auto del: subsetI)
avigad@16908
   430
    qed
avigad@16908
   431
    also have "... <= O(g)" by (simp add: bigo_plus_idemp)
avigad@16908
   432
    finally show ?thesis .
avigad@16908
   433
  qed
avigad@16908
   434
qed
avigad@16908
   435
avigad@16908
   436
lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
avigad@16908
   437
proof -
avigad@16908
   438
  assume a: "f : O(g)"
avigad@16908
   439
  show "O(g) <= f +o O(g)"
avigad@16908
   440
  proof -
avigad@16908
   441
    from a have "-f : O(g)" by auto
avigad@16908
   442
    then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
avigad@16908
   443
    then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
avigad@16908
   444
    also have "f +o (-f +o O(g)) = O(g)"
avigad@16908
   445
      by (simp add: set_plus_rearranges)
avigad@16908
   446
    finally show ?thesis .
avigad@16908
   447
  qed
avigad@16908
   448
qed
avigad@16908
   449
avigad@16908
   450
lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
avigad@16908
   451
  apply (rule equalityI)
avigad@16908
   452
  apply (erule bigo_plus_absorb_lemma1)
avigad@16908
   453
  apply (erule bigo_plus_absorb_lemma2)
wenzelm@22665
   454
  done
avigad@16908
   455
avigad@16908
   456
lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
avigad@16908
   457
  apply (subgoal_tac "f +o A <= f +o O(g)")
avigad@16908
   458
  apply force+
wenzelm@22665
   459
  done
avigad@16908
   460
avigad@16908
   461
lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
avigad@16908
   462
  apply (subst set_minus_plus [symmetric])
avigad@16908
   463
  apply (subgoal_tac "g - f = - (f - g)")
avigad@16908
   464
  apply (erule ssubst)
avigad@16908
   465
  apply (rule bigo_minus)
avigad@16908
   466
  apply (subst set_minus_plus)
avigad@16908
   467
  apply assumption
avigad@16908
   468
  apply  (simp add: diff_minus add_ac)
wenzelm@22665
   469
  done
avigad@16908
   470
avigad@16908
   471
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
avigad@16908
   472
  apply (rule iffI)
avigad@16908
   473
  apply (erule bigo_add_commute_imp)+
wenzelm@22665
   474
  done
avigad@16908
   475
avigad@16908
   476
lemma bigo_const1: "(%x. c) : O(%x. 1)"
wenzelm@22665
   477
  by (auto simp add: bigo_def mult_ac)
avigad@16908
   478
avigad@16908
   479
lemma bigo_const2 [intro]: "O(%x. c) <= O(%x. 1)"
avigad@16908
   480
  apply (rule bigo_elt_subset)
avigad@16908
   481
  apply (rule bigo_const1)
wenzelm@22665
   482
  done
avigad@16908
   483
haftmann@35028
   484
lemma bigo_const3: "(c::'a::linordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
avigad@16908
   485
  apply (simp add: bigo_def)
avigad@16908
   486
  apply (rule_tac x = "abs(inverse c)" in exI)
avigad@16908
   487
  apply (simp add: abs_mult [symmetric])
wenzelm@22665
   488
  done
avigad@16908
   489
haftmann@35028
   490
lemma bigo_const4: "(c::'a::linordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
wenzelm@22665
   491
  by (rule bigo_elt_subset, rule bigo_const3, assumption)
avigad@16908
   492
haftmann@35028
   493
lemma bigo_const [simp]: "(c::'a::linordered_field) ~= 0 ==> 
avigad@16908
   494
    O(%x. c) = O(%x. 1)"
wenzelm@22665
   495
  by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
avigad@16908
   496
avigad@16908
   497
lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
avigad@16908
   498
  apply (simp add: bigo_def)
avigad@16908
   499
  apply (rule_tac x = "abs(c)" in exI)
avigad@16908
   500
  apply (auto simp add: abs_mult [symmetric])
wenzelm@22665
   501
  done
avigad@16908
   502
avigad@16908
   503
lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
wenzelm@22665
   504
  by (rule bigo_elt_subset, rule bigo_const_mult1)
avigad@16908
   505
haftmann@35028
   506
lemma bigo_const_mult3: "(c::'a::linordered_field) ~= 0 ==> f : O(%x. c * f x)"
avigad@16908
   507
  apply (simp add: bigo_def)
avigad@16908
   508
  apply (rule_tac x = "abs(inverse c)" in exI)
avigad@16908
   509
  apply (simp add: abs_mult [symmetric] mult_assoc [symmetric])
wenzelm@22665
   510
  done
avigad@16908
   511
haftmann@35028
   512
lemma bigo_const_mult4: "(c::'a::linordered_field) ~= 0 ==> 
avigad@16908
   513
    O(f) <= O(%x. c * f x)"
wenzelm@22665
   514
  by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
avigad@16908
   515
haftmann@35028
   516
lemma bigo_const_mult [simp]: "(c::'a::linordered_field) ~= 0 ==> 
avigad@16908
   517
    O(%x. c * f x) = O(f)"
wenzelm@22665
   518
  by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
avigad@16908
   519
haftmann@35028
   520
lemma bigo_const_mult5 [simp]: "(c::'a::linordered_field) ~= 0 ==> 
avigad@16908
   521
    (%x. c) *o O(f) = O(f)"
avigad@16908
   522
  apply (auto del: subsetI)
avigad@16908
   523
  apply (rule order_trans)
avigad@16908
   524
  apply (rule bigo_mult2)
avigad@16908
   525
  apply (simp add: func_times)
avigad@16908
   526
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
avigad@16908
   527
  apply (rule_tac x = "%y. inverse c * x y" in exI)
avigad@16908
   528
  apply (simp add: mult_assoc [symmetric] abs_mult)
avigad@16908
   529
  apply (rule_tac x = "abs (inverse c) * ca" in exI)
avigad@16908
   530
  apply (rule allI)
avigad@16908
   531
  apply (subst mult_assoc)
avigad@16908
   532
  apply (rule mult_left_mono)
avigad@16908
   533
  apply (erule spec)
avigad@16908
   534
  apply force
wenzelm@22665
   535
  done
avigad@16908
   536
avigad@16908
   537
lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
avigad@16908
   538
  apply (auto intro!: subsetI
avigad@16908
   539
    simp add: bigo_def elt_set_times_def func_times)
avigad@16908
   540
  apply (rule_tac x = "ca * (abs c)" in exI)
avigad@16908
   541
  apply (rule allI)
avigad@16908
   542
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
avigad@16908
   543
  apply (erule ssubst)
avigad@16908
   544
  apply (subst abs_mult)
avigad@16908
   545
  apply (rule mult_left_mono)
avigad@16908
   546
  apply (erule spec)
avigad@16908
   547
  apply simp
avigad@16908
   548
  apply(simp add: mult_ac)
wenzelm@22665
   549
  done
avigad@16908
   550
avigad@16908
   551
lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
avigad@16908
   552
proof -
avigad@16908
   553
  assume "f =o O(g)"
avigad@16908
   554
  then have "(%x. c) * f =o (%x. c) *o O(g)"
avigad@16908
   555
    by auto
avigad@16908
   556
  also have "(%x. c) * f = (%x. c * f x)"
avigad@16908
   557
    by (simp add: func_times)
avigad@16908
   558
  also have "(%x. c) *o O(g) <= O(g)"
avigad@16908
   559
    by (auto del: subsetI)
avigad@16908
   560
  finally show ?thesis .
avigad@16908
   561
qed
avigad@16908
   562
avigad@16908
   563
lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
avigad@16908
   564
by (unfold bigo_def, auto)
avigad@16908
   565
avigad@16908
   566
lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o 
avigad@16908
   567
    O(%x. h(k x))"
berghofe@26814
   568
  apply (simp only: set_minus_plus [symmetric] diff_minus fun_Compl_def
avigad@16908
   569
      func_plus)
avigad@16908
   570
  apply (erule bigo_compose1)
avigad@16908
   571
done
avigad@16908
   572
wenzelm@22665
   573
avigad@16908
   574
subsection {* Setsum *}
avigad@16908
   575
avigad@16908
   576
lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==> 
avigad@16908
   577
    EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
avigad@16908
   578
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"  
avigad@16908
   579
  apply (auto simp add: bigo_def)
avigad@16908
   580
  apply (rule_tac x = "abs c" in exI)
wenzelm@17199
   581
  apply (subst abs_of_nonneg) back back
avigad@16908
   582
  apply (rule setsum_nonneg)
avigad@16908
   583
  apply force
ballarin@19279
   584
  apply (subst setsum_right_distrib)
avigad@16908
   585
  apply (rule allI)
avigad@16908
   586
  apply (rule order_trans)
avigad@16908
   587
  apply (rule setsum_abs)
avigad@16908
   588
  apply (rule setsum_mono)
avigad@16908
   589
  apply (rule order_trans)
avigad@16908
   590
  apply (drule spec)+
avigad@16908
   591
  apply (drule bspec)+
avigad@16908
   592
  apply assumption+
avigad@16908
   593
  apply (drule bspec)
avigad@16908
   594
  apply assumption+
avigad@16908
   595
  apply (rule mult_right_mono) 
avigad@16908
   596
  apply (rule abs_ge_self)
avigad@16908
   597
  apply force
wenzelm@22665
   598
  done
avigad@16908
   599
avigad@16908
   600
lemma bigo_setsum1: "ALL x y. 0 <= h x y ==> 
avigad@16908
   601
    EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
avigad@16908
   602
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
avigad@16908
   603
  apply (rule bigo_setsum_main)
avigad@16908
   604
  apply force
avigad@16908
   605
  apply clarsimp
avigad@16908
   606
  apply (rule_tac x = c in exI)
avigad@16908
   607
  apply force
wenzelm@22665
   608
  done
avigad@16908
   609
avigad@16908
   610
lemma bigo_setsum2: "ALL y. 0 <= h y ==> 
avigad@16908
   611
    EX c. ALL y. abs(f y) <= c * (h y) ==>
avigad@16908
   612
      (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
wenzelm@22665
   613
  by (rule bigo_setsum1, auto)  
avigad@16908
   614
avigad@16908
   615
lemma bigo_setsum3: "f =o O(h) ==>
avigad@16908
   616
    (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   617
      O(%x. SUM y : A x. abs(l x y * h(k x y)))"
avigad@16908
   618
  apply (rule bigo_setsum1)
avigad@16908
   619
  apply (rule allI)+
avigad@16908
   620
  apply (rule abs_ge_zero)
avigad@16908
   621
  apply (unfold bigo_def)
avigad@16908
   622
  apply auto
avigad@16908
   623
  apply (rule_tac x = c in exI)
avigad@16908
   624
  apply (rule allI)+
avigad@16908
   625
  apply (subst abs_mult)+
avigad@16908
   626
  apply (subst mult_left_commute)
avigad@16908
   627
  apply (rule mult_left_mono)
avigad@16908
   628
  apply (erule spec)
avigad@16908
   629
  apply (rule abs_ge_zero)
wenzelm@22665
   630
  done
avigad@16908
   631
avigad@16908
   632
lemma bigo_setsum4: "f =o g +o O(h) ==>
avigad@16908
   633
    (%x. SUM y : A x. l x y * f(k x y)) =o
avigad@16908
   634
      (%x. SUM y : A x. l x y * g(k x y)) +o
avigad@16908
   635
        O(%x. SUM y : A x. abs(l x y * h(k x y)))"
avigad@16908
   636
  apply (rule set_minus_imp_plus)
berghofe@26814
   637
  apply (subst fun_diff_def)
avigad@16908
   638
  apply (subst setsum_subtractf [symmetric])
avigad@16908
   639
  apply (subst right_diff_distrib [symmetric])
avigad@16908
   640
  apply (rule bigo_setsum3)
berghofe@26814
   641
  apply (subst fun_diff_def [symmetric])
avigad@16908
   642
  apply (erule set_plus_imp_minus)
wenzelm@22665
   643
  done
avigad@16908
   644
avigad@16908
   645
lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==> 
avigad@16908
   646
    ALL x. 0 <= h x ==>
avigad@16908
   647
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   648
        O(%x. SUM y : A x. (l x y) * h(k x y))" 
avigad@16908
   649
  apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) = 
avigad@16908
   650
      (%x. SUM y : A x. abs((l x y) * h(k x y)))")
avigad@16908
   651
  apply (erule ssubst)
avigad@16908
   652
  apply (erule bigo_setsum3)
avigad@16908
   653
  apply (rule ext)
avigad@16908
   654
  apply (rule setsum_cong2)
avigad@16908
   655
  apply (subst abs_of_nonneg)
avigad@16908
   656
  apply (rule mult_nonneg_nonneg)
avigad@16908
   657
  apply auto
wenzelm@22665
   658
  done
avigad@16908
   659
avigad@16908
   660
lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
avigad@16908
   661
    ALL x. 0 <= h x ==>
avigad@16908
   662
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   663
        (%x. SUM y : A x. (l x y) * g(k x y)) +o
avigad@16908
   664
          O(%x. SUM y : A x. (l x y) * h(k x y))" 
avigad@16908
   665
  apply (rule set_minus_imp_plus)
berghofe@26814
   666
  apply (subst fun_diff_def)
avigad@16908
   667
  apply (subst setsum_subtractf [symmetric])
avigad@16908
   668
  apply (subst right_diff_distrib [symmetric])
avigad@16908
   669
  apply (rule bigo_setsum5)
berghofe@26814
   670
  apply (subst fun_diff_def [symmetric])
avigad@16908
   671
  apply (drule set_plus_imp_minus)
avigad@16908
   672
  apply auto
wenzelm@22665
   673
  done
wenzelm@22665
   674
avigad@16908
   675
avigad@16908
   676
subsection {* Misc useful stuff *}
avigad@16908
   677
avigad@16908
   678
lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
berghofe@26814
   679
  A \<oplus> B <= O(f)"
avigad@16908
   680
  apply (subst bigo_plus_idemp [symmetric])
avigad@16908
   681
  apply (rule set_plus_mono2)
avigad@16908
   682
  apply assumption+
wenzelm@22665
   683
  done
avigad@16908
   684
avigad@16908
   685
lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
avigad@16908
   686
  apply (subst bigo_plus_idemp [symmetric])
avigad@16908
   687
  apply (rule set_plus_intro)
avigad@16908
   688
  apply assumption+
wenzelm@22665
   689
  done
avigad@16908
   690
  
haftmann@35028
   691
lemma bigo_useful_const_mult: "(c::'a::linordered_field) ~= 0 ==> 
avigad@16908
   692
    (%x. c) * f =o O(h) ==> f =o O(h)"
avigad@16908
   693
  apply (rule subsetD)
avigad@16908
   694
  apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
avigad@16908
   695
  apply assumption
avigad@16908
   696
  apply (rule bigo_const_mult6)
avigad@16908
   697
  apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
avigad@16908
   698
  apply (erule ssubst)
avigad@16908
   699
  apply (erule set_times_intro2)
nipkow@23413
   700
  apply (simp add: func_times)
wenzelm@22665
   701
  done
avigad@16908
   702
avigad@16908
   703
lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
avigad@16908
   704
    f =o O(h)"
avigad@16908
   705
  apply (simp add: bigo_alt_def)
avigad@16908
   706
  apply auto
avigad@16908
   707
  apply (rule_tac x = c in exI)
avigad@16908
   708
  apply auto
avigad@16908
   709
  apply (case_tac "x = 0")
avigad@16908
   710
  apply simp
avigad@16908
   711
  apply (rule mult_nonneg_nonneg)
avigad@16908
   712
  apply force
avigad@16908
   713
  apply force
avigad@16908
   714
  apply (subgoal_tac "x = Suc (x - 1)")
wenzelm@17199
   715
  apply (erule ssubst) back
avigad@16908
   716
  apply (erule spec)
avigad@16908
   717
  apply simp
wenzelm@22665
   718
  done
avigad@16908
   719
avigad@16908
   720
lemma bigo_fix2: 
avigad@16908
   721
    "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==> 
avigad@16908
   722
       f 0 = g 0 ==> f =o g +o O(h)"
avigad@16908
   723
  apply (rule set_minus_imp_plus)
avigad@16908
   724
  apply (rule bigo_fix)
berghofe@26814
   725
  apply (subst fun_diff_def)
berghofe@26814
   726
  apply (subst fun_diff_def [symmetric])
avigad@16908
   727
  apply (rule set_plus_imp_minus)
avigad@16908
   728
  apply simp
berghofe@26814
   729
  apply (simp add: fun_diff_def)
wenzelm@22665
   730
  done
wenzelm@22665
   731
avigad@16908
   732
avigad@16908
   733
subsection {* Less than or equal to *}
avigad@16908
   734
wenzelm@19736
   735
definition
haftmann@35028
   736
  lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)"
wenzelm@21404
   737
    (infixl "<o" 70) where
wenzelm@19736
   738
  "f <o g = (%x. max (f x - g x) 0)"
avigad@16908
   739
avigad@16908
   740
lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
avigad@16908
   741
    g =o O(h)"
avigad@16908
   742
  apply (unfold bigo_def)
avigad@16908
   743
  apply clarsimp
avigad@16908
   744
  apply (rule_tac x = c in exI)
avigad@16908
   745
  apply (rule allI)
avigad@16908
   746
  apply (rule order_trans)
avigad@16908
   747
  apply (erule spec)+
wenzelm@22665
   748
  done
avigad@16908
   749
avigad@16908
   750
lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
avigad@16908
   751
      g =o O(h)"
avigad@16908
   752
  apply (erule bigo_lesseq1)
avigad@16908
   753
  apply (rule allI)
avigad@16908
   754
  apply (drule_tac x = x in spec)
avigad@16908
   755
  apply (rule order_trans)
avigad@16908
   756
  apply assumption
avigad@16908
   757
  apply (rule abs_ge_self)
wenzelm@22665
   758
  done
avigad@16908
   759
avigad@16908
   760
lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
wenzelm@22665
   761
    g =o O(h)"
avigad@16908
   762
  apply (erule bigo_lesseq2)
avigad@16908
   763
  apply (rule allI)
avigad@16908
   764
  apply (subst abs_of_nonneg)
avigad@16908
   765
  apply (erule spec)+
wenzelm@22665
   766
  done
avigad@16908
   767
avigad@16908
   768
lemma bigo_lesseq4: "f =o O(h) ==>
avigad@16908
   769
    ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
avigad@16908
   770
      g =o O(h)"
avigad@16908
   771
  apply (erule bigo_lesseq1)
avigad@16908
   772
  apply (rule allI)
avigad@16908
   773
  apply (subst abs_of_nonneg)
avigad@16908
   774
  apply (erule spec)+
wenzelm@22665
   775
  done
avigad@16908
   776
avigad@16908
   777
lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
avigad@16908
   778
  apply (unfold lesso_def)
avigad@16908
   779
  apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
avigad@16908
   780
  apply (erule ssubst)
avigad@16908
   781
  apply (rule bigo_zero)
avigad@16908
   782
  apply (unfold func_zero)
avigad@16908
   783
  apply (rule ext)
avigad@16908
   784
  apply (simp split: split_max)
wenzelm@22665
   785
  done
avigad@16908
   786
avigad@16908
   787
lemma bigo_lesso2: "f =o g +o O(h) ==>
avigad@16908
   788
    ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
avigad@16908
   789
      k <o g =o O(h)"
avigad@16908
   790
  apply (unfold lesso_def)
avigad@16908
   791
  apply (rule bigo_lesseq4)
avigad@16908
   792
  apply (erule set_plus_imp_minus)
avigad@16908
   793
  apply (rule allI)
avigad@16908
   794
  apply (rule le_maxI2)
avigad@16908
   795
  apply (rule allI)
berghofe@26814
   796
  apply (subst fun_diff_def)
avigad@16908
   797
  apply (case_tac "0 <= k x - g x")
avigad@16908
   798
  apply simp
avigad@16908
   799
  apply (subst abs_of_nonneg)
wenzelm@17199
   800
  apply (drule_tac x = x in spec) back
nipkow@29667
   801
  apply (simp add: algebra_simps)
avigad@16908
   802
  apply (subst diff_minus)+
avigad@16908
   803
  apply (rule add_right_mono)
avigad@16908
   804
  apply (erule spec)
avigad@16908
   805
  apply (rule order_trans) 
avigad@16908
   806
  prefer 2
avigad@16908
   807
  apply (rule abs_ge_zero)
nipkow@29667
   808
  apply (simp add: algebra_simps)
wenzelm@22665
   809
  done
avigad@16908
   810
avigad@16908
   811
lemma bigo_lesso3: "f =o g +o O(h) ==>
avigad@16908
   812
    ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
avigad@16908
   813
      f <o k =o O(h)"
avigad@16908
   814
  apply (unfold lesso_def)
avigad@16908
   815
  apply (rule bigo_lesseq4)
avigad@16908
   816
  apply (erule set_plus_imp_minus)
avigad@16908
   817
  apply (rule allI)
avigad@16908
   818
  apply (rule le_maxI2)
avigad@16908
   819
  apply (rule allI)
berghofe@26814
   820
  apply (subst fun_diff_def)
avigad@16908
   821
  apply (case_tac "0 <= f x - k x")
avigad@16908
   822
  apply simp
avigad@16908
   823
  apply (subst abs_of_nonneg)
wenzelm@17199
   824
  apply (drule_tac x = x in spec) back
nipkow@29667
   825
  apply (simp add: algebra_simps)
avigad@16908
   826
  apply (subst diff_minus)+
avigad@16908
   827
  apply (rule add_left_mono)
avigad@16908
   828
  apply (rule le_imp_neg_le)
avigad@16908
   829
  apply (erule spec)
avigad@16908
   830
  apply (rule order_trans) 
avigad@16908
   831
  prefer 2
avigad@16908
   832
  apply (rule abs_ge_zero)
nipkow@29667
   833
  apply (simp add: algebra_simps)
wenzelm@22665
   834
  done
avigad@16908
   835
haftmann@35028
   836
lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::linordered_field) ==>
avigad@16908
   837
    g =o h +o O(k) ==> f <o h =o O(k)"
avigad@16908
   838
  apply (unfold lesso_def)
avigad@16908
   839
  apply (drule set_plus_imp_minus)
wenzelm@17199
   840
  apply (drule bigo_abs5) back
berghofe@26814
   841
  apply (simp add: fun_diff_def)
avigad@16908
   842
  apply (drule bigo_useful_add)
avigad@16908
   843
  apply assumption
wenzelm@17199
   844
  apply (erule bigo_lesseq2) back
avigad@16908
   845
  apply (rule allI)
nipkow@29667
   846
  apply (auto simp add: func_plus fun_diff_def algebra_simps
avigad@16908
   847
    split: split_max abs_split)
wenzelm@22665
   848
  done
avigad@16908
   849
avigad@16908
   850
lemma bigo_lesso5: "f <o g =o O(h) ==>
avigad@16908
   851
    EX C. ALL x. f x <= g x + C * abs(h x)"
avigad@16908
   852
  apply (simp only: lesso_def bigo_alt_def)
avigad@16908
   853
  apply clarsimp
avigad@16908
   854
  apply (rule_tac x = c in exI)
avigad@16908
   855
  apply (rule allI)
avigad@16908
   856
  apply (drule_tac x = x in spec)
avigad@16908
   857
  apply (subgoal_tac "abs(max (f x - g x) 0) = max (f x - g x) 0")
nipkow@29667
   858
  apply (clarsimp simp add: algebra_simps) 
avigad@16908
   859
  apply (rule abs_of_nonneg)
avigad@16908
   860
  apply (rule le_maxI2)
wenzelm@22665
   861
  done
avigad@16908
   862
avigad@16908
   863
lemma lesso_add: "f <o g =o O(h) ==>
avigad@16908
   864
      k <o l =o O(h) ==> (f + k) <o (g + l) =o O(h)"
avigad@16908
   865
  apply (unfold lesso_def)
avigad@16908
   866
  apply (rule bigo_lesseq3)
avigad@16908
   867
  apply (erule bigo_useful_add)
avigad@16908
   868
  apply assumption
avigad@16908
   869
  apply (force split: split_max)
avigad@16908
   870
  apply (auto split: split_max simp add: func_plus)
wenzelm@22665
   871
  done
avigad@16908
   872
haftmann@29786
   873
lemma bigo_LIMSEQ1: "f =o O(g) ==> g ----> 0 ==> f ----> (0::real)"
huffman@31337
   874
  apply (simp add: LIMSEQ_iff bigo_alt_def)
haftmann@29786
   875
  apply clarify
haftmann@29786
   876
  apply (drule_tac x = "r / c" in spec)
haftmann@29786
   877
  apply (drule mp)
haftmann@29786
   878
  apply (erule divide_pos_pos)
haftmann@29786
   879
  apply assumption
haftmann@29786
   880
  apply clarify
haftmann@29786
   881
  apply (rule_tac x = no in exI)
haftmann@29786
   882
  apply (rule allI)
haftmann@29786
   883
  apply (drule_tac x = n in spec)+
haftmann@29786
   884
  apply (rule impI)
haftmann@29786
   885
  apply (drule mp)
haftmann@29786
   886
  apply assumption
haftmann@29786
   887
  apply (rule order_le_less_trans)
haftmann@29786
   888
  apply assumption
haftmann@29786
   889
  apply (rule order_less_le_trans)
haftmann@29786
   890
  apply (subgoal_tac "c * abs(g n) < c * (r / c)")
haftmann@29786
   891
  apply assumption
haftmann@29786
   892
  apply (erule mult_strict_left_mono)
haftmann@29786
   893
  apply assumption
haftmann@29786
   894
  apply simp
haftmann@29786
   895
done
haftmann@29786
   896
haftmann@29786
   897
lemma bigo_LIMSEQ2: "f =o g +o O(h) ==> h ----> 0 ==> f ----> a 
haftmann@29786
   898
    ==> g ----> (a::real)"
haftmann@29786
   899
  apply (drule set_plus_imp_minus)
haftmann@29786
   900
  apply (drule bigo_LIMSEQ1)
haftmann@29786
   901
  apply assumption
haftmann@29786
   902
  apply (simp only: fun_diff_def)
haftmann@29786
   903
  apply (erule LIMSEQ_diff_approach_zero2)
haftmann@29786
   904
  apply assumption
haftmann@29786
   905
done
haftmann@29786
   906
avigad@16908
   907
end