1280 qed |
1280 qed |
1281 |
1281 |
1282 |
1282 |
1283 subsection {* The formula for arithmetic sums *} |
1283 subsection {* The formula for arithmetic sums *} |
1284 |
1284 |
1285 lemma gauss_sum: (* FIXME: rephrase in terms of "2" *) |
1285 lemma gauss_sum: |
1286 "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) = |
1286 "(2::'a::comm_semiring_1)*(\<Sum>i\<in>{1..n}. of_nat i) = |
1287 of_nat n*((of_nat n)+1)" |
1287 of_nat n*((of_nat n)+1)" |
1288 proof (induct n) |
1288 proof (induct n) |
1289 case 0 |
1289 case 0 |
1290 show ?case by simp |
1290 show ?case by simp |
1291 next |
1291 next |
1292 case (Suc n) |
1292 case (Suc n) |
1293 then show ?case by (simp add: algebra_simps del: one_add_one) (* FIXME *) |
1293 then show ?case |
|
1294 by (simp add: algebra_simps add: one_add_one [symmetric] del: one_add_one) |
|
1295 (* FIXME: make numeral cancellation simprocs work for semirings *) |
1294 qed |
1296 qed |
1295 |
1297 |
1296 theorem arith_series_general: |
1298 theorem arith_series_general: |
1297 "((1::'a::comm_semiring_1) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) = |
1299 "(2::'a::comm_semiring_1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) = |
1298 of_nat n * (a + (a + of_nat(n - 1)*d))" |
1300 of_nat n * (a + (a + of_nat(n - 1)*d))" |
1299 proof cases |
1301 proof cases |
1300 assume ngt1: "n > 1" |
1302 assume ngt1: "n > 1" |
1301 let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n" |
1303 let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n" |
1302 have |
1304 have |
1305 by (rule setsum_addf) |
1307 by (rule setsum_addf) |
1306 also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp |
1308 also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp |
1307 also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))" |
1309 also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))" |
1308 unfolding One_nat_def |
1310 unfolding One_nat_def |
1309 by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac) |
1311 by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac) |
1310 also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)" |
1312 also have "2*\<dots> = 2*?n*a + d*2*(\<Sum>i\<in>{1..<n}. ?I i)" |
1311 by (simp add: left_distrib right_distrib del: one_add_one) |
1313 by (simp add: algebra_simps) |
1312 also from ngt1 have "{1..<n} = {1..n - 1}" |
1314 also from ngt1 have "{1..<n} = {1..n - 1}" |
1313 by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost) |
1315 by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost) |
1314 also from ngt1 |
1316 also from ngt1 |
1315 have "(1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..n - 1}. ?I i) = ((1+1)*?n*a + d*?I (n - 1)*?I n)" |
1317 have "2*?n*a + d*2*(\<Sum>i\<in>{1..n - 1}. ?I i) = (2*?n*a + d*?I (n - 1)*?I n)" |
1316 by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def) |
1318 by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def) |
1317 (simp add: mult_ac trans [OF add_commute of_nat_Suc [symmetric]]) |
1319 (simp add: mult_ac trans [OF add_commute of_nat_Suc [symmetric]]) |
1318 finally show ?thesis by (simp add: algebra_simps del: one_add_one) |
1320 finally show ?thesis |
|
1321 unfolding mult_2 by (simp add: algebra_simps) |
1319 next |
1322 next |
1320 assume "\<not>(n > 1)" |
1323 assume "\<not>(n > 1)" |
1321 hence "n = 1 \<or> n = 0" by auto |
1324 hence "n = 1 \<or> n = 0" by auto |
1322 thus ?thesis by (auto simp: algebra_simps mult_2_right) |
1325 thus ?thesis by (auto simp: mult_2) |
1323 qed |
1326 qed |
1324 |
1327 |
1325 lemma arith_series_nat: |
1328 lemma arith_series_nat: |
1326 "Suc (Suc 0) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))" |
1329 "(2::nat) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))" |
1327 proof - |
1330 proof - |
1328 have |
1331 have |
1329 "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) = |
1332 "2 * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) = |
1330 of_nat(n) * (a + (a + of_nat(n - 1)*d))" |
1333 of_nat(n) * (a + (a + of_nat(n - 1)*d))" |
1331 by (rule arith_series_general) |
1334 by (rule arith_series_general) |
1332 thus ?thesis |
1335 thus ?thesis |
1333 unfolding One_nat_def by auto |
1336 unfolding One_nat_def by auto |
1334 qed |
1337 qed |
1335 |
1338 |
1336 lemma arith_series_int: |
1339 lemma arith_series_int: |
1337 "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) = |
1340 "2 * (\<Sum>i\<in>{..<n}. a + int i * d) = int n * (a + (a + int(n - 1)*d))" |
1338 of_nat n * (a + (a + of_nat(n - 1)*d))" |
1341 by (fact arith_series_general) (* FIXME: duplicate *) |
1339 proof - |
|
1340 have |
|
1341 "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) = |
|
1342 of_nat(n) * (a + (a + of_nat(n - 1)*d))" |
|
1343 by (rule arith_series_general) |
|
1344 thus ?thesis by simp |
|
1345 qed |
|
1346 |
1342 |
1347 lemma sum_diff_distrib: |
1343 lemma sum_diff_distrib: |
1348 fixes P::"nat\<Rightarrow>nat" |
1344 fixes P::"nat\<Rightarrow>nat" |
1349 shows |
1345 shows |
1350 "\<forall>x. Q x \<le> P x \<Longrightarrow> |
1346 "\<forall>x. Q x \<le> P x \<Longrightarrow> |