-
Notifications
You must be signed in to change notification settings - Fork 10
/
GenMatrix.v
4537 lines (3966 loc) · 145 KB
/
GenMatrix.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 matrices and prove many basic facts from linear algebra *)
Require Import Psatz.
Require Import String.
Require Import Program.
Require Import List.
Require Export Summation.
(* TODO: Use matrix equality everywhere, declare equivalence relation *)
(* TODO: Make all nat arguments to matrix lemmas implicit *)
(** * Matrix definitions and infrastructure **)
Declare Scope genmatrix_scope.
Delimit Scope genmatrix_scope with GM.
Open Scope genmatrix_scope.
Section LinAlgOverCommRing.
Variables (F : Type). (* F for ring, too bad R is taken :( *)
Variable (R0 : Monoid F).
Variable (R1 : Group F).
Variable (R2 : Comm_Group F).
Variable (R3 : Ring F).
Variable (R4 : Comm_Ring F).
(* TODO: make this better (although it already works well despite being naive) *)
Ltac dumb_lRa := repeat (repeat rewrite Gmult_plus_distr_l;
repeat rewrite Gmult_plus_distr_r;
repeat rewrite Gmult_assoc;
repeat rewrite Gmult_1_l;
repeat rewrite Gmult_1_r;
repeat rewrite Gmult_0_l;
repeat rewrite Gmult_0_r;
repeat rewrite Gplus_assoc;
repeat rewrite Gplus_0_l;
repeat rewrite Gplus_0_r; try easy).
Lemma F_ring_theory : ring_theory 0%G 1%G Gplus Gmult Gminus Gopp eq.
Proof. apply (@G_ring_theory F _ _ _ _ R4). Qed.
Add Ring F_ring_ring : F_ring_theory.
Local Open Scope nat_scope.
Local Open Scope group_scope.
Definition GenMatrix (m n : nat) := nat -> nat -> F.
Definition WF_GenMatrix {m n: nat} (A : GenMatrix m n) : Prop :=
forall x y, x >= m \/ y >= n -> A x y = 0.
Notation Vector n := (GenMatrix n 1).
Notation Square n := (GenMatrix n n).
(** Equality via functional extensionality *)
Ltac prep_genmatrix_equality :=
let x := fresh "x" in
let y := fresh "y" in
apply functional_extensionality; intros x;
apply functional_extensionality; intros y.
(** Matrix equivalence *)
Definition genmat_equiv {m n : nat} (A B : GenMatrix m n) : Prop :=
forall i j, i < m -> j < n -> A i j = B i j.
Infix "==" := genmat_equiv (at level 70) : genmatrix_scope.
Lemma genmat_equiv_refl : forall m n (A : GenMatrix m n), genmat_equiv A A.
Proof. unfold genmat_equiv; reflexivity. Qed.
Lemma genmat_equiv_eq : forall {m n : nat} (A B : GenMatrix m n),
WF_GenMatrix A ->
WF_GenMatrix B ->
A == B ->
A = B.
Proof.
intros m n A' B' WFA WFB Eq.
prep_genmatrix_equality.
unfold genmat_equiv in Eq.
bdestruct (x <? m).
bdestruct (y <? n).
+ apply Eq; easy.
+ rewrite WFA, WFB; trivial; right; try lia.
+ rewrite WFA, WFB; trivial; left; try lia.
Qed.
(** Printing *)
Parameter print_F : F -> string.
Fixpoint print_row {m n} i j (A : GenMatrix m n) : string :=
match j with
| 0 => "\n"
| S j' => print_F (A i j') ++ ", " ++ print_row i j' A
end.
Fixpoint print_rows {m n} i j (A : GenMatrix m n) : string :=
match i with
| 0 => ""
| S i' => print_row i' n A ++ print_rows i' n A
end.
Definition print_genmatrix {m n} (A : GenMatrix m n) : string :=
print_rows m n A.
(** 2D list representation *)
Definition list2D_to_genmatrix (l : list (list F)) :
GenMatrix (length l) (length (hd [] l)) :=
(fun x y => nth y (nth x l []) 0).
Lemma WF_list2D_to_genmatrix : forall m n li,
length li = m ->
(forall li', In li' li -> length li' = n) ->
@WF_GenMatrix m n (list2D_to_genmatrix li).
Proof.
intros m n li L f x y [l | r].
- unfold list2D_to_genmatrix.
rewrite (nth_overflow _ []).
destruct y; easy.
rewrite L. apply l.
- unfold list2D_to_genmatrix.
rewrite (nth_overflow _ 0).
easy.
destruct (nth_in_or_default x li []) as [IN | DEF].
apply f in IN.
rewrite IN. apply r.
rewrite DEF.
simpl; lia.
Qed.
(** Example *)
Definition M23 : GenMatrix 2 3 :=
fun x y =>
match (x, y) with
| (0, 0) => 1
| (0, 1) => 1+1
| (0, 2) => 1+1+1
| (1, 0) => 1+1+1+1
| (1, 1) => 1+1+1+1+1
| (1, 2) => 1+1+1+1+1+1
| _ => 0
end.
Definition M23' : GenMatrix 2 3 :=
list2D_to_genmatrix
([ [1; 1+1; 1+1+1];
[1+1+1+1; 1+1+1+1+1; 1+1+1+1+1+1] ]).
Lemma M23eq : M23 = M23'.
Proof.
unfold M23'.
compute.
prep_genmatrix_equality.
do 4 (try destruct x; try destruct y; simpl; trivial).
Qed.
(** * Operands and operations **)
Definition Zero {m n : nat} : GenMatrix m n := fun x y => 0.
Definition I (n : nat) : Square n :=
(fun x y => if (x =? y) && (x <? n) then 1 else 0).
(* Optional coercion to scalar (should be limited to 1 × 1 matrices):
Definition to_scalar (m n : nat) (A: GenMatrix m n) : C := A 0 0.
Coercion to_scalar : GenMatrix >-> C.
*)
(* This isn't used, but is interesting *)
Definition I__inf := fun x y => if x =? y then 1 else 0.
Notation "I∞" := I__inf : genmatrix_scope.
(*TODO: the placement of G's is horribly inconsistent... can probably be fixed since
eventually Matrix n m will be something more specific like CMatrix n m *)
Definition trace {n : nat} (A : Square n) :=
big_sum (fun x => A x x) n.
Definition scale {m n : nat} (r : F) (A : GenMatrix m n) : GenMatrix m n :=
fun x y => (r * A x y).
Definition dot {n : nat} (A : Vector n) (B : Vector n) : F :=
big_sum (fun x => A x 0 * B x 0) n.
Definition GMplus {m n : nat} (A B : GenMatrix m n) : GenMatrix m n :=
fun x y => (A x y + B x y).
Definition GMopp {m n : nat} (A : GenMatrix m n) : GenMatrix m n :=
scale (Gopp 1) A.
Definition GMminus {m n : nat} (A B : GenMatrix m n) : GenMatrix m n :=
GMplus A (GMopp B).
Definition GMmult {m n o : nat} (A : GenMatrix m n) (B : GenMatrix n o) : GenMatrix m o :=
fun x z => big_sum (fun y => A x y * B y z) n.
(* Only well-defined when o and p are non-zero *)
Definition Gkron {m n o p : nat} (A : GenMatrix m n) (B : GenMatrix o p) :
GenMatrix (m*o) (n*p) :=
fun x y => Gmult (A (x / o)%nat (y / p)%nat) (B (x mod o) (y mod p)).
Definition direct_sum {m n o p : nat} (A : GenMatrix m n) (B : GenMatrix o p) :
GenMatrix (m+o) (n+p) :=
fun x y => if (x <? m) || (y <? n) then A x y else B (x - m)%nat (y - n)%nat.
Definition transpose {m n} (A : GenMatrix m n) : GenMatrix n m :=
fun x y => A y x.
(* NB: no adjoint!
Definition adjoint {m n} (A : GenMatrix m n) : GenMatrix n m :=
fun x y => (A y x)^*.
*)
(* no adjoint! so these are defined in terms of transpose. good for R, but is this correct? *)
Definition inner_product {n} (u v : Vector n) : F :=
GMmult (transpose u) (v) 0 0.
Definition outer_product {n} (u v : Vector n) : Square n :=
GMmult u (transpose v).
(** Kronecker of n copies of A *)
Fixpoint kron_n n {m1 m2} (A : GenMatrix m1 m2) : GenMatrix (m1^n) (m2^n) :=
match n with
| 0 => I 1
| S n' => Gkron (kron_n n' A) A
end.
(** Kronecker product of a list *)
Fixpoint big_kron {m n} (As : list (GenMatrix m n)) :
GenMatrix (m^(length As)) (n^(length As)) :=
match As with
| [] => I 1
| A :: As' => Gkron A (big_kron As')
end.
(** Product of n copies of A, basically GMpow *)
Fixpoint GMmult_n {m} (A : Square m) p : Square m :=
match p with
| 0 => I m
| S p' => GMmult A (GMmult_n A p')
end.
(** Direct sum of n copies of A *)
Fixpoint direct_sum_n n {m1 m2} (A : GenMatrix m1 m2) : GenMatrix (n*m1) (n*m2) :=
match n with
| 0 => @Zero 0 0
| S n' => direct_sum A (direct_sum_n n' A)
end.
(** Notations *)
Infix "∘" := dot (at level 40, left associativity) : genmatrix_scope.
Infix ".+" := GMplus (at level 50, left associativity) : genmatrix_scope.
Infix ".*" := scale (at level 40, left associativity) : genmatrix_scope.
Infix "×" := GMmult (at level 40, left associativity) : genmatrix_scope.
Infix "⊗" := Gkron (at level 40, left associativity) : genmatrix_scope.
Infix ".⊕" := direct_sum (at level 20) : genmatrix_scope. (* should have different level and assoc *)
Infix "≡" := genmat_equiv (at level 70) : genmatrix_scope.
Notation "A ⊤" := (transpose A) (at level 0) : genmatrix_scope.
(* Notation "A †" := (adjoint A) (at level 0) : genmatrix_scope. *)
Notation Σ := (@big_sum F R0). (* we intoduce Σ notation here *)
Notation "n ⨂ A" := (kron_n n A) (at level 30, no associativity) : genmatrix_scope.
Notation "⨂ A" := (big_kron A) (at level 60): genmatrix_scope.
Notation "p ⨉ A" := (GMmult_n A p) (at level 30, no associativity) : genmatrix_scope.
Notation "⟨ u , v ⟩" := (inner_product u v) (at level 0) : genmatrix_scope.
Hint Unfold Zero I trace dot GMplus GMopp scale GMmult Gkron genmat_equiv transpose : U_db.
Ltac destruct_m_1 :=
match goal with
| [ |- context[match ?x with
| 0 => _
| S _ => _
end] ] => is_var x; destruct x
end.
Ltac destruct_m_eq := repeat (destruct_m_1; simpl).
Ltac lgma :=
autounfold with U_db;
prep_genmatrix_equality;
destruct_m_eq;
(* lca. *) (* !!! everything is destroyed without lca for rings *)
ring.
Ltac solve_end :=
match goal with
| H : lt _ O |- _ => apply Nat.nlt_0_r in H; contradict H
end.
Ltac by_cell :=
intros;
let i := fresh "i" in
let j := fresh "j" in
let Hi := fresh "Hi" in
let Hj := fresh "Hj" in
intros i j Hi Hj; try solve_end;
repeat (destruct i as [|i]; simpl; [|apply <- Nat.succ_lt_mono in Hi]; try solve_end); clear Hi;
repeat (destruct j as [|j]; simpl; [|apply <- Nat.succ_lt_mono in Hj]; try solve_end); clear Hj.
Ltac lgma' :=
apply genmat_equiv_eq;
repeat match goal with
| [ |- WF_GenMatrix (?A) ] => auto with wf_db (* (try show_wf) *)
| [ |- genmat_equiv (?A) (?B) ] => by_cell; try ring (* try lca *)
end.
(** * Showing that M is a vector space *)
Program Instance GM_is_monoid : forall n m, Monoid (GenMatrix n m) :=
{ Gzero := @Zero n m
; Gplus := GMplus
}.
Solve All Obligations with program_simpl; prep_genmatrix_equality;
autounfold with U_db; ring.
Program Instance GM_is_group : forall n m, Group (GenMatrix n m) :=
{ Gopp := GMopp }.
Solve All Obligations with program_simpl; prep_genmatrix_equality;
autounfold with U_db; try ring.
Program Instance M_is_comm_group : forall n m, Comm_Group (GenMatrix n m).
Solve All Obligations with program_simpl; prep_genmatrix_equality;
autounfold with U_db; ring.
Program Instance M_is_module_space : forall n m, Module_Space (GenMatrix n m) F :=
{ Vscale := scale }.
Solve All Obligations with program_simpl; prep_genmatrix_equality;
autounfold with U_db; ring.
(* lemmas which are useful for simplifying proofs involving matrix operations *)
Lemma kron_simplify : forall (n m o p : nat) (a b : GenMatrix n m) (c d : GenMatrix o p),
a = b -> c = d -> (a ⊗ c)%GM = (b ⊗ d)%GM.
Proof. intros; subst; easy.
Qed.
Lemma n_kron_simplify : forall (n m : nat) (a b : GenMatrix n m) (n m : nat),
a = b -> n = m -> n ⨂ a = m ⨂ b.
Proof. intros; subst; easy.
Qed.
Lemma Mtranspose_simplify : forall (n m : nat) (a b : GenMatrix n m),
a = b -> a⊤ = b⊤.
Proof. intros; subst; easy.
Qed.
(*
Lemma Madjoint_simplify : forall (n m : nat) (a b : GenMatrix n m),
a = b -> a† = b†.
Proof. intros; subst; easy.
Qed.
*)
Lemma Mmult_simplify : forall (n m o : nat) (a b : GenMatrix n m) (c d : GenMatrix m o),
a = b -> c = d -> a × c = b × d.
Proof. intros; subst; easy.
Qed.
Lemma Mmult_n_simplify : forall (n : nat) (a b : Square n) (c d : nat),
a = b -> c = d -> c ⨉ a = d ⨉ b.
Proof. intros; subst; easy.
Qed.
Lemma dot_simplify : forall (n : nat) (a b c d: Vector n),
a = b -> c = d -> a ∘ c = b ∘ c.
Proof. intros; subst; easy.
Qed.
Lemma Mplus_simplify : forall (n m: nat) (a b : GenMatrix n m) (c d : GenMatrix n m),
a = b -> c = d -> a .+ c = b .+ d.
Proof. intros; subst; easy.
Qed.
Lemma Mscale_simplify : forall (n m: nat) (a b : GenMatrix n m) (c d : F),
a = b -> c = d -> c .* a = d .* b.
Proof. intros; subst; easy.
Qed.
(** * Proofs about well-formedness **)
Lemma WF_GenMatrix_dim_change : forall (m n m' n' : nat) (A : GenMatrix m n),
m = m' ->
n = n' ->
@WF_GenMatrix m n A ->
@WF_GenMatrix m' n' A.
Proof. intros. subst. easy. Qed.
Lemma WF_Zero : forall m n : nat, WF_GenMatrix (@Zero m n).
Proof. intros m n. unfold WF_GenMatrix. reflexivity. Qed.
Lemma WF_I : forall n : nat, WF_GenMatrix (I n).
Proof.
unfold WF_GenMatrix, I. intros n x y H. simpl.
destruct H; bdestruct (x =? y); bdestruct (x <? n); trivial; lia.
Qed.
Lemma WF_I1 : WF_GenMatrix (I 1). Proof. apply WF_I. Qed.
Lemma WF_scale : forall {m n : nat} (r : F) (A : GenMatrix m n),
WF_GenMatrix A -> WF_GenMatrix (scale r A).
Proof.
unfold WF_GenMatrix, scale.
intros m n r A H x y H0. simpl.
rewrite H; trivial.
rewrite Gmult_0_r.
reflexivity.
Qed.
Lemma WF_plus : forall {m n} (A B : GenMatrix m n),
WF_GenMatrix A -> WF_GenMatrix B -> WF_GenMatrix (A .+ B).
Proof.
unfold WF_GenMatrix, GMplus.
intros m n A B H H0 x y H1. simpl.
rewrite H, H0; trivial.
rewrite Gplus_0_l.
reflexivity.
Qed.
Lemma WF_mult : forall {m n o : nat} (A : GenMatrix m n) (B : GenMatrix n o),
WF_GenMatrix A -> WF_GenMatrix B -> WF_GenMatrix (A × B).
Proof.
unfold WF_GenMatrix, GMmult.
intros m n o A B H H0 x y D.
apply (@big_sum_0 F R0).
destruct D; intros z.
+ rewrite H; [rewrite Gmult_0_l; easy | auto].
+ rewrite H0; [rewrite Gmult_0_r; easy | auto].
Qed.
Lemma WF_kron : forall {m n o p q r : nat} (A : GenMatrix m n) (B : GenMatrix o p),
q = (m * o)%nat -> r = (n * p)%nat ->
WF_GenMatrix A -> WF_GenMatrix B -> @WF_GenMatrix q r (A ⊗ B).
Proof.
unfold WF_GenMatrix, Gkron.
intros m n o p q r A B Nn No H H0 x y H1. subst.
bdestruct (o =? 0). rewrite H0; [rewrite Gmult_0_r; easy|lia].
bdestruct (p =? 0). rewrite H0; [rewrite Gmult_0_r; easy|lia].
rewrite H.
rewrite Gmult_0_l; reflexivity.
destruct H1.
unfold ge in *.
left.
apply Nat.div_le_lower_bound; trivial.
rewrite Nat.mul_comm.
assumption.
right.
apply Nat.div_le_lower_bound; trivial.
rewrite Nat.mul_comm.
assumption.
Qed.
Lemma WF_direct_sum : forall {m n o p q r : nat} (A : GenMatrix m n) (B : GenMatrix o p),
q = (m + o)%nat -> r = (n + p)%nat ->
WF_GenMatrix A -> WF_GenMatrix B -> @WF_GenMatrix q r (A .⊕ B).
Proof.
unfold WF_GenMatrix, direct_sum.
intros; subst.
destruct H3; bdestruct_all; simpl; try apply H1; try apply H2.
all : try lia.
Qed.
Lemma WF_transpose : forall {m n : nat} (A : GenMatrix m n),
WF_GenMatrix A -> WF_GenMatrix A⊤.
Proof. unfold WF_GenMatrix, transpose. intros m n A H x y H0. apply H.
destruct H0; auto. Qed.
(*
Lemma WF_adjoint : forall {m n : nat} (A : GenMatrix m n),
WF_GenMatrix A -> WF_GenMatrix A†.
Proof. unfold WF_GenMatrix, adjoint, Cconj. intros m n A H x y H0. simpl.
rewrite H. lca. lia. Qed.
*)
Lemma WF_outer_product : forall {n} (u v : Vector n),
WF_GenMatrix u ->
WF_GenMatrix v ->
WF_GenMatrix (outer_product u v).
Proof. intros. apply WF_mult; [|apply WF_transpose]; assumption. Qed.
Lemma WF_kron_n : forall n {m1 m2} (A : GenMatrix m1 m2),
WF_GenMatrix A -> WF_GenMatrix (kron_n n A).
Proof.
intros.
induction n; simpl.
- apply WF_I.
- apply WF_kron; try lia; assumption.
Qed.
Lemma WF_big_kron : forall n m (l : list (GenMatrix m n)) (A : GenMatrix m n),
(forall i, WF_GenMatrix (nth i l A)) ->
WF_GenMatrix (⨂ l).
Proof.
intros n m l A H.
induction l.
- simpl. apply WF_I.
- simpl. apply WF_kron; trivial. apply (H O).
apply IHl. intros i. apply (H (S i)).
Qed.
(* alternate version that uses In instead of nth *)
Lemma WF_big_kron' : forall n m (l : list (GenMatrix m n)),
(forall A, In A l -> WF_GenMatrix A) ->
WF_GenMatrix (⨂ l).
Proof.
intros n m l H.
induction l.
- simpl. apply WF_I.
- simpl. apply WF_kron; trivial. apply H; left; easy.
apply IHl. intros A' H0. apply H; right; easy.
Qed.
Lemma WF_GMmult_n : forall n {m} (A : Square m),
WF_GenMatrix A -> WF_GenMatrix (GMmult_n A n).
Proof.
intros.
induction n; simpl.
- apply WF_I.
- apply WF_mult; assumption.
Qed.
Lemma WF_direct_sum_n : forall n {m1 m2} (A : GenMatrix m1 m2),
WF_GenMatrix A -> WF_GenMatrix (direct_sum_n n A).
Proof.
intros.
induction n; simpl.
- apply WF_Zero.
- apply WF_direct_sum; try lia; assumption.
Qed.
Lemma WF_Msum : forall d1 d2 n (f : nat -> GenMatrix d1 d2),
(forall i, (i < n)%nat -> WF_GenMatrix (f i)) ->
WF_GenMatrix (big_sum f n).
Proof.
intros.
apply big_sum_prop_distr; intros.
apply WF_plus; auto.
apply WF_Zero.
auto.
Qed.
Local Close Scope nat_scope.
(** * Tactics for showing well-formedness *)
Local Open Scope nat.
Local Open Scope G.
(* Much less awful *)
Ltac show_wf :=
unfold WF_GenMatrix;
let x := fresh "x" in
let y := fresh "y" in
let H := fresh "H" in
intros x y [H | H];
apply le_plus_minus' in H; rewrite H;
cbv;
destruct_m_eq;
try ring.
(* Create HintDb wf_db. *)
Hint Resolve WF_Zero WF_I WF_I1 WF_mult WF_plus WF_scale WF_transpose
WF_outer_product WF_big_kron WF_kron_n WF_kron
WF_GMmult_n (* WF_Msum *) : wf_db.
Hint Extern 2 (_ = _) => unify_pows_two : wf_db.
(* Utility tactics *)
Ltac has_hyp P :=
match goal with
| [ _ : P |- _ ] => idtac
end.
Ltac no_hyp P :=
match goal with
| [ _ : P |- _ ] => fail 1
| _ => idtac
end.
(* staggered, because it seems to speed things up (it shouldn't) *)
Ltac auto_wf :=
try match goal with
|- WF_GenMatrix _ => auto with wf_db;
auto 10 with wf_db;
auto 20 with wf_db;
auto 40 with wf_db;
auto 80 with wf_db;
auto 160 with wf_db
end.
(* Puts all well-formedness conditions for M into the context *)
Ltac collate_wf' M :=
match M with
(* already in context *)
| ?A => has_hyp (WF_GenMatrix A)
(* recursive case *)
| ?op ?A ?B => collate_wf' A;
collate_wf' B;
assert (WF_GenMatrix (op A B)) by auto with wf_db
(* base case *)
| ?A => assert (WF_GenMatrix A) by auto with wf_db
(* not a matrix *)
| _ => idtac
end.
(* Aggregates well-formedness conditions for context *)
Ltac collate_wf :=
match goal with
| |- ?A = ?B => collate_wf' A; collate_wf' B
| |- ?A == ?B => collate_wf' A; collate_wf' B
| |- WF_GenMatrix ?A => collate_wf' A
| |- context[?A] => collate_wf' A
end.
Ltac solve_wf := collate_wf; easy.
(** * Basic matrix lemmas *)
Lemma WF0_Zero_l :forall (n : nat) (A : GenMatrix 0%nat n), WF_GenMatrix A -> A = Zero.
Proof.
intros n A WFA.
prep_genmatrix_equality.
rewrite WFA.
reflexivity.
lia.
Qed.
Lemma WF0_Zero_r :forall (n : nat) (A : GenMatrix n 0%nat), WF_GenMatrix A -> A = Zero.
Proof.
intros n A WFA.
prep_genmatrix_equality.
rewrite WFA.
reflexivity.
lia.
Qed.
Lemma WF0_Zero :forall (A : GenMatrix 0%nat 0%nat), WF_GenMatrix A -> A = Zero.
Proof.
apply WF0_Zero_l.
Qed.
Lemma I0_Zero : I 0 = Zero.
Proof.
apply WF0_Zero.
apply WF_I.
Qed.
Lemma trace_plus_dist : forall (n : nat) (A B : Square n),
trace (A .+ B) = (trace A + trace B).
Proof.
intros.
unfold trace, GMplus.
rewrite (@big_sum_plus F _ _ R2).
easy.
Qed.
Lemma trace_mult_dist : forall n p (A : Square n), trace (p .* A) = (p * trace A).
Proof.
intros.
unfold trace, scale.
rewrite (@big_sum_mult_l F _ _ _ R3).
easy.
Qed.
Lemma GMplus_0_l : forall (m n : nat) (A : GenMatrix m n), Zero .+ A = A.
Proof. intros. lgma. Qed.
Lemma GMplus_0_r : forall (m n : nat) (A : GenMatrix m n), A .+ Zero = A.
Proof. intros. lgma. Qed.
Lemma GMmult_0_l : forall (m n o : nat) (A : GenMatrix n o), @Zero m n × A = Zero.
Proof.
intros m n o A.
unfold GMmult, Zero.
prep_genmatrix_equality.
apply (@big_sum_0 F R0).
intros.
ring.
Qed.
Lemma GMmult_0_r : forall (m n o : nat) (A : GenMatrix m n), A × @Zero n o = Zero.
Proof.
intros m n o A.
unfold Zero, GMmult.
prep_genmatrix_equality.
apply (@big_sum_0 F R0).
intros.
ring.
Qed.
(* using <= because our form big_sum is exclusive. *)
Lemma GMmult_1_l_gen: forall (m n : nat) (A : GenMatrix m n) (x z k : nat),
(k <= m)%nat ->
((k <= x)%nat -> big_sum (fun y : nat => I m x y * A y z) k = 0) /\
((k > x)%nat -> big_sum (fun y : nat => I m x y * A y z) k = A x z).
Proof.
intros m n A x z k B.
induction k.
* simpl. split. reflexivity. lia.
* destruct IHk as [IHl IHr]. lia.
split.
+ intros leSkx.
simpl.
unfold I.
bdestruct (x =? k); try lia.
simpl; dumb_lRa.
apply IHl.
lia.
+ intros gtSkx.
simpl in *.
unfold I in *.
bdestruct (x =? k); bdestruct (x <? m); subst; try lia.
rewrite IHl by lia; simpl; ring.
rewrite IHr by lia; simpl; ring.
Qed.
Lemma GMmult_1_l_mat_eq : forall (m n : nat) (A : GenMatrix m n), I m × A == A.
Proof.
intros m n A i j Hi Hj.
unfold GMmult.
edestruct (@GMmult_1_l_gen m n) as [Hl Hr].
apply Nat.le_refl.
unfold get.
apply Hr.
simpl in *.
lia.
Qed.
Lemma GMmult_1_l: forall (m n : nat) (A : GenMatrix m n),
WF_GenMatrix A -> I m × A = A.
Proof.
intros m n A H.
apply genmat_equiv_eq; trivial.
auto with wf_db.
apply GMmult_1_l_mat_eq.
Qed.
Lemma GMmult_1_r_gen: forall (m n : nat) (A : GenMatrix m n) (x z k : nat),
(k <= n)%nat ->
((k <= z)%nat -> big_sum (fun y : nat => A x y * (I n) y z) k = 0) /\
((k > z)%nat -> big_sum (fun y : nat => A x y * (I n) y z) k = A x z).
Proof.
intros m n A x z k B.
induction k.
simpl. split. reflexivity. lia.
destruct IHk as [IHl IHr].
lia.
split.
+ intros leSkz.
simpl in *.
unfold I.
bdestruct (k =? z); try lia.
simpl; dumb_lRa.
apply IHl; lia.
+ intros gtSkz.
simpl in *.
unfold I in *.
bdestruct (k =? z); subst.
- bdestruct (z <? n); try lia.
rewrite IHl by lia; simpl; ring.
- rewrite IHr by lia; simpl; ring.
Qed.
Lemma GMmult_1_r_mat_eq : forall (m n : nat) (A : GenMatrix m n), A × I n ≡ A.
Proof.
intros m n A i j Hi Hj.
unfold GMmult.
edestruct (@GMmult_1_r_gen m n) as [Hl Hr].
apply Nat.le_refl.
unfold get; simpl.
apply Hr.
lia.
Qed.
Lemma GMmult_1_r: forall (m n : nat) (A : GenMatrix m n),
WF_GenMatrix A -> A × I n = A.
Proof.
intros m n A H.
apply genmat_equiv_eq; trivial.
auto with wf_db.
apply GMmult_1_r_mat_eq.
Qed.
(* Cool facts about I∞, not used in the development *)
Lemma GMmult_inf_l : forall(m n : nat) (A : GenMatrix m n),
WF_GenMatrix A -> I∞ × A = A.
Proof.
intros m n A H.
prep_genmatrix_equality.
unfold GMmult.
edestruct (@GMmult_1_l_gen m n) as [Hl Hr].
apply Nat.le_refl.
bdestruct (m <=? x).
rewrite H by auto.
apply (@big_sum_0_bounded F R0).
intros z L.
unfold I__inf, I.
bdestruct (x =? z). lia. ring.
unfold I__inf, I in *.
erewrite big_sum_eq.
apply Hr.
assumption.
bdestruct (x <? m); [|lia].
apply functional_extensionality. intros. rewrite andb_true_r. reflexivity.
Qed.
Lemma GMmult_inf_r : forall(m n : nat) (A : GenMatrix m n),
WF_GenMatrix A -> A × I∞ = A.
Proof.
intros m n A H.
prep_genmatrix_equality.
unfold GMmult.
edestruct (@GMmult_1_r_gen m n) as [Hl Hr].
apply Nat.le_refl.
bdestruct (n <=? y).
rewrite H by auto.
apply (@big_sum_0_bounded F R0).
intros z L.
unfold I__inf, I.
bdestruct (z =? y). lia. ring.
unfold I__inf, I in *.
erewrite big_sum_eq.
apply Hr.
assumption.
apply functional_extensionality. intros z.
bdestruct (z =? y); bdestruct (z <? n); simpl; dumb_lRa; lia.
Qed.
Lemma kron_0_l : forall (m n o p : nat) (A : GenMatrix o p),
@Zero m n ⊗ A = Zero.
Proof.
intros m n o p A.
prep_genmatrix_equality.
unfold Zero, Gkron.
rewrite Gmult_0_l.
reflexivity.
Qed.
Lemma kron_0_r : forall (m n o p : nat) (A : GenMatrix m n),
A ⊗ @Zero o p = Zero.
Proof.
intros m n o p A.
prep_genmatrix_equality.
unfold Zero, Gkron.
rewrite Gmult_0_r.
reflexivity.
Qed.
Lemma kron_1_r : forall (m n : nat) (A : GenMatrix m n), A ⊗ I 1 = A.
Proof.
intros m n A.
prep_genmatrix_equality.
unfold I, Gkron.
rewrite 2 Nat.div_1_r.
rewrite 2 Nat.mod_1_r.
simpl.
ring.
Qed.
(* This side is more limited *)
Lemma kron_1_l : forall (m n : nat) (A : GenMatrix m n),
WF_GenMatrix A -> I 1 ⊗ A = A.
Proof.
intros m n A WF.
prep_genmatrix_equality.
unfold I, Gkron.
bdestruct (m =? 0). rewrite 2 WF by lia. ring.
bdestruct (n =? 0). rewrite 2 WF by lia. ring.
bdestruct (x / m <? 1); rename H1 into Eq1.
bdestruct (x / m =? y / n); rename H1 into Eq2; simpl.
+ assert (x / m = 0)%nat by lia. clear Eq1. rename H1 into Eq1.
rewrite Eq1 in Eq2.
symmetry in Eq2.
rewrite Nat.div_small_iff in Eq2 by lia.
rewrite Nat.div_small_iff in Eq1 by lia.
rewrite 2 Nat.mod_small; trivial.
ring.
+ assert (x / m = 0)%nat by lia. clear Eq1.
rewrite H1 in Eq2. clear H1.
assert (y / n <> 0)%nat by lia. clear Eq2.
rewrite Nat.div_small_iff in H1 by lia.
rewrite Gmult_0_l.
destruct WF with (x := x) (y := y). lia.
reflexivity.
+ rewrite andb_false_r.
assert (x / m <> 0)%nat by lia. clear Eq1.
rewrite Nat.div_small_iff in H1 by lia.
rewrite Gmult_0_l.
destruct WF with (x := x) (y := y). lia.
reflexivity.
Qed.
Theorem transpose_involutive : forall (m n : nat) (A : GenMatrix m n), (A⊤)⊤ = A.
Proof. reflexivity. Qed.
(*
Theorem adjoint_involutive : forall (m n : nat) (A : GenMatrix m n), A†† = A.
Proof. intros. lma. Qed.
*)
Lemma id_transpose_eq : forall n, (I n)⊤ = (I n).
Proof.
intros n. unfold transpose, I.
prep_genmatrix_equality.
bdestruct (y =? x); bdestruct (x =? y); bdestruct (y <? n); bdestruct (x <? n);
trivial; lia.
Qed.
Lemma zero_transpose_eq : forall m n, (@Zero m n)⊤ = @Zero m n.
Proof. reflexivity. Qed.
(*
Lemma id_adjoint_eq : forall n, (I n)† = (I n).
Proof.
intros n.
unfold adjoint, I.
prep_genmatrix_equality.
bdestruct (y =? x); bdestruct (x =? y); bdestruct (y <? n); bdestruct (x <? n);
try lia; lca.
Qed.
*)
(*
Lemma zero_adjoint_eq : forall m n, (@Zero m n)† = @Zero n m.
Proof. unfold adjoint, Zero. rewrite Cconj_0. reflexivity. Qed.
*)
Theorem GMplus_comm : forall (m n : nat) (A B : GenMatrix m n), A .+ B = B .+ A.
Proof.
unfold GMplus.
intros m n A B.
prep_genmatrix_equality.
apply Gplus_comm.
Qed.
Theorem GMplus_assoc : forall (m n : nat) (A B C : GenMatrix m n), A .+ B .+ C = A .+ (B .+ C).
Proof.
unfold GMplus.
intros m n A B C.
prep_genmatrix_equality.
rewrite Gplus_assoc.
reflexivity.
Qed.
Theorem GMmult_assoc : forall {m n o p : nat} (A : GenMatrix m n) (B : GenMatrix n o)
(C: GenMatrix o p), A × B × C = A × (B × C).
Proof.
intros m n o p A B C.
unfold GMmult.
prep_genmatrix_equality.
replace (fun y0 : nat => Σ (fun y1 : nat => A x y1 * B y1 y0) n * C y0 y) with
(fun y0 : nat => Σ (fun y1 : nat => A x y1 * B y1 y0 * C y0 y) n).
replace (fun y0 : nat => A x y0 * Σ (fun y1 : nat => B y0 y1 * C y1 y) o) with
(fun y0 : nat => Σ (fun y1 : nat => A x y0 * (B y0 y1 * C y1 y)) o).
rewrite big_sum_swap_order.
do 2 (apply big_sum_eq_bounded; intros; dumb_lRa).
all : apply functional_extensionality; intros.
rewrite big_sum_mult_l; easy.