src/HOL/Binomial.thy
changeset 61552 980dd46a03fb
parent 61531 ab2e862263e7
child 61554 84901b8aa4f5
equal deleted inserted replaced
61532:e3984606b4b6 61552:980dd46a03fb
    11 imports Main
    11 imports Main
    12 begin
    12 begin
    13 
    13 
    14 subsection \<open>Factorial\<close>
    14 subsection \<open>Factorial\<close>
    15 
    15 
    16 fun fact :: "nat \<Rightarrow> ('a::semiring_char_0)"
    16 fun (in semiring_char_0) fact :: "nat \<Rightarrow> 'a"
    17   where "fact 0 = 1" | "fact (Suc n) = of_nat (Suc n) * fact n"
    17   where "fact 0 = 1" | "fact (Suc n) = of_nat (Suc n) * fact n"
    18 
    18 
    19 lemmas fact_Suc = fact.simps(2)
    19 lemmas fact_Suc = fact.simps(2)
    20 
    20 
    21 lemma fact_1 [simp]: "fact 1 = 1"
    21 lemma fact_1 [simp]: "fact 1 = 1"
   440 
   440 
   441 subsection\<open>Pochhammer's symbol : generalized rising factorial\<close>
   441 subsection\<open>Pochhammer's symbol : generalized rising factorial\<close>
   442 
   442 
   443 text \<open>See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"}\<close>
   443 text \<open>See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"}\<close>
   444 
   444 
   445 definition "pochhammer (a::'a::comm_semiring_1) n =
   445 definition (in comm_semiring_1) "pochhammer (a :: 'a) n =
   446   (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
   446   (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
   447 
   447 
   448 lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
   448 lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
   449   by (simp add: pochhammer_def)
   449   by (simp add: pochhammer_def)
   450 
   450 
   619 
   619 
   620 lemma pochhammer_product: 
   620 lemma pochhammer_product: 
   621   "m \<le> n \<Longrightarrow> pochhammer z n = pochhammer z m * pochhammer (z + of_nat m) (n - m)"
   621   "m \<le> n \<Longrightarrow> pochhammer z n = pochhammer z m * pochhammer (z + of_nat m) (n - m)"
   622   using pochhammer_product'[of z m "n - m"] by simp
   622   using pochhammer_product'[of z m "n - m"] by simp
   623 
   623 
       
   624 lemma pochhammer_times_pochhammer_half:
       
   625   fixes z :: "'a :: field_char_0"
       
   626   shows "pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n) = (\<Prod>k=0..2*n+1. z + of_nat k / 2)"
       
   627 proof (induction n)
       
   628   case (Suc n)
       
   629   def n' \<equiv> "Suc n"
       
   630   have "pochhammer z (Suc n') * pochhammer (z + 1 / 2) (Suc n') =
       
   631           (pochhammer z n' * pochhammer (z + 1 / 2) n') * 
       
   632           ((z + of_nat n') * (z + 1/2 + of_nat n'))" (is "_ = _ * ?A")
       
   633      by (simp_all add: pochhammer_rec' mult_ac)
       
   634   also have "?A = (z + of_nat (Suc (2 * n + 1)) / 2) * (z + of_nat (Suc (Suc (2 * n + 1))) / 2)"
       
   635     (is "_ = ?A") by (simp add: field_simps n'_def of_nat_mult)
       
   636   also note Suc[folded n'_def]
       
   637   also have "(\<Prod>k = 0..2 * n + 1. z + of_nat k / 2) * ?A = (\<Prod>k = 0..2 * Suc n + 1. z + of_nat k / 2)"
       
   638     by (simp add: setprod_nat_ivl_Suc)
       
   639   finally show ?case by (simp add: n'_def)
       
   640 qed (simp add: setprod_nat_ivl_Suc)
       
   641 
       
   642 lemma pochhammer_double:
       
   643   fixes z :: "'a :: field_char_0"
       
   644   shows "pochhammer (2 * z) (2 * n) = of_nat (2^(2*n)) * pochhammer z n * pochhammer (z+1/2) n"
       
   645 proof (induction n)
       
   646   case (Suc n)
       
   647   have "pochhammer (2 * z) (2 * (Suc n)) = pochhammer (2 * z) (2 * n) * 
       
   648           (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1)"
       
   649     by (simp add: pochhammer_rec' ac_simps of_nat_mult)
       
   650   also note Suc
       
   651   also have "of_nat (2 ^ (2 * n)) * pochhammer z n * pochhammer (z + 1/2) n *
       
   652                  (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1) =
       
   653              of_nat (2 ^ (2 * (Suc n))) * pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n)"
       
   654     by (simp add: of_nat_mult field_simps pochhammer_rec')
       
   655   finally show ?case .
       
   656 qed simp
       
   657 
   624 lemma pochhammer_absorb_comp:
   658 lemma pochhammer_absorb_comp:
   625   "((r :: 'a :: comm_ring_1) - of_nat k) * pochhammer (- r) k = r * pochhammer (-r + 1) k" 
   659   "((r :: 'a :: comm_ring_1) - of_nat k) * pochhammer (- r) k = r * pochhammer (-r + 1) k" 
   626   (is "?lhs = ?rhs")
   660   (is "?lhs = ?rhs")
   627 proof -
   661 proof -
   628   have "?lhs = -pochhammer (-r) (Suc k)" by (subst pochhammer_rec') (simp add: algebra_simps)
   662   have "?lhs = -pochhammer (-r) (Suc k)" by (subst pochhammer_rec') (simp add: algebra_simps)
   631 qed
   665 qed
   632 
   666 
   633 
   667 
   634 subsection\<open>Generalized binomial coefficients\<close>
   668 subsection\<open>Generalized binomial coefficients\<close>
   635 
   669 
   636 definition gbinomial :: "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
   670 definition (in field_char_0) gbinomial :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
   637   where "a gchoose n =
   671   where "a gchoose n =
   638     (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / (fact n))"
   672     (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / (fact n))"
   639 
   673 
   640 lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
   674 lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
   641   by (simp_all add: gbinomial_def)
   675   by (simp_all add: gbinomial_def)
   650   have eq: "(- (1::'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
   684   have eq: "(- (1::'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
   651     by auto
   685     by auto
   652   from False show ?thesis
   686   from False show ?thesis
   653     by (simp add: pochhammer_def gbinomial_def field_simps
   687     by (simp add: pochhammer_def gbinomial_def field_simps
   654       eq setprod.distrib[symmetric])
   688       eq setprod.distrib[symmetric])
       
   689 qed
       
   690 
       
   691 lemma gbinomial_pochhammer':
       
   692   "(s :: 'a :: field_char_0) gchoose n = pochhammer (s - of_nat n + 1) n / fact n"
       
   693 proof -
       
   694   have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n"
       
   695     by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac)
       
   696   also have "(-1 :: 'a)^n * (-1)^n = 1" by (subst power_add [symmetric]) simp
       
   697   finally show ?thesis by simp
   655 qed
   698 qed
   656 
   699 
   657 lemma binomial_gbinomial: 
   700 lemma binomial_gbinomial: 
   658     "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
   701     "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
   659 proof -
   702 proof -