-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKronecker.v
1890 lines (1745 loc) · 56.4 KB
/
Kronecker.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 Export Matrix.
Require Import RowColOps.
Import Complex.
Require Import Modulus.
Local Open Scope nat_scope.
Local Open Scope C_scope.
Lemma kron_I_r {n m p} (A : Matrix n m) :
mat_equiv (A ⊗ I p)
(fun i j => if i mod p =? j mod p then A (i / p)%nat (j / p)%nat else C0).
Proof.
intros i j Hi Hj.
unfold kron, I.
pose proof (Nat.mod_upper_bound i p ltac:(lia)).
bdestructΩ'; lca.
Qed.
Lemma kron_I_l {n m p} (A : Matrix n m) :
mat_equiv (I p ⊗ A)
(fun i j => if i / n =? j / m then A (i mod n) (j mod m) else C0).
Proof.
intros i j Hi Hj.
unfold kron, I.
rewrite Nat.mul_comm in Hi.
pose proof (Nat.Div0.div_lt_upper_bound _ _ _ Hi).
bdestructΩ'; lca.
Qed.
Lemma kron_I_l_eq {n m} (A : Matrix n m) p : WF_Matrix A ->
I p ⊗ A =
(fun i j : nat =>
if ((i / n =? j / m) && (i <? p * n))%nat
then A (i mod n) (j mod m)
else C0).
Proof.
intros HA.
apply mat_equiv_eq.
- auto_wf.
- intros i j []; bdestructΩ'.
bdestruct (m =? 0); [subst; now rewrite Nat.mod_0_r, HA by lia|].
pose proof (Nat.div_le_lower_bound j m p).
pose proof (Nat.Div0.div_lt_upper_bound i n p).
lia.
- autounfold with U_db.
intros i j Hi Hj.
simplify_bools_lia_one_kernel.
simplify_bools_moddy_lia_one_kernel.
bdestructΩ'; lca.
Qed.
Lemma kron_I_r_eq {n m} (A : Matrix n m) p : WF_Matrix A ->
A ⊗ I p =
(fun i j : nat =>
if (i mod p =? j mod p) && (i <? n * p) then
A (i / p)%nat (j / p)%nat else C0).
Proof.
intros HA.
apply mat_equiv_eq.
- auto_wf.
- bdestruct (p =? 0); [subst; intros i j _; bdestructΩ'|].
intros i j []; (rewrite HA; [bdestructΩ'|]); [left | right];
apply Nat.div_le_lower_bound; lia.
- autounfold with U_db.
intros i j Hi Hj.
simplify_bools_lia_one_kernel.
simplify_bools_moddy_lia_one_kernel.
bdestructΩ'; lca.
Qed.
Ltac bdestructΩ'simp :=
bdestructΩ'_with
ltac:(subst; simpl_bools; simpl; try easy; try lca; try lia).
Definition kron_comm p q : Matrix (p*q) (q*p):=
@make_WF (p*q) (q*p) (fun s t =>
(* have blocks H_ij, p by q of them, and each is q by p *)
let i := (s / q)%nat in let j := (t / p)%nat in
let k := (s mod q)%nat in let l := (t mod p) in
if (i =? l) && (j =? k) then C1 else C0
).
Lemma WF_kron_comm p q : WF_Matrix (kron_comm p q).
Proof. unfold kron_comm;
rewrite Nat.mul_comm;
trivial with wf_db. Qed.
#[export] Hint Resolve WF_kron_comm : wf_db.
Lemma kron_comm_transpose_mat_equiv : forall p q,
(kron_comm p q) ⊤ ≡ kron_comm q p.
Proof.
intros p q.
intros i j Hi Hj.
unfold kron_comm, transpose, make_WF.
rewrite andb_comm, Nat.mul_comm.
rewrite (andb_comm (_ =? _)).
easy.
Qed.
Lemma kron_comm_transpose : forall p q,
(kron_comm p q) ⊤ = kron_comm q p.
Proof.
intros p q.
apply mat_equiv_eq; auto with wf_db.
apply kron_comm_transpose_mat_equiv.
Qed.
Lemma kron_comm_adjoint : forall p q,
(kron_comm p q) † = kron_comm q p.
Proof.
intros p q.
apply mat_equiv_eq; [auto with wf_db..|].
unfold adjoint.
intros i j Hi Hj.
change (kron_comm p q j i) with ((kron_comm p q) ⊤ i j).
rewrite kron_comm_transpose_mat_equiv by easy.
unfold kron_comm, make_WF.
rewrite !(@if_dist C C).
bdestructΩ'; lca.
Qed.
Lemma kron_comm_1_r_mat_equiv : forall p,
(kron_comm p 1) ≡ Matrix.I p.
Proof.
intros p.
intros s t Hs Ht.
unfold kron_comm.
unfold make_WF.
unfold Matrix.I.
rewrite Nat.mul_1_r, Nat.div_1_r, Nat.mod_1_r, Nat.div_small, Nat.mod_small by lia.
bdestructΩ'.
Qed.
Lemma kron_comm_1_r : forall p,
(kron_comm p 1) = Matrix.I p.
Proof.
intros p.
apply mat_equiv_eq; [|rewrite Nat.mul_1_l, Nat.mul_1_r|]; auto with wf_db.
apply kron_comm_1_r_mat_equiv.
Qed.
Lemma kron_comm_1_l_mat_equiv : forall p,
(kron_comm 1 p) ≡ Matrix.I p.
Proof.
intros p.
intros s t Hs Ht.
unfold kron_comm.
unfold make_WF.
unfold Matrix.I.
rewrite Nat.mul_1_l, Nat.div_1_r, Nat.mod_1_r, Nat.div_small, Nat.mod_small by lia.
bdestructΩ'.
Qed.
Lemma kron_comm_1_l : forall p,
(kron_comm 1 p) = Matrix.I p.
Proof.
intros p.
apply mat_equiv_eq; [|rewrite Nat.mul_1_l, Nat.mul_1_r|]; auto with wf_db.
apply kron_comm_1_l_mat_equiv.
Qed.
Definition mx_to_vec {n m} (A : Matrix n m) : Vector (m * n) :=
make_WF (fun i j => A (i mod n)%nat (i / n)%nat
(* Note: goes columnwise. Rowwise would be:
make_WF (fun i j => A (i / m)%nat (i mod n)%nat
*)
).
Lemma WF_mx_to_vec {n m} (A : Matrix n m) : WF_Matrix (mx_to_vec A).
Proof. unfold mx_to_vec; auto with wf_db. Qed.
#[export] Hint Resolve WF_mx_to_vec : wf_db.
(* Compute vec_to_list (mx_to_vec (Matrix.I 2)). *)
From Coq Require Import ZArith.
Ltac Zify.zify_post_hook ::= PreOmega.Z.div_mod_to_equations.
Lemma kron_comm_mx_to_vec_helper : forall i p q, (i < p * q)%nat ->
(p * (i mod q) + i / q < p * q)%nat.
Proof.
intros i p q Hi.
show_moddy_lt.
Qed.
Lemma mx_to_vec_additive_mat_equiv {n m} (A B : Matrix n m) :
mx_to_vec (A .+ B) ≡ mx_to_vec A .+ mx_to_vec B.
Proof.
intros i j Hi Hj.
replace j with O by lia; clear dependent j.
unfold mx_to_vec, make_WF, Mplus.
bdestructΩ'.
Qed.
Lemma mx_to_vec_additive {n m} (A B : Matrix n m) :
mx_to_vec (A .+ B) = mx_to_vec A .+ mx_to_vec B.
Proof.
apply mat_equiv_eq; auto with wf_db.
apply mx_to_vec_additive_mat_equiv.
Qed.
Lemma if_mult_dist_r (b : bool) (z : C) :
(if b then C1 else C0) * z =
if b then z else C0.
Proof.
destruct b; lca.
Qed.
Lemma if_mult_dist_l (b : bool) (z : C) :
z * (if b then C1 else C0) =
if b then z else C0.
Proof.
destruct b; lca.
Qed.
Lemma if_mult_and (b c : bool) :
(if b then C1 else C0) * (if c then C1 else C0) =
if (b && c) then C1 else C0.
Proof.
destruct b; destruct c; lca.
Qed.
Lemma kron_comm_mx_to_vec_mat_equiv : forall p q (A : Matrix p q),
kron_comm p q × mx_to_vec A ≡ mx_to_vec (A ⊤).
Proof.
intros p q A.
intros i j Hi Hj.
replace j with O by lia; clear dependent j.
unfold transpose, mx_to_vec, kron_comm, make_WF, Mmult.
rewrite (Nat.mul_comm q p).
replace_bool_lia (i <? p * q) true.
replace_bool_lia (0 <? 1) true.
simpl.
erewrite big_sum_eq_bounded.
2: {
intros k Hk.
rewrite andb_true_r, <- andb_if.
replace_bool_lia (k <? p * q) true.
simpl.
rewrite if_mult_dist_r.
replace ((i / q =? k mod p) && (k / p =? i mod q)) with
(k =? p * (i mod q) + (i/q));
[reflexivity|]. (* Set this as our new Σ body; NTS the equality we claimed*)
rewrite eq_iff_eq_true.
rewrite andb_true_iff, 3!Nat.eqb_eq.
split.
- intros ->.
destruct p; [lia|].
destruct q; [lia|].
split.
+ rewrite Nat.add_comm, Nat.mul_comm.
rewrite Nat.Div0.mod_add by easy.
rewrite Nat.mod_small; [lia|].
show_moddy_lt.
+ rewrite Nat.mul_comm, Nat.div_add_l by easy.
rewrite Nat.div_small; [lia|].
show_moddy_lt.
- intros [Hmodp Hdivp].
rewrite (Nat.div_mod_eq k p).
lia.
}
apply big_sum_unique.
exists (p * (i mod q) + i / q)%nat; repeat split;
[apply kron_comm_mx_to_vec_helper; easy | rewrite Nat.eqb_refl | intros;
bdestructΩ'simp].
destruct p; [lia|];
destruct q; [lia|].
f_equal.
- rewrite Nat.add_comm, Nat.mul_comm, Nat.Div0.mod_add, Nat.mod_small; try easy.
show_moddy_lt.
- rewrite Nat.mul_comm, Nat.div_add_l by easy.
rewrite Nat.div_small; [lia|].
show_moddy_lt.
Qed.
Lemma kron_comm_mx_to_vec : forall p q (A : Matrix p q),
kron_comm p q × mx_to_vec A = mx_to_vec (A ⊤).
Proof.
intros p q A.
apply mat_equiv_eq; auto with wf_db.
apply kron_comm_mx_to_vec_mat_equiv.
Qed.
Lemma kron_comm_ei_kron_ei_sum_mat_equiv : forall p q,
kron_comm p q ≡
big_sum (G:=Matrix (p*q) (q*p))
(fun i => big_sum (fun j =>
(@e_i p i ⊗ @e_i q j) × ((@e_i q j ⊗ @e_i p i) ⊤))
q) p.
Proof.
intros p q.
intros i j Hi Hj.
rewrite Msum_Csum.
erewrite big_sum_eq_bounded.
2: {
intros k Hk.
rewrite Msum_Csum.
erewrite big_sum_eq_bounded.
2: {
intros l Hl.
unfold Mmult, kron, transpose, e_i.
erewrite big_sum_eq_bounded.
2: {
intros m Hm.
(* replace m with O by lia. *)
rewrite Nat.div_1_r, Nat.mod_1_r.
replace_bool_lia (m =? 0) true; rewrite 4!andb_true_r.
rewrite 3!if_mult_and.
match goal with
|- context[if ?b then _ else _] =>
replace b with ((i =? k * q + l) && (j =? l * p + k))
end.
1: reflexivity. (* set our new function *)
clear dependent m.
rewrite eq_iff_eq_true, 8!andb_true_iff,
6!Nat.eqb_eq, 4!Nat.ltb_lt.
split.
- intros [Hieq Hjeq].
subst i j.
rewrite 2!Nat.div_add_l, Nat.div_small, Nat.add_0_r by lia.
rewrite Nat.add_comm, Nat.Div0.mod_add, Nat.mod_small,
Nat.div_small, Nat.add_0_r by lia.
rewrite Nat.add_comm, Nat.Div0.mod_add, Nat.mod_small by lia.
easy.
- intros [[[] []] [[] []]].
split.
+ rewrite (Nat.div_mod_eq i q) by lia; lia.
+ rewrite (Nat.div_mod_eq j p) by lia; lia.
}
simpl; rewrite Cplus_0_l.
reflexivity.
}
apply big_sum_unique.
exists (i mod q).
split; [|split].
- apply Nat.mod_upper_bound; lia.
- reflexivity.
- intros l Hl Hnmod.
bdestructΩ'simp.
exfalso; apply Hnmod.
rewrite Nat.add_comm, Nat.Div0.mod_add, Nat.mod_small by lia; lia.
}
symmetry.
apply big_sum_unique.
exists (j mod p).
repeat split.
- apply Nat.mod_upper_bound; lia.
- unfold kron_comm, make_WF.
replace_bool_lia (i <? p * q) true.
replace_bool_lia (j <? q * p) true.
simpl.
apply f_equal_if; [|easy..].
rewrite eq_iff_eq_true, 2!andb_true_iff, 4!Nat.eqb_eq.
split.
+ intros [Hieq Hjeq].
split; [rewrite Hieq | rewrite Hjeq];
rewrite Hieq, Nat.div_add_l by lia;
(rewrite Nat.div_small; [lia|]);
apply Nat.mod_upper_bound; lia.
+ intros [Hidiv Hjdiv].
rewrite (Nat.div_mod_eq i q) at 1 by lia.
rewrite (Nat.div_mod_eq j p) at 2 by lia.
lia.
- intros k Hk Hkmod.
bdestructΩ'simp.
exfalso; apply Hkmod.
rewrite Nat.add_comm, Nat.Div0.mod_add, Nat.mod_small by lia; lia.
Qed.
Lemma kron_comm_ei_kron_ei_sum : forall p q,
kron_comm p q =
big_sum (fun i => big_sum (fun j =>
(@e_i p i ⊗ @e_i q j) × ((@e_i q j ⊗ @e_i p i) ⊤))
q) p.
Proof.
intros p q.
apply mat_equiv_eq; auto 10 with wf_db.
apply kron_comm_ei_kron_ei_sum_mat_equiv.
Qed.
Lemma kron_comm_ei_kron_ei_sum'_mat_equiv : forall p q,
kron_comm p q ≡
big_sum (fun ij =>
let i := (ij / q)%nat in let j := (ij mod q) in
((@e_i p i ⊗ @e_i q j) × ((@e_i q j ⊗ @e_i p i) ⊤))) (p*q).
Proof.
intros p q.
rewrite kron_comm_ei_kron_ei_sum, big_sum_double_sum, Nat.mul_comm.
reflexivity.
Qed.
(* TODO: put somewhere sensible *)
Lemma big_sum_mat_equiv_bounded : forall {o p} (f g : nat -> Matrix o p) (n : nat),
(forall x : nat, (x < n)%nat -> f x ≡ g x) -> big_sum f n ≡ big_sum g n.
Proof.
intros.
induction n.
- easy.
- simpl.
rewrite IHn, H; [easy|lia|auto].
Qed.
Lemma kron_comm_Hij_sum_mat_equiv : forall p q,
kron_comm p q ≡
big_sum (fun i => big_sum (fun j =>
@kron p q q p (@e_i p i × ((@e_i q j) ⊤))
((@Mmult p 1 q (@e_i p i) (((@e_i q j) ⊤))) ⊤)) q) p.
Proof.
intros p q.
rewrite kron_comm_ei_kron_ei_sum_mat_equiv.
apply big_sum_mat_equiv_bounded; intros i Hi.
apply big_sum_mat_equiv_bounded; intros j Hj.
rewrite kron_transpose, kron_mixed_product.
rewrite Mmult_transpose, transpose_involutive.
easy.
Qed.
Lemma kron_comm_Hij_sum : forall p q,
kron_comm p q =
big_sum (fun i => big_sum (fun j =>
e_i i × (e_i j) ⊤ ⊗
(e_i i × (e_i j) ⊤) ⊤) q) p.
Proof.
intros p q.
apply mat_equiv_eq; [auto 10 with wf_db.. | ].
apply kron_comm_Hij_sum_mat_equiv.
Qed.
Lemma kron_comm_ei_kron_ei_sum' : forall p q,
kron_comm p q =
big_sum (fun ij =>
let i := (ij / q)%nat in let j := (ij mod q) in
((e_i i ⊗ e_i j) × ((e_i j ⊗ e_i i) ⊤))) (p*q).
Proof.
intros p q.
rewrite kron_comm_ei_kron_ei_sum, big_sum_double_sum, Nat.mul_comm.
reflexivity.
Qed.
Local Notation H := (fun i j => e_i i × (e_i j)⊤).
Lemma kron_comm_Hij_sum'_mat_equiv : forall p q,
kron_comm p q ≡
big_sum ( fun ij =>
let i := (ij / q)%nat in let j := (ij mod q) in
H i j ⊗ (H i j) ⊤) (p*q).
Proof.
intros p q.
rewrite kron_comm_Hij_sum_mat_equiv, big_sum_double_sum, Nat.mul_comm.
easy.
Qed.
Lemma kron_comm_Hij_sum' : forall p q,
kron_comm p q =
big_sum (fun ij =>
let i := (ij / q)%nat in let j := (ij mod q) in
H i j ⊗ (H i j) ⊤) (p*q).
Proof.
intros p q.
rewrite kron_comm_Hij_sum, big_sum_double_sum, Nat.mul_comm.
easy.
Qed.
Lemma div_eq_iff : forall a b c, b <> O ->
(a / b)%nat = c <-> (b * c <= a /\ a < b * (S c))%nat.
Proof.
intros a b c Hb.
split.
intros Hadivb.
split;
subst c.
- rewrite (Nat.div_mod_eq a b) at 2; lia.
- now apply Nat.mul_succ_div_gt.
- intros [Hge Hlt].
symmetry.
apply (Nat.div_unique _ _ _ (a - b*c)); lia.
Qed.
Lemma div_eqb_iff : forall a b c, b <> O ->
(a / b)%nat =? c = ((b * c <=? a) && (a <? b * (S c))).
Proof.
intros a b c Hb.
apply eq_iff_eq_true.
rewrite andb_true_iff, Nat.leb_le, Nat.ltb_lt, Nat.eqb_eq.
now apply div_eq_iff.
Qed.
Lemma kron_e_i_transpose_l : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k)⊤ ⊗ A = (fun i j =>
if (i <? m) && (j / o =? k) then A i (j - k * o)%nat else C0).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, transpose, e_i.
rewrite if_mult_dist_r.
bdestruct (i <? m).
- rewrite (Nat.div_small i m),
(Nat.mod_small i m), Nat.eqb_refl, andb_true_r, andb_true_l by easy.
replace ((j / o =? k) && (j / o <? n)) with (j / o =? k) by bdestructΩ'simp.
bdestruct_one; [|easy].
rewrite mod_eq_sub; f_equal;
lia.
- bdestructΩ'simp.
rewrite Nat.div_small_iff in *; lia.
Qed.
Lemma kron_e_i_transpose_l_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k)⊤ ⊗ A ≡ (fun i j =>
if (i <? m) && (j / o =? k) then A i (j - k * o)%nat else 0%R).
Proof.
intros.
rewrite kron_e_i_transpose_l; easy.
Qed.
Lemma kron_e_i_transpose_l_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(@e_i n k)⊤ ⊗ A ≡ (fun i j =>
if (i <? m) && (j / o =? k) then A i (j - k * o)%nat else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_transpose_l; easy.
Qed.
Lemma kron_e_i_l : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k) ⊗ A = (fun i j =>
if (j <? o) && (i / m =? k) then A (i - k * m)%nat j else 0%R).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, transpose, e_i.
rewrite if_mult_dist_r.
bdestruct (j <? o).
- rewrite (Nat.div_small j o),
(Nat.mod_small j o), Nat.eqb_refl, andb_true_r, andb_true_l by easy.
replace ((i / m =? k) && (i / m <? n)) with (i / m =? k) by bdestructΩ'.
bdestruct_one; [|easy].
rewrite mod_eq_sub; f_equal;
lia.
- bdestructΩ'simp.
rewrite Nat.div_small_iff in *; lia.
Qed.
Lemma kron_e_i_l_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k) ⊗ A ≡ (fun i j =>
if (j <? o) && (i / m =? k) then A (i - k * m)%nat j else 0%R).
Proof.
intros.
rewrite kron_e_i_l; easy.
Qed.
Lemma kron_e_i_l_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(@e_i n k) ⊗ A ≡ (fun i j =>
if (j <? o) && (i / m =? k) then A (i - k * m)%nat j else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_l; easy.
Qed.
Lemma kron_e_i_transpose_l' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k)⊤ ⊗ A = (fun i j =>
if (i <? m) && (k * o <=? j) && (j <? (S k) * o) then A i (j - k * o)%nat else 0%R).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, transpose, e_i.
rewrite if_mult_dist_r.
bdestruct (i <? m).
- rewrite (Nat.div_small i m), Nat.eqb_refl, andb_true_r, andb_true_l by easy.
rewrite Nat.mod_small by easy.
replace ((j / o =? k) && (j / o <? n)) with ((k * o <=? j) && (j <? S k * o)).
+ do 2 bdestruct_one_old; simpl; try easy.
destruct o; [lia|].
f_equal.
rewrite mod_eq_sub, Nat.mul_comm.
do 2 f_equal.
rewrite div_eq_iff; lia.
+ rewrite eq_iff_eq_true, 2!andb_true_iff, Nat.eqb_eq, 2!Nat.ltb_lt, Nat.leb_le.
assert (Hrw: ((j / o)%nat = k /\ (j / o < n)%nat) <-> ((j/o)%nat=k)) by lia;
rewrite Hrw; clear Hrw.
symmetry.
rewrite div_eq_iff by lia.
lia.
- replace (i / m =? 0) with false.
rewrite andb_false_r; easy.
symmetry.
rewrite Nat.eqb_neq.
rewrite Nat.div_small_iff; lia.
Qed.
Lemma kron_e_i_transpose_l'_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k)⊤ ⊗ A ≡ (fun i j =>
if (i <? m) && (k * o <=? j) && (j <? (S k) * o) then A i (j - k * o)%nat else 0%R).
Proof.
intros.
rewrite kron_e_i_transpose_l'; easy.
Qed.
Lemma kron_e_i_transpose_l'_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(@e_i n k)⊤ ⊗ A ≡ (fun i j =>
if (i <? m) && (k * o <=? j) && (j <? (S k) * o) then A i (j - k * o)%nat else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_transpose_l'; easy.
Qed.
Lemma kron_e_i_l' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k) ⊗ A = (fun i j =>
if (j <? o) && (k * m <=? i) && (i <? (S k) * m) then A (i - k * m)%nat j else 0%R).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, e_i.
rewrite if_mult_dist_r.
bdestruct (j <? o).
- rewrite (Nat.div_small j o), Nat.eqb_refl, andb_true_r, andb_true_l by easy.
rewrite (Nat.mod_small j o) by easy.
replace ((i / m =? k) && (i / m <? n)) with ((k * m <=? i) && (i <? S k * m)).
+ do 2 bdestruct_one_old; simpl; try easy.
destruct m; [lia|].
f_equal.
rewrite mod_eq_sub, Nat.mul_comm.
do 2 f_equal.
rewrite div_eq_iff; lia.
+ rewrite eq_iff_eq_true, 2!andb_true_iff, Nat.eqb_eq, 2!Nat.ltb_lt, Nat.leb_le.
assert (Hrw: ((i/m)%nat=k/\(i/m<n)%nat) <-> ((i/m)%nat=k)) by lia;
rewrite Hrw; clear Hrw.
symmetry.
rewrite div_eq_iff by lia.
lia.
- replace (j / o =? 0) with false.
rewrite andb_false_r; easy.
symmetry.
rewrite Nat.eqb_neq.
rewrite Nat.div_small_iff; lia.
Qed.
Lemma kron_e_i_l'_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k) ⊗ A ≡ (fun i j =>
if (j <? o) && (k * m <=? i) && (i <? (S k) * m) then A (i - k * m)%nat j else 0%R).
Proof.
intros.
rewrite kron_e_i_l'; easy.
Qed.
Lemma kron_e_i_l'_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
(@e_i n k) ⊗ A ≡ (fun i j =>
if (j <? o) && (k * m <=? i) && (i <? (S k) * m) then A (i - k * m)%nat j else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_l'; easy.
Qed.
Lemma kron_e_i_r : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
A ⊗ (@e_i n k) = (fun i j =>
if (i mod n =? k) then A (i / n)%nat j else 0%R).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, e_i.
rewrite if_mult_dist_l, Nat.div_1_r.
rewrite Nat.mod_1_r, Nat.eqb_refl, andb_true_r.
replace (i mod n <? n) with true;
[rewrite andb_true_r; easy |].
symmetry; rewrite Nat.ltb_lt.
apply Nat.mod_upper_bound; lia.
Qed.
Lemma kron_e_i_r_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
A ⊗ (@e_i n k) ≡ (fun i j =>
if (i mod n =? k) then A (i / n)%nat j else 0%R).
Proof.
intros.
rewrite kron_e_i_r; easy.
Qed.
Lemma kron_e_i_r_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
A ⊗ (@e_i n k) ≡ (fun i j =>
if (i mod n =? k) then A (i / n)%nat j else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_r; easy.
Qed.
Lemma kron_e_i_transpose_r : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
A ⊗ (@e_i n k) ⊤ = (fun i j =>
if (j mod n =? k) then A i (j / n)%nat else 0%R).
Proof.
intros k n m o A Hk Ho Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
unfold kron, transpose, e_i.
rewrite if_mult_dist_l, Nat.div_1_r.
rewrite Nat.mod_1_r, Nat.eqb_refl, andb_true_r.
replace (j mod n <? n) with true;
[rewrite andb_true_r; easy |].
symmetry; rewrite Nat.ltb_lt.
apply Nat.mod_upper_bound; lia.
Qed.
Lemma kron_e_i_transpose_r_mat_equiv : forall k n m o (A : Matrix m o), (k < n)%nat ->
(o <> O) -> (m <> O) ->
A ⊗ (@e_i n k) ⊤ ≡ (fun i j =>
if (j mod n =? k) then A i (j / n)%nat else 0%R).
Proof.
intros.
rewrite kron_e_i_transpose_r; easy.
Qed.
Lemma kron_e_i_transpose_r_mat_equiv' : forall k n m o (A : Matrix m o), (k < n)%nat ->
A ⊗ (@e_i n k) ⊤ ≡ (fun i j =>
if (j mod n =? k) then A i (j / n)%nat else 0%R).
Proof.
intros.
destruct m; [|destruct o];
try (intros i j Hi Hj; lia).
rewrite kron_e_i_transpose_r; easy.
Qed.
Lemma ei_kron_I_kron_ei : forall m n k, (k < n)%nat -> m <> O ->
(@e_i n k) ⊤ ⊗ (Matrix.I m) ⊗ (@e_i n k) =
(fun i j => if (i mod n =? k) && (j / m =? k)%nat
&& (i / n =? j - k * m) && (i / n <? m)
then 1%R else 0%R).
Proof.
intros m n k Hk Hm.
apply functional_extensionality; intros i;
apply functional_extensionality; intros j.
rewrite kron_e_i_transpose_l by easy.
rewrite kron_e_i_r; try lia;
[| rewrite Nat.mul_eq_0; lia].
unfold Matrix.I.
rewrite <- 2!andb_if.
bdestruct_one_old; [
rewrite 2!andb_true_r, andb_true_l | rewrite 4!andb_false_r; easy
].
easy.
Qed.
Lemma ei_kron_I_kron_ei_mat_equiv : forall m n k, (k < n)%nat -> m <> O ->
(@e_i n k) ⊤ ⊗ (Matrix.I m) ⊗ (@e_i n k) ≡
(fun i j => if (i mod n =? k) && (j / m =? k)%nat
&& (i / n =? j - k * m) && (i / n <? m)
then 1%R else 0%R).
Proof.
intros.
rewrite ei_kron_I_kron_ei; easy.
Qed.
Lemma ei_kron_I_kron_ei_mat_equiv' : forall m n k, (k < n)%nat ->
(@e_i n k) ⊤ ⊗ (Matrix.I m) ⊗ (@e_i n k) ≡
(fun i j => if (i mod n =? k) && (j / m =? k)%nat
&& (i / n =? j - k * m) && (i / n <? m)
then 1%R else 0%R).
Proof.
intros.
destruct m; try (intros i j Hi Hj; lia).
rewrite ei_kron_I_kron_ei; easy.
Qed.
Lemma kron_comm_kron_form_sum_mat_equiv : forall m n,
kron_comm m n ≡ big_sum (fun j =>
(@e_i n j) ⊤ ⊗ (Matrix.I m) ⊗ (@e_i n j)) n.
Proof.
intros m n.
intros i j Hi Hj.
rewrite Msum_Csum.
erewrite big_sum_eq_bounded.
2: {
intros ij Hij.
rewrite ei_kron_I_kron_ei by lia.
reflexivity.
}
unfold kron_comm, make_WF.
do 2 simplify_bools_lia_one_kernel.
replace (i / n <? m) with true by show_moddy_lt.
bdestruct_one; [bdestruct_one|]; simpl; symmetry; [
apply big_sum_unique;
exists (j / m)%nat;
split; [ apply Nat.Div0.div_lt_upper_bound; lia | ];
split; [rewrite (Nat.mul_comm (j / m) m), <- mod_eq_sub by lia; bdestructΩ'|];
intros k Hk Hkne; bdestructΩ'simp
| |];
(rewrite big_sum_0; [easy|]; intros k; bdestructΩ'simp).
pose proof (mod_eq_sub j m); lia.
Qed.
Lemma kron_comm_kron_form_sum : forall m n,
kron_comm m n = big_sum
(fun j => (@e_i n j) ⊤ ⊗ (Matrix.I m) ⊗ (@e_i n j)) n.
Proof.
intros m n.
apply mat_equiv_eq;
[|eapply WF_Matrix_dim_change; [lia..|]|];
[auto with wf_db..|].
apply kron_comm_kron_form_sum_mat_equiv; easy.
Qed.
Lemma kron_comm_kron_form_sum' : forall m n,
kron_comm m n = big_sum (fun i =>
(@e_i m i) ⊗ (Matrix.I n) ⊗ (@e_i m i)⊤) m.
Proof.
intros.
rewrite <- (kron_comm_transpose n m).
rewrite (kron_comm_kron_form_sum n m).
replace (n * m)%nat with (1 * n * m)%nat by lia.
replace (m * n)%nat with (m * n * 1)%nat by lia.
rewrite (Nat.mul_1_r (m * n * 1)).
etransitivity;
[apply Msum_transpose|].
apply big_sum_eq_bounded.
intros k Hk.
restore_dims.
rewrite !kron_transpose.
now rewrite id_transpose_eq, transpose_involutive.
Qed.
Lemma kron_comm_kron_form_sum'_mat_equiv : forall m n,
kron_comm m n ≡ big_sum (fun i =>
(@e_i m i) ⊗ (Matrix.I n) ⊗ (@e_i m i)⊤) m.
Proof.
intros.
rewrite kron_comm_kron_form_sum'; easy.
Qed.
Lemma e_i_dot_is_component_mat_equiv : forall p k (x : Vector p),
(k < p)%nat ->
(@e_i p k) ⊤ × x ≡ x k O .* Matrix.I 1.
Proof.
intros p k x Hk.
intros i j Hi Hj;
replace i with O by lia;
replace j with O by lia;
clear i Hi;
clear j Hj.
unfold Mmult, transpose, scale, e_i, Matrix.I.
simpl_bools.
rewrite Cmult_1_r.
apply big_sum_unique.
exists k.
split; [easy|].
bdestructΩ'simp.
rewrite Cmult_1_l.
split; [easy|].
intros l Hl Hkl.
bdestructΩ'simp.
Qed.
Lemma e_i_dot_is_component : forall p k (x : Vector p),
(k < p)%nat -> WF_Matrix x ->
(@e_i p k) ⊤ × x = x k O .* Matrix.I 1.
Proof.
intros p k x Hk HWF.
apply mat_equiv_eq; auto with wf_db.
apply e_i_dot_is_component_mat_equiv; easy.
Qed.
Lemma kron_e_i_e_i : forall p q k l,
(k < p)%nat -> (l < q)%nat ->
@e_i q l ⊗ @e_i p k = @e_i (p*q) (l*p + k).
Proof.
intros p q k l Hk Hl.
apply functional_extensionality; intro i.
apply functional_extensionality; intro j.
unfold kron, e_i.
rewrite Nat.mod_1_r, Nat.div_1_r.
rewrite if_mult_and.
apply f_equal_if; [|easy..].
rewrite Nat.eqb_refl, andb_true_r.
destruct (j =? 0); [|rewrite 2!andb_false_r; easy].
rewrite 2!andb_true_r.
rewrite eq_iff_eq_true, 4!andb_true_iff, 3!Nat.eqb_eq, 3!Nat.ltb_lt.
split.
- intros [[] []].
rewrite (Nat.div_mod_eq i p).
split; nia.
- intros [].
subst i.
rewrite Nat.div_add_l, Nat.div_small, Nat.add_0_r,
Nat.add_comm, Nat.Div0.mod_add, Nat.mod_small by lia.
easy.
Qed.
Lemma kron_e_i_e_i_mat_equiv : forall p q k l,
(k < p)%nat -> (l < q)%nat ->
@e_i q l ⊗ @e_i p k ≡ @e_i (p*q) (l*p + k).
Proof.
intros p q k l; intros.
rewrite (kron_e_i_e_i p q); easy.
Qed.
Lemma kron_e_i_e_i_split : forall p q k, (k < p * q)%nat ->
@e_i (p*q) k = @e_i q (k / p) ⊗ @e_i p (k mod p).
Proof.
intros p q k Hk.
rewrite (kron_e_i_e_i p q) by show_moddy_lt.
rewrite (Nat.div_mod_eq k p) at 1.
f_equal; lia.
Qed.
Lemma kron_eq_sum_mat_equiv : forall p q (x : Vector q) (y : Vector p),
y ⊗ x ≡ big_sum (fun ij =>
let i := (ij / q)%nat in let j := ij mod q in
(x j O * y i O) .* (@e_i p i ⊗ @e_i q j)) (p * q).
Proof.
intros p q x y.
erewrite big_sum_eq_bounded.
2: {
intros ij Hij.
simpl.
rewrite (@kron_e_i_e_i q p) by
(try apply Nat.mod_upper_bound; try apply Nat.Div0.div_lt_upper_bound; lia).
rewrite (Nat.mul_comm (ij / q) q).
rewrite <- (Nat.div_mod_eq ij q).
reflexivity.
}
intros i j Hi Hj.
replace j with O by lia; clear j Hj.
simpl.
rewrite Msum_Csum.
symmetry.
apply big_sum_unique.
exists i.
split; [lia|].
unfold e_i; split.
- unfold scale, kron; bdestructΩ'simp.
- intros j Hj Hij.
unfold scale, kron; bdestructΩ'simp.
Qed.
Lemma kron_eq_sum : forall p q (x : Vector q) (y : Vector p),
WF_Matrix x -> WF_Matrix y ->
y ⊗ x = big_sum (fun ij =>
let i := (ij / q)%nat in let j := ij mod q in
(x j O * y i O) .* (@e_i p i ⊗ @e_i q j)) (p * q).
Proof.
intros p q x y Hwfx Hwfy.
apply mat_equiv_eq; [| |]; auto with wf_db.
apply kron_eq_sum_mat_equiv.
Qed.
Lemma kron_comm_commutes_vectors_l_mat_equiv : forall p q (x : Vector q) (y : Vector p),
kron_comm p q × (x ⊗ y) ≡ (y ⊗ x).
Proof.
intros p q x y.
rewrite kron_comm_ei_kron_ei_sum'_mat_equiv, Mmult_Msum_distr_r.
rewrite (big_sum_mat_equiv_bounded _
(fun k => x (k mod q) 0 * y (k / q) 0 .* (e_i (k / q) ⊗ e_i (k mod q)))%nat);
[rewrite <- kron_eq_sum_mat_equiv; easy|].
intros k Hk.
simpl.
rewrite Mmult_assoc.
change 1%nat with (1 * 1)%nat.
restore_dims.
rewrite (kron_transpose' (@e_i q (k mod q)) (@e_i p (k / q))).
rewrite kron_mixed_product.
rewrite 2!(e_i_dot_is_component_mat_equiv) by show_moddy_lt.
rewrite Mscale_kron_dist_l, Mscale_kron_dist_r, Mscale_assoc.
rewrite kron_1_l, Mscale_mult_dist_r, Mmult_1_r by auto with wf_db.
reflexivity.
Qed.
Lemma kron_comm_commutes_vectors_l : forall p q (x : Vector q) (y : Vector p),
WF_Matrix x -> WF_Matrix y ->
kron_comm p q × (x ⊗ y) = (y ⊗ x).
Proof.
intros p q x y Hwfx Hwfy.
apply mat_equiv_eq; auto with wf_db.
apply kron_comm_commutes_vectors_l_mat_equiv.
Qed.
(* Lemma kron_basis_vector_basis_vector : forall p q k l,