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