-
Notifications
You must be signed in to change notification settings - Fork 10
/
Matrix.v
2263 lines (1951 loc) · 65.6 KB
/
Matrix.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 Export Complex.
Require Import List.
(* TODO: Use matrix equality everywhere, declare equivalence relation *)
(* TODO: Make all nat arguments to matrix lemmas implicit *)
(** * Matrix definitions and infrastructure **)
(* TODO: make this file with general field
Section LinearAlgebra.
Variables (F : Type).
Variable (H0 : Monoid F).
Variable (H1 : Group F).
Variable (H2 : Comm_Group F).
Variable (H3 : Ring F).
Variable (H4 : Comm_Ring F).
Variable (H5 : Field F).
End LinearAlgebra.
*)
Declare Scope matrix_scope.
Delimit Scope matrix_scope with M.
Open Scope matrix_scope.
Local Open Scope nat_scope.
Definition Matrix (m n : nat) := nat -> nat -> C.
Definition WF_Matrix {m n: nat} (A : Matrix m n) : Prop :=
forall x y, x >= m \/ y >= n -> A x y = C0.
(* makes a matrix well formed *)
Definition make_WF {m n} (A : Matrix m n) : Matrix m n :=
fun i j => if (i <? m) && (j <? n) then A i j else C0.
Notation Vector n := (Matrix n 1).
Notation Square n := (Matrix n n).
(** Equality via functional extensionality *)
Ltac prep_matrix_equality :=
let x := fresh "x" in
let y := fresh "y" in
apply functional_extensionality; intros x;
apply functional_extensionality; intros y.
(** Matrix equivalence *)
Definition mat_equiv {m n : nat} (A B : Matrix m n) : Prop :=
forall i j, i < m -> j < n -> A i j = B i j.
Infix "==" := mat_equiv (at level 70) : matrix_scope.
Lemma mat_equiv_refl : forall m n (A : Matrix m n), mat_equiv A A.
Proof. unfold mat_equiv; reflexivity. Qed.
Lemma mat_equiv_eq : forall {m n : nat} (A B : Matrix m n),
WF_Matrix A ->
WF_Matrix B ->
A == B ->
A = B.
Proof.
intros m n A' B' WFA WFB Eq.
prep_matrix_equality.
unfold mat_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_C : C -> string.
Fixpoint print_row {m n} i j (A : Matrix m n) : string :=
match j with
| 0 => "\n"
| S j' => print_C (A i j') ++ ", " ++ print_row i j' A
end.
Fixpoint print_rows {m n} i j (A : Matrix m n) : string :=
match i with
| 0 => ""
| S i' => print_row i' n A ++ print_rows i' n A
end.
Definition print_matrix {m n} (A : Matrix m n) : string :=
print_rows m n A.
(** 2D list representation *)
Definition list2D_to_matrix (l : list (list C)) :
Matrix (length l) (length (hd [] l)) :=
(fun x y => nth y (nth x l []) 0%R).
Lemma WF_list2D_to_matrix : forall m n li,
length li = m ->
(forall li', In li' li -> length li' = n) ->
@WF_Matrix m n (list2D_to_matrix li).
Proof.
intros m n li L F x y [l | r].
- unfold list2D_to_matrix.
rewrite (nth_overflow _ []).
destruct y; easy.
rewrite L. apply l.
- unfold list2D_to_matrix.
rewrite (nth_overflow _ C0).
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 : Matrix 2 3 :=
fun x y =>
match (x, y) with
| (0, 0) => 1%R
| (0, 1) => 2%R
| (0, 2) => 3%R
| (1, 0) => 4%R
| (1, 1) => 5%R
| (1, 2) => 6%R
| _ => C0
end.
Definition M23' : Matrix 2 3 :=
list2D_to_matrix
([ [RtoC 1; RtoC 2; RtoC 3];
[RtoC 4; RtoC 5; RtoC 6] ]).
Lemma M23eq : M23 = M23'.
Proof.
unfold M23'.
compute.
prep_matrix_equality.
do 4 (try destruct x; try destruct y; simpl; trivial).
Qed.
(** * Operands and operations **)
Definition Zero {m n : nat} : Matrix m n := fun x y => 0%R.
Definition I (n : nat) : Square n :=
(fun x y => if (x =? y) && (x <? n) then C1 else C0).
(* in many cases, n needs to be made explicit, but not always, hence it is made implicit here *)
Definition e_i {n : nat} (i : nat) : Vector n :=
fun x y => (if (x =? i) && (x <? n) && (y =? 0) then C1 else C0).
(* Optional coercion to scalar (should be limited to 1 × 1 matrices):
Definition to_scalar (m n : nat) (A: Matrix m n) : C := A 0 0.
Coercion to_scalar : Matrix >-> C.
*)
(* This isn't used, but is interesting *)
Definition I__inf := fun x y => if x =? y then C1 else C0.
Notation "I∞" := I__inf : matrix_scope.
Definition trace {n : nat} (A : Square n) :=
big_sum (fun x => A x x) n.
Definition scale {m n : nat} (r : C) (A : Matrix m n) : Matrix m n :=
fun x y => (r * A x y)%C.
Definition dot {n : nat} (A : Vector n) (B : Vector n) : C :=
big_sum (fun x => A x 0 * B x 0)%C n.
Definition Mplus {m n : nat} (A B : Matrix m n) : Matrix m n :=
fun x y => (A x y + B x y)%C.
Definition Mopp {m n : nat} (A : Matrix m n) : Matrix m n :=
scale (-C1) A.
Definition Mminus {m n : nat} (A B : Matrix m n) : Matrix m n :=
Mplus A (Mopp B).
Definition Mmult {m n o : nat} (A : Matrix m n) (B : Matrix n o) : Matrix m o :=
fun x z => big_sum (fun y => A x y * B y z)%C n.
(* Only well-defined when o and p are non-zero *)
Definition kron {m n o p : nat} (A : Matrix m n) (B : Matrix o p) :
Matrix (m*o) (n*p) :=
fun x y => Cmult (A (x / o) (y / p)) (B (x mod o) (y mod p)).
Definition direct_sum {m n o p : nat} (A : Matrix m n) (B : Matrix o p) :
Matrix (m+o) (n+p) :=
fun x y => if (x <? m) || (y <? n) then A x y else B (x - m) (y - n).
Definition transpose {m n} (A : Matrix m n) : Matrix n m :=
fun x y => A y x.
Definition adjoint {m n} (A : Matrix m n) : Matrix n m :=
fun x y => (A y x)^*.
(* note that the convention of quantum computing differs from abstract math,
hence this def *)
Definition inner_product {n} (u v : Vector n) : C :=
Mmult (adjoint u) (v) 0 0.
Definition outer_product {n} (u v : Vector n) : Square n :=
Mmult u (adjoint v).
(** Kronecker of n copies of A *)
Fixpoint kron_n n {m1 m2} (A : Matrix m1 m2) : Matrix (m1^n) (m2^n) :=
match n with
| 0 => I 1
| S n' => kron (kron_n n' A) A
end.
(** Kronecker product of a list *)
Fixpoint big_kron {m n} (As : list (Matrix m n)) :
Matrix (m^(length As)) (n^(length As)) :=
match As with
| [] => I 1
| A :: As' => kron A (big_kron As')
end.
(** Product of n copies of A *)
Fixpoint Mmult_n n {m} (A : Square m) : Square m :=
match n with
| 0 => I m
| S n' => Mmult A (Mmult_n n' A)
end.
(** Direct sum of n copies of A *)
Fixpoint direct_sum_n n {m1 m2} (A : Matrix m1 m2) : Matrix (n*m1) (n*m2) :=
match n with
| 0 => @Zero 0 0
| S n' => direct_sum A (direct_sum_n n' A)
end.
(** * Showing that M is a vector space *)
#[global] Program Instance M_is_monoid : forall n m, Monoid (Matrix n m) :=
{ Gzero := @Zero n m
; Gplus := Mplus
}.
Solve All Obligations with program_simpl; prep_matrix_equality; lca.
#[global] Program Instance M_is_group : forall n m, Group (Matrix n m) :=
{ Gopp := Mopp }.
Solve All Obligations with program_simpl; prep_matrix_equality; lca.
#[global] Program Instance M_is_comm_group : forall n m, Comm_Group (Matrix n m).
Solve All Obligations with program_simpl; prep_matrix_equality; lca.
#[global] Program Instance M_is_module_space : forall n m, Module_Space (Matrix n m) C :=
{ Vscale := scale }.
Solve All Obligations with program_simpl; prep_matrix_equality; lca.
#[global] Program Instance M_is_vector_space : forall n m, Vector_Space (Matrix n m) C.
(** Notations *)
Infix "∘" := dot (at level 40, left associativity) : matrix_scope.
Infix ".+" := Mplus (at level 50, left associativity) : matrix_scope.
Infix ".*" := scale (at level 40, left associativity) : matrix_scope.
Infix "×" := Mmult (at level 40, left associativity) : matrix_scope.
Infix "⊗" := kron (at level 40, left associativity) : matrix_scope.
Infix ".⊕" := direct_sum (at level 20) : matrix_scope. (* should have different level and assoc *)
Infix "≡" := mat_equiv (at level 70) : matrix_scope.
Notation "A ⊤" := (transpose A) (at level 0) : matrix_scope.
Notation "A †" := (adjoint A) (at level 0) : matrix_scope.
Notation Σ := (@big_sum C C_is_monoid). (* we intoduce Σ notation here *)
Notation "n ⨂ A" := (kron_n n A) (at level 30, no associativity) : matrix_scope.
Notation "⨂ A" := (big_kron A) (at level 60): matrix_scope.
Notation "n ⨉ A" := (Mmult_n n A) (at level 30, no associativity) : matrix_scope.
Notation "⟨ u , v ⟩" := (inner_product u v) (at level 0) : matrix_scope.
#[export] Hint Unfold Zero I e_i trace dot Mplus scale Mmult kron mat_equiv transpose
adjoint : 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 lma :=
autounfold with U_db;
prep_matrix_equality;
destruct_m_eq;
lca.
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 lma' :=
apply mat_equiv_eq;
repeat match goal with
| [ |- WF_Matrix (?A) ] => auto with wf_db (* (try show_wf) *)
| [ |- mat_equiv (?A) (?B) ] => by_cell; try lca
end.
(* lemmas which are useful for simplifying proofs involving matrix operations *)
Lemma kron_simplify : forall (n m o p : nat) (a b : Matrix n m) (c d : Matrix o p),
a = b -> c = d -> a ⊗ c = b ⊗ d.
Proof. intros; subst; easy.
Qed.
Lemma n_kron_simplify : forall (n m : nat) (a b : Matrix 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 : Matrix n m),
a = b -> a⊤ = b⊤.
Proof. intros; subst; easy.
Qed.
Lemma Madjoint_simplify : forall (n m : nat) (a b : Matrix n m),
a = b -> a† = b†.
Proof. intros; subst; easy.
Qed.
Lemma Mmult_simplify : forall (n m o : nat) (a b : Matrix n m) (c d : Matrix 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 : Matrix n m) (c d : Matrix n m),
a = b -> c = d -> a .+ c = b .+ d.
Proof. intros; subst; easy.
Qed.
Lemma Mscale_simplify : forall (n m: nat) (a b : Matrix n m) (c d : C),
a = b -> c = d -> c .* a = d .* b.
Proof. intros; subst; easy.
Qed.
(** * Proofs about well-formedness **)
Lemma WF_Matrix_dim_change : forall (m n m' n' : nat) (A : Matrix m n),
m = m' ->
n = n' ->
@WF_Matrix m n A ->
@WF_Matrix m' n' A.
Proof. intros. subst. easy. Qed.
Lemma WF_make_WF : forall {m n} (A : Matrix m n), WF_Matrix (make_WF A).
Proof. intros.
unfold WF_Matrix, make_WF; intros.
destruct H as [H | H].
bdestruct (x <? m); try lia; easy.
bdestruct (y <? n); bdestruct (x <? m); try lia; easy.
Qed.
Lemma WF_Zero : forall m n : nat, WF_Matrix (@Zero m n).
Proof. intros m n. unfold WF_Matrix. reflexivity. Qed.
Lemma WF_I : forall n : nat, WF_Matrix (I n).
Proof.
unfold WF_Matrix, I. intros n x y H. simpl.
destruct H; bdestruct (x =? y); bdestruct (x <? n); trivial; lia.
Qed.
Lemma WF_I1 : WF_Matrix (I 1). Proof. apply WF_I. 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.
Lemma WF_scale : forall {m n : nat} (r : C) (A : Matrix m n),
WF_Matrix A -> WF_Matrix (scale r A).
Proof.
unfold WF_Matrix, scale.
intros m n r A H x y H0. simpl.
rewrite H; trivial.
rewrite Cmult_0_r.
reflexivity.
Qed.
Lemma WF_plus : forall {m n} (A B : Matrix m n),
WF_Matrix A -> WF_Matrix B -> WF_Matrix (A .+ B).
Proof.
unfold WF_Matrix, Mplus.
intros m n A B H H0 x y H1. simpl.
rewrite H, H0; trivial.
rewrite Cplus_0_l.
reflexivity.
Qed.
Lemma WF_mult : forall {m n o : nat} (A : Matrix m n) (B : Matrix n o),
WF_Matrix A -> WF_Matrix B -> WF_Matrix (A × B).
Proof.
unfold WF_Matrix, Mmult.
intros m n o A B H H0 x y D.
apply (@big_sum_0 C C_is_monoid).
destruct D; intros z.
+ rewrite H; [lca | auto].
+ rewrite H0; [lca | auto].
Qed.
Lemma WF_kron : forall {m n o p q r : nat} (A : Matrix m n) (B : Matrix o p),
q = m * o -> r = n * p ->
WF_Matrix A -> WF_Matrix B -> @WF_Matrix q r (A ⊗ B).
Proof.
unfold WF_Matrix, kron.
intros m n o p q r A B Nn No H H0 x y H1. subst.
bdestruct (o =? 0). rewrite H0; [lca|lia].
bdestruct (p =? 0). rewrite H0; [lca|lia].
rewrite H.
rewrite Cmult_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 : Matrix m n) (B : Matrix o p),
q = m + o -> r = n + p ->
WF_Matrix A -> WF_Matrix B -> @WF_Matrix q r (A .⊕ B).
Proof.
unfold WF_Matrix, direct_sum.
intros; subst.
destruct H3; bdestruct_all; simpl; try apply H1; try apply H2.
all : lia.
Qed.
Lemma WF_transpose : forall {m n : nat} (A : Matrix m n),
WF_Matrix A -> WF_Matrix A⊤.
Proof. unfold WF_Matrix, transpose. intros m n A H x y H0. apply H.
destruct H0; auto. Qed.
Lemma WF_adjoint : forall {m n : nat} (A : Matrix m n),
WF_Matrix A -> WF_Matrix A†.
Proof. unfold WF_Matrix, 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_Matrix u ->
WF_Matrix v ->
WF_Matrix (outer_product u v).
Proof. intros. apply WF_mult; [|apply WF_adjoint]; assumption. Qed.
Lemma WF_kron_n : forall n {m1 m2} (A : Matrix m1 m2),
WF_Matrix A -> WF_Matrix (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 (Matrix m n)) (A : Matrix m n),
(forall i, WF_Matrix (nth i l A)) ->
WF_Matrix (⨂ 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 (Matrix m n)),
(forall A, In A l -> WF_Matrix A) ->
WF_Matrix (⨂ 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_Mmult_n : forall n {m} (A : Square m),
WF_Matrix A -> WF_Matrix (Mmult_n n A).
Proof.
intros.
induction n; simpl.
- apply WF_I.
- apply WF_mult; assumption.
Qed.
Lemma WF_direct_sum_n : forall n {m1 m2} (A : Matrix m1 m2),
WF_Matrix A -> WF_Matrix (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 -> Matrix d1 d2),
(forall i, (i < n)%nat -> WF_Matrix (f i)) ->
WF_Matrix (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 R.
Local Open Scope C.
(* Much less awful *)
Ltac show_wf :=
unfold WF_Matrix;
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 lca.
(* Create HintDb wf_db. *)
#[export] Hint Resolve WF_Zero WF_I WF_I1 WF_e_i WF_mult WF_plus WF_scale WF_transpose
WF_adjoint WF_outer_product WF_big_kron WF_kron_n WF_kron
WF_Mmult_n WF_make_WF WF_Msum : wf_db.
#[export] 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_Matrix _ => 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_Matrix A)
(* recursive case *)
| ?op ?A ?B => collate_wf' A;
collate_wf' B;
assert (WF_Matrix (op A B)) by auto with wf_db
(* base case *)
| ?A => assert (WF_Matrix 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_Matrix ?A => collate_wf' A
| |- context[?A] => collate_wf' A
end.
Ltac solve_wf := collate_wf; easy.
(** * Basic matrix lemmas *)
Lemma mat_equiv_make_WF : forall {m n} (T : Matrix m n),
T == make_WF T.
Proof. unfold make_WF, mat_equiv; intros.
bdestruct (i <? m); bdestruct (j <? n); try lia; easy.
Qed.
Lemma eq_make_WF : forall {n m} (T : Matrix m n),
WF_Matrix T -> T = make_WF T.
Proof. intros.
apply mat_equiv_eq; auto with wf_db.
apply mat_equiv_make_WF.
Qed.
Lemma Mplus_make_WF : forall {n m} (A B : Matrix m n),
make_WF A .+ make_WF B = make_WF (A .+ B).
Proof. intros.
apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold make_WF, Mplus.
bdestruct (i <? m); bdestruct (j <? n); try lia; simpl.
easy.
Qed.
Lemma Mmult_make_WF : forall {m n o} (A : Matrix m n) (B : Matrix n o),
make_WF A × make_WF B = make_WF (A × B).
Proof. intros.
apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
unfold make_WF, Mmult.
bdestruct (i <? m); bdestruct (j <? o); try lia; simpl.
apply big_sum_eq_bounded; intros.
bdestruct (x <? n); try lia; easy.
Qed.
Lemma WF0_Zero_l :forall (n : nat) (A : Matrix 0%nat n), WF_Matrix A -> A = Zero.
Proof.
intros n A WFA.
prep_matrix_equality.
rewrite WFA.
reflexivity.
lia.
Qed.
Lemma WF0_Zero_r :forall (n : nat) (A : Matrix n 0%nat), WF_Matrix A -> A = Zero.
Proof.
intros n A WFA.
prep_matrix_equality.
rewrite WFA.
reflexivity.
lia.
Qed.
Lemma WF0_Zero :forall (A : Matrix 0%nat 0%nat), WF_Matrix 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)%C.
Proof.
intros.
unfold trace, Mplus.
rewrite (@big_sum_plus C _ _ C_is_comm_group).
easy.
Qed.
Lemma trace_mult_dist : forall n p (A : Square n), trace (p .* A) = (p * trace A)%C.
Proof.
intros.
unfold trace, scale.
rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
easy.
Qed.
Lemma Mplus_0_l : forall (m n : nat) (A : Matrix m n), Zero .+ A = A.
Proof. intros. lma. Qed.
Lemma Mplus_0_r : forall (m n : nat) (A : Matrix m n), A .+ Zero = A.
Proof. intros. lma. Qed.
Lemma Mmult_0_l : forall (m n o : nat) (A : Matrix n o), @Zero m n × A = Zero.
Proof.
intros m n o A.
unfold Mmult, Zero.
prep_matrix_equality.
apply (@big_sum_0 C C_is_monoid).
intros.
lca.
Qed.
Lemma Mmult_0_r : forall (m n o : nat) (A : Matrix m n), A × @Zero n o = Zero.
Proof.
intros m n o A.
unfold Zero, Mmult.
prep_matrix_equality.
apply (@big_sum_0 C C_is_monoid).
intros.
lca.
Qed.
(* using <= because our form big_sum is exclusive. *)
Lemma Mmult_1_l_gen: forall (m n : nat) (A : Matrix 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.
autorewrite with C_db.
apply IHl.
lia.
+ intros gtSkx.
simpl in *.
unfold I in *.
bdestruct (x =? k); bdestruct (x <? m); subst; try lia.
rewrite IHl by lia; simpl; lca.
rewrite IHr by lia; simpl; lca.
Qed.
Lemma Mmult_1_l_mat_eq : forall (m n : nat) (A : Matrix m n), I m × A == A.
Proof.
intros m n A i j Hi Hj.
unfold Mmult.
edestruct (@Mmult_1_l_gen m n) as [Hl Hr].
apply Nat.le_refl.
unfold get.
apply Hr.
simpl in *.
lia.
Qed.
Lemma Mmult_1_l: forall (m n : nat) (A : Matrix m n),
WF_Matrix A -> I m × A = A.
Proof.
intros m n A H.
apply mat_equiv_eq; trivial.
auto with wf_db.
apply Mmult_1_l_mat_eq.
Qed.
Lemma Mmult_1_r_gen: forall (m n : nat) (A : Matrix 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.
autorewrite with C_db.
apply IHl; lia.
+ intros gtSkz.
simpl in *.
unfold I in *.
bdestruct (k =? z); subst.
- bdestruct (z <? n); try lia.
rewrite IHl by lia; lca.
- rewrite IHr by lia; simpl; lca.
Qed.
Lemma Mmult_1_r_mat_eq : forall (m n : nat) (A : Matrix m n), A × I n ≡ A.
Proof.
intros m n A i j Hi Hj.
unfold Mmult.
edestruct (@Mmult_1_r_gen m n) as [Hl Hr].
apply Nat.le_refl.
unfold get; simpl.
apply Hr.
lia.
Qed.
Lemma Mmult_1_r: forall (m n : nat) (A : Matrix m n),
WF_Matrix A -> A × I n = A.
Proof.
intros m n A H.
apply mat_equiv_eq; trivial.
auto with wf_db.
apply Mmult_1_r_mat_eq.
Qed.
(* Cool facts about I∞, not used in the development *)
Lemma Mmult_inf_l : forall(m n : nat) (A : Matrix m n),
WF_Matrix A -> I∞ × A = A.
Proof.
intros m n A H.
prep_matrix_equality.
unfold Mmult.
edestruct (@Mmult_1_l_gen m n) as [Hl Hr].
apply Nat.le_refl.
bdestruct (m <=? x).
rewrite H by auto.
apply (@big_sum_0_bounded C C_is_monoid).
intros z L.
unfold I__inf, I.
bdestruct (x =? z). lia. lca.
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 Mmult_inf_r : forall(m n : nat) (A : Matrix m n),
WF_Matrix A -> A × I∞ = A.
Proof.
intros m n A H.
prep_matrix_equality.
unfold Mmult.
edestruct (@Mmult_1_r_gen m n) as [Hl Hr].
apply Nat.le_refl.
bdestruct (n <=? y).
rewrite H by auto.
apply (@big_sum_0_bounded C C_is_monoid).
intros z L.
unfold I__inf, I.
bdestruct (z =? y). lia. lca.
unfold I__inf, I in *.
erewrite big_sum_eq.
apply Hr.
assumption.
apply functional_extensionality. intros z.
bdestruct (z =? y); bdestruct (z <? n); simpl; try lca; try lia.
Qed.
Lemma kron_0_l : forall (m n o p : nat) (A : Matrix o p),
@Zero m n ⊗ A = Zero.
Proof.
intros m n o p A.
prep_matrix_equality.
unfold Zero, kron.
rewrite Cmult_0_l.
reflexivity.
Qed.
Lemma kron_0_r : forall (m n o p : nat) (A : Matrix m n),
A ⊗ @Zero o p = Zero.
Proof.
intros m n o p A.
prep_matrix_equality.
unfold Zero, kron.
rewrite Cmult_0_r.
reflexivity.
Qed.
Lemma kron_1_r : forall (m n : nat) (A : Matrix m n), A ⊗ I 1 = A.
Proof.
intros m n A.
prep_matrix_equality.
unfold I, kron.
rewrite 2 Nat.div_1_r.
rewrite 2 Nat.mod_1_r.
simpl.
autorewrite with C_db.
reflexivity.
Qed.
(* This side is more limited *)
Lemma kron_1_l : forall (m n : nat) (A : Matrix m n),
WF_Matrix A -> I 1 ⊗ A = A.
Proof.
intros m n A WF.
prep_matrix_equality.
unfold kron.
unfold I, kron.
bdestruct (m =? 0). rewrite 2 WF by lia. lca.
bdestruct (n =? 0). rewrite 2 WF by lia. lca.
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.
lca.
+ 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 Cmult_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 Cmult_0_l.
destruct WF with (x := x) (y := y). lia.
reflexivity.
Qed.
Theorem transpose_involutive : forall (m n : nat) (A : Matrix m n), (A⊤)⊤ = A.
Proof. reflexivity. Qed.
Theorem adjoint_involutive : forall (m n : nat) (A : Matrix 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_matrix_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_matrix_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 Mplus_comm : forall (m n : nat) (A B : Matrix m n), A .+ B = B .+ A.
Proof.
unfold Mplus.
intros m n A B.
prep_matrix_equality.
apply Cplus_comm.
Qed.
Theorem Mplus_assoc : forall (m n : nat) (A B C : Matrix m n), A .+ B .+ C = A .+ (B .+ C).
Proof.
unfold Mplus.
intros m n A B C.
prep_matrix_equality.
rewrite Cplus_assoc.
reflexivity.
Qed.
Theorem Mmult_assoc : forall {m n o p : nat} (A : Matrix m n) (B : Matrix n o)
(C: Matrix o p), A × B × C = A × (B × C).
Proof.
intros m n o p A B C.
unfold Mmult.
prep_matrix_equality.
induction n.