-
Notifications
You must be signed in to change notification settings - Fork 10
/
VecSet.v
2363 lines (2166 loc) · 84.7 KB
/
VecSet.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
(** In this file, we define more advanced linear algebra concepts such as bases, linear independence, etc... *)
Require Import Psatz.
Require Import Reals.
Require Export RowColOps.
Local Open Scope nat_scope.
(***************************************************************************)
(** * Defining properties which are invarient under column operations, etc... *)
(***************************************************************************)
Inductive invr_col_swap : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_swap : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m x y : nat) (T : Matrix n m), x < m -> y < m -> P n m T -> P n m (col_swap T x y))
-> invr_col_swap P.
Inductive invr_col_scale : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_scale : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m x : nat) (T : Matrix n m) (c : C), c <> C0 -> P n m T -> P n m (col_scale T x c))
-> invr_col_scale P.
Inductive invr_col_add : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_add : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m x y : nat) (T : Matrix n m) (c : C),
x <> y -> x < m -> y < m -> P n m T -> P n m (col_add T x y c))
-> invr_col_add P.
Inductive invr_col_add_many : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_add_many : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m col : nat) (T : Matrix n m) (as' : Vector m),
col < m -> as' col 0 = C0 -> P n m T -> P n m (col_add_many T as' col))
-> invr_col_add_many P.
Inductive invr_col_add_each : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_add_each : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m col : nat) (T : Matrix n m) (as' : Matrix 1 m),
col < m -> WF_Matrix as' -> P n m T -> P n m (col_add_each T (make_col_val as' col C0) col))
-> invr_col_add_each P.
Inductive invr_pad1 : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| invr_p : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m : nat) (T : Matrix n m) (c : C), c <> C0 -> P (S n) (S m) (pad1 T c) -> P n m T)
-> invr_pad1 P.
Inductive prop_zero_true : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| PZT : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m : nat) (T : Matrix n m), (exists i, i < m /\ get_col T i = Zero) -> P n m T) ->
prop_zero_true P.
Inductive prop_zero_false : (forall n m : nat, Matrix n m -> Prop) -> Prop :=
| PZF : forall (P : (forall n m : nat, Matrix n m -> Prop)),
(forall (n m : nat) (T : Matrix n m), (exists i, i < m /\ get_col T i = Zero) -> ~ (P n m T)) ->
prop_zero_false P.
(* Ltac to help apply these properties of (Mat -> Prop)s *)
Ltac apply_mat_prop tac :=
let H := fresh "H" in
assert (H := tac); inversion H; subst; try apply H.
Lemma mat_prop_col_add_many_some : forall (e n m col : nat) (P : forall n m : nat, Matrix n m -> Prop)
(T : Matrix n m) (as' : Vector m),
(skip_count col e) < m -> col < m ->
(forall i : nat, (skip_count col e) < i -> as' i 0 = C0) -> as' col 0 = C0 ->
invr_col_add P ->
P n m T -> P n m (col_add_many T as' col).
Proof. induction e as [| e].
- intros.
inversion H3; subst.
rewrite (col_add_many_col_add _ _ _ (skip_count col 0));
try lia; try easy.
apply H5; try lia.
apply skip_count_not_skip.
assert (H' : (col_add_many T (make_row_val as' (skip_count col O) C0) col) = T).
{ prep_matrix_equality.
unfold col_add_many, make_row_val, skip_count, gen_new_col, scale in *.
bdestruct (y =? col); try lia; try easy.
rewrite <- Cplus_0_l.
rewrite Cplus_comm.
apply Cplus_simplify; try easy.
rewrite Msum_Csum.
apply (@big_sum_0_bounded C C_is_monoid); intros.
destruct col; simpl in *.
bdestruct (x0 =? 1); try lca.
destruct x0; try rewrite H2; try rewrite H1; try lca; try lia.
destruct x0; try lca; rewrite H1; try lca; lia. }
rewrite H'; easy.
apply skip_count_not_skip.
- intros.
inversion H3; subst.
rewrite (col_add_many_col_add _ _ _ (skip_count col (S e)));
try lia; try easy.
apply H5; try lia.
apply skip_count_not_skip.
apply IHe; try lia; try easy; auto with wf_db.
assert (H' : e < S e). lia.
apply (skip_count_mono col) in H'.
lia.
intros.
unfold skip_count, make_row_val in *.
bdestruct (e <? col); bdestruct (S e <? col); try lia.
bdestruct (i =? S e); try easy; try apply H1; try lia.
bdestruct (i =? S e); bdestruct (i =? S (S e)); try lia; try easy.
bdestruct (S e =? col); try lia. rewrite H9, H11. apply H2.
apply H1; lia.
bdestruct (i =? S e); bdestruct (i =? S (S e)); try lia; try easy.
apply H1; lia.
unfold make_row_val, skip_count.
bdestruct (S e <? col); try lia; bdestruct (col =? S e); bdestruct (col =? S (S e));
try lia; try easy.
apply skip_count_not_skip.
Qed.
Lemma invr_col_add_col_add_many : forall (P : forall n m : nat, Matrix n m -> Prop),
invr_col_add P -> invr_col_add_many P.
Proof. intros.
inversion H; subst.
apply invr_add_many; intros.
destruct m; try lia.
destruct m.
- assert (H' : as' == Zero).
{ unfold mat_equiv; intros.
destruct col; destruct i; destruct j; try lia.
easy. }
rewrite <- col_add_many_0; easy.
- rewrite (col_add_many_mat_equiv _ _ (make_WF as'));
try apply mat_equiv_make_WF.
bdestruct (col =? S m).
+ apply (mat_prop_col_add_many_some m); try lia; try easy.
unfold skip_count. bdestruct (m <? col); lia.
intros.
unfold skip_count in H5; rewrite H4 in H5.
bdestruct (m <? S m); try lia.
unfold make_WF.
bdestruct (i <? S (S m)); bdestruct (0 <? 1); try lia; try easy.
bdestruct (i =? S m); try lia.
rewrite H9, <- H4; easy.
unfold make_WF.
bdestruct_all; auto.
+ apply (mat_prop_col_add_many_some m); try lia; try easy.
unfold skip_count.
bdestruct (m <? col); try lia.
intros. unfold make_WF.
unfold skip_count in H5.
bdestruct (m <? col); try lia.
bdestruct (i <? S (S m)); try lia; try easy.
unfold make_WF.
bdestruct_all; auto.
Qed.
Lemma mat_prop_col_add_each_some : forall (e n m col : nat) (P : forall n m : nat, Matrix n m -> Prop)
(as' : Matrix 1 m) (T : Matrix n m),
WF_Matrix as' -> (skip_count col e) < m -> col < m ->
(forall i : nat, (skip_count col e) < i -> as' 0 i = C0) -> as' 0 col = C0 ->
invr_col_add P ->
P n m T -> P n m (col_add_each T as' col).
Proof. induction e as [| e].
- intros.
inversion H4; subst.
rewrite (col_add_each_col_add _ _ _ (skip_count col 0)); try lia.
apply H6; try lia.
assert (H' := skip_count_not_skip col 0). auto.
assert (H' : (make_col_val as' (skip_count col 0) C0) = Zero).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold make_col_val, skip_count in *.
destruct i; try lia.
destruct col; simpl in *.
all : destruct j; try easy; simpl.
destruct j; try easy; simpl.
all : apply H2; lia. }
rewrite H'.
rewrite <- col_add_each_0; easy.
apply skip_count_not_skip.
intros x. destruct x; try easy.
apply H; lia.
- intros.
inversion H4; subst.
rewrite (col_add_each_col_add _ _ _ (skip_count col (S e))); try lia.
apply H6; try lia.
assert (H' := skip_count_not_skip col (S e)). auto.
apply IHe; try lia; try easy; auto with wf_db.
assert (H' : e < S e). lia.
apply (skip_count_mono col) in H'.
lia.
intros.
unfold skip_count, make_col_val in *.
bdestruct (e <? col); bdestruct (S e <? col); try lia.
bdestruct (i =? S e); try easy; try apply H2; try lia.
bdestruct (i =? S e); bdestruct (i =? S (S e)); try lia; try easy.
bdestruct (S e =? col); try lia. rewrite H10, H12. apply H3.
apply H2; lia.
bdestruct (i =? S e); bdestruct (i =? S (S e)); try lia; try easy.
apply H2; lia.
unfold make_col_val, skip_count.
bdestruct (S e <? col); try lia; bdestruct (col =? S e); bdestruct (col =? S (S e));
try lia; try easy.
assert (H' := skip_count_not_skip col (S e)). auto.
intros. destruct x; try easy.
apply H; lia.
Qed.
Lemma invr_col_add_col_add_each : forall (P : forall n m : nat, Matrix n m -> Prop),
invr_col_add P -> invr_col_add_each P.
Proof. intros.
inversion H; subst.
apply invr_add_each; intros.
destruct m; try lia.
destruct m.
- assert (H' : make_col_val as' col C0 = Zero).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
destruct col; destruct i; destruct j; try lia.
unfold make_col_val.
easy. }
rewrite H'.
rewrite <- col_add_each_0; easy.
- bdestruct (col =? S m).
+ apply (mat_prop_col_add_each_some m); try lia; try easy; auto with wf_db.
unfold skip_count. bdestruct (m <? col); lia.
intros.
unfold make_col_val.
bdestruct (i =? col); try lia; try easy.
rewrite H4 in H5; unfold skip_count in H5.
bdestruct (m <? S m); try lia.
rewrite H2; try lia; easy.
unfold make_col_val.
bdestruct (col =? col); try lia; easy.
+ apply (mat_prop_col_add_each_some m); try lia; try easy; auto with wf_db.
unfold skip_count.
bdestruct (m <? col); try lia.
intros. unfold make_col_val.
bdestruct (i =? col); try lia; try easy.
unfold skip_count in H5.
bdestruct (m <? col); try lia.
apply H2; lia.
unfold make_col_val.
bdestruct (col =? col); try lia; easy.
Qed.
Lemma mat_prop_col_swap_conv : forall {n m} (P : forall n m : nat, Matrix n m -> Prop) (T : Matrix n m) (x y : nat),
invr_col_swap P ->
x < m -> y < m ->
P n m (col_swap T x y) -> P n m T.
Proof. intros.
inversion H; subst.
rewrite (col_swap_inv T x y).
apply H3; easy.
Qed.
Lemma mat_prop_col_scale_conv : forall {n m} (P : forall n m : nat, Matrix n m -> Prop)
(T : Matrix n m) (x : nat) (c : C),
invr_col_scale P ->
c <> C0 ->
P n m (col_scale T x c) -> P n m T.
Proof. intros.
inversion H; subst.
rewrite (col_scale_inv T x c); try easy.
apply H2; try apply nonzero_div_nonzero; easy.
Qed.
Lemma mat_prop_col_add_conv : forall {n m} (P : forall n m : nat, Matrix n m -> Prop)
(T : Matrix n m) (x y : nat) (c : C),
invr_col_add P ->
x <> y -> x < m -> y < m ->
P n m (col_add T x y c) -> P n m T.
Proof. intros.
inversion H; subst.
rewrite (col_add_inv T x y c); try easy.
apply H4; try easy.
Qed.
Lemma mat_prop_col_add_many_conv : forall {n m} (P : forall n m : nat, Matrix n m -> Prop)
(T : Matrix n m) (col : nat) (as' : Vector m),
invr_col_add P ->
col < m -> as' col 0 = C0 ->
P n m (col_add_many T as' col) -> P n m T.
Proof. intros.
apply invr_col_add_col_add_many in H.
inversion H; subst.
rewrite (col_add_many_inv T as' col); try easy.
apply H3; try easy.
unfold scale; rewrite H1.
lca.
Qed.
Lemma mat_prop_col_add_each_conv : forall {n m} (P : forall n m : nat, Matrix n m -> Prop)
(T : Matrix n m) (col : nat) (as' : Matrix 1 m),
invr_col_add P ->
col < m -> WF_Matrix as' ->
P n m (col_add_each T (make_col_val as' col C0) col) -> P n m T.
Proof. intros.
apply invr_col_add_col_add_each in H.
inversion H; subst.
rewrite (col_add_each_inv _ as' col); try easy.
apply H3; try easy.
auto with wf_db.
Qed.
(** * We can now define some invariants for Determinant *)
Definition det_neq_0 {n m : nat} (A : Matrix n m) : Prop :=
n = m /\ @Determinant n A <> C0.
Definition det_eq_c (c : C) {n m : nat} (A : Matrix n m) : Prop :=
n = m /\ @Determinant n A = c.
Lemma det_neq_0_swap_invr : invr_col_swap (@det_neq_0).
Proof. apply invr_swap; intros.
destruct H1; subst.
split; auto.
bdestruct (x =? y); subst.
- rewrite col_swap_same.
easy.
- rewrite Determinant_swap; auto.
unfold not; intros.
apply H2.
rewrite <- (Cmult_1_l _).
replace C1 with ((-C1) * (-C1))%C by lca.
rewrite <- Cmult_assoc, H3.
lca.
Qed.
Lemma det_neq_0_scale_invr : invr_col_scale (@det_neq_0).
Proof. apply invr_scale; intros.
destruct H0; subst.
split; auto.
bdestruct (x <? m).
- rewrite Determinant_col_scale; auto.
apply Cmult_neq_0; easy.
- rewrite Det_make_WF in *.
assert (H' : (make_WF T) = (make_WF (col_scale T x c))).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv, make_WF, col_scale; intros.
bdestruct_all; easy. }
rewrite <- H'; easy.
Qed.
Lemma det_neq_0_add_invr : invr_col_add (@det_neq_0).
Proof. apply invr_add; intros.
destruct H2; subst.
split; auto.
rewrite Determinant_col_add; easy.
Qed.
Lemma det_neq_0_pad1_invr : invr_pad1 (@det_neq_0).
Proof. apply invr_p; intros.
destruct H0; apply eq_add_S in H0; subst.
split; auto.
destruct m.
- apply C1_neq_C0.
- unfold not; intros; apply H1.
rewrite Det_simplify.
apply (@big_sum_0_bounded C C_is_monoid); intros.
destruct x.
+ rewrite <- get_minor_pad1, H0; lca.
+ unfold pad1, col_wedge, row_wedge, e_i, scale.
bdestruct_all; simpl.
lca.
Qed.
Lemma det_neq_0_pzf : prop_zero_false (@det_neq_0).
Proof. apply PZF; intros.
unfold not; intros.
destruct H0; subst.
destruct H as [i [H H0]].
apply H1.
rewrite (col_0_Det_0 T i);
easy.
Qed.
Lemma det_0_swap_invr : invr_col_swap (@det_eq_c C0).
Proof. apply invr_swap; intros.
unfold det_eq_c in *; destruct H1; subst.
split; auto.
bdestruct (x =? y); subst.
- rewrite col_swap_same.
easy.
- rewrite Determinant_swap; auto.
rewrite H2; lca.
Qed.
Lemma det_0_scale_invr : invr_col_scale (@det_eq_c C0).
Proof. apply invr_scale; intros.
unfold det_eq_c in *; destruct H0; subst.
split; auto.
bdestruct (x <? m).
- rewrite Determinant_col_scale; auto.
rewrite H1; lca.
- rewrite Det_make_WF in *.
assert (H' : (make_WF T) = (make_WF (col_scale T x c))).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv, make_WF, col_scale; intros.
bdestruct_all; easy. }
rewrite <- H'; easy.
Qed.
Lemma det_c_add_invr : forall (c : C), invr_col_add (@det_eq_c c).
Proof. intros.
apply invr_add; intros.
unfold det_eq_c in *; destruct H2; subst.
split; auto.
apply Determinant_col_add; easy.
Qed.
Lemma det_0_pad1_invr : invr_pad1 (@det_eq_c C0).
Proof. apply invr_p; intros.
destruct H0; apply eq_add_S in H0; subst.
split; auto.
destruct m.
- simpl in H1.
unfold pad1, col_wedge, row_wedge, e_i, scale in H1;
simpl in H1.
rewrite Cmult_1_r in H1.
easy.
- rewrite Det_simplify in H1.
assert (H' : (c * Determinant (get_minor (pad1 T c) 0 0) = C0)%C).
{ rewrite <- H1, (big_sum_unique (c * Determinant (get_minor (pad1 T c) 0 0))%C).
easy.
exists 0. split; try lia.
split. simpl parity.
apply Cmult_simplify; try easy.
unfold pad1, col_wedge, row_wedge, e_i, scale.
bdestruct_all; lca.
intros.
unfold pad1, col_wedge, row_wedge, e_i, scale.
bdestruct_all; lca. }
rewrite <- get_minor_pad1 in H'.
destruct (Ceq_dec (Determinant T) C0); try easy.
apply (Cmult_neq_0 c _) in n; easy.
Qed.
#[export] Hint Resolve det_neq_0_swap_invr det_neq_0_scale_invr det_neq_0_add_invr det_neq_0_pad1_invr : invr_db.
#[export] Hint Resolve det_neq_0_pzf det_0_swap_invr det_0_scale_invr det_c_add_invr det_0_pad1_invr : invr_db.
Lemma Determinant_col_add_many : forall (n col : nat) (A : Square n) (as' : Vector n),
col < n -> as' col 0 = C0 ->
Determinant A = Determinant (col_add_many A as' col).
Proof. intros.
assert (H' := det_c_add_invr (Determinant A)).
apply invr_col_add_col_add_many in H'.
inversion H'; subst.
apply (H1 n n col A as') in H; try easy.
unfold det_eq_c in *.
destruct H; easy.
Qed.
Lemma Determinant_col_add_each : forall (n col : nat) (as' : Matrix 1 n)
(A : Square n),
col < n -> WF_Matrix as' -> as' 0 col = C0 ->
Determinant A = Determinant (col_add_each A as' col).
Proof. intros.
assert (H' := det_c_add_invr (Determinant A)).
apply invr_col_add_col_add_each in H'.
inversion H'; subst.
assert (H4 : (make_col_val as' col C0) = as').
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold make_col_val.
destruct i; try lia.
bdestruct_all; subst; easy. }
apply (H2 n n col A as') in H; try easy.
unfold det_eq_c in *.
destruct H; rewrite <- H3.
rewrite H4; easy.
Qed.
(***********************************************************)
(** * Defining linear independence, and proving lemmas etc... *)
(***********************************************************)
Definition linearly_independent {n m} (T : Matrix n m) : Prop :=
forall (a : Vector m), WF_Matrix a -> @Mmult n m 1 T a = Zero -> a = Zero.
Definition linearly_dependent {n m} (T : Matrix n m) : Prop :=
exists (a : Vector m), WF_Matrix a /\ a <> Zero /\ @Mmult n m 1 T a = Zero.
Lemma lindep_implies_not_linindep : forall {n m} (T : Matrix n m),
linearly_dependent T -> ~ (linearly_independent T).
Proof. unfold not, linearly_dependent, linearly_independent in *.
intros.
destruct H as [a [H1 [H2 H3]]].
apply H0 in H1; easy.
Qed.
Lemma not_lindep_implies_linindep : forall {n m} (T : Matrix n m),
not (linearly_dependent T) -> linearly_independent T.
Proof. unfold not, linearly_dependent, linearly_independent in *.
intros.
destruct (vec_equiv_dec a Zero).
- apply mat_equiv_eq; auto with wf_db.
- assert (H2 : (exists a : Vector m, WF_Matrix a /\ a <> Zero /\ T × a = Zero)).
{ exists a.
split; auto.
split; try easy.
unfold not; intros.
apply n0.
rewrite H2.
easy. }
apply H in H2.
easy.
Qed.
Lemma lin_indep_vec : forall {n} (v : Vector n),
WF_Matrix v -> v <> Zero -> linearly_independent v.
Proof. intros.
unfold linearly_independent.
intros.
assert (H' : v × a = (a 0 0) .* v).
{ apply mat_equiv_eq; auto with wf_db.
unfold Mmult, scale, mat_equiv.
intros. simpl.
destruct j; try lia; lca. }
assert (H1' := H).
apply nonzero_vec_nonzero_elem in H1'; try easy.
destruct H1' as [x H1'].
destruct (Ceq_dec (a 0 0) C0).
+ prep_matrix_equality.
destruct x0. destruct y.
rewrite e; easy.
all : apply H1; lia.
+ assert (H'' : ((a 0 0) .* v) x 0 = C0).
{ rewrite <- H'. rewrite H2; easy. }
unfold scale in H''.
assert (H3 : (a 0 0 * v x 0)%C <> C0).
{ apply Cmult_neq_0; easy. }
easy.
Qed.
Lemma invertible_l_implies_linind : forall {n} (A B : Square n),
A × B = I n -> linearly_independent B.
Proof. intros.
unfold linearly_independent. intros.
rewrite <- (Mmult_1_l _ _ a); try easy.
rewrite <- H.
rewrite Mmult_assoc, H1.
rewrite Mmult_0_r.
reflexivity.
Qed.
Lemma lin_indep_col_reduce : forall {m n} (A : Matrix m (S n)) (i : nat),
i <= n ->
linearly_independent A ->
linearly_independent (reduce_col A i).
Proof. intros.
unfold linearly_independent in *.
intros.
apply (row_wedge_zero _ i).
apply H0.
apply WF_row_wedge; try easy.
prep_matrix_equality.
rewrite <- H2.
unfold Mmult, row_wedge, Zero.
replace n with (i + (n - i))%nat by lia.
replace (S (i + (n - i)))%nat with (i + 1 + (n - i))%nat by lia.
do 3 rewrite big_sum_sum; simpl.
bdestruct_all; simpl.
rewrite Cmult_0_r, 2 Cplus_0_r.
apply f_equal_gen; try apply f_equal.
all : apply big_sum_eq_bounded; intros.
all : unfold reduce_col.
all : bdestruct_all; try easy.
repeat (apply f_equal_gen; try lia).
all : easy.
Qed.
(* more general than lin_indep_col_reduce_n *)
Lemma lin_indep_smash : forall {m n2 n1} (A1 : Matrix m n1) (A2 : Matrix m n2),
linearly_independent (smash A1 A2) -> linearly_independent A1.
Proof. induction n2 as [| n2'].
- intros.
unfold linearly_independent in *.
intros. assert (H' : (n1 + 0)%nat = n1). lia.
rewrite H' in *.
apply H; try easy.
rewrite <- H1.
unfold smash, Mmult.
prep_matrix_equality.
apply big_sum_eq_bounded.
intros.
bdestruct (x0 <? n1); try lia; easy.
- intros.
assert (H1 := @lin_indep_col_reduce m (n1 + n2') (smash A1 A2) (n1 + n2')).
rewrite <- plus_n_Sm in H.
apply H1 in H; auto.
rewrite smash_reduce in H.
apply (IHn2' n1 A1 (reduce_col A2 n2')).
easy.
Qed.
Lemma lin_dep_mult_r : forall {n m o} (A : Matrix n m) (B : Matrix m o),
linearly_dependent B -> linearly_dependent (A × B).
Proof. intros.
unfold linearly_dependent in *.
destruct H as [a [H [H0 H1]]].
exists a.
repeat split; auto.
rewrite Mmult_assoc, H1, Mmult_0_r; easy.
Qed.
Lemma lin_dep_col_wedge : forall {m n} (A : Matrix m n) (v : Vector m) (i : nat),
i <= n ->
linearly_dependent A ->
linearly_dependent (col_wedge A v i).
Proof. intros.
unfold linearly_dependent in *.
destruct H0 as [a [H0 [H2 H3]]].
exists (row_wedge a Zero i).
split; auto with wf_db.
split. unfold not; intros; apply H2.
apply (row_wedge_zero _ i).
auto.
rewrite wedge_mul; auto.
Qed.
(* proving invr properties for linearly_independent *)
Lemma lin_indep_swap_invr : invr_col_swap (@linearly_independent).
Proof. apply invr_swap; intros.
unfold linearly_independent in *.
intros.
rewrite (row_swap_inv a x y) in H3.
rewrite <- (swap_preserves_mul T (row_swap a x y) x y) in H3; try easy.
apply H1 in H3.
rewrite (row_swap_inv a x y).
rewrite H3.
prep_matrix_equality.
unfold row_swap.
bdestruct (x0 =? x); bdestruct (x0 =? y); easy.
apply WF_row_swap; easy.
Qed.
Lemma lin_indep_scale_invr : invr_col_scale (@linearly_independent).
Proof. apply invr_scale; intros.
unfold linearly_independent in *.
intros.
rewrite <- scale_preserves_mul in H2.
apply H0 in H2.
rewrite (row_scale_inv _ x c); try easy.
rewrite H2.
prep_matrix_equality.
unfold row_scale.
bdestruct (x0 =? x);
lca.
apply WF_row_scale; easy.
Qed.
Lemma lin_indep_add_invr : invr_col_add (@linearly_independent).
Proof. apply invr_add; intros.
unfold linearly_independent in *.
intros.
rewrite <- add_preserves_mul in H4; try easy.
apply H2 in H4.
rewrite (row_add_inv a y x c); try lia.
rewrite H4.
prep_matrix_equality.
unfold row_add.
bdestruct (x0 =? y);
lca.
apply WF_row_add; easy.
Qed.
Lemma lin_indep_pad1_invr : invr_pad1 (@linearly_independent).
Proof. apply invr_p; intros.
unfold linearly_independent in *.
intros.
assert (H3 : @Mmult (S n) (S m) 1 (pad1 T c) (row_wedge a Zero 0) = Zero).
{ prep_matrix_equality.
destruct x. unfold Mmult.
unfold Zero. apply (@big_sum_0_bounded C C_is_monoid).
intros.
unfold pad1, row_wedge, col_wedge, e_i, scale.
bdestruct (x <? 0); bdestruct (x =? 0); try lia. lca.
lca.
assert (p : @Zero (S n) 1 (S x) y = C0).
easy.
assert (H2' : (T × a) x y = C0).
rewrite H2; easy.
rewrite p.
rewrite <- H2'.
unfold Mmult. rewrite <- big_sum_extend_l.
rewrite <- Cplus_0_l.
apply Cplus_simplify.
unfold pad1, row_wedge, col_wedge, e_i.
bdestruct (x <? 0); bdestruct (x =? 0); try lia.
rewrite H4; simpl. lca.
rewrite Sn_minus_1. lca.
apply big_sum_eq_bounded; intros.
rewrite pad1_conv.
unfold row_wedge.
rewrite Sn_minus_1.
easy. }
apply H0 in H3.
prep_matrix_equality.
assert (H4 : row_wedge a Zero 0 (S x) y = C0).
rewrite H3; easy.
unfold Zero. rewrite <- H4.
unfold row_wedge.
rewrite Sn_minus_1.
easy.
apply WF_row_wedge; try lia; easy.
Qed.
Lemma lin_indep_pzf : prop_zero_false (@linearly_independent).
Proof. apply PZF; intros.
unfold not; intros.
unfold linearly_independent in *.
destruct H as [i [H H1]].
assert (H2 : T × @e_i m i = Zero).
{ prep_matrix_equality.
unfold Mmult, Zero, e_i; simpl.
apply (@big_sum_0_bounded C C_is_monoid); intros.
bdestruct_all; try lca;
rewrite <- get_col_conv; subst.
rewrite H1; lca. }
apply H0 in H2; auto with wf_db.
assert (H3 : @e_i m i i 0 = C0).
rewrite H2; easy.
unfold e_i in H3.
apply C1_neq_C0.
rewrite <- H3.
bdestruct_all; easy.
Qed.
Lemma lin_dep_swap_invr : invr_col_swap (@linearly_dependent).
Proof. apply invr_swap; intros.
unfold linearly_dependent in *.
destruct H1 as [a [H1 [H2 H3]]].
rewrite (row_swap_inv a x y) in H3.
rewrite (col_swap_inv T x y) in H3.
rewrite <- (swap_preserves_mul _ (row_swap a x y) x y) in H3; try easy.
exists (row_swap a x y).
split; auto with wf_db.
split; try easy; unfold not in *.
intros; apply H2.
rewrite (row_swap_inv a x y).
rewrite H4.
prep_matrix_equality.
unfold Zero, row_swap.
bdestruct (x0 =? x); bdestruct (x0 =? y); easy.
Qed.
Lemma lin_dep_scale_invr : invr_col_scale (@linearly_dependent).
Proof. intros.
apply invr_scale; intros.
unfold linearly_dependent in *.
destruct H0 as [a [H1 [H2 H3]]].
exists (row_scale a x (/ c)).
split; auto with wf_db.
split. unfold not; intros.
apply H2.
rewrite (row_scale_inv _ x (/ c)); try easy.
rewrite H0.
prep_matrix_equality.
unfold row_scale, Zero.
bdestruct (x0 =? x); try lia; lca.
apply nonzero_div_nonzero; easy.
rewrite scale_preserves_mul.
rewrite <- (col_scale_inv T x c); easy.
Qed.
Lemma lin_dep_add_invr : invr_col_add (@linearly_dependent).
Proof. intros.
apply invr_add; intros.
unfold linearly_dependent in *.
destruct H2 as [a [H2 [H3 H4]]].
exists (row_add a y x (- c)).
split; auto with wf_db.
split. unfold not; intros; apply H3.
rewrite (row_add_inv a y x (- c)); try lia.
rewrite H5.
unfold row_add, Zero.
prep_matrix_equality.
bdestruct (x0 =? y); lca.
rewrite add_preserves_mul; try easy.
rewrite <- (col_add_inv T x y c); try lia; easy.
Qed.
Lemma lin_dep_pzt : prop_zero_true (@linearly_dependent).
Proof. apply PZT; intros.
unfold linearly_dependent in *; intros.
destruct H as [i [H0 H1]].
exists (@e_i m i).
split; auto with wf_db.
split.
unfold not; intros.
assert (H' : (@e_i m i) i 0 = C0).
{ rewrite H; easy. }
unfold e_i in H'; simpl in H'.
bdestruct (i =? i); bdestruct (i <? m); try lia.
simpl in H'.
apply C1_neq_C0.
easy.
rewrite <- matrix_by_basis; easy.
Qed.
#[export] Hint Resolve lin_indep_swap_invr lin_indep_scale_invr lin_indep_add_invr lin_indep_pad1_invr : invr_db.
#[export] Hint Resolve lin_indep_pzf lin_dep_swap_invr lin_dep_scale_invr lin_dep_add_invr lin_dep_pzt : invr_db.
(** we begin to prove that if n < m, then any Matrix n m is linearly_dependent. This is quite useful, as we get a vector that can be used to cancel a column *)
Lemma lin_dep_gen_elem : forall {m n} (T : Matrix n (S m)),
WF_Matrix T -> linearly_dependent T ->
(exists i, i < (S m) /\
(exists v : Vector m, WF_Matrix v /\
@Mmult n m 1 (reduce_col T i) v = (-C1) .* (get_col T i))).
Proof. intros.
unfold linearly_dependent in H.
destruct H0 as [a [H1 [H2 H3]]].
assert (H4 := H1).
apply nonzero_vec_nonzero_elem in H4; try easy.
destruct H4 as [x H4].
exists x.
bdestruct (x <? S m).
- split; try easy.
exists ( (/ (a x 0)) .* (reduce_row a x)).
split; auto with wf_db.
apply mat_equiv_eq; auto with wf_db.
rewrite Mscale_mult_dist_r.
unfold mat_equiv; intros.
unfold Mmult, scale.
assert (H' : (big_sum (fun y : nat => reduce_col T x i y * reduce_row a x y j) m +
(a x 0) * get_col T x i j = @Zero n 1 i j)%C).
{ rewrite <- H3. unfold Mmult.
assert (H'' : m = x + (m - x)). lia.
rewrite H''.
rewrite big_sum_sum.
rewrite <- H''.
assert (H2' : S m = x + S (m - x)). lia.
rewrite H2'.
rewrite big_sum_sum.
rewrite <- big_sum_extend_l.
rewrite <- Cplus_assoc.
apply Cplus_simplify.
apply big_sum_eq_bounded.
intros. unfold reduce_col, reduce_row.
bdestruct (x0 <? x); try lia; easy.
rewrite Cplus_comm.
apply Cplus_simplify.
unfold get_col.
bdestruct (j =? 0); try lia.
assert (p0 : x + 0 = x). lia.
rewrite p0, H7; lca.
apply big_sum_eq_bounded.
intros.
unfold reduce_col, reduce_row.
bdestruct (x + x0 <? x); try lia.
assert (p1 : (1 + (x + x0)) = (x + S x0)). lia.
rewrite p1. easy. }
assert (H1' : (big_sum (fun y : nat => reduce_col T x i y * reduce_row a x y j) m +
(a x 0) * get_col T x i j + (a x 0) * (- (get_col T x i j)) =
(- (a x 0)) * get_col T x i j)%C).
{ rewrite H'. lca. }
rewrite <- Cplus_assoc in H1'.
rewrite <- Cmult_plus_distr_l in H1'.
rewrite Cplus_opp_r in H1'.
rewrite Cmult_0_r, Cplus_0_r in H1'.
rewrite H1'.
rewrite Cmult_assoc.
rewrite <- Copp_mult_distr_r.
rewrite Cinv_l; easy.
- assert (H' : a x 0 = C0).
apply H1; try lia.
easy.
Qed.
Lemma gt_dim_lindep_ind_step1 : forall {n m} (T : Matrix (S n) (S m)) (col : nat),
WF_Matrix T -> col <= m -> get_col T col = @e_i (S n) 0 ->
linearly_dependent (reduce_row (reduce_col T col) 0) -> linearly_dependent T.
Proof. intros.
apply (mat_prop_col_add_each_conv _ _ col (-C1 .* (get_row T 0)));
auto with wf_db; try lia.
apply lin_dep_add_invr.
unfold linearly_dependent in *.
destruct H2 as [a [H3 [H4 H5]]].
repeat rewrite Sn_minus_1 in *.
exists (row_wedge a (@Zero 1 1) col).
split; auto with wf_db.
split.
+ unfold not in *.
intros. apply H4.
prep_matrix_equality.
bdestruct (x <? col).
assert (H' : (row_wedge a Zero col) x y = C0 ).
{ rewrite H2. easy. }
unfold row_wedge in *.
bdestruct (x <? col); try lia. easy.
assert (H' : (row_wedge a Zero col) (S x) y = C0 ).
{ rewrite H2. easy. }
unfold row_wedge in *.
bdestruct (S x <? col); bdestruct (S x =? col); try lia.
rewrite Sn_minus_1 in *; easy.
+ repeat rewrite Sn_minus_1 in *.
apply mat_equiv_eq; auto with wf_db.
apply WF_mult; auto with wf_db.
unfold mat_equiv; intros.
assert (H' : (get_col T col) i 0 = @e_i (S n) 0 i 0).
{ rewrite H1. easy. }
unfold col_add_each, make_col_val, get_row, Mmult, Mplus, get_col,
scale, row_wedge.
destruct i.
* unfold get_col, e_i in H'; simpl in H'.
rewrite H'. unfold Zero.
apply (@big_sum_0_bounded C C_is_monoid).
intros; simpl.
bdestruct (x =? col); bdestruct (x <? col); try lia; lca.
* unfold get_col, e_i in H'; simpl in H'.
assert (H0' : (reduce_row (reduce_col T col) 0 × a) i j = @Zero (S n) 1 (S i) j).
repeat rewrite Sn_minus_1 in *; rewrite H5. easy.
rewrite <- H0'.
unfold Mmult, reduce_row, reduce_col.
repeat rewrite Sn_minus_1 in *.
assert (p : S m = col + (S m - col)). lia.
rewrite p, big_sum_sum.
assert (p1 : S m - col = S (m - col)). lia.
rewrite p1, <- big_sum_extend_l.
simpl. bdestruct (col + 0 =? col); bdestruct (col + 0 <? col); try lia.
assert (p2 : m = col + (m - col)). lia.
rewrite p2, big_sum_sum, <- p2.
apply Cplus_simplify.
apply big_sum_eq_bounded; intros.
bdestruct (x <? col); bdestruct (x =? col); try lia.
rewrite H'. lca.
rewrite <- Cplus_0_l.
apply Cplus_simplify; try lca.
apply big_sum_eq_bounded; intros.
bdestruct (col + S x <? col); bdestruct (col + S x =? col);
bdestruct (col + x <? col); try lia.
assert (p4 : col + S x - 1 = col + x). lia.
assert (p5 : S (col + x) = col + S x). lia.
rewrite H', p4, p5. lca.
Qed.
Lemma gt_dim_lindep_ind_step2 : forall {n m} (T : Matrix (S n) (S m))
(v : Vector (S m)) (col : nat),
WF_Matrix T -> col < S m -> v col 0 = C0 ->
reduce_col (reduce_row T 0) col × (reduce_row v col) =
- C1 .* get_col (reduce_row T 0) col ->
linearly_dependent (reduce_row (reduce_col T col) 0) -> linearly_dependent T.
Proof. intros.
assert (H' := @col_add_many_cancel n m (reduce_row T 0) v col).
assert (H0' : forall i : nat, @col_add_many n (S m) (reduce_row T 0) v col i col = C0).
{ apply H'; try easy. }
apply (mat_prop_col_add_many_conv _ _ col v); try easy.
apply lin_dep_add_invr.
destruct (Ceq_dec ((col_add_many T v col) 0 col) C0).
- apply_mat_prop (@lin_dep_pzt).
apply H5; exists col.
split; auto.
prep_matrix_equality. unfold get_col.
destruct y; try easy; simpl.
destruct x; try easy. unfold Zero.
rewrite <- (H0' x).
unfold col_add_many, reduce_row.
bdestruct (col =? col); bdestruct (x <? 0); try lia.
apply Cplus_simplify.
easy. unfold gen_new_col.
do 2 rewrite Msum_Csum.
apply big_sum_eq_bounded; intros.
unfold scale, get_col; lca.
- apply (mat_prop_col_scale_conv _ _ col (/ (col_add_many T v col 0 col)));
try apply nonzero_div_nonzero; try easy.
apply lin_dep_scale_invr.
apply (gt_dim_lindep_ind_step1 _ col); try lia; auto with wf_db.
apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold get_col, e_i.
bdestruct (j =? 0); bdestruct (i =? 0); bdestruct (i <? S n);
try lia; simpl.
+ unfold col_scale. bdestruct (col =? col); try lia.
rewrite H7. rewrite Cinv_l; easy.
+ unfold col_scale. bdestruct (col =? col); try lia.
destruct i; try easy.
assert (r : col_add_many T v col (S i) col =
col_add_many (reduce_row T 0) v col i col).
{ unfold reduce_row, col_add_many, gen_new_col, get_col, scale.
bdestruct (col =? col); bdestruct (i <? 0); try lia.
apply Cplus_simplify; try easy.
do 2 rewrite Msum_Csum.
apply big_sum_eq_bounded; intros.
bdestruct (i <? 0); try lia; easy. }
rewrite r.
rewrite (H0' i); lca.