-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rpolynomial2.v
1658 lines (1524 loc) · 42.3 KB
/
Rpolynomial2.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
(* Fpolynomial.v *)
(* polynomials on a ring *)
Set Nested Proofs Allowed.
Require Import Utf8 Arith.
Import List ListNotations.
Require Import Misc Ring2 Rsummation.
(* definition of a polynomial *)
Definition rng_eqb {A} {rng : ring A} (a b : A) :=
if rng_eq_dec a b then true else false.
(* (lap : list as polynomial) *)
Record poly A {rng : ring A} := mkpoly
{ lap : list A;
lap_prop : rng_eqb (last lap 1%Rng) 0%Rng = false }.
Arguments lap {A} {rng}.
Theorem eq_poly_eq {A} {rng : ring A} : ∀ p1 p2, p1 = p2 ↔ lap p1 = lap p2.
Proof.
intros.
split; [ now intros; subst p1 | ].
intros Hll.
destruct p1 as (la, lapr).
destruct p2 as (lb, lbpr).
cbn in Hll; subst la; f_equal.
apply (Eqdep_dec.UIP_dec Bool.bool_dec).
Qed.
Theorem eq_poly_prop {A} {rng : ring A} : ∀ la,
rng_eqb (last la 1%Rng) 0%Rng = false ↔ last la 1%Rng ≠ 0%Rng.
Proof.
intros.
unfold rng_eqb.
now destruct (rng_eq_dec (last la 1%Rng) 0%Rng).
Qed.
Arguments mkpoly {_} {_}.
Theorem lap_1_0_prop {A} {rng : ring A} :
rng_eqb (last [] 1%Rng) 0%Rng = false.
Proof.
cbn.
unfold rng_eqb.
destruct (rng_eq_dec 1%Rng 0%Rng); [ | easy ].
now apply rng_1_neq_0 in e.
Qed.
Definition poly_zero {α} {r : ring α} := mkpoly [] lap_1_0_prop.
Definition poly_one {α} {r : ring α} := mkpoly [1%Rng] lap_1_0_prop.
(* normalization *)
Fixpoint strip_0s {A} {rng : ring A} la :=
match la with
| [] => []
| a :: la' => if rng_eq_dec a 0%Rng then strip_0s la' else la
end.
Lemma strip_0s_app {A} {rng : ring A} : ∀ la lb,
strip_0s (la ++ lb) =
match strip_0s la with
| [] => strip_0s lb
| lc => lc ++ lb
end.
Proof.
intros.
revert lb.
induction la as [| a]; intros; [ easy | cbn ].
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]; [ apply IHla | easy ].
Qed.
Definition lap_norm {A} {rng : ring A} la := rev (strip_0s (rev la)).
Theorem poly_norm_prop {A} {rng : ring A} : ∀ la,
last (lap_norm la) 1%Rng ≠ 0%Rng.
Proof.
intros.
unfold lap_norm.
induction la as [| a]; [ apply rng_1_neq_0 | cbn ].
rewrite strip_0s_app.
remember (strip_0s (rev la)) as lb eqn:Hlb; symmetry in Hlb.
destruct lb as [| b]; cbn. {
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]; [ apply rng_1_neq_0 | easy ].
}
cbn in IHla.
rewrite List_last_app.
now rewrite List_last_app in IHla.
Qed.
Definition poly_norm {A} {rng : ring A} la :=
mkpoly (lap_norm la) (proj2 (eq_poly_prop _) (poly_norm_prop la)).
(**)
Require Import ZArith.
Theorem Z_1_neq_0 : (1 ≠ 0)%Z.
Proof. easy. Qed.
Definition Z_ring : ring Z :=
{| rng_zero := 0%Z;
rng_one := 1%Z;
rng_add := Z.add;
rng_mul := Z.mul;
rng_opp := Z.opp;
rng_1_neq_0 := Z_1_neq_0;
rng_eq_dec := Z.eq_dec;
rng_add_comm := Z.add_comm;
rng_add_assoc := Z.add_assoc;
rng_add_0_l := Z.add_0_l;
rng_add_opp_l := Z.add_opp_diag_l;
rng_mul_comm := Z.mul_comm;
rng_mul_assoc := Z.mul_assoc;
rng_mul_1_l := Z.mul_1_l;
rng_mul_add_distr_l := Z.mul_add_distr_l |}.
(* allows to use ring theorems on Z *)
Canonical Structure Z_ring.
(*
Compute (@lap_norm Z Z_ring [3; 4; 0; 5; 0; 0; 0]%Z).
*)
(**)
(* addition *)
Fixpoint lap_add {α} {r : ring α} al1 al2 :=
match al1 with
| [] => al2
| a1 :: bl1 =>
match al2 with
| [] => al1
| a2 :: bl2 => (a1 + a2)%Rng :: lap_add bl1 bl2
end
end.
Definition lap_opp {α} {r : ring α} la := List.map rng_opp la.
Definition lap_sub {A} {rng : ring A} la lb := lap_add la (lap_opp lb).
Definition poly_add {A} {rng : ring A} p1 p2 :=
poly_norm (lap_add (lap p1) (lap p2)).
Definition poly_opp {α} {r : ring α} pol :=
poly_norm (lap_opp (lap pol)).
Definition poly_sub {α} {r : ring α} p1 p2 :=
poly_add p1 (poly_opp p2).
(*
Compute (@poly_add Z Z_ring (poly_norm [3;4;5]%Z) (poly_norm [2;3;-4;5]%Z)).
Compute (@poly_add Z Z_ring (poly_norm [3;4;5]%Z) (poly_norm [2;3;-5]%Z)).
*)
(* multiplication *)
Fixpoint lap_convol_mul {α} {r : ring α} al1 al2 i len :=
match len with
| O => []
| S len1 =>
(Σ (j = 0, i), List.nth j al1 0 * List.nth (i - j) al2 0)%Rng ::
lap_convol_mul al1 al2 (S i) len1
end.
Definition lap_mul {α} {R : ring α} la lb :=
match la with
| [] => []
| _ =>
match lb with
| [] => []
| _ => lap_convol_mul la lb 0 (length la + length lb - 1)
end
end.
Definition poly_mul {A} {rng : ring A} p1 p2 :=
poly_norm (lap_mul (lap p1) (lap p2)).
(*
Compute (@lap_mul Z Z_ring [3;4;5]%Z [2;3;-4;5]%Z).
Compute (@poly_mul Z Z_ring (poly_norm [3;4;5]%Z) (poly_norm [2;3;-4;5]%Z)).
*)
(* power *)
Fixpoint lap_power {α} {r : ring α} la n :=
match n with
| O => [1%Rng]
| S m => lap_mul la (lap_power la m)
end.
Definition poly_power {A} {rng : ring A} pol n :=
poly_norm (lap_power (lap pol) n).
(*
Compute (@poly_power Z Z_ring (poly_norm [1; -1]%Z) 4).
*)
(* composition *)
Definition lap_compose {α} {r : ring α} la lb :=
List.fold_right (λ c accu, lap_add (lap_mul accu lb) [c]) [] la.
Definition lap_compose2 {α} {r : ring α} la lb :=
List.fold_right
(λ i accu,
lap_add accu (lap_mul [List.nth i la 0] (lap_power lb i)))%Rng
[] (List.seq 0 (length la)).
(* *)
Fixpoint list_pad {α} n (zero : α) rem :=
match n with
| O => rem
| S n1 => zero :: list_pad n1 zero rem
end.
Theorem xpow_norm {A} {rng : ring A} : ∀ i,
rng_eqb (last (repeat 0%Rng i ++ [1%Rng]) 1%Rng) 0%Rng = false.
Proof.
intros.
rewrite List_last_app.
unfold rng_eqb.
destruct (rng_eq_dec 1 0) as [H| H]; [ | easy ].
now apply rng_1_neq_0 in H.
Qed.
Definition xpow {α} {r : ring α} i :=
mkpoly (repeat 0%Rng i ++ [1%Rng]) (xpow_norm i).
Declare Scope lap_scope.
Delimit Scope lap_scope with lap.
Notation "1" := [1%Rng] : lap_scope.
Notation "- a" := (lap_opp a) : lap_scope.
Notation "a + b" := (lap_add a b) : lap_scope.
Notation "a - b" := (lap_sub a b) : lap_scope.
Notation "a * b" := (lap_mul a b) : lap_scope.
Notation "a ^ b" := (lap_power a b) : lap_scope.
Definition list_nth_def_0 {α} {R : ring α} n l := List.nth n l 0%Rng.
Declare Scope poly_scope.
Delimit Scope poly_scope with pol.
Notation "0" := poly_zero : poly_scope.
Notation "1" := poly_one : poly_scope.
Notation "- a" := (poly_opp a) : poly_scope.
Notation "a + b" := (poly_add a b) : poly_scope.
Notation "a - b" := (poly_sub a b) : poly_scope.
Notation "a * b" := (poly_mul a b) : poly_scope.
Notation "'ⓧ' ^ a" := (xpow a) (at level 30, format "'ⓧ' ^ a") : poly_scope.
Notation "'ⓧ'" := (xpow 1) (at level 30, format "'ⓧ'") : poly_scope.
(* *)
Theorem lap_convol_mul_comm : ∀ α (R : ring α) l1 l2 i len,
lap_convol_mul l1 l2 i len = lap_convol_mul l2 l1 i len.
Proof.
intros α R l1 l2 i len.
revert i.
induction len; intros; [ reflexivity | simpl ].
rewrite IHlen; f_equal.
rewrite summation_rtl.
apply summation_compat; intros j (_, Hj).
rewrite Nat.add_0_r.
rewrite rng_mul_comm.
apply rng_mul_compat; [ idtac | reflexivity ].
rewrite Nat_sub_sub_distr; [ idtac | easy ].
rewrite Nat.sub_diag, Nat.add_0_l; reflexivity.
Qed.
Theorem lap_convol_mul_nil_l : ∀ α (R : ring α) l i len,
lap_norm (lap_convol_mul [] l i len) = [].
Proof.
intros α R l i len.
unfold lap_norm.
revert i.
induction len; intros; [ reflexivity | ].
cbn - [ summation ].
rewrite all_0_summation_0. {
rewrite strip_0s_app; cbn.
specialize (IHlen (S i)).
apply List_eq_rev_nil in IHlen.
rewrite IHlen.
now destruct (rng_eq_dec 0%Rng 0%Rng).
}
intros k (_, Hk).
now rewrite match_id, rng_mul_0_l.
Qed.
Theorem lap_convol_mul_nil_r : ∀ α (R : ring α) l i len,
lap_norm (lap_convol_mul l [] i len) = [].
Proof.
intros α R l i len.
rewrite lap_convol_mul_comm.
apply lap_convol_mul_nil_l.
Qed.
Theorem list_nth_lap_eq : ∀ α (r : ring α) la lb,
(∀ i, (List.nth i la 0 = List.nth i lb 0)%Rng)
→ lap_norm la = lap_norm lb.
Proof.
intros α r la lb Hi.
unfold lap_norm; f_equal.
revert lb Hi.
induction la as [| a]; intros. {
induction lb as [| b]; [ reflexivity | ].
specialize (Hi 0) as H; cbn in H.
subst b; cbn.
rewrite strip_0s_app; cbn.
remember (strip_0s (rev lb)) as lc eqn:Hlc; symmetry in Hlc.
destruct lc as [| c]; [ now destruct (rng_eq_dec _ _) | ].
assert (H : lap_norm [] = lap_norm lb). {
unfold lap_norm; cbn.
cbn in IHlb.
change (rev [] = rev (strip_0s (rev lb))).
f_equal.
rewrite Hlc.
apply IHlb.
intros i; cbn; rewrite match_id.
now specialize (Hi (S i)); cbn in Hi.
}
cbn in H.
unfold lap_norm in H.
rewrite Hlc in H.
symmetry in H.
now apply List_eq_rev_nil in H.
} {
cbn.
rewrite strip_0s_app.
remember (strip_0s (rev la)) as lc eqn:Hlc; symmetry in Hlc.
destruct lc as [| c]. {
assert (Hla : ∀ i, nth i la 0%Rng = 0%Rng). {
intros i.
clear - Hlc.
revert i.
induction la as [| a]; intros; [ now cbn; rewrite match_id | cbn ].
destruct i. {
cbn in Hlc.
rewrite strip_0s_app in Hlc; cbn in Hlc.
remember (strip_0s (rev la)) as lb eqn:Hlb; symmetry in Hlb.
destruct lb as [| b]; [ now destruct (rng_eq_dec a 0%Rng) | easy ].
}
apply IHla.
cbn in Hlc.
rewrite strip_0s_app in Hlc; cbn in Hlc.
remember (strip_0s (rev la)) as lb eqn:Hlb; symmetry in Hlb.
destruct lb as [| b]; [ now destruct (rng_eq_dec a 0%Rng) | easy ].
}
cbn.
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]. {
assert (Hlb : ∀ i, nth i lb 0%Rng = 0%Rng). {
intros.
rewrite <- Hi; cbn.
destruct i; [ easy | ].
apply Hla.
}
clear - Hlb.
induction lb as [| b]; [ easy | cbn ].
specialize (Hlb 0) as H1; cbn in H1; subst b.
rewrite strip_0s_app; cbn.
rewrite <- IHlb; [ now destruct (rng_eq_dec 0%Rng 0%Rng) | ].
intros i.
now specialize (Hlb (S i)).
}
destruct lb as [| b]; [ now specialize (Hi 0); cbn in Hi | cbn ].
rewrite strip_0s_app; cbn.
remember (strip_0s (rev lb)) as ld eqn:Hld; symmetry in Hld.
destruct ld as [| d]. {
destruct (rng_eq_dec b 0%Rng) as [Hbz| Hbz]. {
subst b.
now specialize (Hi 0).
}
f_equal.
now specialize (Hi 0).
}
specialize (IHla lb).
assert (H : ∀ i : nat, nth i la 0%Rng = nth i lb 0%Rng). {
intros i.
now specialize (Hi (S i)); cbn in Hi.
}
specialize (IHla H); clear H.
now rewrite Hld in IHla.
}
destruct lb as [| b]. {
specialize (IHla []).
assert (H : ∀ i : nat, nth i la 0%Rng = nth i [] 0%Rng). {
intros i; cbn; rewrite match_id.
now specialize (Hi (S i)).
}
now specialize (IHla H).
}
cbn.
rewrite strip_0s_app; cbn.
remember (strip_0s (rev lb)) as ld eqn:Hld; symmetry in Hld.
destruct ld as [| d]. {
destruct (rng_eq_dec b 0%Rng) as [Hbz| Hbz]. {
subst b.
specialize (IHla lb).
assert (H : ∀ i : nat, nth i la 0%Rng = nth i lb 0%Rng). {
intros i.
now specialize (Hi (S i)); cbn in Hi.
}
specialize (IHla H); clear H.
now rewrite Hld in IHla.
}
specialize (IHla lb).
assert (H : ∀ i : nat, nth i la 0%Rng = nth i lb 0%Rng). {
intros i.
now specialize (Hi (S i)); cbn in Hi.
}
specialize (IHla H); clear H.
now rewrite Hld in IHla.
}
specialize (Hi 0) as H1; cbn in H1; subst b.
do 2 rewrite app_comm_cons; f_equal.
rewrite <- Hld.
apply IHla.
now intros i; specialize (Hi (S i)).
}
Qed.
Theorem fold_lap_norm {A} {rng : ring A} :
∀ la, rev (strip_0s (rev la)) = lap_norm la.
Proof. easy. Qed.
Theorem lap_add_0_l {α} {r : ring α} : ∀ la, lap_add [] la = la.
Proof. easy. Qed.
Theorem lap_add_0_r {α} {r : ring α} : ∀ la, lap_add la [] = la.
Proof. intros; now destruct la. Qed.
Theorem poly_add_0_l {α} {r : ring α} : ∀ p, (0 + p)%pol = p.
Proof.
intros (la, lapr).
apply eq_poly_eq; cbn.
apply eq_poly_prop in lapr.
induction la as [| a]; [ easy | cbn ].
rewrite strip_0s_app.
rewrite <- (rev_involutive (strip_0s _)).
rewrite IHla; cbn. {
remember (rev la) as lb eqn:Hlb; symmetry in Hlb.
destruct lb as [| b]. {
apply List_eq_rev_nil in Hlb; subst la.
now destruct (rng_eq_dec a 0).
}
cbn.
rewrite rev_app_distr; cbn; f_equal.
now rewrite <- rev_involutive, Hlb.
} {
destruct la as [| a2]; [ | easy ].
apply rng_1_neq_0.
}
Qed.
Theorem lap_mul_0_l {α} {r : ring α} : ∀ la, lap_norm (lap_mul [] la) = [].
Proof. easy. Qed.
Theorem lap_mul_0_r {α} {r : ring α} : ∀ la, lap_norm (lap_mul la []) = [].
Proof. now intros; destruct la. Qed.
Section lap.
Context {α : Type}.
Context {r : ring α}.
Theorem strip_0s_idemp : ∀ la, strip_0s (strip_0s la) = strip_0s la.
Proof.
intros.
induction la as [| a]; [ easy | cbn ].
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]; [ easy | cbn ].
now destruct (rng_eq_dec a 0%Rng).
Qed.
Theorem lap_norm_idemp : ∀ la, lap_norm (lap_norm la) = lap_norm la.
Proof.
intros.
unfold lap_norm.
rewrite rev_involutive.
now rewrite strip_0s_idemp.
Qed.
Theorem eq_lap_convol_mul_nil : ∀ la lb i len,
lap_convol_mul la lb i len = [] → len = 0.
Proof. now intros; induction len. Qed.
(* addition theorems *)
Theorem lap_add_comm : ∀ al1 al2,
lap_add al1 al2 = lap_add al2 al1.
Proof.
intros al1 al2.
revert al2.
induction al1; intros; [ now destruct al2 | ].
destruct al2; [ easy | cbn ].
now rewrite rng_add_comm, IHal1.
Qed.
Theorem poly_add_comm : ∀ a b, (a + b)%pol = (b + a)%pol.
Proof.
intros.
unfold "+"%pol.
now rewrite lap_add_comm.
Qed.
Theorem lap_add_norm_idemp_l : ∀ la lb,
lap_norm (lap_norm la + lb)%lap = lap_norm (la + lb)%lap.
Proof.
intros.
unfold lap_norm; f_equal.
revert la.
induction lb as [| b]; intros. {
do 2 rewrite lap_add_0_r.
now rewrite rev_involutive, strip_0s_idemp.
}
cbn.
destruct la as [| a]; [ easy | cbn ].
do 2 rewrite strip_0s_app; cbn.
rewrite <- IHlb.
remember (strip_0s (rev la)) as lc eqn:Hlc; symmetry in Hlc.
destruct lc as [| c]. {
cbn.
destruct (rng_eq_dec a 0) as [Haz| Haz]. {
subst a; rewrite rng_add_0_l; cbn.
now rewrite strip_0s_app.
}
cbn.
now rewrite strip_0s_app.
}
cbn.
rewrite rev_app_distr; cbn.
now rewrite strip_0s_app.
Qed.
Theorem lap_add_norm_idemp_r : ∀ la lb,
lap_norm (la + lap_norm lb)%lap = lap_norm (la + lb)%lap.
Proof.
intros.
rewrite lap_add_comm, lap_add_norm_idemp_l.
now rewrite lap_add_comm.
Qed.
Theorem lap_add_assoc : ∀ al1 al2 al3,
lap_add al1 (lap_add al2 al3) = lap_add (lap_add al1 al2) al3.
Proof.
intros al1 al2 al3.
revert al2 al3.
induction al1; intros; [ easy | ].
destruct al2; [ easy | cbn ].
destruct al3; [ easy | cbn ].
rewrite rng_add_assoc.
now rewrite IHal1.
Qed.
Theorem lap_add_add_swap : ∀ la lb lc,
lap_add (lap_add la lb) lc = lap_add (lap_add la lc) lb.
Proof.
intros la lb lc.
do 2 rewrite <- lap_add_assoc.
now rewrite (lap_add_comm lb).
Qed.
Theorem poly_add_assoc : ∀ pa pb pc,
(pa + (pb + pc) = (pa + pb) + pc)%pol.
Proof.
intros (la, lapr) (lb, lbpr) (lc, lcpr).
apply eq_poly_eq.
cbn - [ lap_norm ].
rewrite lap_add_norm_idemp_l.
rewrite lap_add_norm_idemp_r.
now rewrite lap_add_assoc.
Qed.
(* multiplication theorems *)
Theorem lap_mul_comm : ∀ a b, lap_mul a b = lap_mul b a.
Proof.
intros la lb.
unfold lap_mul.
destruct la as [| a]; [ now destruct lb | cbn ].
rewrite <- Nat.add_succ_comm; cbn.
rewrite (Nat.add_comm (length lb)).
now rewrite lap_convol_mul_comm.
Qed.
Theorem poly_mul_comm : ∀ pa pb, (pa * pb)%pol = (pb * pa)%pol.
Proof.
intros.
apply eq_poly_eq; cbn.
now rewrite lap_mul_comm.
Qed.
Theorem list_nth_lap_convol_mul_aux : ∀ la lb n i len,
List.length la + List.length lb - 1 = (i + len)%nat
→ (List.nth n (lap_convol_mul la lb i len) 0%Rng =
Σ (j = 0, n + i),
List.nth j la 0 * List.nth (n + i - j) lb 0)%Rng.
Proof.
intros la lb n i len Hlen.
revert la lb i n Hlen.
induction len; intros.
simpl.
rewrite Nat.add_0_r in Hlen.
rewrite all_0_summation_0; [ destruct n; reflexivity | idtac ].
intros j (_, Hj).
destruct (le_dec (length la) j) as [H1| H1].
rewrite List.nth_overflow; [ idtac | assumption ].
rewrite rng_mul_0_l; reflexivity.
destruct (le_dec (length lb) (n + i - j)) as [H2| H2].
rewrite rng_mul_comm.
rewrite List.nth_overflow; [ idtac | assumption ].
rewrite rng_mul_0_l; reflexivity.
exfalso; apply H2; clear Hj H2.
apply Nat.nle_gt in H1; subst i.
flia H1.
simpl.
destruct n; [ reflexivity | idtac ].
rewrite Nat.add_succ_r, <- Nat.add_succ_l in Hlen.
rewrite IHlen; [ idtac | assumption ].
rewrite Nat.add_succ_r, <- Nat.add_succ_l; reflexivity.
Qed.
(* to be unified perhaps with list_nth_convol_mul below *)
Theorem list_nth_lap_convol_mul : ∀ la lb i len,
len = length la + length lb - 1
→ (List.nth i (lap_convol_mul la lb 0 len) 0 =
Σ (j = 0, i), List.nth j la 0 * List.nth (i - j) lb 0)%Rng.
Proof.
intros la lb i len Hlen.
symmetry in Hlen.
rewrite list_nth_lap_convol_mul_aux; [ idtac | assumption ].
rewrite Nat.add_0_r; reflexivity.
Qed.
Theorem summation_mul_list_nth_lap_convol_mul : ∀ la lb lc k,
(Σ (i = 0, k),
List.nth i la 0 *
List.nth (k - i)
(lap_convol_mul lb lc 0 (length lb + length lc - 1))
0 =
Σ (i = 0, k),
List.nth i la 0 *
Σ (j = 0, k - i),
List.nth j lb 0 * List.nth (k - i - j) lc 0)%Rng.
Proof.
intros la lb lc k.
apply summation_compat; intros i (_, Hi).
f_equal.
rewrite list_nth_lap_convol_mul; reflexivity.
Qed.
Theorem summation_mul_list_nth_lap_convol_mul_2 : ∀ la lb lc k,
(Σ (i = 0, k),
List.nth i lc 0 *
List.nth (k - i)
(lap_convol_mul la lb 0 (length la + length lb - 1)) 0 =
Σ (i = 0, k),
List.nth (k - i) lc 0 *
Σ (j = 0, i),
List.nth j la 0 * List.nth (i - j) lb 0)%Rng.
Proof.
intros la lb lc k.
rewrite summation_rtl.
apply summation_compat; intros i (_, Hi).
rewrite Nat.add_0_r.
f_equal.
rewrite Nat_sub_sub_distr; [ idtac | easy ].
rewrite Nat.sub_diag.
apply list_nth_lap_convol_mul; reflexivity.
Qed.
Lemma lap_norm_mul_assoc : ∀ la lb lc,
lap_norm (la * (lb * lc))%lap = lap_norm (la * lb * lc)%lap.
Proof.
intros la lb lc.
symmetry; rewrite lap_mul_comm.
unfold lap_mul.
destruct lc as [| c]. {
destruct la as [| a]; [ easy | now destruct lb ].
}
destruct la as [| a]; [ easy | ].
destruct lb as [| b]; [ easy | ].
move b before a; move c before b.
remember (a :: la) as la' eqn:Hla'.
remember (b :: lb) as lb' eqn:Hlb'.
remember (c :: lc) as lc' eqn:Hlc'.
apply list_nth_lap_eq; intros k.
remember (lap_convol_mul la' lb' 0 (length la' + length lb' - 1)) as ld
eqn:Hld.
remember (lap_convol_mul lb' lc' 0 (length lb' + length lc' - 1)) as le
eqn:Hle.
symmetry in Hld, Hle.
destruct ld as [| d]. {
destruct le as [| e]; [ easy | cbn ].
rewrite match_id.
move e before c.
apply eq_lap_convol_mul_nil in Hld.
apply Nat.sub_0_le in Hld.
remember (length la' + length lb') as len eqn:Hlen.
symmetry in Hlen.
destruct len. {
apply Nat.eq_add_0 in Hlen.
now subst la'.
}
destruct len; [ clear Hld | flia Hld ].
apply Nat.eq_add_1 in Hlen.
destruct Hlen as [Hlen| Hlen]; [ now rewrite Hlb' in Hlen | ].
now rewrite Hla' in Hlen.
}
destruct le as [| e]. {
cbn; rewrite match_id.
move d before c.
apply eq_lap_convol_mul_nil in Hle.
apply Nat.sub_0_le in Hle.
remember (length lb' + length lc') as len eqn:Hlen.
symmetry in Hlen.
destruct len. {
apply Nat.eq_add_0 in Hlen.
now subst lb'.
}
destruct len; [ clear Hle | flia Hle ].
apply Nat.eq_add_1 in Hlen.
destruct Hlen as [Hlen| Hlen]; [ now rewrite Hlc' in Hlen | ].
now rewrite Hlb' in Hlen.
}
rewrite list_nth_lap_convol_mul; [ idtac | reflexivity ].
rewrite list_nth_lap_convol_mul; [ idtac | reflexivity ].
rewrite <- Hld, <- Hle.
rewrite summation_mul_list_nth_lap_convol_mul_2; symmetry.
rewrite summation_mul_list_nth_lap_convol_mul; symmetry.
rewrite <- summation_summation_mul_swap.
rewrite <- summation_summation_mul_swap.
rewrite summation_summation_exch.
rewrite summation_summation_shift.
apply summation_compat; intros i Hi.
apply summation_compat; intros j Hj.
rewrite rng_mul_comm, rng_mul_assoc.
rewrite Nat.add_comm, Nat.add_sub.
rewrite Nat.add_comm, Nat.sub_add_distr.
reflexivity.
Qed.
Theorem eq_lap_norm_eq_length : ∀ la lb,
lap_norm la = lap_norm lb
→ length la = length lb
→ la = lb.
Proof.
intros * Hll Hlen.
unfold lap_norm in Hll.
apply (f_equal (@rev α)) in Hll.
do 2 rewrite rev_involutive in Hll.
setoid_rewrite <- rev_length in Hlen.
enough (H : rev la = rev lb). {
apply (f_equal (@rev α)) in H.
now do 2 rewrite rev_involutive in H.
}
remember (rev la) as l; clear la Heql; rename l into la.
remember (rev lb) as l; clear lb Heql; rename l into lb.
revert la Hll Hlen.
induction lb as [| b]; intros. {
now apply length_zero_iff_nil in Hlen.
}
destruct la as [| a]; [ easy | ].
cbn in Hll, Hlen.
apply Nat.succ_inj in Hlen.
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]. {
destruct (rng_eq_dec b 0%Rng) as [Hbz| Hbz]. {
subst a b; f_equal.
now apply IHlb.
}
exfalso; clear - Hbz Hll Hlen.
assert (H : length la ≤ length lb) by flia Hlen.
clear Hlen; rename H into Hlen.
induction la as [| a]; [ easy | ].
cbn in Hll.
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]. {
cbn in Hlen.
clear a Haz.
apply IHla; [ easy | flia Hlen ].
}
rewrite Hll in Hlen; cbn in Hlen.
flia Hlen.
}
destruct (rng_eq_dec b 0%Rng) as [Hbz| Hbz]. {
exfalso; clear b Hbz.
clear - Haz Hll Hlen.
assert (H : length lb ≤ length la) by flia Hlen.
clear Hlen; rename H into Hlen.
induction lb as [| b]; [ easy | ].
cbn in Hll.
destruct (rng_eq_dec b 0%Rng) as [Hbz| Hbz]. {
cbn in Hlen.
clear b Hbz.
apply IHlb; [ easy | flia Hlen ].
}
rewrite <- Hll in Hlen; cbn in Hlen.
flia Hlen.
}
easy.
Qed.
Theorem lap_convol_mul_length : ∀ la lb i len,
length (lap_convol_mul la lb i len) = len.
Proof.
intros.
revert la lb i.
induction len; intros; [ easy | ].
cbn.
now rewrite IHlen.
Qed.
Theorem lap_mul_assoc : ∀ la lb lc,
(la * (lb * lc))%lap = ((la * lb) * lc)%lap.
Proof.
intros.
apply eq_lap_norm_eq_length; [ apply lap_norm_mul_assoc | ].
unfold "*"%lap.
destruct la as [| a]; [ easy | ].
destruct lb as [| b]; [ easy | ].
destruct lc as [| c]. {
now destruct (lap_convol_mul _ _ _ _).
}
cbn.
do 4 (rewrite Nat.add_succ_r; cbn); f_equal.
do 2 rewrite rng_add_0_r.
do 4 rewrite lap_convol_mul_length.
apply Nat.add_assoc.
Qed.
Theorem lap_convol_mul_0_l : ∀ la lb i len,
(∀ i, nth i la 0%Rng = 0%Rng)
→ lap_norm (lap_convol_mul la lb i len) = [].
Proof.
intros * Ha.
revert i.
induction len; intros; [ easy | ].
cbn - [ summation ].
rewrite strip_0s_app.
remember (strip_0s (rev _)) as lc eqn:Hlc; symmetry in Hlc.
destruct lc as [| c]. {
rewrite all_0_summation_0. 2: {
intros j Hj.
now rewrite Ha, rng_mul_0_l.
}
cbn.
now destruct (rng_eq_dec 0%Rng 0%Rng).
}
unfold lap_norm in IHlen.
specialize (IHlen (S i)).
rewrite Hlc in IHlen.
now apply List_eq_rev_nil in IHlen.
Qed.
Theorem all_0_all_rev_0 : ∀ la,
(∀ i, nth i la 0%Rng = 0%Rng)
↔ (∀ i, nth i (rev la) 0%Rng = 0%Rng).
Proof.
intros *.
split; intros H i. {
destruct (lt_dec i (length la)) as [Hila| Hila]. {
rewrite rev_nth; [ apply H | easy ].
}
apply nth_overflow; rewrite rev_length; flia Hila.
} {
destruct (lt_dec i (length la)) as [Hila| Hila]. {
rewrite <- (rev_involutive la).
rewrite rev_nth; [ apply H | now rewrite rev_length ].
}
apply nth_overflow; flia Hila.
}
Qed.
Theorem eq_strip_0s_nil : ∀ la,
strip_0s la = [] ↔ ∀ i, nth i la 0%Rng = 0%Rng.
Proof.
intros.
split. {
intros Hla *.
revert i.
induction la as [| a]; intros; [ now destruct i | cbn ].
cbn in Hla.
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]; [ | easy ].
destruct i; [ easy | ].
now apply IHla.
} {
intros Hla.
induction la as [| a]; [ easy | cbn ].
destruct (rng_eq_dec a 0%Rng) as [Haz| Haz]. {
apply IHla.
intros i.
now specialize (Hla (S i)).
}
now specialize (Hla 0).
}
Qed.
Theorem eq_strip_0s_rev_nil : ∀ la,
strip_0s (rev la) = [] ↔ ∀ i, nth i la 0%Rng = 0%Rng.
Proof.
intros.
split; intros Hla. {
apply all_0_all_rev_0.
now apply eq_strip_0s_nil.
} {
apply eq_strip_0s_nil.
apply all_0_all_rev_0.
now rewrite rev_involutive.
}
Qed.
Lemma lap_convol_mul_cons_with_0_l : ∀ a la lb i len,
(∀ i, nth i la 0%Rng = 0%Rng)
→ lap_convol_mul (a :: la) lb i len = lap_convol_mul [a] lb i len.
Proof.
intros * Hla.
revert i.
induction len; intros; [ easy | ].
cbn - [ summation ].
f_equal; [ | apply IHlen ].
apply summation_compat.
intros j Hj.
destruct j; [ easy | ].
rewrite match_id.
now rewrite Hla.
Qed.
Theorem lap_convol_mul_succ : ∀ la lb i len,
lap_convol_mul la lb i (S len) =
lap_convol_mul la lb i len ++
[Σ (j = 0, i + len),
List.nth j la 0 * List.nth (i + len - j) lb 0]%Rng.
Proof.
intros.
cbn - [ summation ].
revert i.
induction len; intros. {
now rewrite Nat.add_0_r.
}
cbn - [ summation ].
f_equal.
specialize (IHlen (S i)).
cbn - [ summation ] in IHlen.
rewrite Nat.add_succ_r.
apply IHlen.
Qed.
Theorem lap_norm_app_0_r : ∀ la lb,
(∀ i, nth i lb 0%Rng = 0%Rng)
→ lap_norm la = lap_norm (la ++ lb).
Proof.
intros * Hlb.
unfold lap_norm; f_equal.
induction la as [| a]. {
cbn; symmetry.
induction lb as [| b]; [ easy | cbn ].
rewrite strip_0s_app.
rewrite IHlb. 2: {
intros i.
now specialize (Hlb (S i)).
}
specialize (Hlb 0); cbn in Hlb; rewrite Hlb; cbn.
now destruct (rng_eq_dec 0%Rng 0%Rng).
}
cbn.
do 2 rewrite strip_0s_app.
now rewrite IHla.
Qed.
Theorem lap_convol_mul_more : ∀ n la lb i len,
length la + length lb - 1 ≤ i + len
→ lap_norm (lap_convol_mul la lb i len) =
lap_norm (lap_convol_mul la lb i (len + n)).
Proof.
intros.
induction n; [ rewrite Nat.add_0_r; reflexivity | idtac ].
rewrite Nat.add_succ_r.
rewrite lap_convol_mul_succ.
rewrite IHn.
apply lap_norm_app_0_r.
intros j.
rewrite all_0_summation_0. {
destruct j; [ easy | now destruct j ].
}
clear j.
intros j (_, Hj).
apply rng_mul_eq_0.
destruct (le_dec (length la) j) as [H1| H1]. {
now left; rewrite List.nth_overflow.
} {
apply Nat.nle_gt in H1.
destruct (le_dec (length lb) (i + (len + n) - j)) as [H2| H2]. {
now right; rewrite nth_overflow.
} {
exfalso; apply H2; clear H2.
flia H H1.
}