-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGenMatrix.v
More file actions
3157 lines (2724 loc) · 90.5 KB
/
GenMatrix.v
File metadata and controls
3157 lines (2724 loc) · 90.5 KB
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.
Require Import Setoid.
Require Import Modulus.
(* 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.
Module Type FieldModule.
Parameter (F : Type)
(R0 : Monoid F) (R1 : Group F)
(R2 : Comm_Group F) (R3 : Ring F)
(R4 : Comm_Ring F) (R5 : Field F).
Definition F_field_theory : field_theory 0%G 1%G Gplus Gmult Gminus Gopp Gdiv Ginv eq := @G_field_theory F R0 R1 R2 R3 R4 R5.
Add Field F_field : F_field_theory.
#[export] Existing Instance R0.
#[export] Existing Instance R1.
#[export] Existing Instance R2.
#[export] Existing Instance R3.
#[export] Existing Instance R4.
#[export] Existing Instance R5.
End FieldModule.
Module LinAlgOverField
(FM : FieldModule).
Include FM.
Ltac Fsimpl :=
repeat match goal with
| _ => rewrite Gmult_0_l
| _ => rewrite Gmult_0_r
| _ => rewrite Gplus_0_l
| _ => rewrite Gplus_0_r
| _ => rewrite Gmult_1_l
| _ => rewrite Gmult_1_r
end.
Ltac Fsimpl_in H :=
repeat
match goal with
| _ => rewrite Gmult_0_l in H
| _ => rewrite Gmult_0_r in H
| _ => rewrite Gplus_0_l in H
| _ => rewrite Gplus_0_r in H
| _ => rewrite Gmult_1_l in H
| _ => rewrite Gmult_1_r in H
end.
Lemma nonzero_div_nonzero : forall c : F, c <> 0%G -> / c <> 0%G.
Proof. intros.
unfold not; intros.
assert (H' : (c * (/ c) = c * 0%G)%G).
{ rewrite H0; easy. }
rewrite Ginv_r in H'; try easy.
rewrite Gmult_0_r in H'.
apply G1_neq_0; easy.
Qed.
(* 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 times_n_F : forall n (f : F),
times_n f n = f * (times_n 1%G n).
Proof.
intros n f.
induction n; simpl; [dumb_lRa|].
rewrite IHn. dumb_lRa.
Qed.
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.
Definition make_WF {n m} (S : GenMatrix n m) : GenMatrix n m :=
fun i j => if (i <? n) && (j <? m) then S i j else 0%G.
Notation GenVector n := (GenMatrix n 1).
Notation GenSquare 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.
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.
(** Equality via bounded equality for WF matrices **)
Ltac prep_genmatrix_equivalence :=
apply genmat_equiv_eq;
[solve [auto 100 with wf_db |
auto 100 using WF_GenMatrix_dim_change with wf_db zarith]..|].
(** 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.
Lemma show_WF_list2D_to_matrix m n li :
length li = m ->
forallb (fun x => length x =? n) li = true ->
@WF_GenMatrix m n (list2D_to_genmatrix li).
Proof.
intros Hlen.
rewrite forallb_forall.
intros Hin.
setoid_rewrite Nat.eqb_eq in Hin.
apply WF_list2D_to_genmatrix.
easy.
intros l Hl.
rewrite Hin by easy.
easy.
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) : GenSquare n :=
(fun x y => if (x =? y) && (x <? n) then 1 else 0).
Definition const_genmatrix {m n} (f : F) : GenMatrix m n :=
make_WF (fun _ _ => f).
(* 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) : GenVector n :=
fun x y => (if (x =? i) && (x <? n) && (y =? 0) 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) : F := A 0 0.
Coercion to_scalar : GenMatrix >-> F.
*)
(* 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 : GenSquare 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 : GenVector n) (B : GenVector 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 : GenVector n) : F :=
GMmult (transpose u) (v) 0 0.
Definition outer_product {n} (u v : GenVector n) : GenSquare 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 *)
Fixpoint GMmult_n n {m} (A : GenSquare m) : GenSquare m :=
match n with
| 0 => I m
| S n' => GMmult A (GMmult_n n' A)
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 "n ⨉ A" := (GMmult_n n A) (at level 30, no associativity) : genmatrix_scope.
Notation "⟨ u , v ⟩" := (inner_product u v) (at level 0) : genmatrix_scope.
#[export] Hint Unfold Zero I e_i trace dot GMplus GMopp scale GMmult Gkron genmat_equiv transpose const_genmatrix make_WF : U_db.
(** * Showing that M is a vector space *)
#[global] 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.
#[global] 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; ring.
#[global] Program Instance GM_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.
#[global] Program Instance GM_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.
#[global] Program Instance GM_is_vector_space : forall n m, Vector_Space (GenMatrix n m) F.
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_no_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 by_cell :=
intros;
by_cell_no_intros.
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.
(* 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 : GenSquare 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: GenVector 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 mat_equiv *)
Lemma genmat_equiv_sym : forall {n m : nat} (A B : GenMatrix n m),
A ≡ B -> B ≡ A.
Proof.
intros n m A B HAB i j Hi Hj.
rewrite HAB by easy.
easy.
Qed.
Lemma genmat_equiv_trans : forall {n m : nat} (A B C : GenMatrix n m),
A ≡ B -> B ≡ C -> A ≡ C.
Proof.
intros n m A B C HAB HBC i j Hi Hj.
rewrite HAB, HBC by easy.
easy.
Qed.
#[global] Add Parametric Relation {n m} : (GenMatrix n m) genmat_equiv
reflexivity proved by (genmat_equiv_refl _ _)
symmetry proved by (genmat_equiv_sym)
transitivity proved by (genmat_equiv_trans)
as genmat_equiv_rel.
Lemma genmat_equiv_eq_iff {n m} : forall (A B : GenMatrix n m),
WF_GenMatrix A -> WF_GenMatrix B -> A ≡ B <-> A = B.
Proof.
intros; split; try apply genmat_equiv_eq;
intros; try subst A; easy.
Qed.
Lemma Mmult_simplify_genmat_equiv : forall {n m o}
(A B : GenMatrix n m) (C D : GenMatrix m o),
A ≡ B -> C ≡ D -> A × C ≡ B × D.
Proof.
intros n m o A B C D HAB HCD.
intros i j Hi Hj.
unfold GMmult.
apply big_sum_eq_bounded.
intros k Hk.
rewrite HAB, HCD by easy.
easy.
Qed.
Add Parametric Morphism {n m o} : (@GMmult n m o)
with signature (@genmat_equiv n m) ==> (@genmat_equiv m o) ==> (@genmat_equiv n o)
as mmult_genmat_equiv_morph.
Proof. intros; apply Mmult_simplify_genmat_equiv; easy. Qed.
Lemma kron_simplify_genmat_equiv {n m o p} : forall (A B : GenMatrix n m)
(C D : GenMatrix o p), A ≡ B -> C ≡ D -> A ⊗ C ≡ B ⊗ D.
Proof.
intros A B C D HAB HCD i j Hi Hj.
unfold Gkron.
rewrite HAB, HCD; try easy.
1,2: apply Nat.mod_upper_bound; lia.
1,2: apply Nat.Div0.div_lt_upper_bound; lia.
Qed.
Add Parametric Morphism {n m o p} : (@Gkron n m o p)
with signature (@genmat_equiv n m) ==> (@genmat_equiv o p)
==> (@genmat_equiv (n*o) (m*p)) as kron_genmat_equiv_morph.
Proof. intros; apply kron_simplify_genmat_equiv; easy. Qed.
Lemma Mplus_simplify_genmat_equiv : forall {n m}
(A B C D : GenMatrix n m),
A ≡ B -> C ≡ D -> A .+ C ≡ B .+ D.
Proof.
intros n m A B C D HAB HCD.
intros i j Hi Hj; unfold ".+";
rewrite HAB, HCD; try easy.
Qed.
Add Parametric Morphism {n m} : (@GMplus n m)
with signature (@genmat_equiv n m) ==> (@genmat_equiv n m) ==> (@genmat_equiv n m)
as Mplus_genmat_equiv_morph.
Proof. intros; apply Mplus_simplify_genmat_equiv; easy. Qed.
Lemma scale_simplify_genmat_equiv : forall {n m}
(x y : F) (A B : GenMatrix n m),
x = y -> A ≡ B -> x .* A ≡ y .* B.
Proof.
intros n m x y A B Hxy HAB i j Hi Hj.
unfold scale.
rewrite Hxy, HAB; easy.
Qed.
Add Parametric Morphism {n m} : (@scale n m)
with signature (@eq F) ==> (@genmat_equiv n m) ==> (@genmat_equiv n m)
as scale_genmat_equiv_morph.
Proof. intros; apply scale_simplify_genmat_equiv; easy. Qed.
Lemma GMopp_simplify_genmat_equiv : forall {n m} (A B : GenMatrix n m),
A ≡ B -> GMopp A ≡ GMopp B.
Proof.
intros n m A B HAB i j Hi Hj.
unfold GMopp, scale.
rewrite HAB; easy.
Qed.
Add Parametric Morphism {n m} : (@GMopp n m)
with signature (@genmat_equiv n m) ==> (@genmat_equiv n m)
as GMopp_genmat_equiv_morph.
Proof. intros; apply GMopp_simplify_genmat_equiv; easy. Qed.
Lemma GMminus_simplify_genmat_equiv : forall {n m}
(A B C D : GenMatrix n m),
A ≡ B -> C ≡ D -> GMminus A C ≡ GMminus B D.
Proof.
intros n m A B C D HAB HCD.
intros i j Hi Hj; unfold GMminus, GMopp, GMplus, scale;
rewrite HAB, HCD; try easy.
Qed.
Add Parametric Morphism {n m} : (@GMminus n m)
with signature (@genmat_equiv n m) ==> (@genmat_equiv n m) ==> (@genmat_equiv n m)
as GMminus_genmat_equiv_morph.
Proof. intros; apply GMminus_simplify_genmat_equiv; easy. Qed.
Lemma dot_simplify_genmat_equiv : forall {n} (A B : GenVector n)
(C D : GenVector n), A ≡ B -> C ≡ D -> dot A C = dot B D.
Proof.
intros n A B C D HAB HCD.
apply big_sum_eq_bounded.
intros k Hk.
rewrite HAB, HCD; unfold "<"%nat; easy.
Qed.
Add Parametric Morphism {n} : (@dot n)
with signature (@genmat_equiv n 1) ==> (@genmat_equiv n 1) ==> (@eq F)
as dot_genmat_equiv_morph.
Proof. intros; apply dot_simplify_genmat_equiv; easy. Qed.
Lemma transpose_simplify_genmat_equiv {n m} : forall (A B : GenMatrix n m),
A ≡ B -> A ⊤ ≡ B ⊤.
Proof.
intros A B HAB i j Hi Hj.
unfold transpose; auto.
Qed.
Lemma transpose_simplify_genmat_equiv_inv {n m} : forall (A B : GenMatrix n m),
A ⊤ ≡ B ⊤ -> A ≡ B.
Proof.
intros A B HAB i j Hi Hj.
unfold transpose in *; auto.
Qed.
Add Parametric Morphism {n m} : (@transpose n m)
with signature (@genmat_equiv n m) ==> (@genmat_equiv m n)
as transpose_genmat_equiv_morph.
Proof. intros; apply transpose_simplify_genmat_equiv; easy. Qed.
(* Adjoints do not exists for general fields F
Lemma adjoint_simplify_genmat_equiv {n m} : forall (A B : GenMatrix n m),
A ≡ B -> A † ≡ B †.
Proof.
intros A B HAB i j Hi Hj.
unfold adjoint;
rewrite HAB by easy; easy.
Qed.
Add Parametric Morphism {n m} : (@adjoint n m)
with signature (@genmat_equiv n m) ==> (@genmat_equiv m n)
as adjoint_genmat_equiv_morph.
Proof. intros; apply adjoint_simplify_genmat_equiv; easy. Qed. *)
Lemma trace_of_genmat_equiv : forall n (A B : GenSquare n),
A ≡ B -> trace A = trace B.
Proof.
intros n A B HAB.
(* unfold trace. *)
apply big_sum_eq_bounded; intros i Hi.
rewrite HAB; auto.
Qed.
Add Parametric Morphism {n} : (@trace n)
with signature (@genmat_equiv n n) ==> (eq)
as trace_genmat_equiv_morph.
Proof. intros; apply trace_of_genmat_equiv; easy. Qed.
Lemma genmat_equiv_equivalence : forall {n m},
equivalence (GenMatrix n m) genmat_equiv.
Proof.
intros n m.
constructor.
- intros A. apply (genmat_equiv_refl).
- intros A; apply genmat_equiv_trans.
- intros A; apply genmat_equiv_sym.
Qed.
Lemma big_sum_genmat_equiv : forall {o p} (f g : nat -> GenMatrix o p)
(Eq_on: forall x : nat, f x ≡ g x) (n : nat), big_sum f n ≡ big_sum g n.
Proof.
intros o p f g Eq_on n.
induction n.
- easy.
- simpl.
rewrite IHn, Eq_on; easy.
Qed.
Add Parametric Morphism {n m} : (@big_sum (GenMatrix n m) (GM_is_monoid n m))
with signature
(Morphisms.pointwise_relation nat (@genmat_equiv n m)) ==> (@eq nat) ==>
(@genmat_equiv n m)
as big_sum_genmat_equiv_morph.
Proof. intros f g Eq_on k. apply big_sum_genmat_equiv; easy. Qed.
(** * Proofs about well-formedness **)
Lemma WF_GenMatrix_dim_change_iff m n m' n' (A : GenMatrix m n) :
m = m' -> n = n' ->
@WF_GenMatrix m' n' A <-> WF_GenMatrix A.
Proof.
intros.
now subst.
Qed.
Lemma WF_make_WF : forall {m n} (A : GenMatrix m n), WF_GenMatrix (make_WF A).
Proof. intros.
unfold WF_GenMatrix, 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_const_genmatrix m n c :
WF_GenMatrix (@const_genmatrix m n c).
Proof.
apply WF_make_WF.
Qed.
Lemma WF_Zero : forall m n : nat, WF_GenMatrix (@Zero m n).
Proof. intros m n. unfold WF_GenMatrix. reflexivity. Qed.
Lemma WF_Zero_alt : forall m n o p : nat,
@WF_GenMatrix m n (@Zero o p).
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_e_i : forall {n : nat} (i : nat),
WF_GenMatrix (@e_i n i).
Proof. unfold WF_GenMatrix, 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 : 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; [ring | auto].
+ rewrite H0; [ring | 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; [ring|lia].
bdestruct (p =? 0). rewrite H0; [ring|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 : 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 : GenVector 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 : GenSquare m),
WF_GenMatrix A -> WF_GenMatrix (GMmult_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 : 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. *)
#[export] Hint Resolve WF_Zero WF_Zero_alt WF_const_genmatrix 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_GMmult_n WF_make_WF WF_Msum
WF_direct_sum WF_direct_sum_n : 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_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 make_WF_equiv n m (A : GenMatrix n m) :
make_WF A ≡ A.
Proof.
unfold make_WF.
intros i j Hi Hj.
bdestruct_all; auto.
Qed.
Lemma genmat_equiv_make_WF : forall {m n} (T : GenMatrix m n),
T == make_WF T.
Proof. unfold make_WF, genmat_equiv; intros.
bdestruct (i <? m); bdestruct (j <? n); try lia; easy.
Qed.
Lemma eq_make_WF : forall {n m} (T : GenMatrix m n),
WF_GenMatrix T -> T = make_WF T.
Proof. intros.
apply genmat_equiv_eq; auto with wf_db.
apply genmat_equiv_make_WF.
Qed.
Lemma Mplus_make_WF : forall {n m} (A B : GenMatrix m n),
make_WF A .+ make_WF B = make_WF (A .+ B).
Proof. intros.
apply genmat_equiv_eq; auto with wf_db.
unfold genmat_equiv; intros.
unfold make_WF, GMplus.
bdestruct (i <? m); bdestruct (j <? n); try lia; simpl.
easy.
Qed.
Lemma Mmult_make_WF : forall {m n o} (A : GenMatrix m n) (B : GenMatrix n o),
make_WF A × make_WF B = make_WF (A × B).