/
Rzeta2.v
1883 lines (1605 loc) · 50.2 KB
/
Rzeta2.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
(C) Copyright 2010, COQTAIL team
Project Info: http://sourceforge.net/projects/coqtail/
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
*)
Require Import Reals.
Require Import Lra.
Require Import Rsequence_facts.
Require Export Rseries.
Require Import Rsequence_subsequence.
Require Import Rtactic.
Require Import Lia.
Open Scope R_scope.
(* begin hide *)
Ltac solve_with_eq a b := let H := fresh in
assert (H : a = b); [ | rewrite H; try reflexivity].
Ltac inject := match goal with |- ?a = ?b => recinject (a = b) end
with recinject t := match t with
| ?a = ?a => idtac
| ?a ?b = ?a ?c => recinject (b = c)
| ?b ?a = ?c ?a => recinject (b = c)
| ?b = ?c => solve_with_eq b c
end.
(* end hide *)
(* Better S_INR *)
Lemma plus_1_S : forall a, INR (S a) = 1 + (INR a).
Proof.
intros.
INR_solve.
Qed.
(** * Convergence of the 1/n² series *)
Definition Rseq_square_inv n := / (INR n) ^ 2.
Definition Rseq_square_inv_s n := / (INR (S n)) ^ 2.
(** * Splitting series in odd and even terms *)
Definition odds Un : Rseq := fun n => Un (S (2 * n)).
Definition evens Un : Rseq := fun n => Un (mult 2 n).
(** Convergence of splitting *)
Lemma Rser_cv_pair_compat : forall Un l, Rser_cv Un l -> Rser_cv (fun n => Un (mult 2 n) + Un (S (mult 2 n))) l.
Proof.
intros Un l Ucv.
intros e epos.
destruct (Ucv e epos) as [N Hu].
exists N; intros n nN.
replace (Rseq_sum (fun n0 : nat => Un (2 * n0)%nat + Un (S (2 * n0))) n)
with (Rseq_sum Un (S (2 * n))).
intros; apply Hu; lia.
clear nN.
induction n.
intros; simpl; ring.
simpl Rseq_sum.
replace (n + S (n + 0))%nat with (S (2 * n)) by ring.
rewrite IHn.
ring_simplify.
assert (SIMPL : forall a b c d, a = c -> a + b + d = b + d + c) by (intros; subst; ring).
apply SIMPL.
apply Rseq_sum_ext; intro.
simpl; trivial.
Qed.
(** Finite sum splitting *)
Lemma sum_odd_even_split : forall an n, sum_f_R0 (odds an) n =
sum_f_R0 an (S (2 * n)) - sum_f_R0 (evens an) n.
Proof.
intros an n.
induction n.
simpl; unfold odds, evens; simpl; ring.
simpl.
rewrite IHn.
replace (n + S (n + 0))%nat with (S (2 * n)) by ring.
unfold odds, evens.
simpl.
replace (S (S (n + S (n + 0)))) with (S (2 * S n)) by ring.
replace (S (S (n + (n + 0)))) with (2 * S n)%nat by ring.
replace (S (n + S (n + 0))) with (2 * S n)%nat by ring.
ring.
Qed.
(** Substracting odd terms *)
Lemma remove_odds : forall Un l, {lu | Rser_cv Un lu} ->
Rser_cv (evens Un) l -> Rser_cv (Un - (odds Un)) l.
Proof.
intros Un lo [lu Hu] Ho.
replace lo with (lu - (lu - lo)) by ring.
apply Rser_cv_minus_compat; try assumption.
apply Rseq_cv_eq_compat with ((fun n => sum_f_R0 Un (S (2 * n))) - sum_f_R0 (evens Un))%Rseq.
intro n; rewrite (sum_odd_even_split); trivial.
apply Rseq_cv_minus_compat.
eapply Rseq_subseq_cv_compat; [|apply Hu].
assert (Hex : is_extractor (fun i => S (2 * i))).
intros n; lia.
exists (exist _ _ Hex).
reflexivity.
assumption.
Qed.
(** Substracting even terms *)
Lemma remove_evens : forall Un l, {lu | Rser_cv Un lu} ->
Rser_cv (odds Un) l -> Rser_cv (Un - evens Un) l.
Proof.
intros Un lo [lu Hu] Ho.
replace lo with (lu - (lu - lo)) by ring.
apply Rser_cv_minus_compat; try assumption.
apply Rseq_cv_eq_compat with ((fun n => sum_f_R0 Un (S (2 * n))) - sum_f_R0 (odds Un))%Rseq.
intro n. symmetry.
assert (REW : forall a b c, a - b = c -> a - c = b) by (intros; subst; ring); apply REW.
rewrite (sum_odd_even_split); trivial.
apply Rseq_cv_minus_compat.
eapply Rseq_subseq_cv_compat; [|apply Hu].
assert (Hex : is_extractor (fun i => S (2 * i))).
intros n; lia.
exists (exist _ _ Hex).
reflexivity.
assumption.
Qed.
(** * Introduction of pi *)
Definition antg : nat -> R := fun n => (- 1)^n / (2 * (INR n) + 1).
Definition antg_neg : nat -> R := fun n => (- 1)^n / (- 2 * (INR n) + 1).
Lemma PI_tg_PI : Rseq_cv (sum_f_R0 (tg_alt PI_tg)) (PI / 4).
Proof.
generalize PI_ineq, exist_PI.
generalize (sum_f_R0 (tg_alt PI_tg)) as u.
generalize (PI / 4) as x.
intros x u Hu [y Hy].
rewrite <-Rseq_cv_Un_cv_equiv in Hy.
change (fun N : nat => u N) with u in Hy.
pose proof Rseq_sandwich_theorem.
assert (uy : Rseq_cv (fun N : nat => u (2 * N)%nat) y).
{ eapply Rseq_subseq_cv_compat; eauto.
apply subsequence_helper with (mult 2). lia. reflexivity. }
assert (uy' : Rseq_cv (fun N : nat => u (S (2 * N))%nat) y).
{ eapply Rseq_subseq_cv_compat; [ | apply Hy].
apply subsequence_helper with (fun n => S (2 * n)). lia. reflexivity. }
pose proof Rseq_sandwich_theorem _ _ _ y uy' uy Hu as xy.
pose proof Rseq_constant_cv x as xx.
pose proof Rseq_cv_unique _ _ _ xx xy.
now subst.
Qed.
Lemma Sum_antg : Rser_cv antg (PI / 4).
Proof.
unfold Rser_cv.
eapply Rseq_cv_eq_compat with (sum_f_R0 (tg_alt PI_tg)). 2: apply PI_tg_PI.
apply Rseq_sum_ext.
intros n.
unfold tg_alt, PI_tg, antg.
INR_group (2 * INR n + 1).
field.
INR_solve.
Qed.
Lemma antg_shift_neg_compat : Rseq_shift antg_neg == antg.
Proof.
intro n.
unfold Rseq_shift.
unfold antg.
unfold antg_neg.
rewrite S_INR.
simpl pow.
field; split;
replace (-2) with (-(2%R)) by reflexivity;
replace 2 with (INR 2) by reflexivity;
replace 1 with (INR 1) by reflexivity.
INR_solve.
intro Hinv.
rewrite Ropp_mult_distr_l_reverse in Hinv.
pose proof (Rplus_opp_r_uniq _ _ Hinv) as H.
rewrite Ropp_involutive in H.
rewrite <- plus_INR in H.
rewrite <- mult_INR in H.
generalize dependent H.
apply not_INR.
lia.
Qed.
Lemma Sum_antg_neg : Rser_cv antg_neg (PI / 4 + 1).
Proof.
apply Rser_cv_shift_rev2.
eapply Rser_cv_ext.
apply antg_shift_neg_compat.
replace (PI / 4 + 1 - antg_neg 0) with (PI / 4) by (compute; field).
apply Sum_antg.
Qed.
Definition bntg n := / (2 * (INR n) + 1) ^ 2.
Definition bntg_neg n := / (- 2 * (INR n) + 1) ^ 2.
Lemma bntg_pos : forall n, 0 < bntg n.
Proof.
intro n.
unfold bntg.
INR_solve.
Qed.
Lemma odd_not_zero : forall n, 2 * (INR n) + 1 <> 0.
Proof.
intros.
INR_solve.
Qed.
Lemma neg_odd_not_zero : forall n, 2 * (INR n) - 1 <> 0.
Proof.
intros.
destruct n.
simpl.
replace (2 * 0 - 1) with (- 1) by field.
apply Ropp_neq_0_compat; apply R1_neq_R0.
INR_solve.
Qed.
Lemma bntg_neg_simpl : forall n,
1 / (- 2 * (INR n) + 1) ^ 2 = 1 / (2 * (INR n) - 1) ^ 2.
Proof.
intros; field.
split.
apply neg_odd_not_zero.
replace (-2 * INR n + 1) with (- (2 * (INR n) - 1)) by field.
apply Ropp_neq_0_compat; apply neg_odd_not_zero.
Qed.
Definition pi_tg2 (n : nat) := 2 / ((4 * (INR n) + 1) * (4 * (INR n) + 3)).
Lemma pi_tg2_corresp : forall n,
pi_tg2 n = tg_alt PI_tg (2 * n) + tg_alt PI_tg (S (2 * n)).
Proof.
intros.
unfold tg_alt, PI_tg, pi_tg2.
rewrite pow_1_odd.
rewrite pow_1_even.
assert (R1 : INR ((2 * (2 * n) + 1)%nat) = 2 * (2 * (INR n)) + 1) by INR_solve.
assert (R2 : INR ((2 * S (2 * n) + 1)%nat) = 2 * (1 + (2 * (INR n))) + 1) by INR_solve.
rewrite R1.
rewrite R2.
field.
split;
[replace (1 + 2 * (INR n)) with (INR (1 + 2 * n)); [apply (odd_not_zero) | rewrite plus_INR]
|replace (2 * (INR n)) with (INR (2 * n)); [apply odd_not_zero | ]];
rewrite mult_INR; trivial.
Qed.
Lemma pi_tg2_cv : { l | Rser_cv pi_tg2 l }.
Proof.
destruct (exist_PI) as [PI_4 Hc].
exists PI_4.
apply Rser_cv_ext with (fun n => tg_alt PI_tg (2 * n) + tg_alt PI_tg (S (2 * n))).
intro n; apply pi_tg2_corresp.
apply Rser_cv_pair_compat.
intros eps epspos.
destruct (Hc eps epspos) as [N H].
exists N.
intros n nN.
apply (H n nN).
Qed.
Lemma Rser_cv_bntg : {l | Rser_cv bntg l}.
Proof.
eapply Rser_pos_maj_cv.
intro; apply Rlt_le; apply bntg_pos.
3: apply Rser_cv_square_inv.
intro; apply Rlt_le; apply Rinv_0_lt_compat; apply pow_lt; INR_solve.
intro n; apply Rle_Rinv; unfold Rseq_shift ; try (apply pow_lt; INR_solve).
unfold pow; INR_solve; try (apply Nat.mul_le_mono; lia).
Qed.
(** * Sums indexed by relative integers (from -N to N) *)
Require Import ZArith.
Fixpoint bisum (f : Z -> R) (N : nat) := match N with
| O => f Z0
| S n => (bisum f n) + f (Z_of_nat N) + f (- Z_of_nat N)%Z
end.
(** - 1 to a relative integer Z *)
Definition pow1_P p := match p with xO _ => 1 | _ => -1 end.
Definition pow1 z := match z with
| Z0 => 1
| Zpos n | Zneg n => pow1_P n
end.
Lemma pow1_P_ind : forall p, pow1_P (Pos.succ (Pos.succ p)) = pow1_P p.
Proof.
destruct p; trivial.
Qed.
Lemma nat_ind2 : forall (P : nat -> Prop),
P O -> P (S O) -> (forall m, P m -> P (S (S m))) -> forall n, P n.
Proof.
intros P H0 H1 H n.
assert (P n /\ P (S n)).
induction n; split; try assumption; [ | apply H]; apply IHn.
apply H2.
Qed.
Lemma pow1_nat : forall n, pow1 (Z_of_nat n) = (- 1) ^ n.
Proof.
intros n.
destruct n.
trivial.
unfold Z_of_nat.
simpl pow1.
generalize dependent n; apply nat_ind2; try (simpl; trivial; ring).
intros n H; simpl.
rewrite pow1_P_ind.
rewrite H.
simpl pow; field.
Qed.
Lemma pow1_nat_neg : forall n, pow1 (- Z_of_nat n) = (- 1) ^ n.
Proof.
intros n.
destruct n.
trivial.
unfold Z_of_nat.
simpl pow1.
generalize dependent n; apply nat_ind2; try (simpl; trivial; ring).
intros n H; simpl.
rewrite pow1_P_ind.
rewrite H.
simpl pow; field.
Qed.
Lemma pow1_squared : forall z, (pow1 z) ^ 2 = 1.
Proof.
destruct z; [simpl; ring | | ]; unfold pow1;
destruct p;
simpl; ring.
Qed.
Lemma pow1_Rabs : forall z, Rabs (pow1 z) = 1.
Proof.
destruct z; try destruct p; simpl; change (-1) with (-(1%R)); try rewrite Rabs_Ropp; apply Rabs_R1.
Qed.
Lemma pow1_P_plus : forall a b, pow1_P (a + b) = pow1_P a * pow1_P b.
Proof.
intros a b.
destruct a; destruct b; simpl; ring.
Qed.
Lemma pow1_succ : forall z, pow1 (Z.succ z) = - pow1 z.
Proof.
destruct z; trivial;
destruct p; simpl; try ring;
destruct p; simpl; ring.
Qed.
Lemma pow1_plus_nat : forall a b, pow1 (a + Z_of_nat b) = (pow1 a) * (-1) ^ b.
Proof.
intros.
induction b.
simpl; ring_simplify; inject; lia.
rewrite inj_S; rewrite <- Zplus_succ_r_reverse; rewrite pow1_succ.
rewrite IHb.
simpl; ring.
Qed.
(** Operations on bisums *)
Definition zr (op : R -> R) (f : Z -> R) z := op (f z).
Definition zr2 (op : R -> R -> R) (f g : Z -> R) z := op (f z) (g z).
Definition zr22 (op : R -> R -> R) (f g : Z -> Z -> R) x y := op (f x y) (g x y).
Lemma bisum_eq_compat : forall f g n, (forall z, f z = g z) -> bisum f n = bisum g n.
Proof.
intros.
induction n.
simpl; apply H.
simpl; do 2 rewrite H.
rewrite IHn; trivial.
Qed.
Lemma bisum_plus : forall f g n, bisum (zr2 Rplus f g) n = bisum f n + bisum g n.
Proof.
induction n.
trivial.
simpl bisum; rewrite IHn.
unfold zr2; ring.
Qed.
Lemma bisum_minus : forall f g n, bisum (zr2 Rminus f g) n = bisum f n - bisum g n.
Proof.
induction n.
trivial.
simpl bisum; rewrite IHn.
unfold zr2; ring.
Qed.
Lemma bisum_scal_mult : forall f a n, bisum (zr (Rmult a) f) n = a * (bisum f n).
Proof.
induction n.
trivial.
simpl bisum; rewrite IHn.
unfold zr; ring.
Qed.
Lemma bisum_mult : forall f g n m, (bisum f n) * (bisum g m) =
bisum (fun i => bisum (fun j => f i * g j) m) n.
Proof.
induction n.
simpl; intro.
replace (fun j : Z => f 0%Z * g j) with (zr (Rmult (f 0%Z)) g) by trivial.
rewrite bisum_scal_mult; trivial.
intros m.
simpl bisum.
repeat rewrite Rmult_plus_distr_r.
rewrite IHn.
replace (fun j : Z => f (Zpos (P_of_succ_nat n)) * g j) with (zr (Rmult (f (Zpos (P_of_succ_nat n)))) g) by trivial.
replace (fun j : Z => f (Zneg (P_of_succ_nat n)) * g j) with (zr (Rmult (f (Zneg (P_of_succ_nat n)))) g) by trivial.
repeat rewrite bisum_scal_mult.
trivial.
Qed.
(** Reversing terms *)
Lemma bisum_reverse : forall f n, bisum f n = bisum (fun i => f (- i)%Z) n.
Proof.
induction n.
trivial.
simpl.
rewrite IHn.
ring.
Qed.
(** Rewriting a bisum as sums *)
Lemma sum_bisum : forall n f, bisum f (S n) =
sum_f_R0 (fun i => f (Z_of_nat i)) (S n) + sum_f_R0 (fun i => f (- Z_of_nat (S i))%Z) n.
Proof.
intros.
induction n.
trivial.
replace (bisum f (S (S n))) with (bisum f (S n) + f (Zpos (Pos.succ (P_of_succ_nat n))) +
f (Zneg (Pos.succ (P_of_succ_nat n)))) by trivial.
rewrite IHn.
simpl.
ring.
Qed.
(** * Introducing pi to bisums *)
Definition anz z := (pow1 z) / (2 * (IZR z) + 1).
Definition bnz z := / (2 * (IZR z) + 1) ^ 2.
Definition An := bisum anz.
Definition Bn := bisum bnz.
Lemma anz_antg : forall n, antg n = anz (Z_of_nat n).
Proof.
intro n.
unfold antg, anz.
destruct n.
simpl; field.
rewrite pow1_nat.
rewrite <- INR_IZR_INZ.
trivial.
Qed.
Lemma anz_antg_neg : forall n, antg_neg n = anz (- Z_of_nat n).
Proof.
intro n.
unfold antg_neg, anz.
destruct n.
simpl; field.
rewrite pow1_nat_neg.
rewrite Ropp_Ropp_IZR.
rewrite <- INR_IZR_INZ.
field.
rewrite plus_1_S.
replace (2 * - (1 + INR n) + 1) with (- (1 + 2 * INR n)) by ring.
apply Ropp_neq_0_compat.
repeat INR_solve.
Qed.
Lemma bisum_anz_antg : (sum_f_R0 antg + (sum_f_R0 antg_neg - 1))%Rseq == bisum anz.
Proof.
intro n.
induction n.
compute; field.
simpl bisum.
rewrite <- IHn.
unfold Rseq_minus.
unfold Rseq_plus.
unfold Rseq_constant.
repeat rewrite tech5.
repeat rewrite anz_antg_neg.
repeat rewrite anz_antg.
replace (Zneg (P_of_succ_nat n)) with (- Z_of_nat (S n))%Z by trivial.
replace (Zpos (P_of_succ_nat n)) with (Z_of_nat (S n))%Z by trivial.
ring.
Qed.
Lemma An_cv : Rseq_cv An (PI / 2).
Proof.
replace (PI / 2) with (PI / 4 + (PI / 4 + 1 - 1)) by field.
eapply Rseq_cv_eq_compat.
2: apply Rseq_cv_plus_compat.
2: apply Sum_antg.
2: eapply Rseq_cv_eq_compat.
3: apply Rseq_cv_minus_compat.
3: apply Sum_antg_neg.
3: apply Rseq_constant_cv.
symmetry. apply bisum_anz_antg.
intro; trivial.
Qed.
Lemma An_squared_cv : Rseq_cv (An * An) (PI ^ 2 / 4).
Proof.
replace (PI ^ 2 / 4) with ((PI / 2) * (PI / 2)) by field.
apply Rseq_cv_mult_compat; apply An_cv.
Qed.
(** * Double sums *)
Definition bisumsum f N := bisum (fun i => (bisum (f i) N)) N.
(** Double sum minus its diagonal *)
Definition bisum_strip f j N := bisum f N - (f j).
Definition bisumsum_strip_diag f N := bisumsum f N - bisum (fun i => (f i i)) N.
(** Double sum in which its diagonal terms are null *)
Definition bisum_strip' f j N := bisum (fun i => if Z.eq_dec i j then 0 else f i) N.
Definition bisumsum_strip_diag' f N := bisumsum (fun i j => if Z.eq_dec i j then 0 else f i j) N.
(** Weak extensional equality *)
Lemma bisumsum_eq_compat : forall f g n, (forall x y, f x y = g x y) ->
bisumsum f n = bisumsum g n.
Proof.
intros; apply bisum_eq_compat.
intros; apply bisum_eq_compat.
apply H.
Qed.
(** Bounded extensional equality *)
Lemma bisum_eq_compat_bounded : forall f g n, (forall z, ((-Z_of_nat n) <= z <= Z_of_nat n)%Z -> f z = g z) ->
bisum f n = bisum g n.
Proof.
intros.
induction n.
simpl; apply H; simpl; lia.
simpl.
rewrite H; [ | simpl; split; [apply Zle_neg_pos | lia]].
rewrite H; [ | simpl; split; [lia | apply Zle_neg_pos]].
rewrite IHn.
trivial.
intros z [H1 H2].
apply H; split; eapply Z.le_trans; [ | apply H1 | apply H2 | ]; rewrite inj_S; lia.
Qed.
(** Double sum distributivity *)
Lemma bisumsum_square : forall f n, bisumsum (fun i j => f i * f j) n = bisum f n * bisum f n.
Proof.
intros.
rewrite bisum_mult.
trivial.
Qed.
(** Inequalities *)
Lemma Psucc_lt : forall p, (Zpos p < Zpos (Pos.succ p))%Z.
Proof.
intros.
replace (Zpos (Pos.succ p)) with (1 + Zpos p)%Z.
lia.
induction p; trivial.
Qed.
Lemma Psucc_lt_neg : forall p, (Zneg (Pos.succ p) < Zneg p)%Z.
Proof.
intros.
replace (Zneg (Pos.succ p)) with (- 1 + Zneg p)%Z.
lia.
induction p; trivial.
Qed.
(** A special term outside the bounds can be ignored *)
Lemma bisum_not_in : forall f g j n, (j < (- Z_of_nat n) \/ Z_of_nat n < j)%Z ->
bisum (fun i : Z => if Z.eq_dec i j then g j else f i) n = bisum f n.
Proof.
intros.
apply bisum_eq_compat_bounded; intros.
destruct H.
destruct (Z.eq_dec z (- Z_of_nat (S n))).
subst; destruct H0; rewrite inj_S in * |-; apply False_ind; lia.
destruct (Z.eq_dec z j).
subst; apply False_ind; lia.
trivial.
destruct (Z.eq_dec z (Z_of_nat (S n))).
subst; destruct H0; rewrite inj_S in * |-; apply False_ind; lia.
destruct (Z.eq_dec z j).
subst; apply False_ind; lia.
trivial.
Qed.
(** A special term between the bounds can be extracted *)
Lemma bisum_in : forall f g j n, (- Z_of_nat n <= j <= Z_of_nat n)%Z ->
bisum (fun i : Z => if Z.eq_dec i j then g j else f i) n = bisum f n - f j + g j.
Proof.
intros.
induction n.
assert (j = Z0) by (replace (Z_of_nat O) with Z0 in H by trivial; lia); subst; trivial.
simpl; ring.
set (h := fun i => if Z.eq_dec i j then g j else f i).
simpl bisum; unfold h in *.
destruct (Z.eq_dec (Zpos (P_of_succ_nat n)) j) as [e|e];
destruct (Z.eq_dec (Zneg (P_of_succ_nat n)) j) as [e'|e']; subst; try inversion e'.
rewrite bisum_not_in.
ring.
right; destruct n; simpl.
lia.
apply Psucc_lt.
rewrite bisum_not_in.
ring.
left; destruct n; simpl.
lia.
apply Psucc_lt_neg.
rewrite IHn.
ring.
split; rewrite inj_S in H.
destruct (Z_le_gt_dec (- Z_of_nat n) j); trivial.
replace (Zneg (P_of_succ_nat n)) with (- Z_of_nat (S n))%Z in e' by trivial.
rewrite inj_S in *.
lia.
destruct (Z_le_gt_dec j (Z_of_nat n)); trivial.
replace (Zpos (P_of_succ_nat n)) with (Z_of_nat (S n))%Z in e by trivial.
rewrite inj_S in *.
lia.
Qed.
(** Stripping terms *)
Definition Zzero : Z -> R := fun _ => 0.
Lemma bisum_strip_equiv : forall f n j, ((- Z_of_nat n) <= j <= Z_of_nat n)%Z ->
bisum_strip f j n = bisum_strip' f j n.
Proof.
intros.
unfold bisum_strip, bisum_strip'.
replace
(bisum (fun i : Z => if Z.eq_dec i j then 0 else f i) n) with
(bisum (fun i : Z => if Z.eq_dec i j then Zzero j else f i) n).
rewrite bisum_in.
unfold Zzero; ring.
apply H.
unfold Zzero; trivial.
Qed.
Lemma bisum_strip_nothing : forall f n j, ((- Z_of_nat (S n)) = j \/ j = Z_of_nat (S n))%Z ->
bisum_strip' f j n = bisum f n.
Proof.
intros.
unfold bisum_strip'.
apply bisum_eq_compat_bounded; intros.
destruct H; subst.
destruct (Z.eq_dec z (- Z_of_nat (S n))).
subst; destruct H0; rewrite inj_S in * |- ; apply False_ind; lia.
trivial.
destruct (Z.eq_dec z (Z_of_nat (S n))).
subst; destruct H0; rewrite inj_S in * |- ; apply False_ind; lia.
trivial.
Qed.
(** Steps of calculus in bisums *)
Lemma bisum_one_step : forall f n, bisum f (S n) = bisum f n + f (Z_of_nat (S n)) + f (- Z_of_nat (S n))%Z.
Proof.
trivial.
Qed.
Lemma bisumsum_one_step : forall f n m,
bisum (fun i => bisum (f i) (S n)) m =
bisum (fun i => bisum (f i) n) m +
bisum (fun i => f i (Z_of_nat (S n)) ) m +
bisum (fun i => f i (- Z_of_nat (S n))%Z ) m.
intros.
simpl.
repeat rewrite <- bisum_plus.
apply bisum_eq_compat; intros.
trivial.
Qed.
(** Switching indices *)
Lemma bisum_eq_sym : forall f z n,
bisum (fun i : Z => if Z.eq_dec i z then 0 else f z i) n =
bisum (fun j : Z => if Z.eq_dec z j then 0 else f z j) n.
Proof.
intros; apply bisum_eq_compat; intros.
destruct (Z.eq_dec z0 z); destruct (Z.eq_dec z z0); try trivial; subst;
apply False_ind; apply n0; trivial.
Qed.
(** Substracting the diagonal terms sum makes them null in the main double sum *)
Lemma strip_diag : forall f n, bisumsum_strip_diag' f n = bisumsum_strip_diag f n.
Proof.
intros.
induction n.
unfold bisumsum_strip_diag', bisumsum_strip_diag, bisumsum.
simpl; ring.
unfold bisumsum_strip_diag', bisumsum_strip_diag, bisumsum.
rewrite bisumsum_one_step.
rewrite bisum_one_step.
unfold bisumsum_strip_diag', bisumsum_strip_diag, bisumsum in IHn.
rewrite IHn.
rewrite (bisum_one_step (fun i : Z => f i i)).
repeat rewrite bisum_one_step.
destruct (Z.eq_dec (Z_of_nat (S n)) (Z_of_nat (S n))); [ | apply False_ind; lia]; clear e.
destruct (Z.eq_dec (Z_of_nat (S n)) (- Z_of_nat (S n))); [ apply False_ind; inversion e | ]; clear n0.
destruct (Z.eq_dec (- Z_of_nat (S n)) (Z_of_nat (S n))); [ apply False_ind; inversion e | ]; clear n0.
destruct (Z.eq_dec (- Z_of_nat (S n)) (- Z_of_nat (S n))); [ | apply False_ind; lia]; clear e.
ring_simplify.
assert (H : forall BII FNP FPN A B C D BIJ BIJS BPJS BNJS,
BIJ + A + B + C + D = BIJS + BPJS + BNJS ->
BIJ - BII + A + B + C + FNP + D + FPN = - BII + FNP + FPN + BIJS + BPJS + BNJS)
by (intros; assert (H' : BIJ = - (A + B + C + D) + (BIJS + BPJS + BNJS)) by (rewrite <- H; ring); rewrite H'; ring).
erewrite H; [reflexivity | ].
replace (bisum (fun j : Z => if Z.eq_dec (Z_of_nat (S n)) j then 0 else f (Z_of_nat (S n)) j) n)
with (bisum_strip' (f (Z_of_nat (S n))) (Z_of_nat (S n)) n) by apply bisum_eq_sym.
replace (bisum (fun j : Z => if Z.eq_dec (- Z_of_nat (S n)) j then 0 else f (- Z_of_nat (S n))%Z j) n)
with (bisum_strip' (f (- Z_of_nat (S n)))%Z (- Z_of_nat (S n)) n)%Z by apply bisum_eq_sym.
replace (bisum (fun i : Z => if Z.eq_dec i (- Z_of_nat (S n)) then 0 else f i (- Z_of_nat (S n))%Z) n)
with (bisum_strip' (fun i => f i (- Z_of_nat (S n)))%Z (- Z_of_nat (S n)) n)%Z by trivial.
replace (bisum (fun i : Z => if Z.eq_dec i (Z_of_nat (S n)) then 0 else f i (Z_of_nat (S n))) n)
with (bisum_strip' (fun i => f i (Z_of_nat (S n)))%Z (Z_of_nat (S n)) n)%Z by trivial.
repeat rewrite bisum_strip_nothing; [ | left | right | left | right ]; trivial.
rewrite bisumsum_one_step.
ring.
Qed.
(** * Rewriting double sums *)
(** Switching indices in a double sum *)
Lemma bisumsum_switch_index : forall f n, bisumsum (fun i j => f j i) n = bisumsum f n.
Proof.
intros.
induction n.
trivial.
unfold bisumsum in *.
rewrite bisumsum_one_step.
rewrite bisumsum_one_step.
simpl.
rewrite IHn.
replace (bisum (f (Zneg (P_of_succ_nat n))) n) with (bisum (fun i => f (Zneg (P_of_succ_nat n)) i) n) by (apply bisum_eq_compat; trivial).
replace (bisum (f (Zpos (P_of_succ_nat n))) n) with (bisum (fun i => f (Zpos (P_of_succ_nat n)) i) n) by (apply bisum_eq_compat; trivial).
ring.
Qed.
(** Switching indices in a double sum (where diagonal terms are null) *)
Lemma bisumsum_strip_diag'_switch_index : forall f n,
bisumsum_strip_diag' (fun i j => f j i) n =
bisumsum_strip_diag' f n.
Proof.
intros.
unfold bisumsum_strip_diag'.
rewrite bisumsum_switch_index.
apply bisumsum_eq_compat; intros.
destruct (Z.eq_dec y x); destruct (Z.eq_dec x y); try trivial;
subst; apply False_ind; apply n0; trivial.
Qed.
(** Adding double sums *)
Lemma bisumsum_strip_diag'_plus : forall f g n,
bisumsum_strip_diag' (fun i j => f i j + g i j) n =
bisumsum_strip_diag' f n +
bisumsum_strip_diag' g n.
Proof.
intros.
unfold bisumsum_strip_diag'.
unfold bisumsum.
rewrite <- bisum_plus.
apply bisum_eq_compat; intros.
unfold zr2.
rewrite <- bisum_plus.
apply bisum_eq_compat; intros.
unfold zr2.
destruct (Z.eq_dec z z0); ring.
Qed.
(** Switching indices of only one term in a sum in a double sum *)
Lemma bisumsum_plus_switch : forall f g n,
bisumsum_strip_diag' (fun i j => f i j + g i j) n =
bisumsum_strip_diag' (fun i j => f i j + g j i) n.
Proof.
intros.
rewrite bisumsum_strip_diag'_plus.
rewrite <- bisumsum_strip_diag'_switch_index with g n.
rewrite <- bisumsum_strip_diag'_plus.
trivial.
Qed.
(** Extensional equality but on the diagonal terms *)
Lemma bisumsum_strip_diag'_eq_but_diag_compat : forall f g n,
(forall i j, i <> j -> f i j = g i j) ->
bisumsum_strip_diag' f n = bisumsum_strip_diag' g n.
Proof.
intros.
apply bisum_eq_compat; intros.
apply bisum_eq_compat; intros.
destruct (Z.eq_dec z z0).
trivial.
apply (H _ _ n0).
Qed.
(** Shifting a sequence (with an integer) *)
Fixpoint sum1 u n := match n with
| O => 0
| S n' => (sum1 u n') + u (Z_of_nat n)
end.
Lemma sum_f_R0_sum1 : forall u n, sum1 u (S n) =
sum_f_R0 (fun i => u (Z_of_nat (S i))) n.
Proof.
intros.
induction n.
compute; ring.
simpl in *.
rewrite IHn.
trivial.
Qed.
Definition shiftp (u:Z->R) (p:nat) (i:Z) := u (i + (Z_of_nat p))%Z.
Lemma bisum_shifting_S : forall u a b,
bisum (shiftp u (S b)) (S a) =
bisum (shiftp u b) a +
u (Z_of_nat (S (a + b))) +
u (Z_of_nat (S (S (a + b))))
.
Proof.
intros.
induction a.
unfold bisum, shiftp.
assert (AP : forall a b c a' b' c', a = a' -> b = b' -> c = c' ->
a + b + c = c' + a' + b') by (intros; subst; ring);
apply AP; clear AP;
inject;
simpl (0 + b)%nat; simpl (Z_of_nat 1); repeat rewrite inj_S; lia.
rewrite bisum_one_step.
rewrite IHa; clear IHa.
rewrite bisum_one_step.
repeat rewrite Rplus_assoc.
inject.
unfold shiftp.
repeat rewrite <- Rplus_assoc.
assert (AP : forall a b c d a' b' c' d', a = a' -> b = b' -> c = c' -> d = d' ->
a + b + c + d = a' + d' + b' + c') by (intros; subst; ring);
apply AP; clear AP;
inject;
try (simpl (0 + b)%nat; simpl (Z_of_nat 1); repeat rewrite inj_S; lia);
repeat rewrite inj_S; rewrite inj_plus; repeat rewrite inj_S; lia.
Qed.
Lemma bisum_shifting : forall u n p,
bisum (shiftp u p) (n + p) =
bisum u n + sum1 (shiftp u n) (2 * p)
.
Proof.
intros.
induction p.
rewrite bisum_eq_compat with _ u _;
[| intro; unfold shiftp; inject; simpl Z_of_nat; lia].
simpl.
ring_simplify; inject; lia.
rewrite <- plus_n_Sm.
replace (mult 2 (S p)) with (S (S (2 * p))) by lia.
replace (sum1 (shiftp u n) (S (S (2 * p))))
with (sum1 (shiftp u n) (2 * p) +
(shiftp u n) (Z_of_nat (S (2 * p))) +
(shiftp u n) (Z_of_nat (S (S (2 * p)))))
by trivial.
repeat rewrite <- Rplus_assoc.
rewrite <- IHp; clear IHp.
replace (bisum (shiftp u (S p)) (S (n + p)))
with (bisum (shiftp u p) (n + p) +
(shiftp u n) (Z_of_nat (S (2 * p))) +
(shiftp u n) (Z_of_nat (S (S (2 * p)))))
; [trivial | ].
rewrite bisum_shifting_S.
unfold shiftp.
replace (mult 2 p) with (plus p p) by lia.
assert (AP : forall a b c a' b' c', a = a' -> b = b' -> c = c' ->
a + b + c = a' + b' + c') by (intros; subst; ring);
apply AP; clear AP; trivial;
inject;
repeat (repeat rewrite inj_S; repeat rewrite inj_plus); lia.
Qed.
(** * Rewriting An * An - Bn *)
Definition d x := 2 * x + 1.
Definition d' z := d (IZR z).
Lemma splitmn : forall n m, d n <> 0 -> d m <> 0 -> m <> n ->
/ ((d n) * (d m)) = / (2 * (m - n)) * (/ (d n) - / (d m)).
Proof.
intros.
unfold d in *.
field.
split; [|split].
assumption.
assumption.
apply Rminus_eq_contra; assumption.
Qed.
Lemma d_not_null : forall z, d' z <> 0.
Proof.
intros.
unfold d', d.
discrR; lia.
Qed.
Lemma calc1 : forall N, (An N) * (An N) - Bn N =
bisumsum_strip_diag' (fun n m =>
(pow1 m * pow1 n) * / (2 * (IZR (m - n))) * (/ (d' n) - / (d' m))
) N.
Proof.
assert (forall z, 2 * IZR z + 1 <> 0) by (intro; discrR; lia).