src/HOL/Inequalities.thy
author nipkow
Fri May 01 20:25:57 2015 +0200 (2015-05-01)
changeset 60167 9a97407488cd
parent 60150 bd773c47ad0b
child 60758 d8d85a8172b5
permissions -rw-r--r--
simplified statement and proof
wenzelm@59720
     1
(*  Title:     HOL/Inequalities.thy
hoelzl@59712
     2
    Author:    Tobias Nipkow
hoelzl@59712
     3
    Author:    Johannes Hölzl
hoelzl@59712
     4
*)
hoelzl@59712
     5
hoelzl@59712
     6
theory Inequalities
hoelzl@59712
     7
  imports Real_Vector_Spaces
hoelzl@59712
     8
begin
hoelzl@59712
     9
nipkow@60167
    10
lemma Setsum_Icc_int: "(m::int) \<le> n \<Longrightarrow> \<Sum> {m..n} = (n*(n+1) - m*(m-1)) div 2"
nipkow@60167
    11
proof(induct i == "nat(n-m)" arbitrary: m n)
nipkow@60167
    12
  case 0
nipkow@60167
    13
  hence "m = n" by arith
nipkow@60167
    14
  thus ?case by (simp add: algebra_simps)
nipkow@60167
    15
next
nipkow@60167
    16
  case (Suc i)
nipkow@60167
    17
  have 0: "i = nat((n-1) - m)" "m \<le> n-1" using Suc(2,3) by arith+
nipkow@60167
    18
  have "\<Sum> {m..n} = \<Sum> {m..1+(n-1)}" by simp
nipkow@60167
    19
  also have "\<dots> = \<Sum> {m..n-1} + n" using `m \<le> n`
nipkow@60167
    20
    by(subst atLeastAtMostPlus1_int_conv) simp_all
nipkow@60167
    21
  also have "\<dots> = ((n-1)*(n-1+1) - m*(m-1)) div 2 + n"
nipkow@60167
    22
    by(simp add: Suc(1)[OF 0])
nipkow@60167
    23
  also have "\<dots> = ((n-1)*(n-1+1) - m*(m-1) + 2*n) div 2" by simp
nipkow@60167
    24
  also have "\<dots> = (n*(n+1) - m*(m-1)) div 2" by(simp add: algebra_simps)
nipkow@60167
    25
  finally show ?case .
hoelzl@59712
    26
qed
hoelzl@59712
    27
hoelzl@59712
    28
lemma Setsum_Icc_nat: assumes "(m::nat) \<le> n"
hoelzl@59712
    29
shows "\<Sum> {m..n} = (n*(n+1) - m*(m-1)) div 2"
hoelzl@59712
    30
proof -
hoelzl@59712
    31
  have "m*(m-1) \<le> n*(n + 1)"
hoelzl@59712
    32
   using assms by (meson diff_le_self order_trans le_add1 mult_le_mono)
hoelzl@59712
    33
  hence "int(\<Sum> {m..n}) = int((n*(n+1) - m*(m-1)) div 2)" using assms
nipkow@60167
    34
    by (auto simp: Setsum_Icc_int[transferred, OF assms] zdiv_int int_mult
hoelzl@59712
    35
      split: zdiff_int_split)
hoelzl@59712
    36
  thus ?thesis by simp
hoelzl@59712
    37
qed
hoelzl@59712
    38
hoelzl@59712
    39
lemma Setsum_Ico_nat: assumes "(m::nat) \<le> n"
hoelzl@59712
    40
shows "\<Sum> {m..<n} = (n*(n-1) - m*(m-1)) div 2"
hoelzl@59712
    41
proof cases
hoelzl@59712
    42
  assume "m < n"
hoelzl@59712
    43
  hence "{m..<n} = {m..n-1}" by auto
hoelzl@59712
    44
  hence "\<Sum>{m..<n} = \<Sum>{m..n-1}" by simp
