15 by (simp add: Bernstein_def) |
15 by (simp add: Bernstein_def) |
16 |
16 |
17 lemma Bernstein_pos: "\<lbrakk>0 < x; x < 1; k \<le> n\<rbrakk> \<Longrightarrow> 0 < Bernstein n k x" |
17 lemma Bernstein_pos: "\<lbrakk>0 < x; x < 1; k \<le> n\<rbrakk> \<Longrightarrow> 0 < Bernstein n k x" |
18 by (simp add: Bernstein_def) |
18 by (simp add: Bernstein_def) |
19 |
19 |
20 lemma sum_Bernstein [simp]: "(\<Sum> k = 0..n. Bernstein n k x) = 1" |
20 lemma sum_Bernstein [simp]: "(\<Sum>k\<le>n. Bernstein n k x) = 1" |
21 using binomial_ring [of x "1-x" n] |
21 using binomial_ring [of x "1-x" n] |
22 by (simp add: Bernstein_def) |
22 by (simp add: Bernstein_def) |
23 |
23 |
24 lemma binomial_deriv1: |
24 lemma binomial_deriv1: |
25 "(\<Sum>k=0..n. (of_nat k * of_nat (n choose k)) * a^(k-1) * b^(n-k)) = real_of_nat n * (a+b) ^ (n-1)" |
25 "(\<Sum>k\<le>n. (of_nat k * of_nat (n choose k)) * a^(k-1) * b^(n-k)) = real_of_nat n * (a+b) ^ (n-1)" |
26 apply (rule DERIV_unique [where f = "\<lambda>a. (a+b)^n" and x=a]) |
26 apply (rule DERIV_unique [where f = "\<lambda>a. (a+b)^n" and x=a]) |
27 apply (subst binomial_ring) |
27 apply (subst binomial_ring) |
28 apply (rule derivative_eq_intros sum.cong | simp)+ |
28 apply (rule derivative_eq_intros sum.cong | simp add: atMost_atLeast0)+ |
29 done |
29 done |
30 |
30 |
31 lemma binomial_deriv2: |
31 lemma binomial_deriv2: |
32 "(\<Sum>k=0..n. (of_nat k * of_nat (k-1) * of_nat (n choose k)) * a^(k-2) * b^(n-k)) = |
32 "(\<Sum>k\<le>n. (of_nat k * of_nat (k-1) * of_nat (n choose k)) * a^(k-2) * b^(n-k)) = |
33 of_nat n * of_nat (n-1) * (a+b::real) ^ (n-2)" |
33 of_nat n * of_nat (n-1) * (a+b::real) ^ (n-2)" |
34 apply (rule DERIV_unique [where f = "\<lambda>a. of_nat n * (a+b::real) ^ (n-1)" and x=a]) |
34 apply (rule DERIV_unique [where f = "\<lambda>a. of_nat n * (a+b::real) ^ (n-1)" and x=a]) |
35 apply (subst binomial_deriv1 [symmetric]) |
35 apply (subst binomial_deriv1 [symmetric]) |
36 apply (rule derivative_eq_intros sum.cong | simp add: Num.numeral_2_eq_2)+ |
36 apply (rule derivative_eq_intros sum.cong | simp add: Num.numeral_2_eq_2)+ |
37 done |
37 done |
38 |
38 |
39 lemma sum_k_Bernstein [simp]: "(\<Sum>k = 0..n. real k * Bernstein n k x) = of_nat n * x" |
39 lemma sum_k_Bernstein [simp]: "(\<Sum>k\<le>n. real k * Bernstein n k x) = of_nat n * x" |
40 apply (subst binomial_deriv1 [of n x "1-x", simplified, symmetric]) |
40 apply (subst binomial_deriv1 [of n x "1-x", simplified, symmetric]) |
41 apply (simp add: sum_distrib_right) |
41 apply (simp add: sum_distrib_right) |
42 apply (auto simp: Bernstein_def algebra_simps realpow_num_eq_if intro!: sum.cong) |
42 apply (auto simp: Bernstein_def algebra_simps realpow_num_eq_if intro!: sum.cong) |
43 done |
43 done |
44 |
44 |
45 lemma sum_kk_Bernstein [simp]: "(\<Sum> k = 0..n. real k * (real k - 1) * Bernstein n k x) = real n * (real n - 1) * x\<^sup>2" |
45 lemma sum_kk_Bernstein [simp]: "(\<Sum>k\<le>n. real k * (real k - 1) * Bernstein n k x) = real n * (real n - 1) * x\<^sup>2" |
46 proof - |
46 proof - |
47 have "(\<Sum> k = 0..n. real k * (real k - 1) * Bernstein n k x) = real_of_nat n * real_of_nat (n - Suc 0) * x\<^sup>2" |
47 have "(\<Sum>k\<le>n. real k * (real k - 1) * Bernstein n k x) = |
48 apply (subst binomial_deriv2 [of n x "1-x", simplified, symmetric]) |
48 (\<Sum>k\<le>n. real k * real (k - Suc 0) * real (n choose k) * x ^ (k - 2) * (1 - x) ^ (n - k) * x\<^sup>2)" |
49 apply (simp add: sum_distrib_right) |
49 proof (rule sum.cong [OF refl], simp) |
50 apply (rule sum.cong [OF refl]) |
50 fix k |
51 apply (simp add: Bernstein_def power2_eq_square algebra_simps) |
51 assume "k \<le> n" |
52 apply (rename_tac k) |
52 then consider "k = 0" | "k = 1" | k' where "k = Suc (Suc k')" |
53 apply (subgoal_tac "k = 0 \<or> k = 1 \<or> (\<exists>k'. k = Suc (Suc k'))") |
53 by (metis One_nat_def not0_implies_Suc) |
54 apply (force simp add: field_simps of_nat_Suc power2_eq_square) |
54 then show "k = 0 \<or> |
55 by presburger |
55 (real k - 1) * Bernstein n k x = |
|
56 real (k - Suc 0) * |
|
57 (real (n choose k) * (x ^ (k - 2) * ((1 - x) ^ (n - k) * x\<^sup>2)))" |
|
58 by cases (auto simp add: Bernstein_def power2_eq_square algebra_simps) |
|
59 qed |
|
60 also have "... = real_of_nat n * real_of_nat (n - Suc 0) * x\<^sup>2" |
|
61 by (subst binomial_deriv2 [of n x "1-x", simplified, symmetric]) (simp add: sum_distrib_right) |
56 also have "... = n * (n - 1) * x\<^sup>2" |
62 also have "... = n * (n - 1) * x\<^sup>2" |
57 by auto |
63 by auto |
58 finally show ?thesis |
64 finally show ?thesis |
59 by auto |
65 by auto |
60 qed |
66 qed |
63 |
69 |
64 lemma Bernstein_Weierstrass: |
70 lemma Bernstein_Weierstrass: |
65 fixes f :: "real \<Rightarrow> real" |
71 fixes f :: "real \<Rightarrow> real" |
66 assumes contf: "continuous_on {0..1} f" and e: "0 < e" |
72 assumes contf: "continuous_on {0..1} f" and e: "0 < e" |
67 shows "\<exists>N. \<forall>n x. N \<le> n \<and> x \<in> {0..1} |
73 shows "\<exists>N. \<forall>n x. N \<le> n \<and> x \<in> {0..1} |
68 \<longrightarrow> \<bar>f x - (\<Sum>k = 0..n. f(k/n) * Bernstein n k x)\<bar> < e" |
74 \<longrightarrow> \<bar>f x - (\<Sum>k\<le>n. f(k/n) * Bernstein n k x)\<bar> < e" |
69 proof - |
75 proof - |
70 have "bounded (f ` {0..1})" |
76 have "bounded (f ` {0..1})" |
71 using compact_continuous_image compact_imp_bounded contf by blast |
77 using compact_continuous_image compact_imp_bounded contf by blast |
72 then obtain M where M: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<bar>f x\<bar> \<le> M" |
78 then obtain M where M: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<bar>f x\<bar> \<le> M" |
73 by (force simp add: bounded_iff) |
79 by (force simp add: bounded_iff) |
84 using e \<open>0<d\<close> by simp |
90 using e \<open>0<d\<close> by simp |
85 also have "... \<le> M * 4" |
91 also have "... \<le> M * 4" |
86 using \<open>0\<le>M\<close> by simp |
92 using \<open>0\<le>M\<close> by simp |
87 finally have [simp]: "real_of_int (nat \<lceil>4 * M / (e * d\<^sup>2)\<rceil>) = real_of_int \<lceil>4 * M / (e * d\<^sup>2)\<rceil>" |
93 finally have [simp]: "real_of_int (nat \<lceil>4 * M / (e * d\<^sup>2)\<rceil>) = real_of_int \<lceil>4 * M / (e * d\<^sup>2)\<rceil>" |
88 using \<open>0\<le>M\<close> e \<open>0<d\<close> |
94 using \<open>0\<le>M\<close> e \<open>0<d\<close> |
89 by (simp add: of_nat_Suc field_simps) |
95 by (simp add: field_simps) |
90 have "4*M/(e*d\<^sup>2) + 1 \<le> real (Suc (nat\<lceil>4*M/(e*d\<^sup>2)\<rceil>))" |
96 have "4*M/(e*d\<^sup>2) + 1 \<le> real (Suc (nat\<lceil>4*M/(e*d\<^sup>2)\<rceil>))" |
91 by (simp add: of_nat_Suc real_nat_ceiling_ge) |
97 by (simp add: real_nat_ceiling_ge) |
92 also have "... \<le> real n" |
98 also have "... \<le> real n" |
93 using n by (simp add: of_nat_Suc field_simps) |
99 using n by (simp add: field_simps) |
94 finally have nbig: "4*M/(e*d\<^sup>2) + 1 \<le> real n" . |
100 finally have nbig: "4*M/(e*d\<^sup>2) + 1 \<le> real n" . |
95 have sum_bern: "(\<Sum>k = 0..n. (x - k/n)\<^sup>2 * Bernstein n k x) = x * (1 - x) / n" |
101 have sum_bern: "(\<Sum>k\<le>n. (x - k/n)\<^sup>2 * Bernstein n k x) = x * (1 - x) / n" |
96 proof - |
102 proof - |
97 have *: "\<And>a b x::real. (a - b)\<^sup>2 * x = a * (a - 1) * x + (1 - 2 * b) * a * x + b * b * x" |
103 have *: "\<And>a b x::real. (a - b)\<^sup>2 * x = a * (a - 1) * x + (1 - 2 * b) * a * x + b * b * x" |
98 by (simp add: algebra_simps power2_eq_square) |
104 by (simp add: algebra_simps power2_eq_square) |
99 have "(\<Sum> k = 0..n. (k - n * x)\<^sup>2 * Bernstein n k x) = n * x * (1 - x)" |
105 have "(\<Sum>k\<le>n. (k - n * x)\<^sup>2 * Bernstein n k x) = n * x * (1 - x)" |
100 apply (simp add: * sum.distrib) |
106 apply (simp add: * sum.distrib) |
101 apply (simp add: sum_distrib_left [symmetric] mult.assoc) |
107 apply (simp add: sum_distrib_left [symmetric] mult.assoc) |
102 apply (simp add: algebra_simps power2_eq_square) |
108 apply (simp add: algebra_simps power2_eq_square) |
103 done |
109 done |
104 then have "(\<Sum> k = 0..n. (k - n * x)\<^sup>2 * Bernstein n k x)/n^2 = x * (1 - x) / n" |
110 then have "(\<Sum>k\<le>n. (k - n * x)\<^sup>2 * Bernstein n k x)/n^2 = x * (1 - x) / n" |
105 by (simp add: power2_eq_square) |
111 by (simp add: power2_eq_square) |
106 then show ?thesis |
112 then show ?thesis |
107 using n by (simp add: sum_divide_distrib divide_simps mult.commute power2_commute) |
113 using n by (simp add: sum_divide_distrib divide_simps mult.commute power2_commute) |
108 qed |
114 qed |
109 { fix k |
115 { fix k |
135 also have "... \<le> e/2 + 2 * M / d\<^sup>2 * (x - k/n)\<^sup>2" |
141 also have "... \<le> e/2 + 2 * M / d\<^sup>2 * (x - k/n)\<^sup>2" |
136 using e by simp |
142 using e by simp |
137 finally show ?thesis . |
143 finally show ?thesis . |
138 qed |
144 qed |
139 } note * = this |
145 } note * = this |
140 have "\<bar>f x - (\<Sum> k = 0..n. f(k / n) * Bernstein n k x)\<bar> \<le> \<bar>\<Sum> k = 0..n. (f x - f(k / n)) * Bernstein n k x\<bar>" |
146 have "\<bar>f x - (\<Sum>k\<le>n. f(k / n) * Bernstein n k x)\<bar> \<le> \<bar>\<Sum>k\<le>n. (f x - f(k / n)) * Bernstein n k x\<bar>" |
141 by (simp add: sum_subtractf sum_distrib_left [symmetric] algebra_simps) |
147 by (simp add: sum_subtractf sum_distrib_left [symmetric] algebra_simps) |
142 also have "... \<le> (\<Sum> k = 0..n. (e/2 + (2 * M / d\<^sup>2) * (x - k / n)\<^sup>2) * Bernstein n k x)" |
148 also have "... \<le> (\<Sum>k\<le>n. (e/2 + (2 * M / d\<^sup>2) * (x - k / n)\<^sup>2) * Bernstein n k x)" |
143 apply (rule order_trans [OF sum_abs sum_mono]) |
149 apply (rule order_trans [OF sum_abs sum_mono]) |
144 using * |
150 using * |
145 apply (simp add: abs_mult Bernstein_nonneg x mult_right_mono) |
151 apply (simp add: abs_mult Bernstein_nonneg x mult_right_mono) |
146 done |
152 done |
147 also have "... \<le> e/2 + (2 * M) / (d\<^sup>2 * n)" |
153 also have "... \<le> e/2 + (2 * M) / (d\<^sup>2 * n)" |
891 using g real_polynomial_function_compose [OF f] |
897 using g real_polynomial_function_compose [OF f] |
892 by (auto simp: polynomial_function_def o_def) |
898 by (auto simp: polynomial_function_def o_def) |
893 |
899 |
894 lemma sum_max_0: |
900 lemma sum_max_0: |
895 fixes x::real (*in fact "'a::comm_ring_1"*) |
901 fixes x::real (*in fact "'a::comm_ring_1"*) |
896 shows "(\<Sum>i = 0..max m n. x^i * (if i \<le> m then a i else 0)) = (\<Sum>i = 0..m. x^i * a i)" |
902 shows "(\<Sum>i\<le>max m n. x^i * (if i \<le> m then a i else 0)) = (\<Sum>i\<le>m. x^i * a i)" |
897 proof - |
903 proof - |
898 have "(\<Sum>i = 0..max m n. x^i * (if i \<le> m then a i else 0)) = (\<Sum>i = 0..max m n. (if i \<le> m then x^i * a i else 0))" |
904 have "(\<Sum>i\<le>max m n. x^i * (if i \<le> m then a i else 0)) = (\<Sum>i\<le>max m n. (if i \<le> m then x^i * a i else 0))" |
899 by (auto simp: algebra_simps intro: sum.cong) |
905 by (auto simp: algebra_simps intro: sum.cong) |
900 also have "... = (\<Sum>i = 0..m. (if i \<le> m then x^i * a i else 0))" |
906 also have "... = (\<Sum>i\<le>m. (if i \<le> m then x^i * a i else 0))" |
901 by (rule sum.mono_neutral_right) auto |
907 by (rule sum.mono_neutral_right) auto |
902 also have "... = (\<Sum>i = 0..m. x^i * a i)" |
908 also have "... = (\<Sum>i\<le>m. x^i * a i)" |
903 by (auto simp: algebra_simps intro: sum.cong) |
909 by (auto simp: algebra_simps intro: sum.cong) |
904 finally show ?thesis . |
910 finally show ?thesis . |
905 qed |
911 qed |
906 |
912 |
907 lemma real_polynomial_function_imp_sum: |
913 lemma real_polynomial_function_imp_sum: |
908 assumes "real_polynomial_function f" |
914 assumes "real_polynomial_function f" |
909 shows "\<exists>a n::nat. f = (\<lambda>x. \<Sum>i=0..n. a i * x ^ i)" |
915 shows "\<exists>a n::nat. f = (\<lambda>x. \<Sum>i\<le>n. a i * x ^ i)" |
910 using assms |
916 using assms |
911 proof (induct f) |
917 proof (induct f) |
912 case (linear f) |
918 case (linear f) |
913 then show ?case |
919 then show ?case |
914 apply (clarsimp simp add: real_bounded_linear) |
920 apply (clarsimp simp add: real_bounded_linear) |
923 apply (rule_tac x=0 in exI) |
929 apply (rule_tac x=0 in exI) |
924 apply (auto simp: mult_ac of_nat_Suc) |
930 apply (auto simp: mult_ac of_nat_Suc) |
925 done |
931 done |
926 case (add f1 f2) |
932 case (add f1 f2) |
927 then obtain a1 n1 a2 n2 where |
933 then obtain a1 n1 a2 n2 where |
928 "f1 = (\<lambda>x. \<Sum>i = 0..n1. a1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i = 0..n2. a2 i * x ^ i)" |
934 "f1 = (\<lambda>x. \<Sum>i\<le>n1. a1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i\<le>n2. a2 i * x ^ i)" |
929 by auto |
935 by auto |
930 then show ?case |
936 then show ?case |
931 apply (rule_tac x="\<lambda>i. (if i \<le> n1 then a1 i else 0) + (if i \<le> n2 then a2 i else 0)" in exI) |
937 apply (rule_tac x="\<lambda>i. (if i \<le> n1 then a1 i else 0) + (if i \<le> n2 then a2 i else 0)" in exI) |
932 apply (rule_tac x="max n1 n2" in exI) |
938 apply (rule_tac x="max n1 n2" in exI) |
933 using sum_max_0 [where m=n1 and n=n2] sum_max_0 [where m=n2 and n=n1] |
939 using sum_max_0 [where m=n1 and n=n2] sum_max_0 [where m=n2 and n=n1] |
934 apply (simp add: sum.distrib algebra_simps max.commute) |
940 apply (simp add: sum.distrib algebra_simps max.commute) |
935 done |
941 done |
936 case (mult f1 f2) |
942 case (mult f1 f2) |
937 then obtain a1 n1 a2 n2 where |
943 then obtain a1 n1 a2 n2 where |
938 "f1 = (\<lambda>x. \<Sum>i = 0..n1. a1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i = 0..n2. a2 i * x ^ i)" |
944 "f1 = (\<lambda>x. \<Sum>i\<le>n1. a1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i\<le>n2. a2 i * x ^ i)" |
939 by auto |
945 by auto |
940 then obtain b1 b2 where |
946 then obtain b1 b2 where |
941 "f1 = (\<lambda>x. \<Sum>i = 0..n1. b1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i = 0..n2. b2 i * x ^ i)" |
947 "f1 = (\<lambda>x. \<Sum>i\<le>n1. b1 i * x ^ i)" "f2 = (\<lambda>x. \<Sum>i\<le>n2. b2 i * x ^ i)" |
942 "b1 = (\<lambda>i. if i\<le>n1 then a1 i else 0)" "b2 = (\<lambda>i. if i\<le>n2 then a2 i else 0)" |
948 "b1 = (\<lambda>i. if i\<le>n1 then a1 i else 0)" "b2 = (\<lambda>i. if i\<le>n2 then a2 i else 0)" |
943 by auto |
949 by auto |
944 then show ?case |
950 then show ?case |
945 apply (rule_tac x="\<lambda>i. \<Sum>k\<le>i. b1 k * b2 (i - k)" in exI) |
951 apply (rule_tac x="\<lambda>i. \<Sum>k\<le>i. b1 k * b2 (i - k)" in exI) |
946 apply (rule_tac x="n1+n2" in exI) |
952 apply (rule_tac x="n1+n2" in exI) |