src/HOL/Inequalities.thy
author wenzelm
Fri May 27 20:23:55 2016 +0200 (2016-05-27)
changeset 63170 eae6549dbea2
parent 62348 9a5f43dac883
child 63918 6bf55e6e0b75
permissions -rw-r--r--
tuned proofs, to allow unfold_abs_def;
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
wenzelm@60758
    19
  also have "\<dots> = \<Sum> {m..n-1} + n" using \<open>m \<le> n\<close>
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
haftmann@62348
    34
    by (auto simp: Setsum_Icc_int[transferred, OF assms] zdiv_int of_nat_mult simp del: of_nat_setsum
lp15@61609
    35
          split: zdiff_int_split)
lp15@61609
    36
  thus ?thesis
lp15@61694
    37
    using of_nat_eq_iff by blast
hoelzl@59712
    38
qed
hoelzl@59712
    39
hoelzl@59712
    40
lemma Setsum_Ico_nat: assumes "(m::nat) \<le> n"
hoelzl@59712
    41
shows "\<Sum> {m..<n} = (n*(n-1) - m*(m-1)) div 2"
hoelzl@59712
    42
proof cases
hoelzl@59712
    43
  assume "m < n"
hoelzl@59712
    44
  hence "{m..<n} = {m..n-1}" by auto
hoelzl@59712
    45
  hence "\<Sum>{m..<n} = \<Sum>{m..n-1}" by simp
hoelzl@59712
    46
  also have "\<dots> = (n*(n-1) - m*(m-1)) div 2"
wenzelm@60758
    47
    using assms \<open>m < n\<close> by (simp add: Setsum_Icc_nat mult.commute)
hoelzl@59712
    48
  finally show ?thesis .
hoelzl@59712
    49
next
hoelzl@59712
    50
  assume "\<not> m < n" with assms show ?thesis by simp
hoelzl@59712
    51
qed
hoelzl@59712
    52
hoelzl@59712
    53
lemma Chebyshev_sum_upper:
hoelzl@59712
    54
  fixes a b::"nat \<Rightarrow> 'a::linordered_idom"
hoelzl@59712
    55
  assumes "\<And>i j. i \<le> j \<Longrightarrow> j < n \<Longrightarrow> a i \<le> a j"
hoelzl@59712
    56
  assumes "\<And>i j. i \<le> j \<Longrightarrow> j < n \<Longrightarrow> b i \<ge> b j"
hoelzl@59712
    57
  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
    58
proof -
hoelzl@59712
    59
  let ?S = "(\<Sum>j=0..<n. (\<Sum>k=0..<n. (a j - a k) * (b j - b k)))"
hoelzl@59712
    60
  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"
wenzelm@63170
    61
    by (simp only: one_add_one[symmetric] algebra_simps)
wenzelm@63170
    62
      (simp add: algebra_simps setsum_subtractf setsum.distrib setsum.commute[of "\<lambda>i j. a i * b j"] setsum_right_distrib)
hoelzl@59712
    63
  also
hoelzl@59712
    64
  { fix i j::nat assume "i<n" "j<n"
hoelzl@59712
    65
    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
    66
      using assms by (cases "i \<le> j") (auto simp: algebra_simps)
haftmann@62348
    67
  } then have "?S \<le> 0"
hoelzl@59712
    68
    by (auto intro!: setsum_nonpos simp: mult_le_0_iff)
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]
lp15@61609
    78
by (simp del: of_nat_mult of_nat_setsum  add: of_nat_mult[symmetric] of_nat_setsum[symmetric])
hoelzl@59712
    79
hoelzl@59712
    80
end