
haftmann@28952: (*  Author      : Jacques D. Fleuriot
paulson@12224:     Copyright   : 2001 University of Edinburgh
paulson@15079:     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
bulwahn@41368:     Conversion of Mac Laurin to Isar by Lukas Bulwahn and Bernhard Häupler, 2005
paulson@12224: *)
paulson@12224: 
paulson@15944: header{*MacLaurin Series*}
paulson@15944: 
nipkow@15131: theory MacLaurin
chaieb@29748: imports Transcendental
nipkow@15131: begin
obua@14738: 
paulson@15079: subsection{*Maclaurin's Theorem with Lagrange Form of Remainder*}
paulson@15079: 
paulson@15079: text{*This is a very long, messy proof even now that it's been broken down
paulson@15079: into lemmas.*}
paulson@15079: 
paulson@15079: lemma Maclaurin_lemma:
paulson@15079:     "0 < h ==>
nipkow@15539:      \<exists>B. f h = (\<Sum>m=0..<n. (j m / real (fact m)) * (h^m)) +
paulson@15079:                (B * ((h^n) / real(fact n)))"
hoelzl@41412: by (rule exI[where x = "(f h - (\<Sum>m=0..<n. (j m / real (fact m)) * h^m)) *
hoelzl@41412:                  real(fact n) / (h^n)"]) simp
paulson@15079: 
paulson@15079: lemma eq_diff_eq': "(x = y - z) = (y = x + (z::real))"
paulson@15079: by arith
paulson@15079: 
avigad@32031: lemma fact_diff_Suc [rule_format]:
avigad@32031:   "n < Suc m ==> fact (Suc m - n) = (Suc m - n) * fact (m - n)"
avigad@32031:   by (subst fact_reduce_nat, auto)
avigad@32031: 
paulson@15079: lemma Maclaurin_lemma2:
hoelzl@41412:   fixes B
bulwahn@41368:   assumes DERIV : "\<forall>m t. m < n \<and> 0\<le>t \<and> t\<le>h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
hoelzl@41412:     and INIT : "n = Suc k"
hoelzl@41412:   defines "difg \<equiv> (\<lambda>m t. diff m t - ((\<Sum>p = 0..<n - m. diff (m + p) 0 / real (fact p) * t ^ p) +
hoelzl@41412:     B * (t ^ (n - m) / real (fact (n - m)))))" (is "difg \<equiv> (\<lambda>m t. diff m t - ?difg m t)")
bulwahn@41368:   shows "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41412: proof (rule allI impI)+
hoelzl@41412:   fix m t assume INIT2: "m < n & 0 \<le> t & t \<le> h"
hoelzl@41412:   have "DERIV (difg m) t :> diff (Suc m) t -
hoelzl@41412:     ((\<Sum>x = 0..<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / real (fact x)) +
hoelzl@41412:      real (n - m) * t ^ (n - Suc m) * B / real (fact (n - m)))" unfolding difg_def
hoelzl@41412:     by (auto intro!: DERIV_intros DERIV[rule_format, OF INIT2])
bulwahn@41368:       moreover
hoelzl@41412:   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
hoelzl@41412:     unfolding atLeast0LessThan[symmetric] by auto
hoelzl@41412:   have "(\<Sum>x = 0..<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / real (fact x)) =
hoelzl@41412:       (\<Sum>x = 0..<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)))"
hoelzl@41412:     unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
hoelzl@41412:   moreover
hoelzl@41412:   have fact_neq_0: "\<And>x::nat. real (fact x) + real x * real (fact x) \<noteq> 0"
hoelzl@41412:     by (metis fact_gt_zero_nat not_add_less1 real_of_nat_add real_of_nat_mult real_of_nat_zero_iff)
hoelzl@41412:   have "\<And>x. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)) =
hoelzl@41412:       diff (Suc m + x) 0 * t^x / real (fact x)"
hoelzl@41412:     by (auto simp: field_simps real_of_nat_Suc fact_neq_0 intro!: nonzero_divide_eq_eq[THEN iffD2])
hoelzl@41412:   moreover
hoelzl@41412:   have "real (n - m) * t ^ (n - Suc m) * B / real (fact (n - m)) =
hoelzl@41412:       B * (t ^ (n - Suc m) / real (fact (n - Suc m)))"
hoelzl@41412:     using `0 < n - m` by (simp add: fact_reduce_nat)
hoelzl@41412:   ultimately show "DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41412:     unfolding difg_def by simp
bulwahn@41368: qed
avigad@32031: 
paulson@15079: lemma Maclaurin:
huffman@29187:   assumes h: "0 < h"
huffman@29187:   assumes n: "0 < n"
huffman@29187:   assumes diff_0: "diff 0 = f"
huffman@29187:   assumes diff_Suc:
huffman@29187:     "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187:   shows
huffman@29187:     "\<exists>t. 0 < t & t < h &
paulson@15079:               f h =
nipkow@15539:               setsum (%m. (diff m 0 / real (fact m)) * h ^ m) {0..<n} +
hoelzl@41412:               (diff n t / real (fact n)) * h ^ n"
huffman@29187: proof -
huffman@29187:   from n obtain m where m: "n = Suc m"
hoelzl@41412:     by (cases n) (simp add: n)
huffman@29187: 
huffman@29187:   obtain B where f_h: "f h =
huffman@29187:         (\<Sum>m = 0..<n. diff m (0\<Colon>real) / real (fact m) * h ^ m) +
huffman@29187:         B * (h ^ n / real (fact n))"
huffman@29187:     using Maclaurin_lemma [OF h] ..
huffman@29187: 
hoelzl@41412:   def g \<equiv> "(\<lambda>t. f t -
hoelzl@41412:     (setsum (\<lambda>m. (diff m 0 / real(fact m)) * t^m) {0..<n}
hoelzl@41412:       + (B * (t^n / real(fact n)))))"
huffman@29187: 
huffman@29187:   have g2: "g 0 = 0 & g h = 0"
huffman@29187:     apply (simp add: m f_h g_def del: setsum_op_ivl_Suc)
huffman@30019:     apply (cut_tac n = m and k = "Suc 0" in sumr_offset2)
huffman@29187:     apply (simp add: eq_diff_eq' diff_0 del: setsum_op_ivl_Suc)
huffman@29187:     done
huffman@29187: 
hoelzl@41412:   def difg \<equiv> "(%m t. diff m t -
huffman@29187:     (setsum (%p. (diff (m + p) 0 / real (fact p)) * (t ^ p)) {0..<n-m}
hoelzl@41412:       + (B * ((t ^ (n - m)) / real (fact (n - m))))))"
huffman@29187: 
huffman@29187:   have difg_0: "difg 0 = g"
huffman@29187:     unfolding difg_def g_def by (simp add: diff_0)
huffman@29187: 
huffman@29187:   have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
huffman@29187:         m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41412:     using diff_Suc m unfolding difg_def by (rule Maclaurin_lemma2)
huffman@29187: 
huffman@29187:   have difg_eq_0: "\<forall>m. m < n --> difg m 0 = 0"
huffman@29187:     apply clarify
huffman@29187:     apply (simp add: m difg_def)
huffman@29187:     apply (frule less_iff_Suc_add [THEN iffD1], clarify)
huffman@29187:     apply (simp del: setsum_op_ivl_Suc)
huffman@30019:     apply (insert sumr_offset4 [of "Suc 0"])
avigad@32039:     apply (simp del: setsum_op_ivl_Suc fact_Suc)
huffman@29187:     done
huffman@29187: 
huffman@29187:   have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
huffman@29187:     by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
huffman@29187: 
huffman@29187:   have differentiable_difg:
huffman@29187:     "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable x"
huffman@29187:     by (rule differentiableI [OF difg_Suc [rule_format]]) simp
huffman@29187: 
huffman@29187:   have difg_Suc_eq_0: "\<And>m t. \<lbrakk>m < n; 0 \<le> t; t \<le> h; DERIV (difg m) t :> 0\<rbrakk>
huffman@29187:         \<Longrightarrow> difg (Suc m) t = 0"
huffman@29187:     by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
huffman@29187: 
huffman@29187:   have "m < n" using m by simp
huffman@29187: 
huffman@29187:   have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
huffman@29187:   using `m < n`
huffman@29187:   proof (induct m)
hoelzl@41412:     case 0
huffman@29187:     show ?case
huffman@29187:     proof (rule Rolle)
huffman@29187:       show "0 < h" by fact
huffman@29187:       show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
huffman@29187:       show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
huffman@29187:         by (simp add: isCont_difg n)
huffman@29187:       show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable x"
huffman@29187:         by (simp add: differentiable_difg n)
huffman@29187:     qed
huffman@29187:   next
hoelzl@41412:     case (Suc m')
huffman@29187:     hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
huffman@29187:     then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
huffman@29187:     have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
huffman@29187:     proof (rule Rolle)
huffman@29187:       show "0 < t" by fact
huffman@29187:       show "difg (Suc m') 0 = difg (Suc m') t"
huffman@29187:         using t `Suc m' < n` by (simp add: difg_Suc_eq_0 difg_eq_0)
huffman@29187:       show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
huffman@29187:         using `t < h` `Suc m' < n` by (simp add: isCont_difg)
huffman@29187:       show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable x"
huffman@29187:         using `t < h` `Suc m' < n` by (simp add: differentiable_difg)
huffman@29187:     qed
huffman@29187:     thus ?case
huffman@29187:       using `t < h` by auto
huffman@29187:   qed
huffman@29187: 
huffman@29187:   then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
huffman@29187: 
huffman@29187:   hence "difg (Suc m) t = 0"
huffman@29187:     using `m < n` by (simp add: difg_Suc_eq_0)
huffman@29187: 
huffman@29187:   show ?thesis
huffman@29187:   proof (intro exI conjI)
huffman@29187:     show "0 < t" by fact
huffman@29187:     show "t < h" by fact
huffman@29187:     show "f h =
huffman@29187:       (\<Sum>m = 0..<n. diff m 0 / real (fact m) * h ^ m) +
huffman@29187:       diff n t / real (fact n) * h ^ n"
huffman@29187:       using `difg (Suc m) t = 0`
avigad@32039:       by (simp add: m f_h difg_def del: fact_Suc)
huffman@29187:   qed
huffman@29187: qed
paulson@15079: 
paulson@15079: lemma Maclaurin_objl:
nipkow@25162:   "0 < h & n>0 & diff 0 = f &
nipkow@25134:   (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
nipkow@25134:    --> (\<exists>t. 0 < t & t < h &
nipkow@25134:             f h = (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
nipkow@25134:                   diff n t / real (fact n) * h ^ n)"
paulson@15079: by (blast intro: Maclaurin)
paulson@15079: 
paulson@15079: 
paulson@15079: lemma Maclaurin2:
bulwahn@41368:   assumes INIT1: "0 < h " and INIT2: "diff 0 = f"
bulwahn@41368:   and DERIV: "\<forall>m t.
bulwahn@41368:   m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41368:   shows "\<exists>t. 0 < t \<and> t \<le> h \<and> f h =
bulwahn@41368:   (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41368:   diff n t / real (fact n) * h ^ n"
bulwahn@41368: proof (cases "n")
bulwahn@41368:   case 0 with INIT1 INIT2 show ?thesis by fastsimp
bulwahn@41368: next
hoelzl@41412:   case Suc
bulwahn@41368:   hence "n > 0" by simp
bulwahn@41368:   from INIT1 this INIT2 DERIV have "\<exists>t>0. t < h \<and>
bulwahn@41368:     f h =
hoelzl@41412:     (\<Sum>m = 0..<n. diff m 0 / real (fact m) * h ^ m) + diff n t / real (fact n) * h ^ n"
bulwahn@41368:     by (rule Maclaurin)
bulwahn@41368:   thus ?thesis by fastsimp
bulwahn@41368: qed
paulson@15079: 
paulson@15079: lemma Maclaurin2_objl:
paulson@15079:      "0 < h & diff 0 = f &
paulson@15079:        (\<forall>m t.
paulson@15079:           m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
paulson@15079:     --> (\<exists>t. 0 < t &
paulson@15079:               t \<le> h &
paulson@15079:               f h =
nipkow@15539:               (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079:               diff n t / real (fact n) * h ^ n)"
paulson@15079: by (blast intro: Maclaurin2)
paulson@15079: 
paulson@15079: lemma Maclaurin_minus:
hoelzl@41412:   assumes "h < 0" "0 < n" "diff 0 = f"
hoelzl@41412:   and DERIV: "\<forall>m t. m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41368:   shows "\<exists>t. h < t & t < 0 &
bulwahn@41368:          f h = (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41368:          diff n t / real (fact n) * h ^ n"
bulwahn@41368: proof -
hoelzl@41412:   txt "Transform @{text ABL'} into @{text DERIV_intros} format."
hoelzl@41412:   note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong]
hoelzl@41412:   from assms
hoelzl@41412:   have "\<exists>t>0. t < - h \<and>
bulwahn@41368:     f (- (- h)) =
bulwahn@41368:     (\<Sum>m = 0..<n.
bulwahn@41368:     (- 1) ^ m * diff m (- 0) / real (fact m) * (- h) ^ m) +
hoelzl@41412:     (- 1) ^ n * diff n (- t) / real (fact n) * (- h) ^ n"
hoelzl@41412:     by (intro Maclaurin) (auto intro!: DERIV_intros DERIV')
hoelzl@41412:   then guess t ..
bulwahn@41368:   moreover
bulwahn@41368:   have "-1 ^ n * diff n (- t) * (- h) ^ n / real (fact n) = diff n (- t) * h ^ n / real (fact n)"
bulwahn@41368:     by (auto simp add: power_mult_distrib[symmetric])
bulwahn@41368:   moreover
bulwahn@41368:   have "(SUM m = 0..<n. -1 ^ m * diff m 0 * (- h) ^ m / real (fact m)) = (SUM m = 0..<n. diff m 0 * h ^ m / real (fact m))"
bulwahn@41368:     by (auto intro: setsum_cong simp add: power_mult_distrib[symmetric])
bulwahn@41368:   ultimately have " h < - t \<and>
bulwahn@41368:     - t < 0 \<and>
bulwahn@41368:     f h =
bulwahn@41368:     (\<Sum>m = 0..<n. diff m 0 / real (fact m) * h ^ m) + diff n (- t) / real (fact n) * h ^ n"
bulwahn@41368:     by auto
bulwahn@41368:   thus ?thesis ..
bulwahn@41368: qed
paulson@15079: 
paulson@15079: lemma Maclaurin_minus_objl:
nipkow@25162:      "(h < 0 & n > 0 & diff 0 = f &
paulson@15079:        (\<forall>m t.
paulson@15079:           m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t))
paulson@15079:     --> (\<exists>t. h < t &
paulson@15079:               t < 0 &
paulson@15079:               f h =
nipkow@15539:               (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079:               diff n t / real (fact n) * h ^ n)"
paulson@15079: by (blast intro: Maclaurin_minus)
paulson@15079: 
paulson@15079: 
paulson@15079: subsection{*More Convenient "Bidirectional" Version.*}
paulson@15079: 
paulson@15079: (* not good for PVS sin_approx, cos_approx *)
paulson@15079: 
paulson@15079: lemma Maclaurin_bi_le_lemma [rule_format]:
nipkow@25162:   "n>0 \<longrightarrow>
nipkow@25134:    diff 0 0 =
nipkow@25134:    (\<Sum>m = 0..<n. diff m 0 * 0 ^ m / real (fact m)) +
nipkow@25134:    diff n 0 * 0 ^ n / real (fact n)"
hoelzl@41412: by (induct "n") auto
paulson@15079: 
paulson@15079: lemma Maclaurin_bi_le:
hoelzl@41412:    assumes "diff 0 = f"
bulwahn@41368:    and DERIV : "\<forall>m t. m < n & abs t \<le> abs x --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41368:    shows "\<exists>t. abs t \<le> abs x &
paulson@15079:               f x =
nipkow@15539:               (\<Sum>m=0..<n. diff m 0 / real (fact m) * x ^ m) +
hoelzl@41412:      diff n t / real (fact n) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41412: proof cases
hoelzl@41412:   assume "n = 0" with `diff 0 = f` show ?thesis by force
bulwahn@41368: next
hoelzl@41412:   assume "n \<noteq> 0"
hoelzl@41412:   show ?thesis
hoelzl@41412:   proof (cases rule: linorder_cases)
hoelzl@41412:     assume "x = 0" with `n \<noteq> 0` `diff 0 = f` DERIV
hoelzl@41412:     have "\<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by (force simp add: Maclaurin_bi_le_lemma)
hoelzl@41412:     thus ?thesis ..
bulwahn@41368:   next
hoelzl@41412:     assume "x < 0"
hoelzl@41412:     with `n \<noteq> 0` DERIV
hoelzl@41412:     have "\<exists>t>x. t < 0 \<and> diff 0 x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41412:     then guess t ..
hoelzl@41412:     with `x < 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41412:     thus ?thesis ..
hoelzl@41412:   next
hoelzl@41412:     assume "x > 0"
hoelzl@41412:     with `n \<noteq> 0` `diff 0 = f` DERIV
hoelzl@41412:     have "\<exists>t>0. t < x \<and> diff 0 x = ?f x t" by (intro Maclaurin) auto
hoelzl@41412:     then guess t ..
hoelzl@41412:     with `x > 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41412:     thus ?thesis ..
bulwahn@41368:   qed
bulwahn@41368: qed
bulwahn@41368: 
paulson@15079: lemma Maclaurin_all_lt:
bulwahn@41368:   assumes INIT1: "diff 0 = f" and INIT2: "0 < n" and INIT3: "x \<noteq> 0"
bulwahn@41368:   and DERIV: "\<forall>m x. DERIV (diff m) x :> diff(Suc m) x"
hoelzl@41412:   shows "\<exists>t. 0 < abs t & abs t < abs x & f x =
hoelzl@41412:     (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41412:                 (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> _ \<and> f x = ?f x t")
hoelzl@41412: proof (cases rule: linorder_cases)
hoelzl@41412:   assume "x = 0" with INIT3 show "?thesis"..
hoelzl@41412: next
hoelzl@41412:   assume "x < 0"
hoelzl@41412:   with assms have "\<exists>t>x. t < 0 \<and> f x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41412:   then guess t ..
hoelzl@41412:   with `x < 0` have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41412:   thus ?thesis ..
hoelzl@41412: next
hoelzl@41412:   assume "x > 0"
hoelzl@41412:   with assms have "\<exists>t>0. t < x \<and> f x = ?f x t " by (intro Maclaurin) auto
hoelzl@41412:   then guess t ..
hoelzl@41412:   with `x > 0` have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41412:   thus ?thesis ..
bulwahn@41368: qed
bulwahn@41368: 
paulson@15079: 
paulson@15079: lemma Maclaurin_all_lt_objl:
paulson@15079:      "diff 0 = f &
paulson@15079:       (\<forall>m x. DERIV (diff m) x :> diff(Suc m) x) &
nipkow@25162:       x ~= 0 & n > 0
paulson@15079:       --> (\<exists>t. 0 < abs t & abs t < abs x &
nipkow@15539:                f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079:                      (diff n t / real (fact n)) * x ^ n)"
paulson@15079: by (blast intro: Maclaurin_all_lt)
paulson@15079: 
paulson@15079: lemma Maclaurin_zero [rule_format]:
paulson@15079:      "x = (0::real)
nipkow@25134:       ==> n \<noteq> 0 -->
nipkow@15539:           (\<Sum>m=0..<n. (diff m (0::real) / real (fact m)) * x ^ m) =
paulson@15079:           diff 0 0"
paulson@15079: by (induct n, auto)
paulson@15079: 
bulwahn@41368: 
bulwahn@41368: lemma Maclaurin_all_le:
bulwahn@41368:   assumes INIT: "diff 0 = f"
bulwahn@41368:   and DERIV: "\<forall>m x. DERIV (diff m) x :> diff (Suc m) x"
hoelzl@41412:   shows "\<exists>t. abs t \<le> abs x & f x =
hoelzl@41412:     (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41412:     (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41412: proof cases
hoelzl@41412:   assume "n = 0" with INIT show ?thesis by force
bulwahn@41368:   next
hoelzl@41412:   assume "n \<noteq> 0"
hoelzl@41412:   show ?thesis
hoelzl@41412:   proof cases
hoelzl@41412:     assume "x = 0"
hoelzl@41412:     with `n \<noteq> 0` have "(\<Sum>m = 0..<n. diff m 0 / real (fact m) * x ^ m) = diff 0 0"
hoelzl@41412:       by (intro Maclaurin_zero) auto
hoelzl@41412:     with INIT `x = 0` `n \<noteq> 0` have " \<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by force
hoelzl@41412:     thus ?thesis ..
hoelzl@41412:   next
hoelzl@41412:     assume "x \<noteq> 0"
hoelzl@41412:     with INIT `n \<noteq> 0` DERIV have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t"
hoelzl@41412:       by (intro Maclaurin_all_lt) auto
hoelzl@41412:     then guess t ..
hoelzl@41412:     hence "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41412:     thus ?thesis ..
bulwahn@41368:   qed
bulwahn@41368: qed
bulwahn@41368: 
paulson@15079: lemma Maclaurin_all_le_objl: "diff 0 = f &
paulson@15079:       (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x)
paulson@15079:       --> (\<exists>t. abs t \<le> abs x &
nipkow@15539:               f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079:                     (diff n t / real (fact n)) * x ^ n)"
paulson@15079: by (blast intro: Maclaurin_all_le)
paulson@15079: 
paulson@15079: 
paulson@15079: subsection{*Version for Exponential Function*}
paulson@15079: 
nipkow@25162: lemma Maclaurin_exp_lt: "[| x ~= 0; n > 0 |]
paulson@15079:       ==> (\<exists>t. 0 < abs t &
paulson@15079:                 abs t < abs x &
nipkow@15539:                 exp x = (\<Sum>m=0..<n. (x ^ m) / real (fact m)) +
paulson@15079:                         (exp t / real (fact n)) * x ^ n)"
paulson@15079: by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_lt_objl, auto)
paulson@15079: 
paulson@15079: 
paulson@15079: lemma Maclaurin_exp_le:
paulson@15079:      "\<exists>t. abs t \<le> abs x &
nipkow@15539:             exp x = (\<Sum>m=0..<n. (x ^ m) / real (fact m)) +
paulson@15079:                        (exp t / real (fact n)) * x ^ n"
paulson@15079: by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_le_objl, auto)
paulson@15079: 
paulson@15079: 
paulson@15079: subsection{*Version for Sine Function*}
paulson@15079: 
paulson@15079: lemma mod_exhaust_less_4:
nipkow@25134:   "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
webertj@20217: by auto
paulson@15079: 
paulson@15079: lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
nipkow@25134:   "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
paulson@15251: by (induct "n", auto)
paulson@15079: 
paulson@15079: lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
nipkow@25134:   "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
paulson@15251: by (induct "n", auto)
paulson@15079: 
paulson@15079: lemma Suc_mult_two_diff_one [rule_format, simp]:
nipkow@25134:   "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
paulson@15251: by (induct "n", auto)
paulson@15079: 
paulson@15234: 
paulson@15234: text{*It is unclear why so many variant results are needed.*}
paulson@15079: 
huffman@36974: lemma sin_expansion_lemma:
hoelzl@41412:      "sin (x + real (Suc m) * pi / 2) =
huffman@36974:       cos (x + real (m) * pi / 2)"
huffman@36974: by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib left_distrib, auto)
huffman@36974: 
huffman@45166: lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
huffman@45166:   unfolding sin_coeff_def by simp (* TODO: move *)
huffman@45166: 
paulson@15079: lemma Maclaurin_sin_expansion2:
paulson@15079:      "\<exists>t. abs t \<le> abs x &
paulson@15079:        sin x =
huffman@45166:        (\<Sum>m=0..<n. sin_coeff m * x ^ m)
paulson@15079:       + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = sin and n = n and x = x
paulson@15079:         and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
paulson@15079: apply safe
paulson@15079: apply (simp (no_asm))
huffman@36974: apply (simp (no_asm) add: sin_expansion_lemma)
huffman@45166: apply (subst (asm) setsum_0', clarify, case_tac "a", simp, simp)
huffman@45166: apply (cases n, simp, simp)
paulson@15079: apply (rule ccontr, simp)
paulson@15079: apply (drule_tac x = x in spec, simp)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15234: lemma Maclaurin_sin_expansion:
paulson@15234:      "\<exists>t. sin x =
huffman@45166:        (\<Sum>m=0..<n. sin_coeff m * x ^ m)
paulson@15234:       + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
hoelzl@41412: apply (insert Maclaurin_sin_expansion2 [of x n])
hoelzl@41412: apply (blast intro: elim:)
paulson@15234: done
paulson@15234: 
paulson@15079: lemma Maclaurin_sin_expansion3:
nipkow@25162:      "[| n > 0; 0 < x |] ==>
paulson@15079:        \<exists>t. 0 < t & t < x &
paulson@15079:        sin x =
huffman@45166:        (\<Sum>m=0..<n. sin_coeff m * x ^ m)
paulson@15079:       + ((sin(t + 1/2 * real(n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = sin and n = n and h = x and diff = "%n x. sin (x + 1/2*real (n) *pi)" in Maclaurin_objl)
paulson@15079: apply safe
paulson@15079: apply simp
huffman@36974: apply (simp (no_asm) add: sin_expansion_lemma)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15079: lemma Maclaurin_sin_expansion4:
paulson@15079:      "0 < x ==>
paulson@15079:        \<exists>t. 0 < t & t \<le> x &
paulson@15079:        sin x =
huffman@45166:        (\<Sum>m=0..<n. sin_coeff m * x ^ m)
paulson@15079:       + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = sin and n = n and h = x and diff = "%n x. sin (x + 1/2*real (n) *pi)" in Maclaurin2_objl)
paulson@15079: apply safe
paulson@15079: apply simp
huffman@36974: apply (simp (no_asm) add: sin_expansion_lemma)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15079: 
paulson@15079: subsection{*Maclaurin Expansion for Cosine Function*}
paulson@15079: 
huffman@45166: lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
huffman@45166:   unfolding cos_coeff_def by simp (* TODO: move *)
huffman@45166: 
paulson@15079: lemma sumr_cos_zero_one [simp]:
huffman@45166:   "(\<Sum>m=0..<(Suc n). cos_coeff m * 0 ^ m) = 1"
paulson@15251: by (induct "n", auto)
paulson@15079: 
huffman@36974: lemma cos_expansion_lemma:
huffman@36974:   "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
huffman@36974: by (simp only: cos_add sin_add real_of_nat_Suc left_distrib add_divide_distrib, auto)
huffman@36974: 
paulson@15079: lemma Maclaurin_cos_expansion:
paulson@15079:      "\<exists>t. abs t \<le> abs x &
paulson@15079:        cos x =
huffman@45166:        (\<Sum>m=0..<n. cos_coeff m * x ^ m)
paulson@15079:       + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = cos and n = n and x = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_all_lt_objl)
paulson@15079: apply safe
paulson@15079: apply (simp (no_asm))
huffman@36974: apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079: apply (case_tac "n", simp)
nipkow@15561: apply (simp del: setsum_op_ivl_Suc)
paulson@15079: apply (rule ccontr, simp)
paulson@15079: apply (drule_tac x = x in spec, simp)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15079: lemma Maclaurin_cos_expansion2:
nipkow@25162:      "[| 0 < x; n > 0 |] ==>
paulson@15079:        \<exists>t. 0 < t & t < x &
paulson@15079:        cos x =
huffman@45166:        (\<Sum>m=0..<n. cos_coeff m * x ^ m)
paulson@15079:       + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = cos and n = n and h = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_objl)
paulson@15079: apply safe
paulson@15079: apply simp
huffman@36974: apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15234: lemma Maclaurin_minus_cos_expansion:
nipkow@25162:      "[| x < 0; n > 0 |] ==>
paulson@15079:        \<exists>t. x < t & t < 0 &
paulson@15079:        cos x =
huffman@45166:        (\<Sum>m=0..<n. cos_coeff m * x ^ m)
paulson@15079:       + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079: apply (cut_tac f = cos and n = n and h = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_minus_objl)
paulson@15079: apply safe
paulson@15079: apply simp
huffman@36974: apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079: apply (erule ssubst)
paulson@15079: apply (rule_tac x = t in exI, simp)
nipkow@15536: apply (rule setsum_cong[OF refl])
huffman@45166: apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079: done
paulson@15079: 
paulson@15079: (* ------------------------------------------------------------------------- *)
paulson@15079: (* Version for ln(1 +/- x). Where is it??                                    *)
paulson@15079: (* ------------------------------------------------------------------------- *)
paulson@15079: 
paulson@15079: lemma sin_bound_lemma:
paulson@15081:     "[|x = y; abs u \<le> (v::real) |] ==> \<bar>(x + u) - y\<bar> \<le> v"
paulson@15079: by auto
paulson@15079: 
paulson@15079: lemma Maclaurin_sin_bound:
huffman@45166:   "abs(sin x - (\<Sum>m=0..<n. sin_coeff m * x ^ m))
huffman@45166:   \<le> inverse(real (fact n)) * \<bar>x\<bar> ^ n"
obua@14738: proof -
paulson@15079:   have "!! x (y::real). x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y"
obua@14738:     by (rule_tac mult_right_mono,simp_all)
obua@14738:   note est = this[simplified]
huffman@22985:   let ?diff = "\<lambda>(n::nat) x. if n mod 4 = 0 then sin(x) else if n mod 4 = 1 then cos(x) else if n mod 4 = 2 then -sin(x) else -cos(x)"
huffman@22985:   have diff_0: "?diff 0 = sin" by simp
huffman@22985:   have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x"
huffman@22985:     apply (clarify)
huffman@22985:     apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
huffman@22985:     apply (cut_tac m=m in mod_exhaust_less_4)
hoelzl@31880:     apply (safe, auto intro!: DERIV_intros)
huffman@22985:     done
huffman@22985:   from Maclaurin_all_le [OF diff_0 DERIV_diff]
huffman@22985:   obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" and
huffman@22985:     t2: "sin x = (\<Sum>m = 0..<n. ?diff m 0 / real (fact m) * x ^ m) +
huffman@22985:       ?diff n t / real (fact n) * x ^ n" by fast
huffman@22985:   have diff_m_0:
huffman@22985:     "\<And>m. ?diff m 0 = (if even m then 0
huffman@23177:          else -1 ^ ((m - Suc 0) div 2))"
huffman@22985:     apply (subst even_even_mod_4_iff)
huffman@22985:     apply (cut_tac m=m in mod_exhaust_less_4)
huffman@22985:     apply (elim disjE, simp_all)
huffman@22985:     apply (safe dest!: mod_eqD, simp_all)
huffman@22985:     done
obua@14738:   show ?thesis
huffman@45166:     unfolding sin_coeff_def
huffman@22985:     apply (subst t2)
paulson@15079:     apply (rule sin_bound_lemma)
nipkow@15536:     apply (rule setsum_cong[OF refl])
huffman@22985:     apply (subst diff_m_0, simp)
paulson@15079:     apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
hoelzl@41412:                 simp add: est mult_nonneg_nonneg mult_ac divide_inverse
paulson@16924:                           power_abs [symmetric] abs_mult)
obua@14738:     done
obua@14738: qed
obua@14738: 
paulson@15079: end




