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