-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF2Math.v
1861 lines (1734 loc) · 85.4 KB
/
F2Math.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
Require Export QuantumLib.GenSubspaces.
(** ** mathematics for semantical proof of separability ** **)
Declare Scope F2_scope.
Delimit Scope F2_scope with F2.
Local Open Scope F2_scope.
Inductive F2 : Type := zero | one.
Notation "0" := zero : F2_scope.
Notation "1" := one : F2_scope.
Definition F2plus (z1 z2 : F2) : F2 :=
match z1, z2 with
| zero, zero => zero
| zero, one => one
| one, zero => one
| one, one => zero
end.
Infix "+" := F2plus (at level 50, left associativity) : F2_scope.
Lemma F2plus_0_l : forall z : F2, 0 + z = z. Proof. intros; destruct z; auto. Qed.
Lemma F2plus_0_r : forall z : F2, z + 0 = z. Proof. intros; destruct z; auto. Qed.
Lemma F2plus_assoc : forall z1 z2 z3 : F2,
z1 + (z2 + z3) = (z1 + z2) + z3.
Proof. intros; destruct z1, z2, z3; auto. Qed.
Lemma F2plus_flip_l_0 : forall z : F2, 1 + z = 0 -> z = 1. Proof. intros; destruct z; auto. Qed.
Lemma F2plus_flip_l_1 : forall z : F2, 1 + z = 1 -> z = 0. Proof. intros; destruct z; auto. Qed.
Lemma F2plus_flip_r_0 : forall z : F2, z + 1 = 0 -> z = 1. Proof. intros; destruct z; auto. Qed.
Lemma F2plus_flip_r_1 : forall z : F2, z + 1 = 1 -> z = 0. Proof. intros; destruct z; auto. Qed.
#[global] Instance F2_is_monoid : Monoid F2 :=
{ Gzero := zero;
Gplus := F2plus;
Gplus_0_l := F2plus_0_l;
Gplus_0_r := F2plus_0_r;
Gplus_assoc := F2plus_assoc }.
(* Definition F2_is_monoid : Monoid F2 :=
Build_Monoid F2 zero F2plus F2plus_0_l F2plus_0_r F2plus_assoc. *)
Existing Instance F2_is_monoid.
Definition F2opp (z : F2) : F2 := z.
Notation "- z" := (F2opp z) : F2_scope.
Definition F2minus (z1 z2 : F2) : F2 := z1 + (- z2).
Infix "-" := F2minus (at level 50, left associativity) : F2_scope.
Lemma F2opp_l : forall z : F2, - z + z = 0. Proof. intros; destruct z; auto. Qed.
Lemma F2opp_r : forall z : F2, z + (- z) = 0. Proof. intros; destruct z; auto. Qed.
Definition F2_is_group : Group F2 :=
Build_Group F2 F2_is_monoid F2opp F2opp_l F2opp_r.
Existing Instance F2_is_group.
Lemma F2plus_comm : forall z1 z2 : F2, z1 + z2 = z2 + z1.
Proof. intros; destruct z1, z2; auto. Qed.
Definition F2_is_comm_group : Comm_Group F2 :=
Build_Comm_Group F2 F2_is_monoid F2_is_group F2plus_comm.
Existing Instance F2_is_comm_group.
Definition F2mult (z1 z2 : F2) : F2 :=
match z1, z2 with
| zero, zero => zero
| zero, one => zero
| one, zero => zero
| one, one => one
end.
Infix "·" := F2mult (at level 40, left associativity) : F2_scope.
Lemma F2mult_1_l : forall z : F2, 1 · z = z. Proof. intros; destruct z; auto. Qed.
Lemma F2mult_1_r : forall z : F2, z · 1 = z. Proof. intros; destruct z; auto. Qed.
Lemma F2mult_assoc : forall z1 z2 z3, z1 · (z2 · z3) = (z1 · z2) · z3.
Proof. intros; destruct z1, z2, z3; auto. Qed.
Lemma F2mult_plus_distr_l : forall z1 z2 z3, z3 · (z1 + z2) = (z3 · z1) + (z3 · z2).
Proof. intros; destruct z1, z2, z3; auto. Qed.
Lemma F2mult_plus_distr_r : forall z1 z2 z3, (z1 + z2) · z3 = (z1 · z3) + (z2 · z3).
Proof. intros; destruct z1, z2, z3; auto. Qed.
Lemma F2eq_dec : forall z1 z2 : F2, { z1 = z2 } + { z1 <> z2 }.
Proof. intros; destruct z1, z2; auto; try (left; easy); try (right; easy). Qed.
Definition F2_is_ring : Ring F2 :=
Build_Ring F2 F2_is_monoid F2_is_group F2_is_comm_group one F2mult
F2mult_1_l F2mult_1_r F2mult_assoc F2mult_plus_distr_l F2mult_plus_distr_r
F2eq_dec.
Existing Instance F2_is_ring.
Lemma F2mult_comm : forall z1 z2 : F2, F2mult z1 z2 = F2mult z2 z1.
Proof. intros; destruct z1, z2; auto. Qed.
Definition F2_is_comm_ring : Comm_Ring F2 :=
Build_Comm_Ring F2 F2_is_monoid F2_is_group F2_is_comm_group F2_is_ring
F2mult_comm.
Existing Instance F2_is_comm_ring.
Lemma F2_ring_theory : ring_theory 0%F2 1%F2 F2plus F2mult F2minus F2opp eq.
Proof. apply (@G_ring_theory F2 F2_is_monoid F2_is_group F2_is_comm_group F2_is_ring F2_is_comm_ring). Qed.
Add Ring F_ring_ring : F2_ring_theory.
Definition F2inv (z : F2) : F2 := z.
Notation "/ z" := (F2inv z) : F2_scope.
Definition F2div (z1 z2 : F2) : F2 := z1 · (/ z2).
Lemma F2_1_neq_0 : 1 <> 0. Proof. intro. discriminate. Qed.
Lemma F2inv_r : forall z : F2, z <> 0 -> z · (/ z) = 1. Proof. intros; destruct z; auto; contradiction. Qed.
Definition F2_is_field : Field F2 :=
Build_Field F2 F2_is_monoid F2_is_group F2_is_comm_group F2_is_ring
F2_is_comm_ring F2inv F2_1_neq_0 F2inv_r.
Existing Instance F2_is_field.
Lemma F2_field_theory : field_theory 0%F2 1%F2 F2plus F2mult F2minus F2opp F2div F2inv eq.
Proof. apply (@G_field_theory F2 F2_is_monoid F2_is_group F2_is_comm_group F2_is_ring F2_is_comm_ring F2_is_field). Qed.
Add Field F_field : F2_field_theory.
Lemma F2mult_0_l : forall z : F2, 0 · z = 0. Proof. intros; destruct z; auto. Qed.
Lemma F2mult_0_r : forall z : F2, z · 0 = 0. Proof. intros; destruct z; auto. Qed.
Definition F2_beq (z1 z2 : F2) : bool :=
match z1, z2 with
| zero, zero => true
| zero, one => false
| one, zero => false
| one, one => true
end.
Infix "=?" := F2_beq : F2_scope.
Lemma F2_beq_true_iff : forall z1 z2 : F2, (z1 =? z2)%F2 = true <-> z1 = z2.
Proof. intros z1 z2. split; intros; destruct z1, z2; simpl in *; auto; discriminate. Qed.
Lemma F2_beq_false_iff : forall z1 z2 : F2, (z1 =? z2)%F2 = false <-> z1 <> z2.
Proof. intros z1 z2. split; intros; destruct z1, z2; simpl in *; auto; try discriminate; contradiction. Qed.
Lemma F2_neq0_iff_eq1 : forall z : F2, z <> 0 <-> z = 1.
Proof. intros z. split; intros H; destruct z; try contradiction; try discriminate; auto. Qed.
Lemma F2_neq1_iff_eq0 : forall z : F2, z <> 1 <-> z = 0.
Proof. intros z. split; intros H; destruct z; try contradiction; try discriminate; auto. Qed.
Ltac F2simpl :=
repeat
match goal with
| _ => rewrite F2mult_0_l
| _ => rewrite F2mult_0_r
| _ => rewrite F2plus_0_l
| _ => rewrite F2plus_0_r
| _ => rewrite F2mult_1_l
| _ => rewrite F2mult_1_r
end.
Declare Module F2Field : FieldModule
with Definition F := F2
with Definition R0 := F2_is_monoid
with Definition R1 := F2_is_group
with Definition R2 := F2_is_comm_group
with Definition R3 := F2_is_ring
with Definition R4 := F2_is_comm_ring
with Definition R5 := F2_is_field.
Module F2Module := SubspacesOverField F2Field.
Notation MatrixF2 := (F2Module.GenMatrix).
Notation VectorF2 n := (F2Module.GenMatrix n%nat 1%nat).
Notation SquareF2 n := (F2Module.GenMatrix n%nat n%nat).
Notation WF_MatrixF2 := (F2Module.WF_GenMatrix).
Notation ZeroF2 := (F2Module.Zero).
Notation IF2 := (F2Module.I).
Notation traceF2 := (F2Module.trace).
Notation scaleF2 := (F2Module.scale).
Notation MplusF2 := (F2Module.GMplus).
Notation MoppF2 := (F2Module.GMopp).
Notation MminusF2 := (F2Module.GMminus).
Notation MmultF2 := (F2Module.GMmult).
Notation kronF2 := (F2Module.Gkron).
Notation transposeF2 := (F2Module.transpose).
Notation Mmult_nF2 := (F2Module.GMmult_n).
Notation genmat_equivF2 := (F2Module.genmat_equiv).
Notation get_colF2 := (F2Module.get_col).
Notation get_col_convF2 := (F2Module.get_col_conv).
Notation get_rowF2 := (F2Module.get_row).
Notation reduce_rowF2 := (F2Module.reduce_row).
Notation reduce_colF2 := (F2Module.reduce_col).
Notation col_swapF2 := (F2Module.col_swap).
Notation row_swapF2 := (F2Module.row_swap).
Notation col_addF2 := (F2Module.col_add).
Notation row_addF2 := (F2Module.row_add).
Notation col_swap_invF2 := (F2Module.col_swap_inv).
Notation row_swap_invF2 := (F2Module.row_swap_inv).
Notation col_add_invF2 := (F2Module.col_add_inv).
Notation row_add_invF2 := (F2Module.row_add_inv).
Notation swap_preserves_mul_ltF2 := (F2Module.swap_preserves_mul_lt).
Notation swap_preserves_mulF2 := (F2Module.swap_preserves_mul).
Notation add_preserves_mul_ltF2 := (F2Module.add_preserves_mul_lt).
Notation add_preserves_mulF2 := (F2Module.add_preserves_mul).
Notation WF_col_swapF2 := (F2Module.WF_col_swap).
Notation WF_row_swapF2 := (F2Module.WF_row_swap).
Notation WF_col_addF2 := (F2Module.WF_col_add).
Notation WF_row_addF2 := (F2Module.WF_row_add).
Notation col_swap_mult_rF2 := (F2Module.col_swap_mult_r).
Notation col_add_mult_rF2 := (F2Module.col_add_mult_r).
Notation col_row_swap_invr_IF2 := (F2Module.col_row_swap_invr_I).
Notation col_row_add_invr_IF2 := (F2Module.col_row_add_invr_I).
Notation e_iF2 := (F2Module.e_i).
Notation WF_e_iF2 := (F2Module.WF_e_i).
Notation matrix_by_basisF2 := (F2Module.matrix_by_basis).
Notation linearly_independentF2 := (F2Module.linearly_independent).
Notation linearly_dependentF2 := (F2Module.linearly_dependent).
Notation lindep_implies_not_linindepF2 := (F2Module.lindep_implies_not_linindep).
Notation not_lindep_implies_linindepF2 := (F2Module.not_lindep_implies_linindep).
Notation invr_col_swapF2 := (F2Module.invr_col_swap).
Notation invr_col_addF2 := (F2Module.invr_col_add).
Notation prop_zero_trueF2 := (F2Module.prop_zero_true).
Notation prop_zero_falseF2 := (F2Module.prop_zero_false).
Notation mat_prop_col_swap_convF2 := (F2Module.mat_prop_col_swap_conv).
Notation mat_prop_col_add_convF2 := (F2Module.mat_prop_col_add_conv).
Notation lin_indep_swap_invrF2 := (F2Module.lin_indep_swap_invr).
Notation lin_dep_swap_invrF2 := (F2Module.lin_dep_swap_invr).
Notation lin_indep_add_invrF2 := (F2Module.lin_indep_add_invr).
Notation lin_dep_add_invrF2 := (F2Module.lin_dep_add_invr).
Notation lin_indep_pzfF2 := (F2Module.lin_indep_pzf).
Notation lin_dep_pztF2 := (F2Module.lin_dep_pzt).
Notation smashF2 := (F2Module.smash).
Notation WF_smashF2 := (F2Module.WF_smash).
Infix ".+" := MplusF2 (at level 50, left associativity) : F2_scope.
Infix ".*" := scaleF2 (at level 40, left associativity) : F2_scope.
Infix "×" := MmultF2 (at level 40, left associativity) : F2_scope.
Infix "⊗" := kronF2 (at level 40, left associativity) : F2_scope.
Infix "≡" := genmat_equivF2 (at level 70) : F2_scope.
Notation "A ⊤" := (transposeF2 A) (at level 0) : F2_scope.
Notation Σ2 := (@big_sum F2Field.F F2Field.R0). (* we intoduce Σ2 notation here *)
Notation "p ⨉ A" := (Mmult_nF2 A p) (at level 30, no associativity) : F2_scope.
Check eq_refl : F2Field.F = F2.
Check eq_refl : F2Field.R0 = F2_is_monoid.
(** ** Defining a transposed Gaussian elimination. ** **)
Definition col_search_ones_right {m n : nat} (M : MatrixF2 m n) (r c : nat) :=
filter (fun i : nat => (M r i =? one)%F2) (List.seq c (n - c)).
Fixpoint col_add_rec {m n : nat} (M : MatrixF2 m n) (c : nat) (cols : list nat) : MatrixF2 m n :=
match cols with
| [] => M
| col :: t => col_add_rec (col_addF2 M col c 1%F2) c t
end.
Definition col_add_right_ones {m n : nat} (M : MatrixF2 m n) (r c : nat): MatrixF2 m n :=
col_add_rec M c (col_search_ones_right M r (Datatypes.S c)).
Fixpoint gaussian_elimination_transposed_rec {m n : nat} (M : MatrixF2 m n) (row_count col : nat) {struct row_count} : MatrixF2 m n :=
match row_count with
| 0%nat => M
| Datatypes.S row_count' =>
match hd_error (col_search_ones_right M (m - row_count) col) with
| None => gaussian_elimination_transposed_rec M row_count' col
| Some c =>
if (col <? c)
then gaussian_elimination_transposed_rec
(col_add_right_ones (col_swapF2 M col c) (m - row_count) col)
row_count' (Datatypes.S col)
else gaussian_elimination_transposed_rec
(col_add_right_ones M (m - row_count) col)
row_count' (Datatypes.S col)
end
end.
Definition gaussian_elimination_transposed {m n : nat} (M : MatrixF2 m n) :=
gaussian_elimination_transposed_rec M m 0%nat.
(** ** Elementary column operations for matrices over F2 ** **)
Inductive elem_col_ops_chainF2 {m n : nat} : MatrixF2 m n -> MatrixF2 m n -> Prop :=
| idColOpsChain : forall (M : MatrixF2 m n), elem_col_ops_chainF2 M M
| swapColOpsChain : forall (M : MatrixF2 m n) (x y : nat), (x < n)%nat -> (y < n)%nat ->
elem_col_ops_chainF2 M (col_swapF2 M x y)
| addColOpsChain : forall (M : MatrixF2 m n) (x y : nat) (z : F2), x <> y -> (x < n)%nat -> (y < n)%nat ->
elem_col_ops_chainF2 M (col_addF2 M x y z)
| concatColOpsChain : forall (M M' M'' : MatrixF2 m n),
elem_col_ops_chainF2 M M' -> elem_col_ops_chainF2 M' M'' ->
elem_col_ops_chainF2 M M''.
Lemma elem_col_ops_chainF2_preserves_lin_indep : forall {m n : nat} (M M' : MatrixF2 m n),
elem_col_ops_chainF2 M M' -> (linearly_independentF2 M <-> linearly_independentF2 M').
Proof. intros m n M M' H.
induction H; split; intros; auto.
- pose lin_indep_swap_invrF2 as H'; inversion H'; subst; clear H'.
apply H2; auto.
- pose lin_indep_swap_invrF2 as H'.
apply mat_prop_col_swap_convF2 in H1; auto.
- pose lin_indep_add_invrF2 as H'; inversion H'; subst; clear H'.
apply H3; auto.
- pose lin_indep_add_invrF2 as H'.
apply mat_prop_col_add_convF2 in H2; auto.
- rewrite <- IHelem_col_ops_chainF2_2, <- IHelem_col_ops_chainF2_1; auto.
- rewrite IHelem_col_ops_chainF2_1, IHelem_col_ops_chainF2_2; auto.
Qed.
Lemma elem_col_ops_chainF2_preserves_lin_dep : forall {m n : nat} (M M' : MatrixF2 m n),
elem_col_ops_chainF2 M M' -> (linearly_dependentF2 M <-> linearly_dependentF2 M').
Proof. intros m n M M' H.
induction H; split; intros; auto.
- pose lin_dep_swap_invrF2 as H'; inversion H'; subst; clear H'.
apply H2; auto.
- pose lin_dep_swap_invrF2 as H'.
apply mat_prop_col_swap_convF2 in H1; auto.
- pose lin_dep_add_invrF2 as H'; inversion H'; subst; clear H'.
apply H3; auto.
- pose lin_dep_add_invrF2 as H'.
apply mat_prop_col_add_convF2 in H2; auto.
- rewrite <- IHelem_col_ops_chainF2_2, <- IHelem_col_ops_chainF2_1; auto.
- rewrite IHelem_col_ops_chainF2_1, IHelem_col_ops_chainF2_2; auto.
Qed.
Lemma elem_col_ops_chainF2_col_add_rec :
forall {m n : nat} (M : MatrixF2 m n) (c : nat) (cols : list nat),
~ In c cols -> (c < n)%nat -> incl cols (List.seq 0%nat n) ->
elem_col_ops_chainF2 M (col_add_rec M c cols).
Proof. intros m n M c cols H H0 H1.
gen M c. induction cols; intros; try constructor; simpl.
apply concatColOpsChain with (M' := (col_addF2 M a c 1)); auto.
- constructor; auto.
+ rewrite not_in_cons in H; destruct H; auto.
+ assert (In a (a :: cols)) by (simpl; auto).
apply H1 in H2. rewrite in_seq in H2. lia.
- apply IHcols; auto.
+ apply incl_cons_inv in H1; destruct H1; auto.
+ rewrite not_in_cons in H; destruct H; auto.
Qed.
Lemma not_in_col_search_ones_right : forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
~ In c (col_search_ones_right M r (S c)).
Proof. intros m n M r c.
unfold col_search_ones_right.
intro H.
rewrite filter_In in H.
destruct H.
rewrite in_seq in H.
lia.
Qed.
Lemma incl_col_search_ones_right_seq : forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
incl (col_search_ones_right M r (S c)) (List.seq 0%nat n).
Proof. intros m n M r c.
unfold col_search_ones_right.
unfold incl; intros.
rewrite filter_In in H.
destruct H.
rewrite in_seq in H.
rewrite in_seq.
lia.
Qed.
Lemma elem_col_ops_chainF2_col_add_right_ones :
forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
(c < n)%nat -> elem_col_ops_chainF2 M (col_add_right_ones M r c).
Proof. intros m n M r c H.
unfold col_add_right_ones.
apply elem_col_ops_chainF2_col_add_rec; auto.
apply not_in_col_search_ones_right.
apply incl_col_search_ones_right_seq.
Qed.
Lemma elem_col_ops_chainF2_gaussian_elimination_transposed_rec :
forall {m n : nat} (M : MatrixF2 m n) (row_count col : nat),
elem_col_ops_chainF2 M (gaussian_elimination_transposed_rec M row_count col).
Proof. intros m n M row_count col.
gen M col. induction row_count; intros;
unfold gaussian_elimination_transposed_rec;
try constructor.
destruct (hd_error (col_search_ones_right M (m - S row_count) col)) eqn:E; auto.
bdestruct_all.
- fold (@gaussian_elimination_transposed_rec m n).
apply concatColOpsChain with (M' := (col_add_right_ones (col_swapF2 M col n0) (m - S row_count) col)); auto.
unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S row_count)%nat i =? 1) (seq col (n - col))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
apply concatColOpsChain with (M' := (col_swapF2 M col n0)).
constructor; lia.
apply elem_col_ops_chainF2_col_add_right_ones; lia.
- fold (@gaussian_elimination_transposed_rec m n).
apply concatColOpsChain with (M' := (col_add_right_ones M (m - S row_count) col)); auto.
unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S row_count)%nat i =? 1) (seq col (n - col))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
apply elem_col_ops_chainF2_col_add_right_ones; lia.
Qed.
Lemma gaussian_elimination_transposed_rec_preserves_lin_indep :
forall {m n : nat} (M : MatrixF2 m n) (row_count col : nat),
linearly_independentF2 M <-> linearly_independentF2 (gaussian_elimination_transposed_rec M row_count col).
Proof. intros m n M row_count col.
apply elem_col_ops_chainF2_preserves_lin_indep.
apply elem_col_ops_chainF2_gaussian_elimination_transposed_rec.
Qed.
Lemma gaussian_elimination_transposed_rec_preserves_lin_dep :
forall {m n : nat} (M : MatrixF2 m n) (row_count col : nat),
linearly_dependentF2 M <-> linearly_dependentF2 (gaussian_elimination_transposed_rec M row_count col).
Proof. intros m n M row_count col.
apply elem_col_ops_chainF2_preserves_lin_dep.
apply elem_col_ops_chainF2_gaussian_elimination_transposed_rec.
Qed.
Lemma elem_col_ops_chainF2_gaussian_elimination_transposed :
forall {m n : nat} (M : MatrixF2 m n),
elem_col_ops_chainF2 M (gaussian_elimination_transposed M).
Proof. intros; apply elem_col_ops_chainF2_gaussian_elimination_transposed_rec. Qed.
Lemma gaussian_elimination_transposed_preserves_lin_indep :
forall {m n : nat} (M : MatrixF2 m n),
linearly_independentF2 M <-> linearly_independentF2 (gaussian_elimination_transposed M).
Proof. intros m n M.
apply elem_col_ops_chainF2_preserves_lin_indep.
apply elem_col_ops_chainF2_gaussian_elimination_transposed.
Qed.
Lemma gaussian_elimination_transposed_preserves_lin_dep :
forall {m n : nat} (M : MatrixF2 m n),
linearly_dependentF2 M <-> linearly_dependentF2 (gaussian_elimination_transposed M).
Proof. intros m n M.
apply elem_col_ops_chainF2_preserves_lin_dep.
apply elem_col_ops_chainF2_gaussian_elimination_transposed.
Qed.
Fixpoint gaussian_elimination_transposed_rec_get_pivot_row {m n : nat} (M : MatrixF2 m n) (row_count col pivot_col : nat) {struct row_count} : (MatrixF2 m n) * option nat :=
match row_count with
| 0%nat => (M, None)
| Datatypes.S row_count' =>
match hd_error (col_search_ones_right M (m - row_count) col) with
| None => gaussian_elimination_transposed_rec_get_pivot_row M row_count' col pivot_col
| Some c => if (col <? pivot_col)
then
if (col <? c)
then gaussian_elimination_transposed_rec_get_pivot_row
(col_add_right_ones (col_swapF2 M col c) (m - row_count) col)
row_count' (Datatypes.S col) pivot_col
else gaussian_elimination_transposed_rec_get_pivot_row
(col_add_right_ones M (m - row_count) col)
row_count' (Datatypes.S col) pivot_col
else
if (col <? c)
then ((col_add_right_ones (col_swapF2 M col c) (m - row_count) col), Some (m - row_count)%nat)
else ((col_add_right_ones M (m - row_count) col), Some (m - row_count)%nat)
end
end.
Lemma WF_col_add_rec : forall {m n : nat} (M : MatrixF2 m n) (c : nat) (cols : list nat),
incl cols (seq 0 n) -> WF_MatrixF2 M -> WF_MatrixF2 (col_add_rec M c cols).
Proof. intros m n M c cols H H0.
gen M c. induction cols; intros; simpl; auto.
apply IHcols.
- unfold incl. intros a0 H1.
assert (In a0 (a :: cols)) by (simpl; auto).
apply H in H2. auto.
- apply F2Module.WF_col_add; auto.
assert (In a (seq 0 n)).
{ assert (In a (a :: cols)) by (simpl; auto).
apply H in H1. auto. }
rewrite in_seq in H1; lia.
Qed.
Lemma WF_col_add_right_ones : forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
(c < n)%nat -> WF_MatrixF2 M -> WF_MatrixF2 (col_add_right_ones M r c).
intros m n M r c H H0.
unfold col_add_right_ones.
apply WF_col_add_rec; auto.
unfold incl. intros a H1.
unfold col_search_ones_right in H1.
apply filter_In in H1. destruct H1.
rewrite in_seq in H1.
rewrite in_seq. lia.
Qed.
Lemma WF_fst_gaussian_elimination_transposed_rec_get_pivot_row :
forall {m n : nat} (M : MatrixF2 m n) (row_count col pivot_col : nat),
WF_MatrixF2 M -> WF_MatrixF2 (fst (gaussian_elimination_transposed_rec_get_pivot_row M row_count col pivot_col)).
Proof. intros m n M row_count col pivot_col H.
unfold WF_MatrixF2.
intros x y H0.
gen M col pivot_col x y. induction row_count; intros; auto.
simpl.
destruct (hd_error (col_search_ones_right M (m - S row_count) col)) eqn:E.
- unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S row_count)%nat i =? 1) (seq col (n - col))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
bdestruct_all.
+ apply IHrow_count; try lia; auto.
apply WF_col_add_right_ones; try lia; auto.
apply F2Module.WF_col_swap; try lia; auto.
+ apply IHrow_count; try lia; auto.
apply WF_col_add_right_ones; try lia; auto.
+ simpl.
assert (WF_MatrixF2 (col_add_right_ones (col_swapF2 M col n0) (m - S row_count) col)).
{ apply WF_col_add_right_ones; try lia; auto.
apply F2Module.WF_col_swap; try lia; auto. }
rewrite H3; auto.
+ simpl.
assert (WF_MatrixF2 (col_add_right_ones M (m - S row_count) col)).
{ apply WF_col_add_right_ones; try lia; auto. }
rewrite H3; auto.
- apply IHrow_count; try lia; auto.
Qed.
Lemma gaussian_elimination_transposed_rec_col_overflow :
forall {m n : nat} (M : MatrixF2 m n) (row_count col : nat),
(col >= n)%nat ->
gaussian_elimination_transposed_rec M row_count col = M.
Proof. intros m n M row_count col H.
gen M col. induction row_count; intros; auto. simpl.
destruct (hd_error (col_search_ones_right M (m - S row_count) col)) eqn:E.
unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S row_count)%nat i =? 1) (seq col (n - col))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
bdestruct_all; try lia.
apply IHrow_count; auto.
Qed.
Lemma gaussian_elimination_transposed_rec_get_pivot_row_saturated :
forall {m n : nat} (M : MatrixF2 m n) (row_count col pivot_col : nat),
(pivot_col >= n - 1)%nat ->
gaussian_elimination_transposed_rec M row_count col =
fst (gaussian_elimination_transposed_rec_get_pivot_row M row_count col pivot_col).
Proof. intros m n M row_count col pivot_col H.
gen M pivot_col col. induction row_count; intros; auto; simpl.
destruct (hd_error (col_search_ones_right M (m - S row_count) col)) eqn:E.
- unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S row_count)%nat i =? 1) (seq col (n - col))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
bdestruct_all; try apply IHrow_count with (pivot_col := n); auto; simpl; try lia.
bdestruct (pivot_col =? col)%nat; try lia. subst.
bdestruct (col =? n-1)%nat; try lia. subst. simpl.
rewrite gaussian_elimination_transposed_rec_col_overflow; auto; lia.
- apply IHrow_count; auto.
Qed.
Lemma WF_gaussian_elimination_transposed : forall {m n : nat} (M : MatrixF2 m n),
WF_MatrixF2 M -> WF_MatrixF2 (gaussian_elimination_transposed M).
Proof. intros m n M H.
unfold gaussian_elimination_transposed.
rewrite gaussian_elimination_transposed_rec_get_pivot_row_saturated with (pivot_col := (n - 1)%nat); try lia; auto.
apply WF_fst_gaussian_elimination_transposed_rec_get_pivot_row; auto.
Qed.
Definition col_slice_one_hd_error {m n : nat} (M : MatrixF2 m n) (row_count col pivot_col : nat) :=
let GM := gaussian_elimination_transposed_rec_get_pivot_row M row_count col pivot_col in
hd_error (filter (fun r : nat => ((fst GM) r pivot_col =? one)%F2) (List.seq (m - row_count) row_count)).
Lemma col_add_rec_one_bool_true :
forall {m n : nat} (M : MatrixF2 m n) (c c' r' : nat) (cols : list nat),
(c' < c)%nat -> (c < n)%nat -> (M r' c' =? 1) = true ->
~ In c cols -> NoDup cols -> incl cols (seq c (n - c)) ->
(forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
(col_add_rec M c cols r' c' =? 1) = true.
Proof. intros m n M c c' r' cols H H0 H1 H2 H3 H4 H5.
gen M c c' r'. induction cols; intros; auto.
simpl. apply IHcols; try lia; auto.
- rewrite NoDup_cons_iff in H3. destruct H3. auto.
- rewrite not_in_cons in H2. destruct H2. auto.
- unfold incl. intros a0 H6.
assert (H' : In a0 (a :: cols)) by (simpl; auto).
apply H4 in H'. auto.
- unfold col_addF2.
bdestruct_all; auto.
rewrite F2_beq_true_iff in H1.
rewrite ! H1.
assert ((M r' c =? 1) = false).
{ apply H5; lia. }
rewrite F2_beq_false_iff in H7.
destruct (M r' c) eqn:E; try contradiction.
F2simpl. auto.
- intros i H6.
unfold col_addF2.
bdestruct_all; subst; auto.
assert ((M r' c =? 1) = false).
{ apply H5; lia. }
assert ((M r' a =? 1) = false).
{ apply H5; lia. }
rewrite F2_beq_false_iff in H7, H8.
destruct (M r' c) eqn:E; try contradiction.
destruct (M r' a) eqn:E'; try contradiction.
F2simpl. auto.
Qed.
Lemma col_add_right_ones_one_bool_true :
forall {m n : nat} (M : MatrixF2 m n) (r r' c c' : nat),
(c' < c)%nat -> (c < n)%nat -> (M r' c' =? 1) = true ->
(forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
(col_add_right_ones M r c r' c' =? 1) = true.
Proof. intros m n M r r' c c' H H0 H1 H2.
unfold col_add_right_ones.
unfold col_search_ones_right.
apply col_add_rec_one_bool_true; try lia; auto.
- intro H3. rewrite filter_In in H3. destruct H3.
rewrite in_seq in H3. lia.
- apply NoDup_filter. apply seq_NoDup.
- unfold incl. intros a H3. rewrite filter_In in H3. destruct H3.
rewrite in_seq in H3. rewrite in_seq. lia.
Qed.
Lemma col_add_right_ones_col_swapF2_one_bool_true :
forall {m n : nat} (M : MatrixF2 m n) (r r' c c' k : nat),
(c' < c)%nat -> (c < k)%nat -> (k < n)%nat -> (M r' c' =? 1) = true ->
(forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
(col_add_right_ones (col_swapF2 M c k) r c r' c' =? 1) = true.
Proof. intros m n M r r' c c' k H H0 H1 H2 H3.
apply col_add_right_ones_one_bool_true; try lia; auto.
unfold col_swapF2. bdestruct_all; subst; auto.
intros i H4. unfold col_swapF2. bdestruct_all; auto; apply H3; try lia.
Qed.
Lemma col_add_rec_one_bool_false :
forall {m n : nat} (M : MatrixF2 m n) (c c' r' i : nat) (cols : list nat),
(c' < c)%nat -> (c < n)%nat -> (c' < i < n)%nat -> (M r' i =? 1) = false ->
(forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
~ In c cols -> NoDup cols -> incl cols (seq c (n - c)) ->
(col_add_rec M c cols r' i =? 1) = false.
Proof. intros m n M c c' r' i cols H H0 H1 H2 H3 H4 H5 H6.
gen M c c' r' i. induction cols; intros; auto.
apply IHcols with (c' := c'); try lia; auto.
- rewrite NoDup_cons_iff in H5. destruct H5. auto.
- rewrite not_in_cons in H4. destruct H4. auto.
- unfold incl. intros a0 H7.
assert (H' : In a0 (a :: cols)) by (simpl; auto).
apply H6 in H'. auto.
- intros i0 H7.
unfold col_addF2.
bdestruct_all; subst; auto.
assert ((M r' c =? 1) = false).
{ apply H3; lia. }
assert ((M r' a =? 1) = false).
{ apply H3; lia. }
rewrite F2_beq_false_iff in H8, H9.
destruct (M r' c) eqn:E; try contradiction.
destruct (M r' a) eqn:E'; try contradiction.
F2simpl. auto.
- unfold col_addF2.
bdestruct_all; subst; auto.
rewrite F2_beq_false_iff in H2.
destruct (M r' a) eqn:E; try contradiction.
assert ((M r' c =? 1) = false).
{ apply H3; lia. }
rewrite F2_beq_false_iff in H7.
destruct (M r' c) eqn:E'; try contradiction.
F2simpl. auto.
Qed.
Lemma col_add_right_ones_one_bool_false :
forall {m n : nat} (M : MatrixF2 m n) (r r' c c' : nat),
(c' < c)%nat -> (c < n)%nat -> (forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
(forall i : nat, (c' < i < n)%nat -> (col_add_right_ones M r c r' i =? 1) = false).
Proof. intros m n M r r' c c' H H0 H1 i H2.
unfold col_add_right_ones.
unfold col_search_ones_right.
apply col_add_rec_one_bool_false with (c' := c'); try lia; auto.
- intro H3. rewrite filter_In in H3. destruct H3.
rewrite in_seq in H3. lia.
- apply NoDup_filter. apply seq_NoDup.
- unfold incl. intros a H3. rewrite filter_In in H3. destruct H3.
rewrite in_seq in H3. rewrite in_seq. lia.
Qed.
Lemma col_add_right_ones_col_swapF2_one_bool_false :
forall {m n : nat} (M : MatrixF2 m n) (r r' c c' k : nat),
(c' < c)%nat -> (c < k)%nat -> (k < n)%nat -> (forall i : nat, (c' < i < n)%nat -> (M r' i =? 1) = false) ->
(forall i : nat, (c' < i < n)%nat -> (col_add_right_ones (col_swapF2 M c k) r c r' i =? 1) = false).
Proof. intros m n M r r' c c' k H H0 H1 H2 i H3.
apply col_add_right_ones_one_bool_false with (c' := c'); try lia; auto.
intros i0 H4. unfold col_swapF2. bdestruct_all; auto; apply H2; try lia.
Qed.
Lemma fst_gaussian_elimination_transposed_rec_get_pivot_row_above_one_false :
forall {m n : nat} (M : MatrixF2 m n) (r c pc r' c' : nat),
(r < r' <= m)%nat -> (c' < c)%nat -> (c <= pc < n)%nat -> (M (m - r')%nat c' =? 1) = true ->
(forall i : nat, (c' < i < n)%nat -> (M (m - r')%nat i =? 1) = false) ->
(fst (gaussian_elimination_transposed_rec_get_pivot_row M r c pc) (m - r')%nat pc =? 1) = false.
Proof. intros m n M r c pc r' c' H H0 H1 H2 H3.
gen M c pc r' c'. induction r; intros; simpl.
- apply H3; lia.
- destruct (hd_error (col_search_ones_right M (m - S r) c)) eqn:E; simpl.
+ unfold col_search_ones_right in E.
destruct (filter (fun i : nat => M (m - S r)%nat i =? 1) (seq c (n - c))) eqn:E'.
apply hd_error_some_nil in E; contradiction.
simpl in E. inversion E; subst; clear E.
assert (H' : In n0 (n0 :: l)) by (simpl; auto).
rewrite <- E' in H'.
rewrite filter_In in H'; destruct H' as [H' H''].
rewrite in_seq in H'.
bdestruct_all; try lia; simpl.
* rewrite IHr with (c' := c'); auto; try lia.
-- apply col_add_right_ones_col_swapF2_one_bool_true; try lia; auto.
-- apply col_add_right_ones_col_swapF2_one_bool_false; try lia; auto.
* rewrite IHr with (c' := c'); auto; try lia.
-- apply col_add_right_ones_one_bool_true; try lia; auto.
-- apply col_add_right_ones_one_bool_false; try lia; auto.
* apply col_add_right_ones_col_swapF2_one_bool_false with (c' := c'); try lia; auto.
* apply col_add_right_ones_one_bool_false with (c' := c'); try lia; auto.
+ bdestruct_all; try lia; simpl.
rewrite IHr with (c' := c'); auto; try lia.
Qed.
Lemma col_addF2_one_bool_true_true : forall {m n : nat} (M : MatrixF2 m n) (r c a : nat),
c <> a -> (M r c =? 1) = true -> (M r a =? 1) = true -> ((col_addF2 M a c 1) r c =? 1) = true.
Proof. intros m n M r c a H H0 H1.
rewrite F2_beq_true_iff in H0, H1.
rewrite F2_beq_true_iff.
unfold col_addF2.
bdestruct_all; auto.
Qed.
Lemma col_add_rec_one_bool_preserve : forall {m n : nat} (M : MatrixF2 m n) (r c : nat) (cols : list nat),
NoDup cols -> (forall a : nat, In a cols -> (M r a =? 1) = true) ->
~ In c cols -> (M r c =? 1) = true -> ((col_add_rec M c cols) r c =? 1) = true.
Proof. intros m n M r c cols H H0 H1 H2.
rewrite F2_beq_true_iff in H2.
rewrite F2_beq_true_iff.
gen M r c. induction cols; intros; auto.
simpl.
rewrite not_in_cons in H1.
destruct H1.
rewrite NoDup_cons_iff in H.
destruct H.
apply IHcols; auto.
- intros a0 H5.
assert (In a0 (a :: cols)) by (simpl; auto).
apply H0 in H6.
rewrite F2_beq_true_iff in H6.
rewrite F2_beq_true_iff.
unfold col_addF2.
bdestruct_all; auto.
subst. contradiction.
- rewrite <- F2_beq_true_iff.
apply col_addF2_one_bool_true_true; auto.
rewrite F2_beq_true_iff; auto.
apply H0; simpl; auto.
Qed.
Lemma col_add_right_ones_one_bool_preserve : forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
(M r c =? 1) = true -> ((col_add_right_ones M r c) r c =? 1) = true.
Proof. intros m n M r c H.
unfold col_add_right_ones.
unfold col_search_ones_right.
simpl.
apply col_add_rec_one_bool_preserve; auto.
- apply NoDup_filter.
apply seq_NoDup.
- intros a H0.
rewrite filter_In in H0.
destruct H0; auto.
- intro.
rewrite filter_In in H0.
destruct H0.
rewrite in_seq in H0.
lia.
Qed.
Lemma col_swapF2_one_bool_preserve : forall {m n : nat} (M : MatrixF2 m n) (r c i : nat),
(M r i =? 1) = true -> ((col_swapF2 M c i) r c =? 1) = true.
Proof. intros m n M r c i H.
rewrite F2_beq_true_iff in H.
rewrite F2_beq_true_iff.
unfold col_swapF2.
bdestruct_all; auto.
Qed.
Lemma col_add_right_ones_col_swapF2_one_bool_preserve :
forall {m n : nat} (M : MatrixF2 m n) (r c i : nat),
(M r i =? 1) = true -> ((col_add_right_ones (col_swapF2 M c i) r c) r c =? 1) = true.
Proof. intros m n M r c i H.
apply col_add_right_ones_one_bool_preserve.
apply col_swapF2_one_bool_preserve; auto.
Qed.
Lemma col_add_rec_zero_bool_preserve :
forall {m n : nat} (M : MatrixF2 m n) (r c : nat) (cols : list nat),
(c < n)%nat -> NoDup cols -> ~ In c cols -> incl cols (seq c (n - c)) ->
(forall i : nat, (c < i < n)%nat -> ((M r i =? 1) = true <-> In i cols)) -> (M r c =? 1) = true ->
(forall i : nat, (c < i < n)%nat -> (col_add_rec M c cols r i =? 1) = false).
Proof. intros m n M r c cols H H0 H1 H2 H3 H4 i H5.
gen M r c. induction cols; intros; simpl.
- rewrite F2_beq_false_iff. intro H6. rewrite <- F2_beq_true_iff in H6.
rewrite H3 in H6; auto.
- apply IHcols; auto.
+ rewrite NoDup_cons_iff in H0.
destruct H0. auto.
+ rewrite not_in_cons in H1.
destruct H1. auto.
+ unfold incl. intros a0 H6.
assert (In a0 (a :: cols)) by (simpl; auto).
apply H2 in H7. auto.
+ intros i0 H6. split; intros H7.
* assert ((M r i0 =? 1) = true).
{ rewrite F2_beq_true_iff.
destruct (M r i0) eqn:E; auto.
rewrite F2_beq_true_iff in H7.
destruct (col_addF2 M a c 1 r i0) eqn:E'; auto.
contradict E'. unfold col_addF2.
bdestruct_all; subst.
- assert (In a (a :: cols)) by (simpl; auto).
rewrite <- H3 in H8; auto.
rewrite F2_beq_true_iff in H8.
rewrite E in H8.
discriminate.
- rewrite E. intro. discriminate. }
remember H8 as H'. clear HeqH'.
rewrite H3 in H8; auto.
destruct H8; auto.
subst.
rewrite F2_beq_true_iff in H7.
contradict H7.
unfold col_addF2.
bdestruct_all; subst; auto.
rewrite F2_beq_true_iff in H4, H'.
rewrite H4, H'.
F2simpl. simpl. intro. discriminate.
* unfold col_addF2.
bdestruct_all; subst; auto.
-- rewrite NoDup_cons_iff in H0. destruct H0. auto.
-- rewrite H3; simpl; auto.
+ unfold col_addF2.
bdestruct_all; subst; auto.
assert (In a (a :: cols)) by (simpl; auto).
contradiction.
Qed.
Lemma col_add_right_ones_zero_bool_preserve :
forall {m n : nat} (M : MatrixF2 m n) (r c : nat),
(c < n)%nat -> (M r c =? 1) = true ->
(forall i : nat, (c < i < n)%nat -> ((col_add_right_ones M r c) r i =? 1) = false).
Proof. intros m n M r c H H0 i H1.
unfold col_add_right_ones.
unfold col_search_ones_right.
apply col_add_rec_zero_bool_preserve; auto.
- apply NoDup_filter. apply seq_NoDup.
- intro H2. rewrite filter_In in H2. destruct H2.
rewrite in_seq in H2. lia.
- unfold incl. intros a H2.
rewrite filter_In in H2. destruct H2.
rewrite in_seq in H2. rewrite in_seq. lia.
- intros i0 H2. split; intros H3.
+ rewrite filter_In. split; auto.
rewrite in_seq; lia.
+ rewrite filter_In in H3. destruct H3. auto.
Qed.
Lemma col_add_right_ones_col_swapF2_zero_bool_preserve :
forall {m n : nat} (M : MatrixF2 m n) (r c k : nat),
(M r k =? 1) = true -> (c < n)%nat -> (c < k < n)%nat ->
(forall i : nat, (c < i < n)%nat -> (col_add_right_ones (col_swapF2 M c k) r c r i =? 1) = false).
Proof. intros m n M r c k H H0 H1 i H2.
apply col_add_right_ones_zero_bool_preserve; auto.
unfold col_swapF2.
bdestruct_all; subst; auto.
Qed.
Lemma col_add_rec_one_bool_false_inclusive_domain :
forall {m n : nat} (M : MatrixF2 m n) (r c i : nat) (cols : list nat),
(c < i < n)%nat -> (forall i : nat, (c <= i < n)%nat -> (M r i =? 1) = false) ->
NoDup cols -> ~ In c cols -> incl cols (seq c (n - c)) ->
(col_add_rec M c cols r i =? 1) = false.
Proof. intros m n M r c i cols H H0 H1 H2 H3.
gen M r c i. induction cols; intros; simpl.
- apply H0; lia.
- apply IHcols; try lia; auto.
+ rewrite NoDup_cons_iff in H1.
destruct H1. auto.
+ intros i0 H4.
unfold col_addF2.
bdestruct_all; subst; auto.
assert (M r a = zero).
{ destruct (M r a) eqn:E; auto.
contradict E.
rewrite <- F2_beq_false_iff.
apply H0. lia. }
assert (M r c = zero).
{ destruct (M r c) eqn:E; auto.
contradict E.
rewrite <- F2_beq_false_iff.
apply H0. lia. }
rewrite H5, H6.
F2simpl. auto.
+ rewrite not_in_cons in H2.
destruct H2. auto.
+ unfold incl. intros a0 H4.
assert (In a0 (a :: cols)) by (simpl; auto).
apply H3 in H5. auto.
Qed.
Lemma col_add_right_ones_one_bool_false_inclusive_domain :
forall {m n : nat} (M : MatrixF2 m n) (r r' c i : nat),
(forall i : nat, (c <= i < n)%nat -> (M r' i =? 1) = false) -> (c < i < n)%nat ->
(col_add_right_ones M r c r' i =? 1) = false.
Proof. intros m n M r r' c i H H0.
unfold col_add_right_ones.
unfold col_search_ones_right.
apply col_add_rec_one_bool_false_inclusive_domain; try lia; auto.
- apply NoDup_filter. apply seq_NoDup.
- intro H1. rewrite filter_In in H1. destruct H1.
rewrite in_seq in H1. lia.
- unfold incl. intros a H1.
rewrite filter_In in H1. destruct H1.
rewrite in_seq in H1. rewrite in_seq. lia.
Qed.
Lemma col_add_right_ones_col_swapF2_one_bool_false_inclusive_domain :
forall {m n : nat} (M : MatrixF2 m n) (r r' c k i : nat),
(forall i : nat, (c <= i < n)%nat -> (M r' i =? 1) = false) ->
(c < k < n)%nat -> (c < i < n)%nat ->
(col_add_right_ones (col_swapF2 M c k) r c r' i =? 1) = false.
Proof. intros m n M r r' c k i H H0 H1.
apply col_add_right_ones_one_bool_false_inclusive_domain; try lia; auto.
- intros i0 H2. unfold col_swapF2.
bdestruct_all; subst; apply H; try lia.
Qed.
Lemma col_add_rec_one_bool_false_pivot_col_is_col_inclusive_domain :
forall {m n : nat} (M : MatrixF2 m n) (r c : nat) (cols : list nat),
(c < n)%nat -> (forall i : nat, (c <= i < n)%nat -> (M r i =? 1) = false) ->
NoDup cols -> ~ In c cols -> incl cols (seq c (n - c)) ->
(col_add_rec M c cols r c =? 1) = false.
Proof. intros m n M r c cols H H0 H1 H2 H3.
gen M r c. induction cols; intros; simpl.
- apply H0; lia.
- apply IHcols; try lia; auto.
+ rewrite NoDup_cons_iff in H1.
destruct H1. auto.
+ intros i0 H4.
unfold col_addF2.
bdestruct_all; subst; auto.
assert (M r a = zero).
{ destruct (M r a) eqn:E; auto.
contradict E.
rewrite <- F2_beq_false_iff.
apply H0. lia. }
assert (M r c = zero).
{ destruct (M r c) eqn:E; auto.
contradict E.
rewrite <- F2_beq_false_iff.
apply H0. lia. }
rewrite H5, H6.
F2simpl. auto.
+ rewrite not_in_cons in H2.
destruct H2. auto.
+ unfold incl. intros a0 H4.
assert (In a0 (a :: cols)) by (simpl; auto).
apply H3 in H5. auto.
Qed.
Lemma col_add_right_ones_one_bool_false_pivot_col_is_col_inclusive_domain : forall {m n : nat} (M : MatrixF2 m n) (r r' c : nat),
(forall i : nat, (c <= i < n)%nat -> (M r' i =? 1) = false) -> (c < n)%nat ->
(col_add_right_ones M r c r' c =? 1) = false.
Proof. intros m n M r r' c H H0.
unfold col_add_right_ones.
unfold col_search_ones_right.
apply col_add_rec_one_bool_false_pivot_col_is_col_inclusive_domain; try lia; auto.
- apply NoDup_filter. apply seq_NoDup.
- intro H1. rewrite filter_In in H1. destruct H1.
rewrite in_seq in H1. lia.
- unfold incl. intros a H1.
rewrite filter_In in H1. destruct H1.
rewrite in_seq in H1. rewrite in_seq. lia.
Qed.
Lemma col_add_right_ones_col_swapF2_one_bool_false_pivot_col_is_col_inclusive_domain : forall {m n : nat} (M : MatrixF2 m n) (r r' c k : nat),
(r <= m)%nat -> (r' < r)%nat -> (forall i : nat, (c <= i < n)%nat -> (M r' i =? 1) = false) -> (c < k < n)%nat ->
(M r k =? 1) = true -> (col_add_right_ones (col_swapF2 M c k) r c r' c =? 1) = false.
Proof. intros m n M r r' c k H H0 H1 H2 H3.
apply col_add_right_ones_one_bool_false_pivot_col_is_col_inclusive_domain; try lia; auto.
- intros i H4. unfold col_swapF2.
bdestruct_all; subst; apply H1; try lia.
Qed.