src/HOL/Metis_Examples/Big_O.thy
changeset 47445 69e96e5500df
parent 47108 2a1953f0d20d
child 49918 cf441f4a358b
equal deleted inserted replaced
47444:d21c95af2df7 47445:69e96e5500df
   144 
   144 
   145 lemma bigo_zero2: "O(\<lambda>x. 0) = {\<lambda>x. 0}"
   145 lemma bigo_zero2: "O(\<lambda>x. 0) = {\<lambda>x. 0}"
   146 by (auto simp add: bigo_def)
   146 by (auto simp add: bigo_def)
   147 
   147 
   148 lemma bigo_plus_self_subset [intro]:
   148 lemma bigo_plus_self_subset [intro]:
   149   "O(f) \<oplus> O(f) <= O(f)"
   149   "O(f) + O(f) <= O(f)"
   150 apply (auto simp add: bigo_alt_def set_plus_def)
   150 apply (auto simp add: bigo_alt_def set_plus_def)
   151 apply (rule_tac x = "c + ca" in exI)
   151 apply (rule_tac x = "c + ca" in exI)
   152 apply auto
   152 apply auto
   153 apply (simp add: ring_distribs func_plus)
   153 apply (simp add: ring_distribs func_plus)
   154 by (metis order_trans abs_triangle_ineq add_mono)
   154 by (metis order_trans abs_triangle_ineq add_mono)
   155 
   155 
   156 lemma bigo_plus_idemp [simp]: "O(f) \<oplus> O(f) = O(f)"
   156 lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)"
   157 by (metis bigo_plus_self_subset bigo_zero set_eq_subset set_zero_plus2)
   157 by (metis bigo_plus_self_subset bigo_zero set_eq_subset set_zero_plus2)
   158 
   158 
   159 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) \<oplus> O(g)"
   159 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
   160 apply (rule subsetI)
   160 apply (rule subsetI)
   161 apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
   161 apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
   162 apply (subst bigo_pos_const [symmetric])+
   162 apply (subst bigo_pos_const [symmetric])+
   163 apply (rule_tac x = "\<lambda>n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
   163 apply (rule_tac x = "\<lambda>n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
   164 apply (rule conjI)
   164 apply (rule conjI)
   185   apply (erule order_trans)
   185   apply (erule order_trans)
   186   apply (simp add: ring_distribs)
   186   apply (simp add: ring_distribs)
   187  apply (metis abs_triangle_ineq mult_le_cancel_left_pos)
   187  apply (metis abs_triangle_ineq mult_le_cancel_left_pos)
   188 by (metis abs_ge_zero abs_of_pos zero_le_mult_iff)
   188 by (metis abs_ge_zero abs_of_pos zero_le_mult_iff)
   189 
   189 
   190 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A \<oplus> B <= O(f)"
   190 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)"
   191 by (metis bigo_plus_idemp set_plus_mono2)
   191 by (metis bigo_plus_idemp set_plus_mono2)
   192 
   192 
   193 lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) \<oplus> O(g)"
   193 lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) + O(g)"
   194 apply (rule equalityI)
   194 apply (rule equalityI)
   195 apply (rule bigo_plus_subset)
   195 apply (rule bigo_plus_subset)
   196 apply (simp add: bigo_alt_def set_plus_def func_plus)
   196 apply (simp add: bigo_alt_def set_plus_def func_plus)
   197 apply clarify
   197 apply clarify
   198 (* sledgehammer *)
   198 (* sledgehammer *)
   282 qed
   282 qed
   283 
   283 
   284 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
   284 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
   285 by (unfold bigo_def, auto)
   285 by (unfold bigo_def, auto)
   286 
   286 
   287 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) \<oplus> O(h)"
   287 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) + O(h)"
   288 proof -
   288 proof -
   289   assume "f : g +o O(h)"
   289   assume "f : g +o O(h)"
   290   also have "... <= O(g) \<oplus> O(h)"
   290   also have "... <= O(g) + O(h)"
   291     by (auto del: subsetI)
   291     by (auto del: subsetI)
   292   also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
   292   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   293     by (metis bigo_abs3)
   293     by (metis bigo_abs3)
   294   also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
   294   also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
   295     by (rule bigo_plus_eq [symmetric], auto)
   295     by (rule bigo_plus_eq [symmetric], auto)
   296   finally have "f : ...".
   296   finally have "f : ...".
   297   then have "O(f) <= ..."
   297   then have "O(f) <= ..."
   298     by (elim bigo_elt_subset)
   298     by (elim bigo_elt_subset)
   299   also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
   299   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   300     by (rule bigo_plus_eq, auto)
   300     by (rule bigo_plus_eq, auto)
   301   finally show ?thesis
   301   finally show ?thesis
   302     by (simp add: bigo_abs3 [symmetric])
   302     by (simp add: bigo_abs3 [symmetric])
   303 qed
   303 qed
   304 
   304 
   305 lemma bigo_mult [intro]: "O(f) \<otimes> O(g) <= O(f * g)"
   305 lemma bigo_mult [intro]: "O(f) * O(g) <= O(f * g)"
   306 apply (rule subsetI)
   306 apply (rule subsetI)
   307 apply (subst bigo_def)
   307 apply (subst bigo_def)
   308 apply (auto simp del: abs_mult mult_ac
   308 apply (auto simp del: abs_mult mult_ac
   309             simp add: bigo_alt_def set_times_def func_times)
   309             simp add: bigo_alt_def set_times_def func_times)
   310 (* sledgehammer *)
   310 (* sledgehammer *)
   356 
   356 
   357 (*proof requires relaxing relevance: 2007-01-25*)
   357 (*proof requires relaxing relevance: 2007-01-25*)
   358 declare bigo_mult6 [simp]
   358 declare bigo_mult6 [simp]
   359 
   359 
   360 lemma bigo_mult7:
   360 lemma bigo_mult7:
   361 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) \<otimes> O(g)"
   361 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) * O(g)"
   362 by (metis bigo_refl bigo_mult6 set_times_mono3)
   362 by (metis bigo_refl bigo_mult6 set_times_mono3)
   363 
   363 
   364 declare bigo_mult6 [simp del]
   364 declare bigo_mult6 [simp del]
   365 declare bigo_mult7 [intro!]
   365 declare bigo_mult7 [intro!]
   366 
   366 
   367 lemma bigo_mult8:
   367 lemma bigo_mult8:
   368 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) \<otimes> O(g)"
   368 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) * O(g)"
   369 by (metis bigo_mult bigo_mult7 order_antisym_conv)
   369 by (metis bigo_mult bigo_mult7 order_antisym_conv)
   370 
   370 
   371 lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
   371 lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
   372 by (auto simp add: bigo_def fun_Compl_def)
   372 by (auto simp add: bigo_def fun_Compl_def)
   373 
   373 
   573 done
   573 done
   574 
   574 
   575 subsection {* Misc useful stuff *}
   575 subsection {* Misc useful stuff *}
   576 
   576 
   577 lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
   577 lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
   578   A \<oplus> B <= O(f)"
   578   A + B <= O(f)"
   579   apply (subst bigo_plus_idemp [symmetric])
   579   apply (subst bigo_plus_idemp [symmetric])
   580   apply (rule set_plus_mono2)
   580   apply (rule set_plus_mono2)
   581   apply assumption+
   581   apply assumption+
   582 done
   582 done
   583 
   583