-
Notifications
You must be signed in to change notification settings - Fork 10
/
RowColOps.v
3716 lines (3273 loc) · 125 KB
/
RowColOps.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 Import Psatz.
Require Import Reals.
Require Export Matrix.
(** Here, we define many different types of row and column operations *)
Local Open Scope nat_scope.
(** * Defining matrix altering/col operations *)
(* in what follows, T is a set of vectors, A is a square, v/as' are vectors/sets of scalars *)
Definition get_col {m n} (T : Matrix m n) (i : nat) : Vector m :=
fun x y => (if (y =? O) then T x i else C0).
#[deprecated(note="Use get_col instead")]
Notation get_vec i S := (get_col S i) (only parsing).
Definition get_row {m n} (T : Matrix m n) (i : nat) : Matrix 1 n :=
fun x y => (if (x =? O) then T i y else C0).
Definition reduce_row {m n} (T : Matrix (S m) n) (row : nat) : Matrix m n :=
fun x y => if x <? row
then T x y
else T (1 + x) y.
Definition reduce_col {m n} (T : Matrix m (S n)) (col : nat) : Matrix m n :=
fun x y => if y <? col
then T x y
else T x (1 + y).
(* more specific form for vectors *)
Definition reduce_vecn {n} (v : Vector (S n)) : Vector n :=
fun x y => if x <? n
then v x y
else v (1 + x) y.
(* More specific form for squares *)
Definition get_minor {m n} (A : Matrix (S m) (S n)) (row col : nat) : Matrix m n :=
fun x y => (if x <? row
then (if y <? col
then A x y
else A x (1+y))
else (if y <? col
then A (1+x) y
else A (1+x) (1+y))).
#[deprecated(note="Use get_minor instead")]
Notation reduce := get_minor (only parsing).
(* more general than col_append *)
Definition smash {m n1 n2} (T1 : Matrix m n1) (T2 : Matrix m n2) : Matrix m (n1 + n2) :=
fun i j => if j <? n1 then T1 i j else T2 i (j - n1).
(* TODO: combine smash and col_wedge *)
Definition col_wedge {m n} (T : Matrix m n) (v : Vector m) (spot : nat) : Matrix m (S n) :=
fun i j => if j <? spot
then T i j
else if j =? spot
then v i 0
else T i (j-1).
Definition row_wedge {m n} (T : Matrix m n) (v : Matrix 1 n) (spot : nat) : Matrix (S m) n :=
fun i j => if i <? spot
then T i j
else if i =? spot
then v 0 j
else T (i-1) j.
(*
#[deprecated(note="Use col_wedge instead")]
*)
Definition col_append {m n} (T : Matrix m n) (v : Vector m) : Matrix m (S n) :=
col_wedge T v n.
(*
#[deprecated(note="Use row_wedge instead")]
*)
Definition row_append {m n} (T : Matrix m n) (v : Matrix 1 n) : Matrix (S m) n :=
row_wedge T v m.
Definition col_swap {m n} (T : Matrix m n) (x y : nat) : Matrix m n :=
fun i j => if (j =? x)
then T i y
else if (j =? y)
then T i x
else T i j.
Definition row_swap {m n} (T : Matrix m n) (x y : nat) : Matrix m n :=
fun i j => if (i =? x)
then T y j
else if (i =? y)
then T x j
else T i j.
Definition col_scale {m n} (T : Matrix m n) (col : nat) (a : C) : Matrix m n :=
fun i j => if (j =? col)
then (a * T i j)%C
else T i j.
Definition row_scale {m n} (T : Matrix m n) (row : nat) (a : C) : Matrix m n :=
fun i j => if (i =? row)
then (a * T i j)%C
else T i j.
(* generalizations of col_scale and row_scale. Both sets of as' are vectors where the col/row
corresponds to the scaled col/row. Can also show that this is the same as multiplying
by the identity with scalars on diag *)
Definition col_scale_many {m n} (T : Matrix m n) (as' : Matrix 1 n) : Matrix m n :=
fun i j => ((as' O j) * (T i j))%C.
Definition row_scale_many {m n} (T : Matrix m n) (as' : Vector m) : Matrix m n :=
fun i j => ((as' i O) * (T i j))%C.
(* adding one column to another *)
Definition col_add {m n} (T : Matrix m n) (col to_add : nat) (a : C) : Matrix m n :=
fun i j => if (j =? col)
then (T i j + a * T i to_add)%C
else T i j.
(* adding one row to another *)
Definition row_add {m n} (T : Matrix m n) (row to_add : nat) (a : C) : Matrix m n :=
fun i j => if (i =? row)
then (T i j + a * T to_add j)%C
else T i j.
(* generalizing col_add *)
Definition gen_new_col (m n : nat) (T : Matrix m n) (as' : Vector n) : Vector m :=
big_sum (fun i => (as' i O) .* (get_col T i)) n.
Definition gen_new_row (m n : nat) (T : Matrix m n) (as' : Matrix 1 m) : Matrix 1 n :=
big_sum (fun i => (as' O i) .* (get_row T i)) m.
(* adds all columns to single column *)
Definition col_add_many {m n} (T : Matrix m n) (as' : Vector n) (col : nat) : Matrix m n :=
fun i j => if (j =? col)
then (T i j + (gen_new_col m n T as') i O)%C
else T i j.
Definition row_add_many {m n} (T : Matrix m n) (as' : Matrix 1 m) (row : nat) : Matrix m n :=
fun i j => if (i =? row)
then (T i j + (gen_new_row m n T as') O j)%C
else T i j.
(* adds single column to each other column *)
Definition col_add_each {m n} (T : Matrix m n) (as' : Matrix 1 n) (col : nat) : Matrix m n :=
T .+ ((get_col T col) × as').
Definition row_add_each {m n} (T : Matrix m n) (as' : Vector m) (row : nat) : Matrix m n :=
T .+ (as' × get_row T row).
Definition make_col_val {m n} (T : Matrix m n) (col : nat) (a : C) : Matrix m n :=
fun i j => if (j =? col) && (i <? m)
then a
else T i j.
Definition make_row_val {m n} (T : Matrix m n) (row : nat) (a : C) : Matrix m n :=
fun i j => if (i =? row) && (j <? n)
then a
else T i j.
Definition col_to_front {m n} (T : Matrix m n) (col : nat) : Matrix m n :=
fun i j => if (j =? O)
then T i col
else if (j <? col + 1) then T i (j - 1) else T i j.
Definition row_to_front {m n} (T : Matrix m n) (row : nat) : Matrix m n :=
fun i j => if (i =? O)
then T row j
else if (i <? row + 1) then T (i - 1) j else T i j.
Definition col_replace {m n} (T : Matrix m n) (col rep : nat) : Matrix m n :=
fun i j => if (j =? rep)
then T i col
else T i j.
Definition row_replace {m n} (T : Matrix m n) (row rep : nat) : Matrix m n :=
fun i j => if (i =? rep)
then T row j
else T i j.
(* using previous def's, takes matrix and increases its rank by 1 (assuming c <> 0) *)
Definition pad1 {m n : nat} (A : Matrix m n) (c : C) : Matrix (S m) (S n) :=
col_wedge (row_wedge A Zero 0) (c .* e_i 0) 0.
(** * WF lemmas about these new operations *)
Lemma WF_get_col : forall {m n} (i : nat) (T : Matrix m n),
WF_Matrix T -> WF_Matrix (get_col T i).
Proof. unfold WF_Matrix, get_col in *.
intros.
bdestruct (y =? 0); try lia; try easy.
apply H.
destruct H0.
left; easy.
lia.
Qed.
Lemma WF_get_row : forall {m n} (i : nat) (T : Matrix m n),
WF_Matrix T -> WF_Matrix (get_row T i).
Proof. unfold WF_Matrix, get_row in *.
intros.
bdestruct (x =? 0); try lia; try easy.
apply H.
destruct H0.
lia.
right; easy.
Qed.
Lemma WF_reduce_row : forall {m n} (row : nat) (T : Matrix (S m) n),
row < (S m) -> WF_Matrix T -> WF_Matrix (reduce_row T row).
Proof. unfold WF_Matrix, reduce_row. intros.
bdestruct (x <? row).
- destruct H1 as [H1 | H1].
+ assert (nibzo : forall (a b c : nat), a < b -> b < c -> 1 + a < c).
{ lia. }
apply (nibzo x row (S m)) in H2.
simpl in H2. lia. apply H.
+ apply H0; auto.
- apply H0. destruct H1.
+ left. simpl. lia.
+ right. apply H1.
Qed.
Lemma WF_reduce_col : forall {m n} (col : nat) (T : Matrix m (S n)),
col < (S n) -> WF_Matrix T -> WF_Matrix (reduce_col T col).
Proof. unfold WF_Matrix, reduce_col. intros.
bdestruct (y <? col).
- destruct H1 as [H1 | H1].
+ apply H0; auto.
+ assert (nibzo : forall (a b c : nat), a < b -> b < c -> 1 + a < c).
{ lia. }
apply (nibzo y col (S n)) in H2.
simpl in H2. lia. apply H.
- apply H0. destruct H1.
+ left. apply H1.
+ right. simpl. lia.
Qed.
Lemma rvn_is_rr_n : forall {n : nat} (v : Vector (S n)),
reduce_vecn v = reduce_row v n.
Proof. intros.
prep_matrix_equality.
unfold reduce_row, reduce_vecn.
easy.
Qed.
Lemma WF_reduce_vecn : forall {n} (v : Vector (S n)),
n <> 0 -> WF_Matrix v -> WF_Matrix (reduce_vecn v).
Proof. intros.
rewrite rvn_is_rr_n.
apply WF_reduce_row; try lia; try easy.
Qed.
Lemma get_minor_is_redrow_redcol : forall {m n} (A : Matrix (S m) (S n)) (row col : nat),
get_minor A row col = reduce_col (reduce_row A row) col.
Proof. intros.
prep_matrix_equality.
unfold get_minor, reduce_col, reduce_row.
bdestruct (x <? row); bdestruct (y <? col); try easy.
Qed.
Lemma get_minor_is_redcol_redrow : forall {m n} (A : Matrix (S m) (S n)) (row col : nat),
get_minor A row col = reduce_row (reduce_col A col) row.
Proof. intros.
prep_matrix_equality.
unfold get_minor, reduce_col, reduce_row.
bdestruct (x <? row); bdestruct (y <? col); try easy.
Qed.
Lemma WF_get_minor : forall {m n} (A : Matrix (S m) (S n)) (row col : nat),
row < S m -> col < S n -> WF_Matrix A -> WF_Matrix (get_minor A row col).
Proof. intros.
rewrite get_minor_is_redrow_redcol.
apply WF_reduce_col; try easy.
apply WF_reduce_row; try easy.
Qed.
Lemma WF_col_swap : forall {m n} (T : Matrix m n) (x y : nat),
x < n -> y < n -> WF_Matrix T -> WF_Matrix (col_swap T x y).
Proof. unfold WF_Matrix, col_swap in *.
intros.
bdestruct (y0 =? x); bdestruct (y0 =? y); destruct H2; try lia.
all : apply H1; try (left; apply H2).
auto.
Qed.
Lemma WF_row_swap : forall {m n} (T : Matrix m n) (x y : nat),
x < m -> y < m -> WF_Matrix T -> WF_Matrix (row_swap T x y).
Proof. unfold WF_Matrix, row_swap in *.
intros.
bdestruct (x0 =? x); bdestruct (x0 =? y); destruct H2; try lia.
all : apply H1; try (right; apply H2).
auto.
Qed.
Lemma WF_col_scale : forall {m n} (T : Matrix m n) (x : nat) (a : C),
WF_Matrix T -> WF_Matrix (col_scale T x a).
Proof. unfold WF_Matrix, col_scale in *.
intros.
apply H in H0.
rewrite H0.
rewrite Cmult_0_r.
bdestruct (y =? x); easy.
Qed.
Lemma WF_row_scale : forall {m n} (T : Matrix m n) (x : nat) (a : C),
WF_Matrix T -> WF_Matrix (row_scale T x a).
Proof. unfold WF_Matrix, row_scale in *.
intros.
apply H in H0.
rewrite H0.
rewrite Cmult_0_r.
bdestruct (x0 =? x); easy.
Qed.
Lemma WF_col_scale_many : forall {m n} (T : Matrix m n) (as' : Matrix m 1),
WF_Matrix T -> WF_Matrix (col_scale_many T as').
Proof. unfold WF_Matrix, col_scale_many in *.
intros.
apply H in H0.
rewrite H0.
rewrite Cmult_0_r; easy.
Qed.
Lemma WF_row_scale_many : forall {m n} (T : Matrix m n) (as' : Vector n),
WF_Matrix T -> WF_Matrix (row_scale_many T as').
Proof. unfold WF_Matrix, row_scale_many in *.
intros.
apply H in H0.
rewrite H0.
rewrite Cmult_0_r; easy.
Qed.
Lemma WF_col_add : forall {m n} (T : Matrix m n) (x y : nat) (a : C),
x < n -> WF_Matrix T -> WF_Matrix (col_add T x y a).
Proof. unfold WF_Matrix, col_add in *.
intros.
bdestruct (y0 =? x); destruct H1; try lia.
do 2 (rewrite H0; auto). ring.
all : apply H0; auto.
Qed.
Lemma WF_row_add : forall {m n} (T : Matrix m n) (x y : nat) (a : C),
x < m -> WF_Matrix T -> WF_Matrix (row_add T x y a).
Proof. unfold WF_Matrix, row_add in *.
intros.
bdestruct (x0 =? x); destruct H1; try lia.
do 2 (rewrite H0; auto). ring.
all : apply H0; auto.
Qed.
Lemma WF_gen_new_col : forall {m n} (T : Matrix m n) (as' : Vector n),
WF_Matrix T -> WF_Matrix (gen_new_col m n T as').
Proof. intros.
unfold gen_new_col.
apply WF_Msum; intros.
apply WF_scale.
apply WF_get_col.
easy.
Qed.
Lemma WF_gen_new_row : forall {m n} (T : Matrix m n) (as' : Matrix 1 m),
WF_Matrix T -> WF_Matrix (gen_new_row m n T as').
Proof. intros.
unfold gen_new_row.
apply WF_Msum; intros.
apply WF_scale.
apply WF_get_row.
easy.
Qed.
Lemma WF_col_add_many : forall {m n} (T : Matrix m n) (as' : Vector n) (col : nat),
col < n -> WF_Matrix T -> WF_Matrix (col_add_many T as' col).
Proof. unfold WF_Matrix, col_add_many.
intros.
bdestruct (y =? col).
assert (H4 := (WF_gen_new_col T as')).
rewrite H4, H0; try easy.
ring. destruct H2; lia.
rewrite H0; easy.
Qed.
Lemma WF_row_add_many : forall {m n} (T : Matrix m n) (as' : Matrix 1 m) (row : nat),
row < m -> WF_Matrix T -> WF_Matrix (row_add_many T as' row).
Proof. unfold WF_Matrix, row_add_many.
intros.
bdestruct (x =? row).
assert (H4 := (WF_gen_new_row T as')).
rewrite H4, H0; try easy.
ring. destruct H2; lia.
rewrite H0; easy.
Qed.
Lemma WF_col_wedge : forall {m n} (T : Matrix m n) (v : Vector m) (spot : nat),
spot <= n -> WF_Matrix T -> WF_Matrix v -> WF_Matrix (col_wedge T v spot).
Proof. unfold WF_Matrix in *.
intros; destruct H2 as [H2 | H2].
- unfold col_wedge.
rewrite H0, H1; try lia.
rewrite H0; try lia.
bdestruct (y <? spot); bdestruct (y =? spot); easy.
- unfold col_wedge.
bdestruct (y <? spot); bdestruct (y =? spot); try lia.
rewrite H0; try lia.
easy.
Qed.
Lemma WF_row_wedge : forall {m n} (T : Matrix m n) (v : Matrix 1 n) (spot : nat),
spot <= m -> WF_Matrix T -> WF_Matrix v -> WF_Matrix (row_wedge T v spot).
Proof. unfold WF_Matrix in *.
intros; destruct H2 as [H2 | H2].
- unfold row_wedge.
bdestruct (x <? spot); bdestruct (x =? spot); try lia.
rewrite H0; try lia.
easy.
- unfold row_wedge.
rewrite H0, H1; try lia.
rewrite H0; try lia.
bdestruct (x <? spot); bdestruct (x =? spot); easy.
Qed.
#[deprecated(note="Use WF_col_wedge instead")]
Notation WF_col_append := WF_col_wedge (only parsing).
#[deprecated(note="Use WF_row_wedge instead")]
Notation WF_row_append := WF_row_wedge (only parsing).
Lemma WF_smash : forall {m n1 n2} (T1 : Matrix m n1) (T2 : Matrix m n2),
WF_Matrix T1 -> WF_Matrix T2 -> WF_Matrix (smash T1 T2).
Proof. unfold WF_Matrix, smash in *.
intros.
bdestruct (y <? n1).
- apply H; lia.
- apply H0; lia.
Qed.
Lemma WF_col_add_each : forall {m n} (col : nat) (as' : Matrix 1 n) (T : Matrix m n),
WF_Matrix T -> WF_Matrix as' -> WF_Matrix (col_add_each T as' col).
Proof. intros.
unfold col_add_each.
apply WF_plus; try easy;
apply WF_mult; try easy;
apply WF_get_col; easy.
Qed.
Lemma WF_row_add_each : forall {m n} (row : nat) (as' : Vector m) (T : Matrix m n),
WF_Matrix T -> WF_Matrix as' -> WF_Matrix (row_add_each T as' row).
Proof. intros.
unfold row_add_each.
apply WF_plus; try easy;
apply WF_mult; try easy;
apply WF_get_row; easy.
Qed.
Lemma WF_make_col_val : forall {m n} (T : Matrix m n) (col : nat) (a : C),
col < n ->
WF_Matrix T -> WF_Matrix (make_col_val T col a).
Proof. unfold make_col_val, WF_Matrix.
intros.
bdestruct_all; simpl; rewrite H0; try easy.
Qed.
Lemma WF_make_row_val : forall {m n} (T : Matrix m n) (row : nat) (a : C),
row < m ->
WF_Matrix T -> WF_Matrix (make_row_val T row a).
Proof. unfold make_row_val, WF_Matrix.
intros.
bdestruct_all; simpl; rewrite H0; try easy.
Qed.
Lemma WF_col_to_front : forall {m n} (T : Matrix m n) (col : nat),
col < n ->
WF_Matrix T ->
WF_Matrix (col_to_front T col).
Proof. intros.
unfold WF_Matrix, col_to_front; intros.
bdestruct_all; apply H0; lia.
Qed.
Lemma WF_row_to_front : forall {m n} (T : Matrix m n) (row : nat),
row < m ->
WF_Matrix T ->
WF_Matrix (row_to_front T row).
Proof. intros.
unfold WF_Matrix, row_to_front; intros.
bdestruct_all; apply H0; lia.
Qed.
Lemma WF_col_replace : forall {m n} (T : Matrix m n) (col rep : nat),
rep < n ->
WF_Matrix T ->
WF_Matrix (col_replace T col rep).
Proof. intros.
unfold WF_Matrix, col_replace; intros.
bdestruct_all; apply H0; lia.
Qed.
Lemma WF_row_replace : forall {m n} (T : Matrix m n) (row rep : nat),
rep < m ->
WF_Matrix T ->
WF_Matrix (row_replace T row rep).
Proof. intros.
unfold WF_Matrix, row_replace; intros.
bdestruct_all; apply H0; lia.
Qed.
#[export] Hint Resolve WF_get_col WF_get_row WF_reduce_row WF_reduce_col WF_reduce_vecn WF_get_minor : wf_db.
#[export] Hint Resolve WF_row_append WF_col_append WF_row_wedge WF_col_wedge WF_smash : wf_db.
#[export] Hint Resolve WF_col_swap WF_row_swap WF_col_scale WF_row_scale WF_col_add WF_row_add : wf_db.
#[export] Hint Resolve WF_gen_new_col WF_gen_new_row WF_col_add_many WF_row_add_many : wf_db.
#[export] Hint Resolve WF_col_scale_many WF_row_scale_many WF_col_add_each WF_row_add_each : wf_db.
#[export] Hint Resolve WF_make_col_val WF_make_row_val WF_col_to_front WF_row_to_front
col_replace row_replace: wf_db.
#[export] Hint Extern 1 (Nat.lt _ _) => lia : wf_db.
#[export] Hint Extern 1 (Nat.le _ _) => lia : wf_db.
#[export] Hint Extern 1 (lt _ _) => lia : wf_db.
#[export] Hint Extern 1 (le _ _) => lia : wf_db.
Lemma WF_pad1 : forall {m n : nat} (A : Matrix m n) (c : C),
WF_Matrix A -> WF_Matrix (pad1 A c).
Proof. intros.
unfold pad1; auto with wf_db.
Qed.
Lemma WF_pad1_conv : forall {m n : nat} (A : Matrix m n) (c : C),
WF_Matrix (pad1 A c) -> WF_Matrix A.
Proof. intros.
unfold pad1, WF_Matrix, col_wedge, row_wedge, e_i in *.
intros.
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.
#[export] Hint Resolve WF_pad1 WF_pad1_conv : wf_db.
(** * proving basic lemmas about these new functions *)
Lemma get_col_reduce_col : forall {m n} (i col : nat) (T : Matrix m (S n)),
i < col -> get_col (reduce_col T col) i = get_col T i.
Proof. intros.
prep_matrix_equality.
unfold get_col, reduce_col.
bdestruct (i <? col); try lia; easy.
Qed.
Lemma get_col_conv : forall {m n} (x y : nat) (T : Matrix m n),
(get_col T y) x 0 = T x y.
Proof. intros. unfold get_col.
easy.
Qed.
Lemma get_row_conv : forall {m n} (x y : nat) (T : Matrix m n),
(get_row T x) 0 y = T x y.
Proof. intros. unfold get_row.
easy.
Qed.
Lemma get_col_mult : forall {n} (i : nat) (A B : Square n),
A × (get_col B i) = get_col (A × B) i.
Proof. intros. unfold get_col, Mmult.
prep_matrix_equality.
bdestruct (y =? 0).
- reflexivity.
- apply @big_sum_0. intros.
apply Cmult_0_r.
Qed.
Lemma det_by_get_col : forall {m n} (A B : Matrix m n),
(forall i, get_col A i = get_col B i) -> A = B.
Proof. intros. prep_matrix_equality.
rewrite <- get_col_conv.
rewrite <- (get_col_conv _ _ B).
rewrite H.
reflexivity.
Qed.
Lemma get_col_reduce_wedge_miss : forall {m n} (T : Matrix m (S n)) (v : Vector m) (i : nat),
i < n -> get_col (col_wedge (reduce_col T n) v n) i = get_col T i.
Proof. intros.
prep_matrix_equality.
unfold get_col, col_wedge, reduce_col.
bdestruct_all; easy.
Qed.
#[deprecated(note="Use get_col_reduce_wedge_miss instead")]
Notation get_col_reduce_append_miss := get_col_reduce_wedge_miss (only parsing).
Lemma get_col_reduce_wedge_hit : forall {m n} (T : Matrix m (S n)) (v : Vector m),
WF_Matrix v -> get_col (col_wedge (reduce_col T n) v n) n = v.
Proof. intros.
unfold get_col, col_wedge, reduce_col.
prep_matrix_equality.
bdestruct (y =? 0).
- bdestruct_all; subst; easy.
- rewrite H; try lia; easy.
Qed.
#[deprecated(note="Use get_col_reduce_wedge_hit instead")]
Notation get_col_reduce_append_hit := get_col_reduce_wedge_hit (only parsing).
Lemma get_col_over : forall {m n} (T : Matrix m (S n)) (i : nat),
WF_Matrix T -> i > n ->
get_col T i = Zero.
Proof. intros.
prep_matrix_equality.
unfold get_col.
bdestruct_all; try easy.
rewrite H. easy.
right. lia.
Qed.
Lemma get_col_col_scale_many : forall {m n} (T : Matrix m n) (as' : Matrix 1 n) (i : nat),
get_col (col_scale_many T as') i = as' O i .* get_col T i.
Proof. intros.
unfold get_col, col_scale_many, scale.
prep_matrix_equality.
bdestruct_all; lca.
Qed.
Lemma get_row_row_scale_many : forall {m n} (T : Matrix m n) (as' : Vector m) (i : nat),
get_row (row_scale_many T as') i = as' i O .* get_row T i.
Proof. intros.
unfold get_row, row_scale_many, scale.
prep_matrix_equality.
bdestruct_all; lca.
Qed.
Lemma col_scale_reduce_col_same : forall {m n} (T : Matrix m (S n)) (y col : nat) (a : C),
y = col -> reduce_col (col_scale T col a) y = reduce_col T y.
Proof. intros.
prep_matrix_equality.
unfold reduce_col, col_scale.
bdestruct (y0 <? y); bdestruct (y0 =? col); bdestruct (1 + y0 =? col); try lia; easy.
Qed.
Lemma col_swap_get_minor_before : forall {n : nat} (A : Square (S n)) (row col c1 c2 : nat),
col < (S c1) -> col < (S c2) ->
get_minor (col_swap A (S c1) (S c2)) row col = col_swap (get_minor A row col) c1 c2.
Proof. intros.
prep_matrix_equality.
unfold get_minor, col_swap.
bdestruct (c1 <? col); bdestruct (c2 <? col); try lia.
simpl.
bdestruct (x <? row); bdestruct (y <? col); bdestruct (y =? c1);
bdestruct (y =? S c1); bdestruct (y =? c2); bdestruct (y =? S c2); try lia; try easy.
Qed.
Lemma col_scale_get_minor_before : forall {n : nat} (A : Square (S n)) (x y col : nat) (a : C),
y < col -> get_minor (col_scale A col a) x y = col_scale (get_minor A x y) (col - 1) a.
Proof. intros.
prep_matrix_equality.
destruct col; try lia.
rewrite Sn_minus_1.
unfold get_minor, col_scale.
bdestruct (x0 <? x); bdestruct (y0 <? y); bdestruct (y0 =? S col);
bdestruct (y0 =? col); bdestruct (1 + y0 =? S col); try lia; easy.
Qed.
Lemma col_scale_get_minor_same : forall {n : nat} (A : Square (S n)) (x y col : nat) (a : C),
y = col -> get_minor (col_scale A col a) x y = get_minor A x y.
Proof. intros.
prep_matrix_equality.
unfold get_minor, col_scale.
bdestruct (x0 <? x); bdestruct (y0 <? y);
bdestruct (y0 =? col); bdestruct (1 + y0 =? col); try lia; easy.
Qed.
Lemma col_scale_get_minor_after : forall {n : nat} (A : Square (S n)) (x y col : nat) (a : C),
y > col -> get_minor (col_scale A col a) x y = col_scale (get_minor A x y) col a.
Proof. intros.
prep_matrix_equality.
unfold get_minor, col_scale.
bdestruct (x0 <? x); bdestruct (y0 <? y);
bdestruct (y0 =? col); bdestruct (1 + y0 =? col); try lia; easy.
Qed.
Lemma row_scale_get_minor_before : forall {n : nat} (A : Square (S n)) (x y row : nat) (a : C),
x < row -> get_minor (row_scale A row a) x y = row_scale (get_minor A x y) (row - 1) a.
Proof. intros.
prep_matrix_equality.
destruct row; try lia.
rewrite Sn_minus_1.
unfold get_minor, row_scale.
bdestruct_all; simpl; easy.
Qed.
Lemma row_scale_get_minor_same : forall {n : nat} (A : Square (S n)) (x y row : nat) (a : C),
x = row -> get_minor (row_scale A row a) x y = get_minor A x y.
Proof. intros.
prep_matrix_equality.
unfold get_minor, row_scale.
bdestruct_all; simpl; easy.
Qed.
Lemma row_scale_get_minor_after : forall {n : nat} (A : Square (S n)) (x y row : nat) (a : C),
x > row -> get_minor (row_scale A row a) x y = row_scale (get_minor A x y) row a.
Proof. intros.
prep_matrix_equality.
unfold get_minor, row_scale.
bdestruct_all; simpl; easy.
Qed.
Lemma mcv_reduce_col_same : forall {m n} (T : Matrix m (S n)) (col : nat) (a : C),
reduce_col (make_col_val T col a) col = reduce_col T col.
Proof. intros.
prep_matrix_equality.
unfold reduce_col, make_col_val.
bdestruct (y <? col); bdestruct (1 + y <? col);
bdestruct (y =? col); bdestruct (1 + y =? col); try lia; easy.
Qed.
Lemma mrv_reduce_row_same : forall {m n} (T : Matrix (S m) n) (row : nat) (a : C),
reduce_row (make_row_val T row a) row = reduce_row T row.
Proof. intros.
prep_matrix_equality.
unfold reduce_row, make_row_val.
bdestruct (x <? row); bdestruct (1 + x <? row);
bdestruct (x =? row); bdestruct (1 + x =? row); try lia; easy.
Qed.
Lemma col_add_many_reduce_col_same : forall {m n} (T : Matrix m (S n)) (v : Vector (S n))
(col : nat),
reduce_col (col_add_many T v col) col = reduce_col T col.
Proof. intros.
unfold reduce_col, col_add_many.
prep_matrix_equality.
bdestruct (y <? col); bdestruct (1 + y <? col);
bdestruct (y =? col); bdestruct (1 + y =? col); try lia; easy.
Qed.
Lemma row_add_many_reduce_row_same : forall {m n} (T : Matrix (S m) n) (v : Matrix 1 (S m))
(row : nat),
reduce_row (row_add_many T v row) row = reduce_row T row.
Proof. intros.
unfold reduce_row, row_add_many.
prep_matrix_equality.
bdestruct (x <? row); bdestruct (1 + x <? row);
bdestruct (x =? row); bdestruct (1 + x =? row); try lia; easy.
Qed.
Lemma col_wedge_reduce_col_same : forall {m n} (T : Matrix m n) (v : Vector n)
(col : nat),
reduce_col (col_wedge T v col) col = T.
Proof. intros.
prep_matrix_equality.
unfold reduce_col, col_wedge.
assert (p : (1 + y - 1) = y). lia.
bdestruct (y <? col); bdestruct (1 + y <? col);
bdestruct (y =? col); bdestruct (1 + y =? col); try lia; try easy.
all : rewrite p; easy.
Qed.
Lemma row_wedge_reduce_row_same : forall {m n} (T : Matrix m n) (v : Matrix 1 m)
(row : nat),
reduce_row (row_wedge T v row) row = T.
Proof. intros.
prep_matrix_equality.
unfold reduce_row, row_wedge.
assert (p : (1 + x - 1) = x). lia.
bdestruct (x <? row); bdestruct (1 + x <? row);
bdestruct (x =? row); bdestruct (1 + x =? row); try lia; try easy.
all : rewrite p; easy.
Qed.
Lemma col_wedge_zero : forall {m n} (T : Matrix m n) (j : nat),
col_wedge T Zero j = Zero ->
T = Zero.
Proof. intros.
prep_matrix_equality.
unfold col_wedge in H.
apply (f_equal_inv x) in H.
bdestruct (y <? j).
- apply (f_equal_inv y) in H.
bdestruct (y <? j); try lia.
easy.
- apply (f_equal_inv (y + 1)) in H.
bdestruct (y + 1 <? j); bdestruct (y + 1 =? j); try lia.
replace (y + 1 - 1)%nat with y in H by lia.
rewrite H; easy.
Qed.
Lemma row_wedge_zero : forall {m n} (T : Matrix m n) (i : nat),
row_wedge T Zero i = Zero ->
T = Zero.
Proof. intros.
prep_matrix_equality.
unfold row_wedge in H.
bdestruct (x <? i).
- apply (f_equal_inv x) in H.
apply (f_equal_inv y) in H.
bdestruct (x <? i); try lia.
easy.
- apply (f_equal_inv (x + 1)) in H.
apply (f_equal_inv y) in H.
bdestruct (x + 1 <? i); bdestruct (x + 1 =? i); try lia.
replace (x + 1 - 1)%nat with x in H by lia.
rewrite H; easy.
Qed.
Lemma col_add_many_reduce_row : forall {m n} (T : Matrix (S m) n) (v : Vector n) (col row : nat),
col_add_many (reduce_row T row) v col = reduce_row (col_add_many T v col) row.
Proof. intros.
prep_matrix_equality.
unfold col_add_many, reduce_row, gen_new_col, scale, get_col.
bdestruct (y =? col); try lia; try easy.
bdestruct (x <? row); try lia.
apply f_equal_gen; auto.
do 2 rewrite Msum_Csum.
apply big_sum_eq_bounded; intros.
bdestruct (x <? row); try lia; easy.
apply f_equal_gen; auto.
do 2 rewrite Msum_Csum.
apply big_sum_eq_bounded; intros.
bdestruct (x <? row); try lia; easy.
Qed.
Lemma col_swap_same : forall {m n} (T : Matrix m n) (x : nat),
col_swap T x x = T.
Proof. intros.
unfold col_swap.
prep_matrix_equality.
bdestruct (y =? x); try easy.
rewrite H; easy.
Qed.
Lemma row_swap_same : forall {m n} (T : Matrix m n) (x : nat),
row_swap T x x = T.
Proof. intros.
unfold row_swap.
prep_matrix_equality.
bdestruct (x0 =? x); try easy.
rewrite H; easy.
Qed.
Lemma col_swap_diff_order : forall {m n} (T : Matrix m n) (x y : nat),
col_swap T x y = col_swap T y x.
Proof. intros.
prep_matrix_equality.
unfold col_swap.
bdestruct (y0 =? x); bdestruct (y0 =? y); try easy.
rewrite <- H, <- H0; easy.
Qed.
Lemma row_swap_diff_order : forall {m n} (T : Matrix m n) (x y : nat),
row_swap T x y = row_swap T y x.
Proof. intros.
prep_matrix_equality.
unfold row_swap.
bdestruct (x0 =? x); bdestruct (x0 =? y); try easy.
rewrite <- H, <- H0; easy.
Qed.
Lemma col_swap_inv : forall {m n} (T : Matrix m n) (x y : nat),
T = col_swap (col_swap T x y) x y.
Proof. intros.
prep_matrix_equality.
unfold col_swap.
bdestruct (y0 =? x); bdestruct (y0 =? y);
bdestruct (y =? x); bdestruct (x =? x); bdestruct (y =? y);
try easy.
all : (try rewrite H; try rewrite H0; try rewrite H1; easy).
Qed.
Lemma row_swap_inv : forall {m n} (T : Matrix m n) (x y : nat),
T = row_swap (row_swap T x y) x y.
Proof. intros.
prep_matrix_equality.
unfold row_swap.
bdestruct (x0 =? x); bdestruct (x0 =? y);
bdestruct (y =? x); bdestruct (x =? x); bdestruct (y =? y);
try easy.
all : (try rewrite H; try rewrite H0; try rewrite H1; easy).
Qed.
Lemma col_swap_get_col : forall {m n} (T : Matrix m n) (x y : nat),
get_col (col_swap T x y) x = get_col T y.
Proof. intros.
prep_matrix_equality.
unfold get_col, col_swap.
bdestruct (x =? x); bdestruct (x =? y); try lia; try easy.
Qed.
Lemma col_swap_get_col_miss : forall {m n} (T : Matrix m n) (i x y : nat),
i <> x -> i <> y ->
get_col (col_swap T x y) i = get_col T i.
Proof. intros.
prep_matrix_equality.
unfold get_col, col_swap.
bdestruct_all; easy.
Qed.
Lemma col_swap_three : forall {m n} (T : Matrix m n) (x y z : nat),
x <> z -> y <> z -> col_swap T x z = col_swap (col_swap (col_swap T x y) y z) x y.
Proof. intros.
bdestruct (x =? y).
rewrite H1, col_swap_same, col_swap_same.
easy.
prep_matrix_equality.
unfold col_swap.
bdestruct (y =? y); bdestruct (y =? x); bdestruct (y =? z); try lia.
bdestruct (x =? y); bdestruct (x =? x); bdestruct (x =? z); try lia.
bdestruct (z =? y); bdestruct (z =? x); try lia.
bdestruct (y0 =? y); bdestruct (y0 =? x); bdestruct (y0 =? z);
try lia; try easy.
rewrite H10.
easy.
Qed.
Lemma col_to_front_swap_col : forall {m n} (T : Matrix m n) (col : nat),
(S col) < n ->
col_to_front T (S col) = col_to_front (col_swap T col (S col)) col.
Proof. intros.
unfold col_to_front, col_swap.
prep_matrix_equality.
bdestruct_all; auto; subst.
replace (S col - 1)%nat with col by lia.
easy.
Qed.
Lemma row_to_front_swap_row : forall {m n} (T : Matrix m n) (row : nat),
(S row) < m ->
row_to_front T (S row) = row_to_front (row_swap T row (S row)) row.
Proof. intros.
unfold row_to_front, row_swap.
prep_matrix_equality.
bdestruct_all; auto; subst.
replace (S row - 1)%nat with row by lia.
easy.
Qed.
Lemma col_to_front_0 : forall {m n} (T : Matrix m n),
col_to_front T 0 = T.
Proof. intros.
unfold col_to_front.
prep_matrix_equality.
bdestruct_all; subst; easy.
Qed.
Lemma row_to_front_0 : forall {m n} (T : Matrix m n),
row_to_front T 0 = T.
Proof. intros.
unfold row_to_front.
prep_matrix_equality.
bdestruct_all; subst; easy.
Qed.
Lemma reduce_wedge_split_0 : forall {m n} (T : Matrix m (S n)),
WF_Matrix T -> T = col_wedge (reduce_col T O) (get_col T O) O.
Proof. intros.
prep_matrix_equality.
unfold col_wedge, get_col, reduce_col.
bdestruct_all; subst; try easy.
replace (1 + (y - 1)) with y by lia.
easy.
Qed.
Lemma reduce_wedge_split_n : forall {m n} (T : Matrix m (S n)),
WF_Matrix T -> T = col_wedge (reduce_col T n) (get_col T n) n.
Proof. intros.
prep_matrix_equality.
unfold col_wedge, get_col, reduce_col.
bdestruct_all; subst; try easy.
do 2 (rewrite H; try lia); easy.
Qed.
#[deprecated(note="Use reduce_wedge_split_n instead")]
Notation reduce_append_split := reduce_wedge_split_n (only parsing).