-
Notifications
You must be signed in to change notification settings - Fork 10
/
Eigenvectors.v
2700 lines (2426 loc) · 88 KB
/
Eigenvectors.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
(** This file contains more concepts relevent to quantum computing, as well as some more general linear algebra concepts such as Gram-Schmidt and eigenvectors/eigenvalues. *)
Require Import List.
Require Export Complex.
Require Export CauchySchwarz.
Require Export Quantum.
Require Import FTA.
Require Import Permutations.
(****************************)
(** * Proving some indentities *)
(****************************)
(* little Ltac for helping with √ 2 *)
Ltac Hhelper :=
unfold Mmult;
unfold big_sum;
unfold I;
simpl;
C_field_simplify;
try lca;
C_field.
Lemma Y_eq_iXZ : σy = Ci .* σx × σz. Proof. lma'. Qed.
Lemma H_eq_Hadjoint : hadamard† = hadamard. Proof. lma'. Qed.
#[global] Hint Rewrite Y_eq_iXZ H_eq_Hadjoint : Q_db.
Lemma ItimesIid : I 2 × I 2 = I 2. Proof. lma'. Qed.
Lemma XtimesXid : σx × σx = I 2. Proof. lma'. Qed.
Lemma YtimesYid : σy × σy = I 2. Proof. lma'. Qed.
Lemma ZtimesZid : σz × σz = I 2. Proof. lma'. Qed.
Lemma HtimesHid : hadamard × hadamard = I 2. Proof. lma'; Hhelper. Qed.
#[global] Hint Rewrite ItimesIid XtimesXid YtimesYid ZtimesZid HtimesHid : Q_db.
Lemma ZH_eq_HX : σz × hadamard = hadamard × σx. Proof. lma'. Qed.
Lemma XH_eq_HZ : σx × hadamard = hadamard × σz. Proof. lma'. Qed.
Lemma SX_eq_YS : Sgate × σx = σy × Sgate. Proof. lma'; unfold Mmult;
simpl; rewrite Cexp_PI2; lca. Qed.
Lemma SZ_eq_ZS : Sgate × σz = σz × Sgate. Proof. lma'; unfold Mmult;
simpl; rewrite Cexp_PI2; lca. Qed.
Lemma cnotX1 : cnot × (σx ⊗ I 2) = (σx ⊗ σx) × cnot. Proof. lma'. Qed.
Lemma cnotX2 : cnot × (I 2 ⊗ σx) = (I 2 ⊗ σx) × cnot. Proof. lma'. Qed.
Lemma cnotZ1 : cnot × (σz ⊗ I 2) = (σz ⊗ I 2) × cnot. Proof. lma'. Qed.
Lemma cnotZ2 : cnot × (I 2 ⊗ σz) = (σz ⊗ σz) × cnot. Proof. lma'. Qed.
#[global] Hint Rewrite ZH_eq_HX XH_eq_HZ SX_eq_YS SZ_eq_ZS cnotX1 cnotX2 cnotZ1 cnotZ2 : Q_db.
(*******************************)
(** * Defining orthonormal matrix *)
(*******************************)
Local Open Scope nat_scope.
Definition orthogonal {n m} (S : Matrix n m) : Prop :=
forall i j, i <> j -> inner_product (get_col S i) (get_col S j) = C0.
Definition orthonormal {n m} (S : Matrix n m) : Prop :=
orthogonal S /\ (forall (i : nat), i < m -> norm (get_col S i) = 1%R).
(* to match WF_Unitary *)
Definition WF_Orthonormal {n m} (S : Matrix n m) : Prop :=
WF_Matrix S /\ orthonormal S.
Lemma inner_product_is_mult : forall {m n} (i j : nat) (S : Matrix m n),
inner_product (get_col S i) (get_col S j) = (S† × S) i j.
Proof. intros. unfold inner_product, get_col, Mmult, adjoint.
apply big_sum_eq.
apply functional_extensionality; intros. simpl.
reflexivity.
Qed.
(* FIXME: this already exists in Cauchyschwarz.v *)
Lemma inner_product_comm_conj : forall {n} (v u : Vector n),
inner_product v u = Cconj (inner_product u v).
Proof. intros.
unfold inner_product.
assert (H' : forall A : Matrix 1 1, (A 0 0) ^* = A† 0 0).
{ unfold adjoint, Cconj.
easy. }
rewrite H'.
rewrite Mmult_adjoint, adjoint_involutive.
easy.
Qed.
(***********************************************)
(** * some useful facts about unitary matrices *)
(***********************************************)
Lemma unit_is_orthonormal : forall {n} (U : Square n),
WF_Unitary U <-> WF_Orthonormal U.
Proof. intros n U. split.
- split; try apply H.
split.
* unfold orthogonal. intros.
rewrite inner_product_is_mult.
destruct H as [H1 H].
rewrite H.
unfold I. bdestruct (i =? j); try lia; easy.
* intros. unfold norm, inner_product.
assert (H1 : ((get_col U i) † × get_col U i) 0%nat 0%nat =
inner_product (get_col U i) (get_col U i)).
{ unfold inner_product. reflexivity. }
rewrite H1. rewrite inner_product_is_mult.
destruct H.
rewrite H2. unfold I.
bdestruct (i =? i); bdestruct (i <? n); try lia.
simpl. apply sqrt_1.
- intros [H1 [H2 H3] ].
split; try easy.
apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros.
rewrite <- inner_product_is_mult.
unfold orthogonal in H2. unfold I.
bdestruct (i =? j); bdestruct (i <? n); try lia; subst.
* unfold norm, inner_product in H3.
apply H3 in H0.
apply eq_sym in H0.
apply sqrt_1_unique in H0.
unfold RtoC.
apply c_proj_eq; try easy.
simpl.
apply norm_real.
* rewrite H2; try nia; easy.
Qed.
Lemma det_by_unit : forall {n} (A B X : Square n),
WF_Matrix A -> WF_Matrix B ->
WF_Unitary X -> (forall i, A × (get_col X i) = B × (get_col X i)) -> A = B.
Proof. intros. assert (H' : A × X = B × X).
{ apply det_by_get_col. intros.
do 2 (rewrite <- get_col_mult).
apply H2. }
rewrite <- Mmult_1_r.
rewrite <- (Mmult_1_r _ _ A).
destruct H1.
apply Minv_flip in H3; auto with wf_db.
rewrite <- H3.
do 2 (rewrite <- Mmult_assoc).
rewrite H'.
reflexivity.
all : easy.
Qed.
Lemma unit_invertible : forall {n} (U : Square n),
WF_Unitary U -> invertible U.
Proof. intros.
destruct H.
exists (adjoint U).
split; auto with wf_db.
split; auto.
apply Minv_flip; auto with wf_db.
Qed.
Lemma unit_det_neq_0 : forall {n} (U : Square n),
WF_Unitary U -> Determinant U <> C0.
Proof. intros.
apply invertible_iff_det_neq_0.
apply H.
apply unit_invertible.
apply H.
Qed.
(***********************************************************************************)
(** * We now define diagonal matrices and diagonizable matrices, proving basic lemmas *)
(***********************************************************************************)
Definition WF_Diagonal {m n : nat} (A : Matrix m n) : Prop :=
WF_Matrix A /\ forall i j, i <> j -> A i j = C0.
Lemma diag_Zero : forall m n : nat, WF_Diagonal (@Zero m n).
Proof. intros n. split; auto with wf_db. Qed.
Lemma diag_I : forall n : nat, WF_Diagonal (I n).
Proof.
intros.
split; auto with wf_db.
intros.
unfold I.
bdestruct (i =? j); try lia; try easy.
Qed.
Lemma diag_I1 : WF_Diagonal (I 1). Proof. apply diag_I. Qed.
Lemma diag_scale : forall {n : nat} (r : C) (A : Square n),
WF_Diagonal A -> WF_Diagonal (r .* A).
Proof.
intros n r A [H H0].
split; auto with wf_db.
intros.
unfold scale.
rewrite H0; try lca; easy.
Qed.
Lemma diag_plus : forall {n} (A B : Square n),
WF_Diagonal A -> WF_Diagonal B -> WF_Diagonal (A .+ B).
Proof.
intros n A B [H H0] [H1 H2].
split; auto with wf_db.
intros.
unfold Mplus.
rewrite H0, H2; try easy; lca.
Qed.
Lemma diag_mult : forall {m n o : nat} (A : Matrix m n) (B : Matrix n o),
WF_Diagonal A -> WF_Diagonal B -> WF_Diagonal (A × B).
Proof.
intros m n o A B [H H0] [H1 H2].
split; auto with wf_db.
intros.
unfold Mmult.
apply (@big_sum_0 C C_is_monoid).
intro.
bdestruct (x =? i).
+ rewrite H2; try lia; lca.
+ rewrite H0, Cmult_0_l.
reflexivity. auto.
Qed.
Lemma diag_pad1 : forall {m n} (A : Matrix m n) (c : C),
WF_Diagonal A <-> WF_Diagonal (pad1 A c).
Proof. intros; split; intros; destruct H; split; auto with wf_db; intros.
destruct i; destruct j; auto; try lia.
unfold pad1, col_wedge, row_wedge, e_i, scale.
lca.
rewrite pad1_conv, H0; auto.
eapply WF_pad1_conv.
apply H.
erewrite <- pad1_conv.
rewrite H0; auto.
Qed.
(* short lemma to prove diag_kron *)
Lemma div_mod_eq : forall (a b m : nat),
m <> 0 -> (a / m = b / m) -> (a mod m = b mod m) -> a = b.
Proof. intros a b m H0 Hdiv Hmod.
rewrite (Nat.mod_eq a m), (Nat.mod_eq b m) in Hmod.
rewrite Hdiv in Hmod.
assert (H : m * (b / m) + (a - m * (b / m)) = m * (b / m) + (b - m * (b / m))).
{ rewrite Hmod. reflexivity. }
rewrite <- (le_plus_minus' (m * (b / m)) a) in H.
rewrite <- (le_plus_minus' (m * (b / m)) b) in H.
apply H.
apply Nat.mul_div_le; apply H0.
rewrite <- Hdiv; apply Nat.mul_div_le; apply H0.
apply H0. apply H0.
Qed.
Lemma diag_kron : forall {n m : nat} (A : Square n) (B : Square m),
WF_Diagonal A -> WF_Diagonal B -> WF_Diagonal (A ⊗ B).
Proof.
intros n m A B [H H0] [H1 H2].
destruct m.
rewrite (WF0_Zero_l 0); try easy.
auto with wf_db.
split; auto with wf_db.
unfold kron.
intros.
bdestruct (i / (S m) =? j / (S m)).
- bdestruct (i mod (S m) =? j mod (S m)).
+ apply (div_mod_eq i j (S m)) in H5; try easy.
+ rewrite H2; try lca; easy.
- rewrite H0; try lca; easy.
Qed.
Lemma diag_transpose : forall {n : nat} (A : Square n),
WF_Diagonal A -> WF_Diagonal A⊤.
Proof. intros n A [H H0].
split; auto with wf_db.
intros.
unfold transpose.
apply H0. auto.
Qed.
Lemma diag_adjoint : forall {n : nat} (A : Square n),
WF_Diagonal A -> WF_Diagonal A†.
Proof. intros n A [H H0].
split; auto with wf_db.
unfold adjoint, Cconj.
intros.
rewrite H0. lca. auto.
Qed.
Lemma diag_kron_n : forall (n : nat) {m : nat} (A : Square m),
WF_Diagonal A -> WF_Diagonal (kron_n n A).
Proof.
intros.
induction n; simpl.
- apply diag_I.
- rewrite Nat.mul_comm.
apply (@diag_kron (m^n) m _ A).
apply IHn. apply H.
Qed.
Lemma diag_big_kron : forall n (l : list (Square n)),
(forall A, In A l -> WF_Diagonal A) ->
WF_Diagonal (⨂ l).
Proof.
intros.
induction l.
- simpl. apply diag_I.
- simpl. apply (@diag_kron _ (n^(length l)) a (⨂ l)).
apply H.
left. easy.
apply IHl.
intros A H'. apply H.
simpl. auto.
Qed.
Lemma diag_Mmult_n : forall n {m} (A : Square m),
WF_Diagonal A -> WF_Diagonal (Mmult_n n A).
Proof.
intros.
induction n; simpl.
- apply diag_I.
- apply diag_mult; assumption.
Qed.
(** defining what it means to be diagonalizable *)
Definition WF_Diagonalizable {n : nat} (A : Square n) : Prop :=
WF_Matrix A /\ (exists (X X' B: Square n),
WF_Diagonal B /\ WF_Matrix X /\ WF_Matrix X' /\ X × X' = I n /\ B = X × A × X').
Lemma diag_imps_diagble : forall {n} (A : Square n),
WF_Diagonal A -> WF_Diagonalizable A.
Proof. intros n A [H H0]. unfold WF_Diagonalizable.
split; auto.
exists (I n), (I n), A.
split.
split; auto.
split; auto with wf_db.
split; auto with wf_db.
rewrite Mmult_1_l; auto with wf_db.
rewrite Mmult_1_l; auto with wf_db.
rewrite Mmult_1_r; auto with wf_db.
Qed.
Lemma diagble_Zero : forall n : nat, WF_Diagonalizable (@Zero n n).
Proof. intros. apply diag_imps_diagble.
apply diag_Zero.
Qed.
Lemma diagble_I : forall n : nat, WF_Diagonalizable (I n).
Proof. intros. apply diag_imps_diagble.
apply diag_I.
Qed.
Lemma diagble_I1 : WF_Diagonal (I 1). Proof. apply diag_I. Qed.
Lemma diagble_scale : forall {n : nat} (r : C) (A : Square n),
WF_Diagonalizable A -> WF_Diagonalizable (r .* A).
Proof.
intros n r A [H H0].
split; auto with wf_db.
do 3 (destruct H0).
destruct H0 as [H1 [H2 [H3 [H4 H5] ] ] ].
exists x, x0, (r .* x1); split.
apply diag_scale; apply H1.
split; try easy.
split; try easy.
split.
apply H4.
rewrite Mscale_mult_dist_r;
rewrite Mscale_mult_dist_l.
rewrite H5.
reflexivity.
Qed.
Lemma diagble_switch : forall {n : nat} (A B X X' : Square n),
WF_Matrix A -> WF_Matrix X -> WF_Matrix X' ->
X × X' = I n -> B = X × A × X' ->
A = X' × B × X.
Proof. intros.
rewrite H3.
repeat rewrite <- Mmult_assoc.
apply Minv_flip in H2; auto.
rewrite H2, Mmult_1_l; auto.
rewrite Mmult_assoc.
rewrite H2, Mmult_1_r; auto.
Qed.
(**************************************)
(** * Defining Cprod, similar to big_sum *)
(**************************************)
(* could define this using the multiplicative monoid on C, but this would
lead to confusing notation, so I just left it *)
Fixpoint Cprod (f : nat -> C) (n : nat) : C :=
match n with
| 0 => C1
| S n' => (Cprod f n' * f n')%C
end.
Lemma Cprod_1_bounded : forall (f : nat -> C) (n : nat),
(forall i, i < n -> f i = C1) -> Cprod f n = C1.
Proof. intros.
induction n as [| n'].
- easy.
- simpl.
rewrite H, IHn'; try lca.
intros.
apply H; lia.
lia.
Qed.
Lemma Cprod_0_bounded : forall (f : nat -> C) (n : nat),
(exists i, i < n /\ f i = C0) -> Cprod f n = C0.
Proof. intros.
induction n as [| n'].
- destruct H; lia.
- destruct H as [i [H1 H2] ].
bdestruct (i <? n'); bdestruct (i =? n'); try lia.
+ simpl. rewrite IHn'; try lca.
exists i. easy.
+ simpl. rewrite <- H0.
rewrite H2; lca.
Qed.
Lemma Cprod_neq_0_bounded : forall (f : nat -> C) (n : nat),
(forall i, i < n -> f i <> C0) -> Cprod f n <> C0.
Proof. induction n; intros; simpl.
apply C1_neq_C0.
apply Cmult_neq_0.
apply IHn; intros.
all : apply H; auto.
Qed.
Lemma Cprod_eq_bounded : forall (f g : nat -> C) (n : nat),
(forall i : nat, i < n -> f i = g i) -> Cprod f n = Cprod g n.
Proof. intros.
induction n as [| n'].
- easy.
- simpl.
rewrite IHn', H; try lia; try easy.
intros. apply H; lia.
Qed.
Lemma Cprod_extend_r : forall (f : nat -> C) (n : nat),
(Cprod f n * f n)%C = Cprod f (S n).
Proof. easy. Qed.
Lemma Cprod_extend_l : forall (f : nat -> C) (n : nat),
(f 0 * (Cprod (fun x => f (S x)) n))%C = Cprod f (S n).
Proof. intros.
induction n.
+ simpl; lca.
+ simpl.
rewrite Cmult_assoc.
rewrite IHn.
simpl.
reflexivity.
Qed.
Lemma Cprod_product : forall (f g h : nat -> C) (n : nat),
(forall i, i < n -> h i = (f i * g i)%C) -> ((Cprod f n) * (Cprod g n))%C = Cprod h n.
Proof. induction n.
+ intros. lca.
+ intros. simpl.
rewrite <- IHn, H; try lca; try lia.
intros. apply H; try lia.
Qed.
(************************************)
(** * Defining upper triangular matrix *)
(************************************)
Definition upper_triangular {n} (A : Square n) : Prop :=
forall i j, i > j -> A i j = C0.
Lemma up_tri_Zero : forall n : nat, upper_triangular (@Zero n n).
Proof. intros n. unfold upper_triangular. reflexivity. Qed.
Lemma up_tri_I : forall n : nat, upper_triangular (I n).
Proof.
unfold upper_triangular, I; intros.
bdestruct (i =? j); try lia; easy.
Qed.
Lemma up_tri_I1 : upper_triangular (I 1). Proof. apply up_tri_I. Qed.
Lemma up_tri_scale : forall {n : nat} (r : C) (A : Square n),
upper_triangular A -> upper_triangular (r .* A).
Proof.
unfold upper_triangular, scale.
intros.
rewrite H; try lca; easy.
Qed.
Lemma up_tri_col_scale_many : forall {n} (A : Square n) (as' : Matrix 1 n),
upper_triangular A -> upper_triangular (col_scale_many A as').
Proof. intros.
unfold col_scale_many, upper_triangular; intros.
rewrite H; auto; lca.
Qed.
Lemma up_tri_plus : forall {n} (A B : Square n),
upper_triangular A -> upper_triangular B -> upper_triangular (A .+ B).
Proof.
unfold upper_triangular, Mplus.
intros n A B H H0 i j H1.
rewrite H, H0; try lca; easy.
Qed.
Lemma up_tri_mult : forall {n : nat} (A B : Square n),
upper_triangular A -> upper_triangular B -> upper_triangular (A × B).
Proof.
unfold upper_triangular, Mmult.
intros n A B H H0 i j D.
apply (@big_sum_0 C C_is_monoid).
intros x.
bdestruct (x <? i); bdestruct (j <? x); try lia.
+ rewrite H; try lca; lia.
+ rewrite H; try lca; lia.
+ rewrite H0; try lca; lia.
Qed.
Lemma up_tri_get_minor_0 : forall {n : nat} (A : Square (S n)),
upper_triangular A -> upper_triangular (get_minor A 0 0).
Proof.
unfold upper_triangular, get_minor.
intros.
bdestruct (i <? 0); bdestruct (j <? 0); try lia.
apply H; lia.
Qed.
Lemma det_up_tri_diags : forall {n : nat} (A : Square n),
upper_triangular A ->
Determinant A = Cprod (fun i => A i i) n.
Proof. induction n as [| n'].
- easy.
- intros. simpl.
destruct n' as [| n''].
+ lca.
+ assert (H' : (Cprod (fun i : nat => A i i) (S n'') * A (S n'') (S n'') =
A 0 0 * Cprod (fun i : nat => (get_minor A 0 0) i i) (S n''))%C).
{ rewrite <- Cprod_extend_l.
rewrite <- Cprod_extend_r.
rewrite <- Cmult_assoc; easy. }
rewrite H'.
rewrite <- big_sum_extend_l.
rewrite <- Cplus_0_r.
rewrite <- Cplus_assoc.
apply Cplus_simplify.
simpl parity.
rewrite IHn'; try lca.
apply up_tri_get_minor_0; easy.
unfold upper_triangular in H.
rewrite H; try lia.
rewrite <- Cplus_0_r.
apply Cplus_simplify; try lca.
apply (@big_sum_0_bounded C C_is_monoid).
intros.
rewrite H; try lia; lca.
Qed.
Lemma up_tri_get_minor_upper_half : forall {n : nat} (A : Square (S n)) (i j : nat),
i < j -> upper_triangular A ->
upper_triangular (get_minor A i j).
Proof. intros.
unfold upper_triangular, get_minor.
intros.
bdestruct_all; apply H0; try lia.
Qed.
Lemma up_tri_adjugate : forall {n : nat} (A : Square n),
upper_triangular A -> upper_triangular (adjugate A).
Proof. intros.
unfold adjugate, upper_triangular; intros.
destruct n; auto.
bdestruct_all; simpl; auto.
rewrite det_up_tri_diags.
rewrite Cprod_0_bounded.
lca.
exists j; split.
lia.
unfold get_minor.
bdestruct_all.
rewrite H; auto; lia.
apply up_tri_get_minor_upper_half; auto.
Qed.
Lemma up_tri_inverse : forall {n : nat} (A : Square n),
upper_triangular A -> upper_triangular (Minverse A).
Proof. intros.
unfold Minverse.
apply up_tri_scale.
apply up_tri_adjugate.
auto.
Qed.
Definition unit_upper_triangular {n} (A : Square n) : Prop :=
upper_triangular A /\ forall i, i < n -> A i i = C1.
Lemma unit_up_tri_I : forall n : nat, unit_upper_triangular (I n).
Proof.
split.
apply up_tri_I.
intros.
unfold I.
bdestruct_all; easy.
Qed.
Lemma unit_up_tri_mult : forall {n : nat} (A B : Square n),
unit_upper_triangular A -> unit_upper_triangular B -> unit_upper_triangular (A × B).
Proof.
intros n A B [H H0] [H1 H2]; split.
apply up_tri_mult; auto.
intros.
unfold Mmult.
rewrite (big_sum_unique C1); auto.
exists i; split; auto; split.
rewrite H0, H2; auto; lca.
intros.
bdestruct (x' <? i); bdestruct (i <? x'); try lia.
rewrite H; auto; lca.
rewrite H1; auto; lca.
Qed.
Lemma unit_up_tri_det_1 : forall {n : nat} (A : Square n),
unit_upper_triangular A ->
Determinant A = C1.
Proof. intros.
rewrite det_up_tri_diags.
rewrite Cprod_1_bounded; auto.
intros.
destruct H.
rewrite H1; auto.
apply H.
Qed.
(*****************************************************************************************)
(** * Explicitly Constructing the QR factorization of an invertible matrix *)
(*****************************************************************************************)
(* proj of v onto u *)
Definition proj {n} (u v : Vector n) : Vector n :=
((inner_product u v) / (inner_product u u)) .* u.
Definition proj_coef {n} (u v : Vector n) : C :=
((inner_product u v) / (inner_product u u)).
Lemma proj_inner_product : forall {n} (u v : Vector n),
WF_Matrix u -> inner_product u (proj u v) = inner_product u v.
Proof. intros.
destruct (mat_equiv_dec u Zero).
- unfold inner_product, Mmult, adjoint, proj.
repeat rewrite big_sum_0_bounded; auto.
all : try intros; rewrite m; auto; lca.
- unfold proj, inner_product.
distribute_scale.
unfold scale.
unfold Cdiv.
rewrite <- Cmult_assoc.
rewrite Cinv_l.
lca.
apply inner_product_zero_iff_zero in H.
contradict n0.
unfold norm, inner_product in H.
apply H in n0.
rewrite n0. easy.
Qed.
(*****************************************************************************************)
(** * Defining and verifying the gram_schmidt algorythm and proving v can be part of an onb *)
(*****************************************************************************************)
Definition gram_schmidt_single_col {n} (T : Square n) (i : nat) : Square n :=
fun x y => if (y =? i) && (x <? i)
then - (proj_coef (get_col T x) (get_col T i))
else I n x y.
Fixpoint gram_schmidt_until_i {n} (T : Square n) (i : nat) : Square n :=
match i with
| O => I n
| S i => (gram_schmidt_until_i T i) ×
gram_schmidt_single_col (T × (gram_schmidt_until_i T i)) (S i)
end.
Definition gram_schmidt {n} (T : Square n) : Square n :=
T × gram_schmidt_until_i T (n - 1).
(* this definition makes the above easier to work with *)
Definition gram_schmidt_on_col {n : nat} (T : Square n) (i : nat) :=
(big_sum (fun j => -C1 .* (proj (get_col T j) (get_col T i))) i) .+ (get_col T i).
Lemma WF_gssc : forall {n} (T : Square n) i,
i < n -> WF_Matrix (gram_schmidt_single_col T i).
Proof. intros.
unfold gram_schmidt_single_col, WF_Matrix, I; intros.
bdestruct_all; easy.
Qed.
Lemma WF_gsoc : forall {n} (T : Square n) i,
i < n -> WF_Matrix T -> WF_Matrix (gram_schmidt_on_col T i).
Proof. intros.
unfold gram_schmidt_on_col.
apply WF_plus; auto with wf_db.
apply WF_Msum; intros.
unfold proj.
auto with wf_db.
Qed.
Lemma WF_gsui : forall {n} (T : Square n) i,
i < n -> WF_Matrix T -> WF_Matrix (gram_schmidt_until_i T i).
Proof. induction i; intros.
simpl; auto with wf_db.
simpl.
apply WF_mult.
apply IHi; auto; lia.
apply WF_gssc; auto.
Qed.
Lemma WF_gram_schmidt : forall {n} (T : Square n),
WF_Matrix T -> WF_Matrix (gram_schmidt T).
Proof. intros.
destruct n.
- unfold gram_schmidt; simpl.
auto with wf_db.
- unfold gram_schmidt.
apply WF_mult; auto.
apply WF_gsui; auto; lia.
Qed.
Lemma unit_upper_triangular_gsui : forall {n} (T : Square n) i,
unit_upper_triangular (gram_schmidt_until_i T i).
Proof. induction i.
intros; simpl.
split.
apply up_tri_I.
unfold I; intros; bdestruct_all; lca.
intros; simpl.
apply unit_up_tri_mult.
apply IHi; lia.
split; unfold upper_triangular, gram_schmidt_single_col, I; intros;
bdestruct_all; simpl; auto.
Qed.
Lemma gram_schmidt_single_col_hit : forall {n} (T : Square n) (i : nat),
WF_Matrix T -> i < n ->
get_col (T × gram_schmidt_single_col T i) i = gram_schmidt_on_col T i.
Proof. intros.
apply mat_equiv_eq.
auto with wf_db.
apply WF_get_col; apply WF_mult; auto.
apply WF_gssc; auto.
apply WF_gsoc; auto.
unfold mat_equiv; intros.
rewrite <- get_col_mult.
unfold Mmult, get_col, gram_schmidt_single_col, gram_schmidt_on_col.
unfold get_col, Mplus, proj, proj_coef, scale.
bdestruct_all.
rewrite Msum_Csum.
replace n with (i + (n-i)) by lia.
rewrite big_sum_sum.
apply f_equal_gen; try apply f_equal.
apply big_sum_eq_bounded; intros.
bdestruct_all; simpl; lca.
apply lt_minus_O_lt in H0.
destruct (n - i); try lia.
rewrite <- big_sum_extend_l.
bdestruct_all; simpl.
replace (big_sum _ _) with C0.
unfold I; bdestruct_all; simpl.
rewrite <- plus_n_O; lca.
rewrite big_sum_0_bounded; auto.
intros.
unfold I; bdestruct_all; simpl; lca.
Qed.
Lemma gram_schmidt_single_col_miss : forall {n} (T : Square n) (i j : nat),
WF_Matrix T -> j < n -> i <> j ->
get_col (T × gram_schmidt_single_col T i) j = get_col T j.
Proof. intros.
prep_matrix_equality.
unfold get_col, gram_schmidt_single_col, Mmult, I.
bdestruct_all; auto.
rewrite (big_sum_unique (T x j)); auto.
exists j; split; auto; split.
simpl; bdestruct_all; lca.
intros.
simpl. bdestruct_all; lca.
Qed.
Lemma gram_schmidt_single_col_ver : forall {n} (T : Square n) (i : nat),
WF_Matrix T -> i < n ->
(forall j k, j < i -> k < i -> j <> k -> inner_product (get_col T j) (get_col T k) = C0) ->
(forall j, j < i -> inner_product (get_col T j) (get_col (T × (gram_schmidt_single_col T i)) i) = C0).
Proof. intros.
rewrite gram_schmidt_single_col_hit; auto.
unfold gram_schmidt_on_col.
rewrite inner_product_plus_r, inner_product_big_sum_r.
erewrite (big_sum_unique _).
2 : exists j; split; auto; split.
2 : reflexivity.
rewrite inner_product_scale_r, proj_inner_product; auto with wf_db.
lca.
intros.
unfold proj.
rewrite 2 inner_product_scale_r, (H1 j x'); auto.
lca.
Qed.
Lemma gram_schmidt_until_i_ver : forall {n} (i j k : nat) (T : Square n),
WF_Matrix T -> i < n -> j <= i -> k <= i -> j <> k ->
inner_product
(get_col (T × gram_schmidt_until_i T i) j)
(get_col (T × gram_schmidt_until_i T i) k) = C0.
Proof. induction i; intros.
destruct j; destruct k; lia.
bdestruct (k <? S i).
- simpl.
rewrite <- Mmult_assoc.
rewrite (gram_schmidt_single_col_miss (T × gram_schmidt_until_i T i) _ k); try lia.
bdestruct (j <? S i).
+ rewrite gram_schmidt_single_col_miss; try lia.
apply IHi; auto; try lia.
apply WF_mult; auto.
apply WF_gsui; auto; lia.
+ bdestruct (j =? S i); try lia; subst.
rewrite inner_product_conj_sym.
rewrite (gram_schmidt_single_col_ver (T × gram_schmidt_until_i T i)); try lia.
lca.
apply WF_mult; auto; apply WF_gsui; auto; lia.
intros.
apply IHi; auto; lia.
+ apply WF_mult; auto; apply WF_gsui; auto; lia.
- bdestruct (k =? S i); try lia; subst.
simpl.
rewrite <- Mmult_assoc.
rewrite gram_schmidt_single_col_miss; try lia.
rewrite gram_schmidt_single_col_ver; auto; try lia.
apply WF_mult; auto; apply WF_gsui; auto; lia.
intros.
apply IHi; auto; try lia.
apply WF_mult; auto; apply WF_gsui; auto; lia.
Qed.
Theorem gram_schmidt_ver : forall {n} (T : Square n),
WF_Matrix T -> orthogonal (gram_schmidt T).
Proof. intros.
destruct n.
- unfold orthogonal, gram_schmidt; intros; simpl; lca.
- unfold orthogonal, gram_schmidt; intros.
replace (S n - 1) with n by lia.
bdestruct (i <? (S n)).
bdestruct (j <? (S n)).
rewrite gram_schmidt_until_i_ver; auto; lia.
rewrite (get_col_over _ j); try lia.
3 : rewrite get_col_over; try lia.
all : try (unfold inner_product, Mmult, adjoint;
rewrite big_sum_0_bounded; auto; intros; lca).
all : apply WF_mult; try apply WF_gsui; auto.
Qed.
Lemma gs_on_lin_indep_nonzero_cols : forall {n} (T : Square n) (i : nat),
WF_Matrix T ->
linearly_independent T -> i < n ->
get_col (gram_schmidt T) i <> Zero.
Proof. intros.
apply lin_indep_det_neq_0 in H0; auto.
destruct H0.
contradict H2.
apply col_0_Det_0 in H2; auto.
unfold gram_schmidt in H2.
rewrite <- Determinant_multiplicative in H2.
rewrite (unit_up_tri_det_1 (gram_schmidt_until_i T (n - 1))) in H2.
rewrite <- H2; lca.
apply unit_upper_triangular_gsui.
Qed.
Definition normalize_cols_scalars {n} (T : Square n) : Matrix 1 n :=
fun i j => if (i =? 0) && (j <? n) then
/ norm (get_col T j) else C0.
Lemma orthogonal_nonzero_cols_implies_orthonomalizable : forall {n} (T : Square n),
WF_Matrix T -> orthogonal T ->
(forall i, i < n -> get_col T i <> Zero) ->
WF_Orthonormal (col_scale_many T (normalize_cols_scalars T)).
Proof. intros.
split; auto with wf_db.
split.
unfold orthogonal; intros.
rewrite 2 get_col_col_scale_many.
rewrite inner_product_scale_l, inner_product_scale_r.
rewrite H0; auto; lca.
intros.
rewrite get_col_col_scale_many.
unfold normalize_cols_scalars.
bdestruct_all; simpl.
apply H1 in H2.
apply norm_nonzero_iff_nonzero in H2; auto with wf_db.
apply normalized_norm_1 in H2.
easy.
Qed.
(* messy, but the important part is the properties *)
Definition QR_factorization_R_inv {n} (T : Square n) :=
(col_scale_many (gram_schmidt_until_i T (n - 1))
(normalize_cols_scalars (T × gram_schmidt_until_i T (n - 1)))).
Definition QR_factorization_R {n} (T : Square n) :=
Minverse (QR_factorization_R_inv T).
Definition QR_factorization_Q {n} (T : Square n) :=
T × (QR_factorization_R_inv T).
Lemma WF_Matrix_R_inv : forall {n} (T : Square n),
WF_Matrix T -> WF_Matrix (QR_factorization_R_inv T).
Proof. intros.
unfold QR_factorization_R_inv.
apply WF_col_scale_many.
destruct n.
- simpl. auto with wf_db.
- apply WF_gsui; auto; try lia.
Qed.
Lemma WF_Matrix_R : forall {n} (T : Square n),
WF_Matrix (QR_factorization_R T).
Proof. intros.
unfold QR_factorization_R, Minverse.
apply WF_scale; apply WF_adjugate.
Qed.
Lemma WF_Matrix_Q : forall {n} (T : Square n),
WF_Matrix T -> WF_Matrix (QR_factorization_Q T).
Proof. intros.
unfold QR_factorization_Q, QR_factorization_R_inv.
destruct n; try easy.
apply WF_mult; auto.
apply WF_col_scale_many.
apply WF_gsui; auto.
lia.
Qed.
#[export] Hint Resolve WF_Matrix_R_inv WF_Matrix_R WF_Matrix_Q : wf_db.
Lemma R_inv_upper_tri : forall {n} (T : Square n),
upper_triangular (QR_factorization_R_inv T).
Proof. intros.
unfold QR_factorization_R_inv.
apply up_tri_col_scale_many.
apply unit_upper_triangular_gsui.
Qed.
Lemma R_upper_tri : forall {n} (T : Square n),
upper_triangular (QR_factorization_R T).
Proof. intros.
unfold QR_factorization_R.
apply up_tri_inverse.
apply R_inv_upper_tri.
Qed.
Lemma R_inv_det_neq_0 : forall {n} (T : Square n),
WF_Matrix T -> linearly_independent T ->
Determinant (QR_factorization_R_inv T) <> C0.
Proof. intros.
rewrite det_up_tri_diags.
apply Cprod_neq_0_bounded; intros.
unfold QR_factorization_R_inv, col_scale_many, normalize_cols_scalars.
assert (H2 := unit_upper_triangular_gsui T (n-1)).
destruct H2; rewrite H3; auto.
bdestruct_all; simpl.
apply Cmult_neq_0.
apply nonzero_div_nonzero.
apply (gs_on_lin_indep_nonzero_cols T i) in H0; auto.
apply norm_nonzero_iff_nonzero in H0.
contradict H0.
apply RtoC_inj in H0.
easy.
apply WF_get_col; apply WF_gram_schmidt; auto.
apply C1_neq_C0.
apply R_inv_upper_tri.
Qed.
Lemma Q_is_unitary : forall {n} (T : Square n),