Datasets:
Tasks:
Text Generation
Modalities:
Text
Sub-tasks:
language-modeling
Languages:
English
Size:
100K - 1M
License:
File size: 10,535 Bytes
4365a98 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
(*
File: Master_Theorem_Examples.thy
Author: Manuel Eberl <manuel@pruvisto.org>
Examples for the application of the Master theorem and related proof methods.
*)
section \<open>Examples\<close>
theory Master_Theorem_Examples
imports
Complex_Main
Akra_Bazzi_Method
Akra_Bazzi_Approximation
begin
subsection \<open>Merge sort\<close>
(* A merge sort cost function that is parametrised with the recombination costs *)
function merge_sort_cost :: "(nat \<Rightarrow> real) \<Rightarrow> nat \<Rightarrow> real" where
"merge_sort_cost t 0 = 0"
| "merge_sort_cost t 1 = 1"
| "n \<ge> 2 \<Longrightarrow> merge_sort_cost t n =
merge_sort_cost t (nat \<lfloor>real n / 2\<rfloor>) + merge_sort_cost t (nat \<lceil>real n / 2\<rceil>) + t n"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma merge_sort_nonneg[simp]: "(\<And>n. t n \<ge> 0) \<Longrightarrow> merge_sort_cost t x \<ge> 0"
by (induction t x rule: merge_sort_cost.induct) (simp_all del: One_nat_def)
lemma "t \<in> \<Theta>(\<lambda>n. real n) \<Longrightarrow> (\<And>n. t n \<ge> 0) \<Longrightarrow> merge_sort_cost t \<in> \<Theta>(\<lambda>n. real n * ln (real n))"
by (master_theorem 2.3) simp_all
subsection \<open>Karatsuba multiplication\<close>
function karatsuba_cost :: "nat \<Rightarrow> real" where
"karatsuba_cost 0 = 0"
| "karatsuba_cost 1 = 1"
| "n \<ge> 2 \<Longrightarrow> karatsuba_cost n =
3 * karatsuba_cost (nat \<lceil>real n / 2\<rceil>) + real n"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma karatsuba_cost_nonneg[simp]: "karatsuba_cost n \<ge> 0"
by (induction n rule: karatsuba_cost.induct) (simp_all del: One_nat_def)
lemma "karatsuba_cost \<in> O(\<lambda>n. real n powr log 2 3)"
by (master_theorem 1 p': 1) (simp_all add: powr_divide)
lemma karatsuba_cost_pos: "n \<ge> 1 \<Longrightarrow> karatsuba_cost n > 0"
by (induction n rule: karatsuba_cost.induct) (auto intro!: add_nonneg_pos simp del: One_nat_def)
lemma "karatsuba_cost \<in> \<Theta>(\<lambda>n. real n powr log 2 3)"
using karatsuba_cost_pos
by (master_theorem 1 p': 1) (auto simp add: powr_divide eventually_at_top_linorder)
subsection \<open>Strassen matrix multiplication\<close>
function strassen_cost :: "nat \<Rightarrow> real" where
"strassen_cost 0 = 0"
| "strassen_cost 1 = 1"
| "n \<ge> 2 \<Longrightarrow> strassen_cost n = 7 * strassen_cost (nat \<lceil>real n / 2\<rceil>) + real (n^2)"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma strassen_cost_nonneg[simp]: "strassen_cost n \<ge> 0"
by (induction n rule: strassen_cost.induct) (simp_all del: One_nat_def)
lemma "strassen_cost \<in> O(\<lambda>n. real n powr log 2 7)"
by (master_theorem 1 p': 2) (auto simp: powr_divide eventually_at_top_linorder)
lemma strassen_cost_pos: "n \<ge> 1 \<Longrightarrow> strassen_cost n > 0"
by (cases n rule: strassen_cost.cases) (simp_all add: add_nonneg_pos del: One_nat_def)
lemma "strassen_cost \<in> \<Theta>(\<lambda>n. real n powr log 2 7)"
using strassen_cost_pos
by (master_theorem 1 p': 2) (auto simp: powr_divide eventually_at_top_linorder)
subsection \<open>Deterministic select\<close>
(* This is not possible with the standard Master theorem from literature *)
function select_cost :: "nat \<Rightarrow> real" where
"n \<le> 20 \<Longrightarrow> select_cost n = 0"
| "n > 20 \<Longrightarrow> select_cost n =
select_cost (nat \<lfloor>real n / 5\<rfloor>) + select_cost (nat \<lfloor>7 * real n / 10\<rfloor> + 6) + 12 * real n / 5"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma "select_cost \<in> \<Theta>(\<lambda>n. real n)"
by (master_theorem 3) auto
subsection \<open>Decreasing function\<close>
function dec_cost :: "nat \<Rightarrow> real" where
"n \<le> 2 \<Longrightarrow> dec_cost n = 1"
| "n > 2 \<Longrightarrow> dec_cost n = 0.5*dec_cost (nat \<lfloor>real n / 2\<rfloor>) + 1 / real n"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma "dec_cost \<in> \<Theta>(\<lambda>x::nat. ln x / x)"
by (master_theorem 2.3) simp_all
subsection \<open>Example taken from Drmota and Szpakowski\<close>
function drmota1 :: "nat \<Rightarrow> real" where
"n < 20 \<Longrightarrow> drmota1 n = 1"
| "n \<ge> 20 \<Longrightarrow> drmota1 n = 2 * drmota1 (nat \<lfloor>real n/2\<rfloor>) + 8/9 * drmota1 (nat \<lfloor>3*real n/4\<rfloor>) + real n^2 / ln (real n)"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma "drmota1 \<in> \<Theta>(\<lambda>n::real. n^2 * ln (ln n))"
by (master_theorem 2.2) (simp_all add: power_divide)
function drmota2 :: "nat \<Rightarrow> real" where
"n < 20 \<Longrightarrow> drmota2 n = 1"
| "n \<ge> 20 \<Longrightarrow> drmota2 n = 1/3 * drmota2 (nat \<lfloor>real n/3 + 1/2\<rfloor>) + 2/3 * drmota2 (nat \<lfloor>2*real n/3 - 1/2\<rfloor>) + 1"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma "drmota2 \<in> \<Theta>(\<lambda>x. ln (real x))"
by master_theorem simp_all
(* Average phrase length of Boncelet arithmetic coding. See Drmota and Szpankowski. *)
lemma boncelet_phrase_length:
fixes p \<delta> :: real assumes p: "p > 0" "p < 1" and \<delta>: "\<delta> > 0" "\<delta> < 1" "2*p + \<delta> < 2"
fixes d :: "nat \<Rightarrow> real"
defines "q \<equiv> 1 - p"
assumes d_nonneg: "\<And>n. d n \<ge> 0"
assumes d_rec: "\<And>n. n \<ge> 2 \<Longrightarrow> d n = 1 + p * d (nat \<lfloor>p * real n + \<delta>\<rfloor>) + q * d (nat \<lfloor>q * real n - \<delta>\<rfloor>)"
shows "d \<in> \<Theta>(\<lambda>x. ln x)"
using assms by (master_theorem recursion: d_rec, simp_all)
subsection \<open>Transcendental exponents\<close>
(* Certain number-theoretic conjectures would imply that if all the parameters are rational,
the Akra-Bazzi parameter is either rational or transcendental. That makes this case
probably transcendental *)
function foo_cost :: "nat \<Rightarrow> real" where
"n < 200 \<Longrightarrow> foo_cost n = 0"
| "n \<ge> 200 \<Longrightarrow> foo_cost n =
foo_cost (nat \<lfloor>real n / 3\<rfloor>) + foo_cost (nat \<lfloor>3 * real n / 4\<rfloor> + 42) + real n"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma foo_cost_nonneg [simp]: "foo_cost n \<ge> 0"
by (induction n rule: foo_cost.induct) simp_all
lemma "foo_cost \<in> \<Theta>(\<lambda>n. real n powr akra_bazzi_exponent [1,1] [1/3,3/4])"
proof (master_theorem 1 p': 1)
have "\<forall>n\<ge>200. foo_cost n > 0" by (simp add: add_nonneg_pos)
thus "eventually (\<lambda>n. foo_cost n > 0) at_top" unfolding eventually_at_top_linorder by blast
qed simp_all
lemma "akra_bazzi_exponent [1,1] [1/3,3/4] \<in> {1.1519623..1.1519624}"
by (akra_bazzi_approximate 29)
subsection \<open>Functions in locale contexts\<close>
locale det_select =
fixes b :: real
assumes b: "b > 0" "b < 7/10"
begin
function select_cost' :: "nat \<Rightarrow> real" where
"n \<le> 20 \<Longrightarrow> select_cost' n = 0"
| "n > 20 \<Longrightarrow> select_cost' n =
select_cost' (nat \<lfloor>real n / 5\<rfloor>) + select_cost' (nat \<lfloor>b * real n\<rfloor> + 6) + 6 * real n + 5"
by force simp_all
termination using b by akra_bazzi_termination simp_all
lemma "a \<ge> 0 \<Longrightarrow> select_cost' \<in> \<Theta>(\<lambda>n. real n)"
using b by (master_theorem 3, force+)
end
subsection \<open>Non-curried functions\<close>
(* Note: either a or b could be seen as recursion variables. *)
function baz_cost :: "nat \<times> nat \<Rightarrow> real" where
"n \<le> 2 \<Longrightarrow> baz_cost (a, n) = 0"
| "n > 2 \<Longrightarrow> baz_cost (a, n) = 3 * baz_cost (a, nat \<lfloor>real n / 2\<rfloor>) + real a"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma baz_cost_nonneg [simp]: "a \<ge> 0 \<Longrightarrow> baz_cost (a, n) \<ge> 0"
by (induction a n rule: baz_cost.induct[split_format (complete)]) simp_all
lemma
assumes "a > 0"
shows "(\<lambda>x. baz_cost (a, x)) \<in> \<Theta>(\<lambda>x. x powr log 2 3)"
proof (master_theorem 1 p': 0)
from assms have "\<forall>x\<ge>3. baz_cost (a, x) > 0" by (auto intro: add_nonneg_pos)
thus "eventually (\<lambda>x. baz_cost (a, x) > 0) at_top" by (force simp: eventually_at_top_linorder)
qed (insert assms, simp_all add: powr_divide)
(* Non-"Akra-Bazzi" variables may even be modified without impacting the termination proof.
However, the Akra-Bazzi theorem and the Master theorem itself do not apply anymore,
because bar_cost cannot be seen as a recursive function with one parameter *)
function bar_cost :: "nat \<times> nat \<Rightarrow> real" where
"n \<le> 2 \<Longrightarrow> bar_cost (a, n) = 0"
| "n > 2 \<Longrightarrow> bar_cost (a, n) = 3 * bar_cost (2 * a, nat \<lfloor>real n / 2\<rfloor>) + real a"
by force simp_all
termination by akra_bazzi_termination simp_all
subsection \<open>Ham-sandwich trees\<close>
(* f(n) = f(n/4) + f(n/2) + 1 *)
function ham_sandwich_cost :: "nat \<Rightarrow> real" where
"n < 4 \<Longrightarrow> ham_sandwich_cost n = 1"
| "n \<ge> 4 \<Longrightarrow> ham_sandwich_cost n =
ham_sandwich_cost (nat \<lfloor>n/4\<rfloor>) + ham_sandwich_cost (nat \<lfloor>n/2\<rfloor>) + 1"
by force simp_all
termination by akra_bazzi_termination simp_all
lemma ham_sandwich_cost_pos [simp]: "ham_sandwich_cost n > 0"
by (induction n rule: ham_sandwich_cost.induct) simp_all
text \<open>The golden ratio\<close>
definition "\<phi> = ((1 + sqrt 5) / 2 :: real)"
lemma \<phi>_pos [simp]: "\<phi> > 0" and \<phi>_nonneg [simp]: "\<phi> \<ge> 0" and \<phi>_nonzero [simp]: "\<phi> \<noteq> 0"
proof-
show "\<phi> > 0" unfolding \<phi>_def by (simp add: add_pos_nonneg)
thus "\<phi> \<ge> 0" "\<phi> \<noteq> 0" by simp_all
qed
lemma "ham_sandwich_cost \<in> \<Theta>(\<lambda>n. n powr (log 2 \<phi>))"
proof (master_theorem 1 p': 0)
have "(1 / 4) powr log 2 \<phi> + (1 / 2) powr log 2 \<phi> =
inverse (2 powr log 2 \<phi>)^2 + inverse (2 powr log 2 \<phi>)"
by (simp add: powr_divide field_simps powr_powr power2_eq_square powr_mult[symmetric]
del: powr_log_cancel)
also have "... = inverse (\<phi>^2) + inverse \<phi>" by (simp add: power2_eq_square)
also have "\<phi> + 1 = \<phi>*\<phi>" by (simp add: \<phi>_def field_simps)
hence "inverse (\<phi>^2) + inverse \<phi> = 1" by (simp add: field_simps power2_eq_square)
finally show "(1 / 4) powr log 2 \<phi> + (1 / 2) powr log 2 \<phi> = 1" by simp
qed simp_all
end
|