hoelzl@59712
    45
  also have "\<dots> = (n*(n-1) - m*(m-1)) div 2"
hoelzl@59712
    46
    using assms `m < n` by (simp add: Setsum_Icc_nat mult.commute)
hoelzl@59712
    47
  finally show ?thesis .
hoelzl@59712
    48
next
hoelzl@59712
    49
  assume "\<not> m < n" with assms show ?thesis by simp
hoelzl@59712
    50
qed
hoelzl@59712
    51
hoelzl@59712
    52
lemma Chebyshev_sum_upper:
hoelzl@59712
    53
  fixes a b::"nat \<Rightarrow> 'a::linordered_idom"
hoelzl@59712
    54
  assumes "\<And>i j. i \<le> j \<Longrightarrow> j < n \<Longrightarrow> a i \<le> a j"
hoelzl@59712
    55
  assumes "\<And>i j. i \<le> j \<Longrightarrow> j < n \<Longrightarrow> b i \<ge> b j"
hoelzl@59712
    56
  shows "of_nat n * (\<Sum>k=0..<n. a k * b k) \<le> (\<Sum>k=0..<n. a k) * (\<Sum>k=0..<n. b k)"
hoelzl@59712
    57
proof -
hoelzl@59712
    58
  let ?S = "(\<Sum>j=0..<n. (\<Sum>k=0..<n. (a j - a k) * (b j - b k)))"
hoelzl@59712
    59
  have "2 * (of_nat n * (\<Sum>j=0..<n. (a j * b j)) - (\<Sum>j=0..<n. b j) * (\<Sum>k=0..<n. a k)) = ?S"
hoelzl@59712
    60
    unfolding one_add_one[symmetric] algebra_simps
hoelzl@59712
    61
    by (simp add: algebra_simps setsum_subtractf setsum.distrib setsum.commute[of "\<lambda>i j. a i * b j"] setsum_right_distrib)
hoelzl@59712
    62
  also
hoelzl@59712
    63
  { fix i j::nat assume "i<n" "j<n"
hoelzl@59712
    64
    hence "a i - a j \<le> 0 \<and> b i - b j \<ge> 0 \<or> a i - a j \<ge> 0 \<and> b i - b j \<le> 0"
hoelzl@59712
    65
      using assms by (cases "i \<le> j") (auto simp: algebra_simps)
hoelzl@59712
    66
  } hence "?S \<le> 0"
hoelzl@59712
    67
    by (auto intro!: setsum_nonpos simp: mult_le_0_iff)
hoelzl@59712
    68
       (auto simp: field_simps)
hoelzl@59712
    69
  finally show ?thesis by (simp add: algebra_simps)
hoelzl@59712
    70
qed
hoelzl@59712
    71
hoelzl@59712
    72
lemma Chebyshev_sum_upper_nat:
hoelzl@59712
    73
  fixes a b :: "nat \<Rightarrow> nat"
hoelzl@59712
    74
  shows "(\<And>i j. \<lbrakk> i\<le>j; j<n \<rbrakk> \<Longrightarrow> a i \<le> a j) \<Longrightarrow>
hoelzl@59712
    75
         (\<And>i j. \<lbrakk> i\<le>j; j<n \<rbrakk> \<Longrightarrow> b i \<ge> b j) \<Longrightarrow>
hoelzl@59712
    76
    n * (\<Sum>i=0..<n. a i * b i) \<le> (\<Sum>i=0..<n. a i) * (\<Sum>i=0..<n. b i)"
hoelzl@59712
    77
using Chebyshev_sum_upper[where 'a=real, of n a b]
hoelzl@59712
    78
by (simp del: real_of_nat_mult real_of_nat_setsum
hoelzl@59712
    79
  add: real_of_nat_mult[symmetric] real_of_nat_setsum[symmetric] real_of_nat_def[symmetric])
hoelzl@59712
    80
hoelzl@59712
    81
end