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