forked from inQWIRE/QuantumLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VecSet.v
2668 lines (2454 loc) · 96.9 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 Matrix.
(************************************)
(** * some preliminary defs and lemmas *)
(************************************)
Local Open Scope nat_scope.
Definition e_i {n : nat} (i : nat) : Vector n :=
fun x y => (if (x =? i) && (x <? n) && (y =? 0) then C1 else C0).
(* using previous def's, takes matrix and increases its rank by 1 (assuming c <> 0) *)
Definition pad1 {n m : nat} (A : Matrix n m) (c : C) : Matrix (S n) (S m) :=
col_wedge (row_wedge A Zero 0) (c .* e_i 0) 0.
Lemma WF_pad1 : forall {n m : nat} (A : Matrix n m) (c : C),
WF_Matrix A <-> WF_Matrix (pad1 A c).
Proof. unfold WF_Matrix, pad1. split.
- intros.
unfold col_wedge, row_wedge, e_i, scale.
bdestruct (y <? 0); bdestruct (y =? 0); try lia.
destruct H0; try lia.
bdestruct (x =? 0); bdestruct (x <? n); try lia; try easy.
lca.
destruct y; try lia.
rewrite Sn_minus_1.
bdestruct (x <? 0); bdestruct (x =? 0); try lia; try easy.
destruct x; try lia.
rewrite Sn_minus_1.
apply H; lia.
- intros.
unfold col_wedge, row_wedge, e_i in H.
rewrite <- (H (S x) (S y)); try lia.
bdestruct (S y <? 0); bdestruct (S y =? 0); try lia.
bdestruct (S x =? 0); bdestruct (S x <? 0); try lia; try easy.
do 2 rewrite Sn_minus_1; easy.
Qed.
Lemma WF_e_i : forall {n : nat} (i : nat),
WF_Matrix (@e_i n i).
Proof. unfold WF_Matrix, e_i.
intros; destruct H as [H | H].
bdestruct (x =? i); bdestruct (x <? n); bdestruct (y =? 0); try lia; easy.
bdestruct (x =? i); bdestruct (x <? n); bdestruct (y =? 0); try lia; easy.
Qed.
#[export] Hint Resolve WF_e_i WF_pad1 : wf_db.
Lemma I_is_eis : forall {n} (i : nat),
get_vec i (I n) = e_i i.
Proof. intros. unfold get_vec, e_i.
prep_matrix_equality.
bdestruct (x =? i).
- bdestruct (y =? 0).
rewrite H. unfold I. simpl.
assert (H1 : (i =? i) && (i <? n) = (i <? n) && true).
{ bdestruct (i =? i). apply andb_comm. easy. }
rewrite H1. reflexivity.
simpl; rewrite andb_false_r; reflexivity.
- simpl. destruct (y =? 0). unfold I.
bdestruct (x =? i). easy.
reflexivity. reflexivity.
Qed.
Lemma reduce_mul_0 : forall {n} (A : Square (S n)) (v : Vector (S n)),
get_vec 0 A = @e_i (S n) 0 -> (reduce A 0 0) × (reduce_row v 0) = reduce_row (A × v) 0.
Proof. intros.
prep_matrix_equality.
unfold Mmult, reduce, reduce_row.
bdestruct (x <? 0); try lia.
rewrite <- big_sum_extend_l.
assert (H' : A (1 + x) 0 = C0).
{ rewrite <- get_vec_conv.
rewrite H. unfold e_i.
bdestruct (1 + x =? 0); try lia.
easy. }
rewrite H'.
rewrite Cmult_0_l.
rewrite Cplus_0_l.
apply big_sum_eq_bounded.
intros. bdestruct (x0 <? 0); try lia; try easy.
Qed.
Lemma reduce_mul_n : forall {n} (A : Square (S n)) (v : Vector (S n)),
get_vec n A = @e_i (S n) n -> (reduce A n n) × (reduce_row v n) = reduce_row (A × v) n.
Proof. intros.
prep_matrix_equality.
unfold Mmult, reduce, reduce_row.
assert (H' : S n - 1 = n). { lia. }
bdestruct (x <? n).
- rewrite <- big_sum_extend_r.
assert (H'' : A x n = C0).
{ rewrite <- get_vec_conv.
rewrite H. unfold e_i.
bdestruct (x =? n); try lia.
easy. }
rewrite H''. rewrite Cmult_0_l.
rewrite Cplus_0_r.
apply big_sum_eq_bounded.
intros. bdestruct (x0 <? n); try lia; try easy.
- rewrite <- big_sum_extend_r.
assert (H'' : A (1 + x) n = C0).
{ rewrite <- get_vec_conv.
rewrite H. unfold e_i.
bdestruct (1 + x =? n); try lia.
easy. }
rewrite H''. rewrite Cmult_0_l.
rewrite Cplus_0_r.
apply big_sum_eq_bounded.
intros.
bdestruct (x0 <? n); try lia; try easy.
Qed.
(* More general case:
Lemma reduce_mul : forall {n} (A : Square (S n)) (v : Vector (S n)) (x : nat),
get_vec x A = @e_i (S n) x -> (reduce A x x) × (reduce_row v x) = reduce_row (A × v) x.
Proof. *)
(* similar lemma for append *)
Lemma append_mul : forall {n m} (A : Matrix n m) (v : Vector n) (a : Vector m),
(col_append A v) × (row_append a (@Zero 1 1)) = A × a.
Proof. intros.
prep_matrix_equality.
unfold Mmult.
simpl.
assert (H' : (col_append A v x m * row_append a Zero m y = C0)%C).
{ unfold col_append, row_append.
bdestruct (m =? m); try lia; lca. }
rewrite H'.
rewrite Cplus_0_r.
apply big_sum_eq_bounded.
intros.
unfold col_append, row_append.
bdestruct (x0 =? m); try lia; try easy.
Qed.
Lemma matrix_by_basis : forall {n m} (T : Matrix n m) (i : nat),
i < m -> get_vec i T = T × e_i i.
Proof. intros. unfold get_vec, e_i, Mmult.
prep_matrix_equality.
bdestruct (y =? 0).
- rewrite (big_sum_unique (T x i) _ m); try easy.
exists i. split.
apply H. split.
bdestruct (i =? i); bdestruct (i <? m); try lia; lca.
intros.
bdestruct (x' =? i); try lia; lca.
- rewrite big_sum_0; try reflexivity.
intros. rewrite andb_false_r.
rewrite Cmult_0_r. reflexivity.
Qed.
Lemma pad1_conv : forall {n m : nat} (A : Matrix n m) (c : C) (i j : nat),
(pad1 A c) (S i) (S j) = A i j.
Proof. intros.
unfold pad1, col_wedge, row_wedge, e_i.
bdestruct (S j <? 0); bdestruct (S j =? 0); try lia.
bdestruct (S i <? 0); bdestruct (S i =? 0); try lia.
do 2 rewrite Sn_minus_1.
easy.
Qed.
Lemma pad1_mult : forall {n m o : nat} (A : Matrix n m) (B : Matrix m o) (c1 c2 : C),
pad1 (A × B) (c1 * c2)%C = (pad1 A c1) × (pad1 B c2).
Proof. intros.
prep_matrix_equality.
unfold Mmult.
destruct x.
- unfold pad1, col_wedge, row_wedge, e_i, scale.
bdestruct_all.
rewrite <- big_sum_extend_l; simpl.
rewrite <- (Cplus_0_r (c1 * c2 * C1)).
apply Cplus_simplify; try lca.
rewrite big_sum_0_bounded; try easy.
intros; lca.
rewrite big_sum_0_bounded; try easy.
simpl; intros.
bdestruct_all; lca.
- destruct y.
unfold pad1, col_wedge, row_wedge, e_i, scale.
simpl.
rewrite big_sum_0_bounded; try lca.
bdestruct_all; lca.
intros. bdestruct_all; lca.
rewrite pad1_conv.
rewrite <- big_sum_extend_l.
rewrite <- (Cplus_0_l (big_sum _ _)).
apply Cplus_simplify.
unfold pad1, col_wedge, row_wedge, e_i, scale.
bdestruct_all; lca.
apply big_sum_eq_bounded; intros.
do 2 rewrite pad1_conv; easy.
Qed.
Lemma pad1_row_wedge_mult : forall {n m : nat} (A : Matrix n m) (v : Vector m) (c : C),
pad1 A c × row_wedge v Zero 0 = row_wedge (A × v) Zero 0.
Proof. intros.
prep_matrix_equality.
destruct x.
- unfold pad1, Mmult, col_wedge, row_wedge, scale, e_i.
bdestruct_all;
rewrite big_sum_0_bounded; try lca; intros;
bdestruct_all; lca.
- destruct y;
unfold pad1, Mmult, col_wedge, row_wedge, scale, e_i;
bdestruct_all;
rewrite <- big_sum_extend_l, <- Cplus_0_l;
apply Cplus_simplify; try lca;
apply big_sum_eq_bounded; intros;
bdestruct_all; do 2 rewrite Sn_minus_1; easy.
Qed.
Lemma pad1_I : forall (n : nat), pad1 (I n) C1 = I (S n).
Proof. intros.
unfold pad1, I, col_wedge, row_wedge, e_i, scale.
prep_matrix_equality.
bdestruct (y <? 0); bdestruct (y =? 0); bdestruct (x <? 0); bdestruct (x <? S n);
bdestruct (x =? 0); bdestruct (x =? y); bdestruct (x - 1 =? y - 1);
bdestruct (x - 1 <? n); try lia; try lca.
Qed.
(* ∃ weakens this lemma, but makes future proofs less messy *)
Lemma pad1ed_matrix : forall {n m : nat} (A : Matrix (S n) (S m)) (c : C),
(forall (i j : nat), (i = 0 \/ j = 0) /\ i <> j -> A i j = C0) -> A 0 0 = c ->
exists a, pad1 a c = A.
Proof. intros.
exists (reduce_col (reduce_row A 0) 0).
unfold pad1, reduce_row, reduce_col, col_wedge, row_wedge, e_i, scale.
prep_matrix_equality.
bdestruct (y <? 0); bdestruct (y =? 0); bdestruct (x <? 0); bdestruct (x =? 0);
try lia.
rewrite H4, H2, H0. lca.
rewrite H; try lia.
destruct x; try lia. lca.
rewrite H; try lia; easy.
destruct x; destruct y; try lia.
do 2 rewrite Sn_minus_1 in *.
bdestruct (x <? 0); bdestruct (y <? 0); try lia.
easy.
Qed.
Lemma reduce_pad1 : forall {n : nat} (A : Square n) (c : C),
A = reduce (pad1 A c) 0 0.
Proof. intros.
prep_matrix_equality.
unfold reduce, pad1, col_wedge, row_wedge, e_i.
bdestruct_all.
destruct x; destruct y; easy.
Qed.
Lemma pad1_col_swap : forall {n m : nat} (A : Matrix n m) (x y : nat) (c : C),
(pad1 (col_swap A x y) c) = col_swap (pad1 A c) (S x) (S y).
Proof. intros.
unfold pad1, col_wedge, row_wedge, col_swap, e_i, scale.
prep_matrix_equality.
bdestruct_all; try easy.
all : rewrite Sn_minus_1; easy.
Qed.
Lemma pad1_col_scale : forall {n m : nat} (A : Matrix n m) (x : nat) (c1 c2 : C),
(pad1 (col_scale A x c1) c2) = col_scale (pad1 A c2) (S x) c1.
Proof. intros.
unfold pad1, col_wedge, row_wedge, col_scale, e_i, scale.
prep_matrix_equality.
bdestruct_all; try easy.
lca.
Qed.
Lemma pad1_col_add : forall {n m : nat} (A : Matrix n m) (x y : nat) (c1 c2 : C),
(pad1 (col_add A x y c1) c2) = col_add (pad1 A c2) (S x) (S y) c1.
Proof. intros.
unfold pad1, col_wedge, row_wedge, col_add, e_i, scale.
prep_matrix_equality.
bdestruct_all; try easy.
all : rewrite Sn_minus_1; try easy.
lca.
Qed.
(***************************************************************************)
(** * 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 col as' T))
-> 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 col (make_col_zero col as') T))
-> 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_vec i T = 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_vec i T = 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 col as' T).
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 col (make_row_zero (skip_count col 0) as') T) = T).
{ prep_matrix_equality.
unfold col_add_many, make_row_zero, skip_count, gen_new_vec, 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_zero 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_zero, 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 col as' T).
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_zero (skip_count col 0) as') = Zero).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold make_col_zero, 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_zero 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_zero, 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_zero col as' = Zero).
{ apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
destruct col; destruct i; destruct j; try lia.
unfold make_col_zero.
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_zero.
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_zero.
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_zero.
bdestruct (i =? col); try lia; try easy.
unfold skip_count in H5.
bdestruct (m <? col); try lia.
apply H2; lia.
unfold make_col_zero.
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 col as' T) -> P n m T.
Proof. intros.
apply invr_col_add_col_add_many in H.
inversion H; subst.
rewrite (col_add_many_inv T col as'); 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 col (make_col_zero col as') T) -> P n m T.
Proof. intros.
apply invr_col_add_col_add_each in H.
inversion H; subst.
rewrite (col_add_each_inv col as'); try easy.
apply H3; try easy.
auto with wf_db.
Qed.
(***********************************************************)
(** * Defining and proving lemmas relating to the determinant *)
(***********************************************************)
Fixpoint parity (n : nat) : C :=
match n with
| 0 => C1
| S 0 => -C1
| S (S n) => parity n
end.
Lemma parity_S : forall (n : nat),
(parity (S n) = -C1 * parity n)%C.
Proof. intros.
induction n as [| n']; try lca.
rewrite IHn'.
simpl. lca.
Qed.
Fixpoint Determinant (n : nat) (A : Square n) : C :=
match n with
| 0 => C1
| S 0 => A 0 0
| S n' => (big_sum (fun i => (parity i) * (A i 0) * (Determinant n' (reduce A i 0)))%C n)
end.
Arguments Determinant {n}.
Lemma Det_simplify : forall {n} (A : Square (S (S n))),
Determinant A =
(big_sum (fun i => (parity i) * (A i 0) * (Determinant (reduce A i 0)))%C (S (S n))).
Proof. intros. easy. Qed.
Lemma Det_simplify_fun : forall {n} (A : Square (S (S (S n)))),
(fun i : nat => parity i * A i 0 * Determinant (reduce A i 0))%C =
(fun i : nat => (big_sum (fun j =>
(parity i) * (A i 0) * (parity j) * ((reduce A i 0) j 0) *
(Determinant (reduce (reduce A i 0) j 0)))%C (S (S n))))%C.
Proof. intros.
apply functional_extensionality; intros.
rewrite Det_simplify.
rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
apply big_sum_eq_bounded; intros.
lca.
Qed.
Lemma reduce_I : forall (n : nat), reduce (I (S n)) 0 0 = I n.
Proof. intros.
apply mat_equiv_eq.
apply WF_reduce; try lia; auto with wf_db.
apply WF_I.
unfold mat_equiv; intros.
unfold reduce, I.
bdestruct (i <? 0); bdestruct (j <? 0); try lia.
easy.
Qed.
Lemma Det_I : forall (n : nat), Determinant (I n) = C1.
Proof. intros.
induction n as [| n'].
- easy.
- simpl. destruct n'; try easy.
rewrite <- big_sum_extend_l.
rewrite <- Cplus_0_r.
rewrite <- Cplus_assoc.
apply Cplus_simplify.
rewrite reduce_I, IHn'.
lca.
rewrite (@big_sum_extend_r C C_is_monoid).
apply (@big_sum_0_bounded C C_is_monoid); intros.
replace (I (S (S n')) (S x) 0) with C0 by easy.
lca.
Qed.
Lemma Det_make_WF : forall (n : nat) (A : Square n),
Determinant A = Determinant (make_WF A).
Proof. induction n as [| n'].
- easy.
- intros.
destruct n'; try easy.
do 2 rewrite Det_simplify.
apply big_sum_eq_bounded; intros.
assert (H' : (reduce (make_WF A) x 0) = make_WF (reduce A x 0)).
{ prep_matrix_equality.
unfold reduce, make_WF.
bdestruct_all; try easy. }
rewrite H', IHn'.
unfold make_WF.
bdestruct_all; easy.
Qed.
Lemma Det_Mmult_make_WF_l : forall (n : nat) (A B : Square n),
Determinant (A × B) = Determinant (make_WF A × B).
Proof. intros.
rewrite Det_make_WF, (Det_make_WF _ (make_WF A × B)).
do 2 rewrite <- Mmult_make_WF.
rewrite <- (eq_make_WF (make_WF A)); auto with wf_db.
Qed.
Lemma Det_Mmult_make_WF_r : forall (n : nat) (A B : Square n),
Determinant (A × B) = Determinant (A × (make_WF B)).
Proof. intros.
rewrite Det_make_WF, (Det_make_WF _ (A × make_WF B)).
do 2 rewrite <- Mmult_make_WF.
rewrite <- (eq_make_WF (make_WF B)); auto with wf_db.
Qed.
Lemma Det_Mmult_make_WF : forall (n : nat) (A B : Square n),
Determinant (A × B) = Determinant ((make_WF A) × (make_WF B)).
Proof. intros.
rewrite <- Det_Mmult_make_WF_r, <- Det_Mmult_make_WF_l; easy.
Qed.
Definition M22 : Square 2 :=
fun x y =>
match (x, y) with
| (0, 0) => 1%R
| (0, 1) => 2%R
| (1, 0) => 4%R
| (1, 1) => 5%R
| _ => C0
end.
Lemma Det_M22 : (Determinant M22) = (Copp (3%R,0%R))%C.
Proof. lca. Qed.
(** Now, we show the effects of the column operations on determinant *)
Lemma Determinant_scale : forall {n} (A : Square n) (c : C) (col : nat),
col < n -> Determinant (col_scale A col c) = (c * Determinant A)%C.
Proof. induction n.
+ intros. easy.
+ intros. simpl.
destruct n.
- simpl. unfold col_scale.
bdestruct (0 =? col); try lia; easy.
- rewrite Cmult_plus_distr_l.
apply Cplus_simplify.
* rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
apply big_sum_eq_bounded.
intros.
destruct col.
rewrite col_scale_reduce_same; try lia.
unfold col_scale. bdestruct (0 =? 0); try lia.
lca.
rewrite col_scale_reduce_before; try lia.
rewrite Sn_minus_1.
rewrite IHn; try lia.
unfold col_scale.
bdestruct (0 =? S col); try lia; lca.
* destruct col.
rewrite col_scale_reduce_same; try lia.
unfold col_scale. bdestruct (0 =? 0); try lia.
lca.
rewrite col_scale_reduce_before; try lia.
rewrite Sn_minus_1.
rewrite IHn; try lia.
unfold col_scale.
bdestruct (0 =? S col); try lia; lca.
Qed.
(* some helper lemmas, since showing the effect of col_swap is a bit tricky *)
Lemma Det_diff_1 : forall {n} (A : Square (S (S (S n)))),
Determinant (col_swap A 0 1) =
big_sum (fun i => (big_sum (fun j => ((A i 1) * (A (skip_count i j) 0) * (parity i) * (parity j) *
Determinant (reduce (reduce A i 0) j 0))%C)
(S (S n)))) (S (S (S n))).
Proof. intros.
rewrite Det_simplify.
rewrite Det_simplify_fun.
apply big_sum_eq_bounded; intros.
apply big_sum_eq_bounded; intros.
replace (col_swap A 0 1 x 0) with (A x 1) by easy.
assert (H' : @reduce (S (S n)) (col_swap A 0 1) x 0 x0 0 = A (skip_count x x0) 0).
{ unfold reduce, col_swap, skip_count.
simpl. bdestruct (x0 <? x); try easy. }
rewrite H'.
apply Cmult_simplify; try easy.
lca.
Qed.
Lemma Det_diff_2 : forall {n} (A : Square (S (S (S n)))),
Determinant A =
big_sum (fun i => (big_sum (fun j => ((A i 0) * (A (skip_count i j) 1) * (parity i) * (parity j) *
Determinant (reduce (reduce A i 0) j 0))%C)
(S (S n)))) (S (S (S n))).
Proof. intros.
rewrite Det_simplify.
rewrite Det_simplify_fun.
apply big_sum_eq_bounded; intros.
apply big_sum_eq_bounded; intros.
apply Cmult_simplify; try easy.
assert (H' : @reduce (S (S n)) A x 0 x0 0 = A (skip_count x x0) 1).
{ unfold reduce, col_swap, skip_count.
simpl. bdestruct (x0 <? x); try easy. }
rewrite H'.
lca.
Qed.
(* if we show that swapping 0th col and 1st col, we can generalize using some cleverness *)
Lemma Determinant_swap_01 : forall {n} (A : Square n),
1 < n -> Determinant (col_swap A 0 1) = (-C1 * (Determinant A))%C.
Proof. intros.
destruct n; try lia.
destruct n; try lia.
destruct n.
- simpl. unfold col_swap, reduce. lca.
- rewrite Det_diff_1, Det_diff_2.
apply big_sum_rearrange; intros.
+ unfold skip_count.
bdestruct (x <? (S y)); bdestruct (y <? x); try lia.
rewrite Cmult_assoc.
apply Cmult_simplify.
rewrite parity_S.
lca.
rewrite reduce_reduce_0; easy.
+ unfold skip_count.
bdestruct (x <? y); bdestruct (y <? (S x)); try lia.
rewrite Cmult_assoc.
apply Cmult_simplify.
rewrite parity_S.
lca.
rewrite <- reduce_reduce_0; easy.
Qed.
(* swapping adjacent columns *)
Lemma Determinant_swap_adj : forall {n} (A : Square n) (i : nat),
S i < n -> Determinant (col_swap A i (S i)) = (-C1 * (Determinant A))%C.
Proof. induction n as [| n'].
- easy.
- intros.
destruct i.
+ apply Determinant_swap_01; easy.
+ simpl. destruct n'; try lia.
do 2 rewrite (@big_sum_extend_r C C_is_monoid).
rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
apply big_sum_eq_bounded; intros.
rewrite col_swap_reduce_before; try lia.
rewrite IHn'; try lia.
replace (col_swap A (S i) (S (S i)) x 0) with (A x 0) by easy.
lca.
Qed.
(* swapping columns i and i + (S k), use previous lemma to induct *)
Lemma Determinant_swap_ik : forall {n} (k i : nat) (A : Square n),
i + (S k) < n -> Determinant (col_swap A i (i + (S k))) = (-C1 * (Determinant A))%C.
Proof. induction k as [| k'].
- intros.
replace (i + 1) with (S i) by lia.
rewrite Determinant_swap_adj; try lia; lca.
- intros.
rewrite (col_swap_three A i (i + (S k')) (i + (S (S k')))); try lia.
rewrite IHk'; try lia.
replace (i + (S (S k'))) with (S (i + (S k'))) by lia.
rewrite Determinant_swap_adj; try lia.
rewrite IHk'; try lia.
lca.
Qed.
(* finally, we can prove Determinant_swap *)
Lemma Determinant_swap : forall {n} (A : Square n) (i j : nat),
i < n -> j < n -> i <> j ->
Determinant (col_swap A i j) = (-C1 * (Determinant A))%C.
Proof. intros.
bdestruct (i <? j); bdestruct (j <? i); try lia.
- replace j with (i + (S (j - i - 1))) by lia.
rewrite Determinant_swap_ik; try lia; easy.
- replace i with (j + (S (i - j - 1))) by lia.
rewrite col_swap_diff_order.
rewrite Determinant_swap_ik; try lia; easy.
Qed.
Lemma col_0_Det_0 : forall {n} (A : Square n),
(exists i, i < n /\ get_vec i A = Zero) -> Determinant A = C0.
Proof. intros n A [i [H H0]].
destruct n; try easy.
destruct n.
destruct i; try lia.
replace C0 with (@Zero 1 1 0 0) by easy.
rewrite <- H0. easy.
destruct i.
- rewrite Det_simplify.
apply (@big_sum_0_bounded C C_is_monoid); intros.
replace (A x 0) with (@Zero (S (S n)) 1 x 0) by (rewrite <- H0; easy).
unfold Zero; lca.
- rewrite (col_swap_inv _ 0 (S i)).
rewrite Determinant_swap; try lia.
rewrite Det_simplify.
rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
apply (@big_sum_0_bounded C C_is_monoid); intros.
replace (col_swap A 0 (S i) x 0) with
(@Zero (S (S n)) 1 x 0) by (rewrite <- H0; easy).
unfold Zero; lca.
Qed.
Lemma col_same_Det_0 : forall {n} (A : Square n) (i j : nat),
i < n -> j < n -> i <> j ->
get_vec i A = get_vec j A ->
Determinant A = C0.
Proof. intros.
apply eq_neg_implies_0.
rewrite <- (Determinant_swap _ i j); try easy.
rewrite (det_by_get_vec (col_swap A i j) A); try easy; intros.
prep_matrix_equality.
destruct y; try easy.
bdestruct (i0 =? i); bdestruct (i0 =? j); try lia.
- rewrite H3, <- col_swap_get_vec, H2; easy.
- rewrite H4, col_swap_diff_order, <- col_swap_get_vec, H2; easy.
- unfold col_swap, get_vec. simpl.
bdestruct (i0 =? i); bdestruct (i0 =? j); try lia; easy.
Qed.
Lemma col_scale_same_Det_0 : forall {n} (A : Square n) (i j : nat) (c : C),
i < n -> j < n -> i <> j ->
get_vec i A = c .* (get_vec j A) ->
Determinant A = C0.
Proof. intros.
destruct (Ceq_dec c C0).
- apply col_0_Det_0.
exists i.
split; try easy.
rewrite H2, e.
apply Mscale_0_l.
- rewrite (col_scale_inv A j c); try easy.
rewrite Determinant_scale; try easy.
assert (H3 : Determinant (col_scale A j c) = C0).
{ apply (col_same_Det_0 _ i j); try easy.
prep_matrix_equality.
unfold get_vec, col_scale.
bdestruct (y =? 0); try easy.
bdestruct (i =? j); bdestruct (j =? j); try lia.
rewrite <- get_vec_conv.
rewrite H2.
unfold scale.
rewrite get_vec_conv.
easy. }
rewrite H3.
lca.
Qed.
(* use this to show det_col_add_0i *)
Lemma Det_col_add_comm : forall {n} (T : Matrix (S n) n) (v1 v2 : Vector (S n)),
(Determinant (col_wedge T v1 0) + Determinant (col_wedge T v2 0) =
Determinant (col_wedge T (v1 .+ v2) 0))%C.
Proof. intros.
destruct n; try easy.
do 3 rewrite Det_simplify.
rewrite <- (@big_sum_plus C _ _ C_is_comm_group).
apply big_sum_eq_bounded; intros.
repeat rewrite reduce_is_redcol_redrow.
repeat rewrite col_wedge_reduce_col_same.
unfold col_wedge, Mplus.
bdestruct (0 <? 0); bdestruct (0 =? 0); try lia.
lca.
Qed.
(* like before, we prove a specific case in order to prove the general case *)
Lemma Determinant_col_add0i : forall {n} (A : Square n) (i : nat) (c : C),
i < n -> i <> 0 -> Determinant (col_add A 0 i c) = Determinant A.
Proof. intros.
destruct n; try easy.
rewrite col_add_split.
assert (H' := (@Det_col_add_comm n (reduce_col A 0) (get_vec 0 A) (c .* get_vec i A))).
rewrite <- H'.
rewrite <- Cplus_0_r.
apply Cplus_simplify.
assert (H1 : col_wedge (reduce_col A 0) (get_vec 0 A) 0 = A).
{ prep_matrix_equality.
unfold col_wedge, reduce_col, get_vec.
destruct y; try easy; simpl.
replace (y - 0) with y by lia; easy. }
rewrite H1; easy.
apply (col_scale_same_Det_0 _ 0 i c); try lia.
prep_matrix_equality.
unfold get_vec, col_wedge, reduce_col, scale; simpl.
bdestruct (y =? 0); bdestruct (i =? 0); try lca; try lia.
replace (S (i - 1)) with i by lia.
easy.
Qed.
Lemma Determinant_col_add : forall {n} (A : Square n) (i j : nat) (c : C),
i < n -> j < n -> i <> j -> Determinant (col_add A i j c) = Determinant A.
Proof. intros.
destruct j.
- rewrite <- col_swap_col_add_0.
rewrite Determinant_swap.
rewrite Determinant_col_add0i.
rewrite Determinant_swap.
lca.
all : easy.
- destruct i.
rewrite Determinant_col_add0i; try easy.
rewrite <- col_swap_col_add_Si.
rewrite Determinant_swap.
rewrite Determinant_col_add0i.
rewrite Determinant_swap.
lca.
all : try easy; try lia.
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 :=