-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.v
5772 lines (5091 loc) · 186 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
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 *)
Local Open Scope nat_scope.
(* Some prelim lemmas. Should probably be moved *)
Lemma easy_sub : forall (n : nat), S n - 1 = n. Proof. lia. Qed.
Lemma Csum_simplify : forall (a b c d : C), a = b -> c = d -> (a + c = b + d)%C.
Proof. intros.
rewrite H, H0; easy.
Qed.
Lemma Cmult_simplify : forall (a b c d : C), a = b -> c = d -> (a * c = b * d)%C.
Proof. intros.
rewrite H, H0; easy.
Qed.
Lemma sqrt_1_unique : forall x, √ x = 1%R -> x = 1%R.
Proof. intros. assert (H' := H). unfold sqrt in H. destruct (Rcase_abs x).
- assert (H0: 1%R <> 0%R). { apply R1_neq_R0. }
rewrite H in H0. easy.
- rewrite <- (sqrt_def x). rewrite H'. lra.
apply Rge_le. easy.
Qed.
(*******************************************)
(** Matrix Definitions and Infrastructure **)
(*******************************************)
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 Vector (n : nat) := Matrix n 1. *)
Definition WF_Matrix {m n: nat} (A : Matrix m n) : Prop :=
forall x y, x >= m \/ y >= n -> A x y = C0.
Notation Vector n := (Matrix n 1).
Notation Square n := (Matrix n n).
(* Showing 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).
(* 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.
*)
(*
Definition I (n : nat) : Square n :=
(fun x y => if (x =? y) && (x <? n) then C1 else C0).
Definition I1 := I (2^0).
Notation "I n" := (I n) (at level 10).
*)
(* 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.
(* sum to n exclusive *)
Fixpoint Csum (f : nat -> C) (n : nat) : C :=
match n with
| 0 => C0
| S n' => (Csum f n' + f n')%C
end.
Definition trace {n : nat} (A : Square n) :=
Csum (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 :=
Csum (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 Mmult {m n o : nat} (A : Matrix m n) (B : Matrix n o) : Matrix m o :=
fun x z => Csum (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 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)^*.
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.
(* Indexed sum over matrices *)
Fixpoint Msum {m1 m2} n (f : nat -> Matrix m1 m2) : Matrix m1 m2 :=
match n with
| 0 => Zero
| S n' => Mplus (Msum n' f) (f n')
end.
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 "≡" := 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 "Σ^ n f" := (Csum f n) (at level 60) : matrix_scope.
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.
Hint Unfold Zero 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 lt_S_n in Hi]; try solve_end); clear Hi;
repeat (destruct j as [|j]; simpl; [|apply lt_S_n 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.
(******************************)
(** Proofs about finite sums **)
(******************************)
Local Close Scope nat_scope.
Lemma Csum_0 : forall f n, (forall x, f x = C0) -> Csum f n = 0.
Proof.
intros.
induction n.
- reflexivity.
- simpl.
rewrite IHn, H.
lca.
Qed.
Lemma Csum_1 : forall f n, (forall x, f x = C1) -> Csum f n = INR n.
Proof.
intros.
induction n.
- reflexivity.
- simpl.
rewrite IHn, H.
destruct n; lca.
Qed.
Lemma Csum_constant : forall c n, Csum (fun x => c) n = INR n * c.
Proof.
intros c n.
induction n.
+ simpl; lca.
+ simpl.
rewrite IHn.
destruct n; lca.
Qed.
Lemma Csum_eq : forall f g n, f = g -> Csum f n = Csum g n.
Proof. intros f g n H. subst. reflexivity. Qed.
Lemma Csum_0_bounded : forall f n, (forall x, (x < n)%nat -> f x = C0) -> Csum f n = 0.
Proof.
intros.
induction n.
- reflexivity.
- simpl.
rewrite IHn, H.
lca.
lia.
intros.
apply H.
lia.
Qed.
Lemma Csum_eq_bounded : forall f g n, (forall x, (x < n)%nat -> f x = g x) -> Csum f n = Csum g n.
Proof.
intros f g n H.
induction n.
+ simpl. reflexivity.
+ simpl.
rewrite H by lia.
rewrite IHn by (intros; apply H; lia).
reflexivity.
Qed.
Lemma Csum_plus : forall f g n, Csum (fun x => f x + g x) n = Csum f n + Csum g n.
Proof.
intros f g n.
induction n.
+ simpl. lca.
+ simpl. rewrite IHn. lca.
Qed.
Lemma Csum_mult_l : forall c f n, c * Csum f n = Csum (fun x => c * f x) n.
Proof.
intros c f n.
induction n.
+ simpl; lca.
+ simpl.
rewrite Cmult_plus_distr_l.
rewrite IHn.
reflexivity.
Qed.
Lemma Csum_mult_r : forall c f n, Csum f n * c = Csum (fun x => f x * c) n.
Proof.
intros c f n.
induction n.
+ simpl; lca.
+ simpl.
rewrite Cmult_plus_distr_r.
rewrite IHn.
reflexivity.
Qed.
Lemma Csum_conj_distr : forall f n, (Csum f n) ^* = Csum (fun x => (f x)^*) n.
Proof.
intros f n.
induction n.
+ simpl; lca.
+ simpl.
rewrite Cconj_plus_distr.
rewrite IHn.
reflexivity.
Qed.
Lemma Csum_extend_r : forall n f, Csum f n + f n = Csum f (S n).
Proof. reflexivity. Qed.
Lemma Csum_extend_l : forall n f, f O + Csum (fun x => f (S x)) n = Csum f (S n).
Proof.
intros n f.
induction n.
+ simpl; lca.
+ simpl.
rewrite Cplus_assoc.
rewrite IHn.
simpl.
reflexivity.
Qed.
Lemma Csum_unique : forall k (f : nat -> C) n,
(exists x, (x < n)%nat /\ f x = k /\ (forall x', x <> x' -> f x' = 0)) ->
Csum f n = k.
Proof.
intros k f n [x [L [Eq Unique]]].
induction n; try lia.
Search Csum.
rewrite <- Csum_extend_r.
destruct (Nat.eq_dec x n).
- subst.
rewrite Csum_0_bounded.
lca.
intros.
apply Unique.
lia.
- rewrite Unique by easy.
Csimpl.
apply IHn.
lia.
Qed.
Lemma Csum_sum : forall m n f, Csum f (m + n) =
Csum f m + Csum (fun x => f (m + x)%nat) n.
Proof.
intros m n f.
induction m.
+ simpl. rewrite Cplus_0_l. reflexivity.
+ simpl.
rewrite IHm.
repeat rewrite <- Cplus_assoc.
remember (fun y => f (m + y)%nat) as g.
replace (f m) with (g O) by (subst; rewrite plus_0_r; reflexivity).
replace (f (m + n)%nat) with (g n) by (subst; reflexivity).
replace (Csum (fun x : nat => f (S (m + x))) n) with
(Csum (fun x : nat => g (S x)) n).
2:{ apply Csum_eq. subst. apply functional_extensionality.
intros; rewrite <- plus_n_Sm. reflexivity. }
rewrite Csum_extend_l.
rewrite Csum_extend_r.
reflexivity.
Qed.
Lemma Csum_product : forall m n f g, n <> O ->
Csum f m * Csum g n =
Csum (fun x => f (x / n)%nat * g (x mod n)%nat) (m * n).
Proof.
intros.
induction m.
+ simpl; lca.
+ simpl.
rewrite Cmult_plus_distr_r.
rewrite IHm. clear IHm.
rewrite Csum_mult_l.
remember ((fun x : nat => f (x / n)%nat * g (x mod n)%nat)) as h.
replace (Csum (fun x : nat => f m * g x) n) with
(Csum (fun x : nat => h ((m * n) + x)%nat) n).
2:{
subst.
apply Csum_eq_bounded.
intros x Hx.
rewrite Nat.div_add_l by assumption.
rewrite Nat.div_small; trivial.
rewrite plus_0_r.
rewrite Nat.add_mod by assumption.
rewrite Nat.mod_mul by assumption.
rewrite plus_0_l.
repeat rewrite Nat.mod_small; trivial. }
rewrite <- Csum_sum.
rewrite plus_comm.
reflexivity.
Qed.
Lemma Csum_ge_0 : forall f n, (forall x, 0 <= fst (f x)) -> 0 <= fst (Csum f n).
Proof.
intros f n H.
induction n.
- simpl. lra.
- simpl in *.
rewrite <- Rplus_0_r at 1.
apply Rplus_le_compat; easy.
Qed.
Lemma Csum_gt_0 : forall f n, (forall x, 0 <= fst (f x)) ->
(exists y : nat, (y < n)%nat /\ 0 < fst (f y)) ->
0 < fst (Csum f n).
Proof.
intros f n H [y [H0 H1]].
induction n.
- simpl. lia.
- simpl in *.
bdestruct (y <? n)%nat; bdestruct (y =? n)%nat; try lia.
+ assert (H' : 0 <= fst (f n)). { apply H. }
apply IHn in H2. lra.
+ apply (Csum_ge_0 f n) in H.
rewrite H3 in H1.
lra.
Qed.
Lemma Csum_member_le : forall (f : nat -> C) (n : nat), (forall x, 0 <= fst (f x)) ->
(forall x, (x < n)%nat -> fst (f x) <= fst (Csum f n)).
Proof.
intros f.
induction n.
- intros H x Lt. inversion Lt.
- intros H x Lt.
bdestruct (Nat.ltb x n).
+ simpl.
rewrite <- Rplus_0_r at 1.
apply Rplus_le_compat.
apply IHn; easy.
apply H.
+ assert (E: x = n) by lia.
rewrite E.
simpl.
rewrite <- Rplus_0_l at 1.
apply Rplus_le_compat.
apply Csum_ge_0; easy.
lra.
Qed.
Lemma Csum_squeeze : forall (f : nat -> C) (n : nat),
(forall x, (0 <= fst (f x)))%R -> Csum f n = C0 ->
(forall x, (x < n)%nat -> fst (f x) = fst C0).
Proof. intros.
assert (H2 : (forall x, (x < n)%nat -> (fst (f x) <= 0)%R)).
{ intros.
replace 0%R with (fst (C0)) by easy.
rewrite <- H0.
apply Csum_member_le; try easy. }
assert (H3 : forall r : R, (r <= 0 -> 0 <= r -> r = 0)%R).
intros. lra.
simpl.
apply H3.
apply H2; easy.
apply H.
Qed.
Lemma Csum_snd_0 : forall n f, (forall x, snd (f x) = 0) -> snd (Csum f n) = 0.
Proof. intros. induction n.
- reflexivity.
- rewrite <- Csum_extend_r.
unfold Cplus. simpl. rewrite H, IHn.
lra.
Qed.
Lemma Csum_comm : forall f g n,
(forall c1 c2 : C, g (c1 + c2) = g c1 + g c2) ->
Csum (fun x => g (f x)) n = g (Csum f n).
Proof. intros. induction n as [| n'].
- simpl.
assert (H0 : g 0 - g 0 = g 0 + g 0 - g 0).
{ rewrite <- H. rewrite Cplus_0_r. easy. }
unfold Cminus in H0.
rewrite <- Cplus_assoc in H0.
rewrite Cplus_opp_r in H0.
rewrite Cplus_0_r in H0.
apply H0.
- do 2 (rewrite <- Csum_extend_r).
rewrite IHn'.
rewrite H.
reflexivity.
Qed.
Local Open Scope nat_scope.
Lemma Csum_double_sum : forall (f : nat -> nat -> C) (n m : nat),
Csum (fun x => (Csum (fun y => f x y) n)) m = Csum (fun z => f (z / n) (z mod n)) (n * m).
Proof. induction m as [| m'].
- rewrite Nat.mul_0_r.
easy.
- rewrite Nat.mul_succ_r.
rewrite <- Csum_extend_r.
rewrite Csum_sum.
apply Csum_simplify; try easy.
apply Csum_eq_bounded; intros.
rewrite mult_comm.
rewrite Nat.div_add_l; try lia.
rewrite (plus_comm (m' * n)).
rewrite Nat.mod_add; try lia.
destruct (Nat.mod_small_iff x n) as [_ HD]; try lia.
destruct (Nat.div_small_iff x n) as [_ HA]; try lia.
rewrite HD, HA; try lia.
rewrite Nat.add_0_r.
easy.
Qed.
Lemma Csum_extend_double : forall (n m : nat) (f : nat -> nat -> C),
(Csum (fun i => Csum (fun j => f i j) (S m)) (S n)) =
((Csum (fun i => Csum (fun j => f i j) m) n) + (Csum (fun j => f n j) m) +
(Csum (fun i => f i m) n) + f n m)%C.
Proof. intros.
rewrite <- Csum_extend_r.
assert (H' : forall a b c d, (a + b + c + d = (a + c) + (b + d))%C).
{ intros. lca. }
rewrite H'.
apply Csum_simplify; try easy.
rewrite <- Csum_plus.
apply Csum_eq_bounded; intros.
easy.
Qed.
Lemma Csum_rearrange : forall (n : nat) (f g : nat -> nat -> C),
(forall x y, x <= y -> f x y = -C1 * g (S y) x)%C ->
(forall x y, y <= x -> f (S x) y = -C1 * g y x)%C ->
Csum (fun i => Csum (fun j => f i j) n) (S n) =
(-C1 * (Csum (fun i => Csum (fun j => g i j) n) (S n)))%C.
Proof. induction n as [| n'].
- intros. lca.
- intros.
do 2 rewrite Csum_extend_double.
rewrite (IHn' f g); try easy.
repeat rewrite Cmult_plus_distr_l.
repeat rewrite <- Cplus_assoc.
apply Csum_simplify; try easy.
assert (H' : forall a b c, (a + (b + c) = (a + c) + b)%C).
intros. lca.
do 2 rewrite H'.
rewrite <- Cmult_plus_distr_l.
do 2 rewrite Csum_extend_r.
do 2 rewrite Csum_mult_l.
rewrite Cplus_comm.
apply Csum_simplify.
all : apply Csum_eq_bounded; intros.
apply H; lia.
apply H0; lia.
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_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_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. simpl.
apply Csum_0.
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.
(* More succinct but sometimes doesn't succeed
Lemma WF_kron : forall {m n o p: nat} (A : Matrix m n) (B : Matrix o p),
WF_Matrix A -> WF_Matrix B -> WF_Matrix (A ⊗ B).
Proof.
unfold WF_Matrix, kron.
intros m n o p A B WFA WFB x y H.
bdestruct (o =? 0). rewrite WFB; [lca|lia].
bdestruct (p =? 0). rewrite WFB; [lca|lia].
rewrite WFA.
rewrite Cmult_0_l; reflexivity.
destruct H.
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_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.
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_Msum : forall d1 d2 n (f : nat -> Matrix d1 d2),
(forall i, (i < n)%nat -> WF_Matrix (f i)) ->
WF_Matrix (Msum n f).
Proof.
intros.
induction n; simpl.
- apply WF_Zero.
- apply WF_plus; auto.
Qed.
Local Close Scope nat_scope.
(***************************************)
(* Tactics for showing well-formedness *)
(***************************************)
Local Open Scope nat.
Local Open Scope R.
Local Open Scope C.
(*
Ltac show_wf :=
repeat match goal with
| [ |- WF_Matrix _ _ (?A × ?B) ] => apply WF_mult
| [ |- WF_Matrix _ _ (?A .+ ?B) ] => apply WF_plus
| [ |- WF_Matrix _ _ (?p .* ?B) ] => apply WF_scale
| [ |- WF_Matrix _ _ (?A ⊗ ?B) ] => apply WF_kron
| [ |- WF_Matrix _ _ (?A⊤) ] => apply WF_transpose
| [ |- WF_Matrix _ _ (?A†) ] => apply WF_adjoint
| [ |- WF_Matrix _ _ (I _) ] => apply WF_I
end;
trivial;
unfold WF_Matrix;
let x := fresh "x" in
let y := fresh "y" in
let H := fresh "H" in
intros x y [H | H];
repeat (destruct x; try reflexivity; try lia);
repeat (destruct y; try reflexivity; try lia).
*)
(* 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. *)
Hint Resolve WF_Zero WF_I WF_I1 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_Msum : wf_db.
Hint Extern 2 (_ = _) => unify_pows_two : wf_db.
(* Hint Resolve WF_Matrix_dim_change : wf_db. *)
(** Basic Matrix Lemmas **)
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.
induction n.
- simpl. lca.
- simpl. rewrite IHn. lca.
Qed.
Lemma trace_mult_dist : forall n p (A : Square n), trace (p .* A) = (p * trace A)%C.
Proof.
intros.
unfold trace, scale.
induction n.
- simpl. lca.
- simpl. rewrite IHn. lca.
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.
induction n.
+ simpl. reflexivity.
+ simpl in *.
autorewrite with C_db.
apply IHn.
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.
induction n.
+ simpl. reflexivity.
+ simpl.
autorewrite with C_db.
apply IHn.
Qed.
(* using <= because our form Csum 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 -> Csum (fun y : nat => I m x y * A y z) k = 0) /\
((k > x)%nat -> Csum (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.