forked from inQWIRE/QuantumLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEigenvectors.v
2020 lines (1838 loc) · 70.7 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 Quantum.
Require Import FTA.
(****************************)
(** * 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.
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.
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.
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_vec i S) (get_vec j S) = C0.
Definition orthonormal {n m} (S : Matrix n m) : Prop :=
orthogonal S /\ (forall (i : nat), i < m -> norm (get_vec i S) = 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 {n} (i j : nat) (S : Square n),
inner_product (get_vec i S) (get_vec j S) = (S† × S) i j.
Proof. intros. unfold inner_product, get_vec, Mmult, adjoint.
apply big_sum_eq.
apply functional_extensionality; intros. simpl.
reflexivity.
Qed.
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.
(***************************************************)
(** * showing that all matrices have some eigenvector *)
(***************************************************)
(* We first must define a new type to connect polynomials to matrices *)
Definition MatrixP (m n : nat) := nat -> nat -> Polynomial.
Notation SquareP n := (MatrixP n n).
Definition eval_matP {n m} (A : MatrixP n m) (c : C) : Matrix n m :=
fun x y => (A x y)[[c]].
Definition reduceP {n} (A : SquareP (S n)) (row col : nat) : SquareP n :=
fun x y => (if x <? row
then (if y <? col
then A x y
else A x (1+y))
else (if y <? col
then A (1+x) y
else A (1+x) (1+y))).
Lemma reduceP_eval_mat : forall {n} (A : SquareP (S n)) (c : C) (x y : nat),
reduce (eval_matP A c) x y = eval_matP (reduceP A x y) c.
Proof. intros.
prep_matrix_equality.
unfold reduce, eval_matP, reduceP.
bdestruct_all; easy.
Qed.
Fixpoint DeterminantP (n : nat) (A : SquareP n) : Polynomial :=
match n with
| 0 => [C1]
| S 0 => A 0 0
| S n' => (big_sum (fun i => [(parity i)] *, (A i 0) *, (DeterminantP n' (reduceP A i 0)))%C n)
end.
Arguments DeterminantP {n}.
Lemma DetP_simplify : forall {n} (A : SquareP (S (S n))),
DeterminantP A =
(big_sum (fun i => [(parity i)] *, (A i 0) *, (DeterminantP (reduceP A i 0)))%C (S (S n))).
Proof. intros. easy. Qed.
Lemma Peval_Det : forall {n} (A : SquareP n) (c : C),
Determinant (eval_matP A c) = (DeterminantP A)[[c]].
Proof. induction n as [| n'].
- intros; lca.
- intros.
destruct n'.
+ simpl. easy.
+ rewrite DetP_simplify, Det_simplify.
rewrite Psum_eval.
apply big_sum_eq_bounded; intros.
rewrite reduceP_eval_mat, IHn'.
do 2 rewrite Pmult_eval.
repeat apply f_equal_gen; try easy.
unfold Peval; lca.
Qed.
(* not really useful except for in the proof of connect *)
Definition prep_mat {n} (A : Square n) : SquareP n :=
(fun x y => if (x =? y) && (x <? n) then [A x y; -C1] else [A x y]).
(* we must first show that degree (DeterminantP (prep_mat A)) = n *)
Definition deg_elem_leq_1 {n} (A : SquareP n) : Prop :=
forall i j, degree (A i j) <= 1.
Lemma del1_reduce : forall {n} (A : SquareP (S n)) (i j : nat),
deg_elem_leq_1 A -> deg_elem_leq_1 (reduceP A i j).
Proof. unfold deg_elem_leq_1, reduceP in *; intros.
bdestruct_all; easy.
Qed.
Lemma bound_deg_matP : forall {n} (A : SquareP n),
deg_elem_leq_1 A -> degree (DeterminantP A) <= n.
Proof. induction n as [| n'].
- intros.
unfold degree, compactify; simpl.
destruct (Ceq_dec C1 C0); easy.
- intros.
destruct n'.
+ simpl.
apply H.
+ rewrite DetP_simplify.
apply Psum_degree; intros.
destruct (Peq_dec (A i 0) []).
rewrite p, Pmult_0_r.
unfold degree; simpl; lia.
destruct (Peq_dec (DeterminantP (reduceP A i 0)) []).
rewrite p, Pmult_0_r.
unfold degree; simpl; lia.
destruct (Peq_dec [parity i] []).
rewrite p.
unfold degree; simpl; lia.
destruct (Peq_dec ([parity i] *, A i 0) []).
rewrite p.
unfold degree; simpl; lia.
repeat rewrite Pmult_degree; auto.
assert (H' : degree [parity i] = 0).
{ unfold degree, compactify; simpl.
destruct (Ceq_dec (parity i) C0); easy. }
rewrite H', <- (Nat.add_1_l (S n')), Nat.add_0_l.
apply Nat.add_le_mono; auto.
apply IHn'.
apply del1_reduce; easy.
Qed.
(* we now prove prepmat is del1 *)
Lemma del1_prep_mat : forall {n} (A : Square n),
deg_elem_leq_1 (prep_mat A).
Proof. unfold deg_elem_leq_1, prep_mat; intros.
destruct ((i =? j) && (i <? n)).
all : unfold degree, compactify; simpl.
destruct (Ceq_dec (- C1) C0); simpl; try lia.
all : destruct (Ceq_dec (A i j) C0); simpl; lia.
Qed.
Lemma reduce_prep_mat : forall {n} (A : Square (S n)),
reduceP (prep_mat A) 0 0 = prep_mat (reduce A 0 0).
Proof. intros.
prep_matrix_equality.
unfold reduceP, reduce, prep_mat.
bdestruct_all; simpl; try easy.
Qed.
(* this got annoyingly long. Probably want to add some helper lemmas at some point *)
Lemma detP_deg : forall {n} (A : Square n),
degree (DeterminantP (prep_mat A)) = n.
Proof. induction n as [| n'].
- intros.
unfold degree, compactify; simpl.
destruct (Ceq_dec C1 C0); easy.
- intros.
destruct n'.
+ unfold degree, compactify; simpl.
destruct (Ceq_dec (- C1) C0); try easy.
assert (H' : - (- C1) = C0).
{ rewrite e; lca. }
replace (- - C1) with C1 in H' by lca.
apply C1_neq_C0 in H'; easy.
+ rewrite DetP_simplify.
assert (H' : forall n f, big_sum f (S n) = f 0 +, big_sum (fun i => f (S i)) n).
{ intros.
induction n.
+ simpl.
destruct (f 0); try easy; simpl.
+ simpl in *.
rewrite IHn, Pplus_assoc; easy. }
assert (H0 : degree (prep_mat A 0 0) = 1).
{ unfold prep_mat.
bdestruct_all; simpl.
unfold degree, compactify; simpl.
destruct (Ceq_dec (- C1) C0); try easy.
assert (H'' := C1_neq_C0).
replace C1 with (-C1 * -C1)%C in H'' by lca.
rewrite e, Cmult_0_l in H''; easy. }
assert (H1 : degree ([parity 0] *, prep_mat A 0 0 *,
DeterminantP (reduceP (prep_mat A) 0 0)) = S (S n')).
{ simpl parity.
rewrite Pmult_1_l, Pmult_degree, reduce_prep_mat, H0, IHn'.
easy.
destruct (Peq_dec (prep_mat A 0 0) []); auto.
rewrite p in H0; easy.
destruct (Peq_dec (DeterminantP (reduceP (prep_mat A) 0 0)) []); auto.
rewrite reduce_prep_mat in *.
assert (H1 := (IHn' (reduce A 0 0))).
rewrite p in H1; easy. }
rewrite H', Pplus_comm, Pplus_degree2; auto.
rewrite H1.
apply le_lt_n_Sm.
apply Psum_degree; intros.
assert (H2 : prep_mat A (S i) 0 = [A (S i) 0]).
{ unfold prep_mat.
bdestruct_all; easy. }
rewrite H2.
replace ([parity (S i)] *, [A (S i) 0]) with [parity (S i) * A (S i) 0]%C.
destruct (Peq_dec [(parity (S i) * A (S i) 0)%C] []).
rewrite p; simpl.
unfold degree, compactify; simpl; try lia.
destruct (Peq_dec (DeterminantP (reduceP (prep_mat A) (S i) 0)) []).
rewrite p, Pmult_0_r.
unfold degree, compactify; simpl; try lia.
rewrite Pmult_degree; auto.
rewrite <- Nat.add_0_l.
apply Nat.add_le_mono.
destruct (parity (S i) * A (S i) 0)%C eqn:E.
unfold degree, compactify; simpl.
destruct (Ceq_dec (r, r0) C0); simpl; lia.
apply bound_deg_matP.
apply del1_reduce.
apply del1_prep_mat.
simpl; rewrite Cplus_0_r. easy.
Qed.
Lemma connect : forall (n : nat) (A : Square (S n)),
exists (p : Polynomial), (Polynomial.degree p) > 0 /\
(forall c : C, Determinant (A .+ (-c .* I (S n))) = p[[c]]).
Proof. intros.
exists (DeterminantP (prep_mat A)).
split; intros.
rewrite detP_deg; lia.
rewrite <- Peval_Det.
apply f_equal_gen; try easy.
prep_matrix_equality.
unfold prep_mat, eval_matP, Peval, I, Mplus, scale.
bdestruct_all; simpl; lca.
Qed.
Lemma connect2 : forall (n : nat) (A : Square (S n)),
exists (c : C), det_eq_c C0 (A .+ (-c .* I (S n))).
Proof. intros.
destruct (connect n A) as [p [H H0] ].
destruct (Fundamental_Theorem_Algebra p); auto.
exists x.
split; auto.
rewrite H0; easy.
Qed.
Lemma exists_eigenvector : forall (n : nat) (A : Square (S n)),
WF_Matrix A ->
exists (c : C) (v : Vector (S n)), WF_Matrix v /\ v <> Zero /\ A × v = c.* v.
Proof. intros.
destruct (connect2 n A) as [c H0].
apply lin_dep_det_eq_0 in H0; auto with wf_db.
destruct H0 as [v [H1 [H2 H3] ] ].
exists c, v.
split; auto.
split; auto.
rewrite Mmult_plus_distr_r, Mscale_mult_dist_l, Mmult_1_l in H3; auto.
assert (H4 : A × v .+ (-c .* v) .+ (c .* v) = (c .* v)).
{ rewrite H3. lma. }
rewrite Mplus_assoc in H4.
rewrite <- Mscale_plus_distr_l in H4.
replace (-c + c)%C with C0 in H4 by lca.
rewrite <- H4.
lma.
Qed.
(************************************)
(** * Lemmas relating to forming bases *)
(************************************)
Definition form_basis {n} (v : Vector n) (non_zero : nat) : Matrix n n :=
fun x y => if (y =? non_zero)
then (v x 0)
else (@e_i n y x 0).
Lemma WF_form_basis : forall {n} (v : Vector n) (x : nat),
WF_Matrix v -> x < n -> WF_Matrix (form_basis v x).
Proof. unfold WF_Matrix, form_basis, e_i.
intros.
bdestruct (y =? x).
apply H.
destruct H1; auto; lia.
bdestruct (x0 =? y); try easy.
bdestruct (x0 <? n); try lia; try easy.
Qed.
Lemma get_v_in_basis : forall {n} (v : Vector n) (x : nat),
WF_Matrix v -> get_vec x (form_basis v x) = v.
Proof. intros.
prep_matrix_equality.
unfold get_vec, form_basis.
bdestruct (y =? 0).
rewrite <- beq_nat_refl, H0; easy.
unfold WF_Matrix in H.
rewrite H; try easy.
right.
destruct y; try lia; try easy.
Qed.
Lemma get_ei_in_basis : forall {n} (v : Vector n) (x y : nat),
y < n -> y <> x -> get_vec y (form_basis v x) = e_i y.
Proof. intros.
prep_matrix_equality.
unfold get_vec, form_basis.
bdestruct (y0 =? 0).
bdestruct (y =? x); try easy.
rewrite H1; easy.
unfold e_i.
bdestruct (x0 =? y); bdestruct (x0 <? n); bdestruct (y0 =? 0); try easy.
Qed.
Lemma form_basis_ver : forall {n} (v : Vector n) (x : nat),
v <> Zero -> WF_Matrix v -> v x 0 <> C0 -> x < n ->
linearly_independent (form_basis v x) /\ get_vec x (form_basis v x) = v.
Proof. intros.
destruct n; try lia. split.
- apply (mat_prop_col_add_many_conv _ _ x (-C1 .* (make_row_zero x v)));
try easy; auto with invr_db.
unfold scale, make_row_zero.
bdestruct (x =? x); try lia; lca.
apply (mat_prop_col_scale_conv _ _ x (/ (v x 0))); auto with invr_db.
apply nonzero_div_nonzero; easy.
assert (H' : forall A : Square (S n), A = I (S n) -> linearly_independent A).
{ intros. rewrite H3.
apply lin_indep_invertible; auto with wf_db.
unfold invertible. exists (I (S n)).
unfold Minv.
split; rewrite Mmult_1_l; auto with wf_db. }
apply H'.
apply mat_equiv_eq; auto with wf_db.
apply WF_col_scale.
apply WF_col_add_many; try easy.
apply WF_form_basis; easy.
unfold mat_equiv; intros.
unfold col_scale, col_add_many, make_row_zero,
form_basis, scale, gen_new_vec, get_vec.
assert (H0' : forall a b : C, a = C0 -> (b + a = b)%C).
{ intros. rewrite H5. lca. }
bdestruct (j =? x); bdestruct (j =? i).
all : try rewrite Msum_Csum.
all : try unfold scale.
rewrite H5 in *. rewrite <- H6.
rewrite H0'.
unfold I.
bdestruct (x =? x); bdestruct (x <? S n); try lia.
rewrite Cinv_l; try easy.
rewrite big_sum_0_bounded; try easy.
unfold e_i.
intros.
bdestruct (x0 =? x); try lia; try lca.
bdestruct (x =? x0); try lia; lca.
rewrite (big_sum_unique (-C1 * (v i 0))%C).
unfold I. bdestruct (i =? j); try lia; simpl.
lca. exists i. split; try easy.
split. simpl.
rewrite H5 in *.
bdestruct (i =? x); try lia.
unfold e_i.
bdestruct (i =? i); bdestruct (i <? S n); simpl; try lia; lca.
intros.
bdestruct (x' =? x); try lca.
simpl; unfold e_i.
bdestruct (i =? x'); simpl; try lia; lca.
rewrite H6.
all : unfold e_i, I.
all : bdestruct (i =? i); bdestruct (i <? S n); simpl; try lia; try easy.
bdestruct (i =? j); easy.
- apply get_v_in_basis; easy.
Qed.
Lemma lin_indep_out_of_v : forall {n} (v : Vector n),
WF_Matrix v -> v <> Zero ->
exists S : Square n, WF_Matrix S /\ linearly_independent S /\ get_vec 0 S = v.
Proof. intros.
destruct n.
- exists Zero.
split. easy.
split.
unfold linearly_independent.
intros. unfold WF_Matrix in H1.
prep_matrix_equality.
apply H1; lia.
prep_matrix_equality.
unfold get_vec, Zero.
unfold WF_Matrix in H.
rewrite H; try lia.
bdestruct (y =? 0); easy.
- assert (H' := H).
apply nonzero_vec_nonzero_elem in H'; try easy.
destruct H'.
exists (col_swap (form_basis v x) x 0).
assert (H' : x < S n).
{ bdestruct (x <? S n); try easy.
unfold WF_Matrix in H.
unfold not in H1.
assert (H' : v x 0 = C0).
{ apply H. lia. }
easy. }
assert (H'' : linearly_independent (form_basis v x) /\ get_vec x (form_basis v x) = v).
{ apply form_basis_ver; try easy. }
split.
apply WF_col_swap; try lia; try easy.
apply WF_form_basis; easy.
split.
+ apply_mat_prop lin_indep_swap_invr.
apply H3; try lia.
easy.
+ rewrite col_swap_diff_order.
rewrite <- (col_swap_get_vec _ 0 x).
apply get_v_in_basis.
easy.
Qed.
(*****************************************************************************************)
(** * Defining and verifying the gram_schmidt algorythm and proving v can be part of an onb *)
(*****************************************************************************************)
(* 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),
(norm u) <> 0%R -> inner_product u (proj u v) = inner_product u v.
Proof. intros.
unfold proj, inner_product.
distribute_scale.
unfold scale.
unfold Cdiv.
rewrite <- Cmult_assoc.
rewrite Cinv_l.
lca.
unfold norm, inner_product in H.
intro. apply H.
rewrite H0. simpl.
rewrite sqrt_0.
easy.
Qed.
Definition gram_schmidt_on_v (n m : nat) (v : Vector n) (S : Matrix n m) :=
v .+ (big_sum (fun i => (-C1) .* (proj (get_vec i S) v)) m).
Definition delta_T {n m} (T : Matrix n (S m)) (i : nat) : C :=
match i =? m with
| true => C1
| _ => - (proj_coef (get_vec i T) (get_vec m T))
end.
(* slightly different version thats easier to work with in general case *)
Definition gram_schmidt_on_T (n m : nat) (T : Matrix n (S m)) : Vector n :=
big_sum (fun i => (delta_T T) i .* (get_vec i T)) (S m).
Lemma WF_gs_on_T : forall {n m} (T : Matrix n (S m)),
WF_Matrix T -> WF_Matrix (gram_schmidt_on_T n m T).
Proof. intros.
unfold gram_schmidt_on_T.
apply WF_Msum; intros.
apply WF_scale.
unfold get_vec, WF_Matrix in *; intros.
destruct H1.
- rewrite H; auto.
bdestruct (y =? 0); easy.
- bdestruct (y =? 0); try lia; try easy.
Qed.
Lemma gram_schmidt_compare : forall {n m} (T : Matrix n (S m)),
inner_product (get_vec m T) (get_vec m T) <> C0 ->
gram_schmidt_on_T n m T = gram_schmidt_on_v n m (get_vec m T) (reduce_col T m).
Proof. intros.
unfold gram_schmidt_on_T, gram_schmidt_on_v.
prep_matrix_equality.
unfold Mplus.
do 2 rewrite Msum_Csum.
rewrite Cplus_comm.
rewrite <- big_sum_extend_r.
apply Cplus_simplify.
- apply big_sum_eq_bounded.
intros.
unfold delta_T.
bdestruct (x0 =? m); try lia.
unfold proj, proj_coef.
distribute_scale.
assert (H' : get_vec x0 (reduce_col T m) = get_vec x0 T).
{ prep_matrix_equality;
unfold get_vec, reduce_col.
bdestruct (x0 <? m); try lia; easy. }
rewrite H'. unfold scale. lca.
- unfold delta_T.
bdestruct (m =? m); try lia.
unfold scale. lca.
Qed.
(* here, we show that gs_on_v preserves normalness *)
Lemma gram_schmidt_orthogonal : forall {n m} (v : Vector n) (S : Matrix n m) (i : nat),
orthonormal S -> i < m -> inner_product (get_vec i S) (gram_schmidt_on_v n m v S) = C0.
Proof. intros.
destruct H as [H H1].
unfold orthogonal in H.
unfold gram_schmidt_on_v.
rewrite inner_product_plus_r, inner_product_big_sum_r.
rewrite (big_sum_unique (-C1 * ⟨ get_vec i S, v ⟩)%C _ m); try lca.
exists i. split; try easy.
split.
- rewrite inner_product_scale_r.
rewrite proj_inner_product.
lca.
rewrite H1; auto; lra.
- intros.
unfold proj.
do 2 rewrite inner_product_scale_r.
apply H in H3.
rewrite H3.
lca.
Qed.
Definition f_to_vec (n : nat) (f : nat -> C) : Vector n :=
fun i j => if (j =? 0) && (i <? n) then f i else C0.
Lemma WF_f_to_vec : forall (n : nat) (f : nat -> C), WF_Matrix (f_to_vec n f).
Proof. intros.
unfold WF_Matrix, f_to_vec.
intros x y [H | H].
- bdestruct (y =? 0); bdestruct (x <? n); try lia; easy.
- bdestruct (y =? 0); bdestruct (x <? n); try lia; easy.
Qed.
Lemma Msum_to_Mmult : forall {n m} (T : Matrix n (S m)) (f : nat -> C),
big_sum (fun i => f i .* get_vec i T) (S m) = T × (f_to_vec (S m) f).
Proof. intros.
prep_matrix_equality.
rewrite Msum_Csum.
unfold Mmult.
apply big_sum_eq_bounded.
intros.
unfold f_to_vec, get_vec, scale.
bdestruct (x0 <? S m); bdestruct (y =? 0); try lia; try lca.
Qed.
(* here, we show gs_on_T is nonzero, true since T is lin indep *)
Lemma gram_schmidt_non_zero : forall {n m} (T : Matrix n (S m)),
linearly_independent T -> gram_schmidt_on_T n m T <> Zero.
Proof. intros.
unfold not, gram_schmidt_on_T; intros.
rewrite (Msum_to_Mmult T (delta_T T)) in H0.
unfold linearly_independent in H.
apply H in H0.
apply C1_neq_C0.
assert (H'' : f_to_vec (S m) (delta_T T) m 0 = C0).
{ rewrite H0. easy. }
rewrite <- H''.
unfold f_to_vec, delta_T.
bdestruct (m <? S m); bdestruct (m =? m); try lia; easy.
apply WF_f_to_vec.
Qed.
Lemma inner_product_zero_normalize : forall {n} (u v : Vector n),
inner_product u v = C0 -> inner_product u (normalize v) = C0.
Proof. intros.
unfold normalize in *.
rewrite inner_product_scale_r.
rewrite H.
lca.
Qed.
Lemma get_vec_reduce_append_miss : forall {n m} (T : Matrix n (S m)) (v : Vector n) (i : nat),
i < m -> get_vec i (col_append (reduce_col T m) v) = get_vec i T.
Proof. intros.
prep_matrix_equality.
unfold get_vec, col_append, reduce_col.
bdestruct_all; easy.
Qed.
Lemma get_vec_reduce_append_hit : forall {n m} (T : Matrix n (S m)) (v : Vector n),
WF_Matrix v -> get_vec m (col_append (reduce_col T m) v) = v.
Proof. intros.
unfold get_vec, col_append, reduce_col.
prep_matrix_equality.
bdestruct (y =? 0).
- bdestruct_all; subst; easy.
- rewrite H; try lia; easy.
Qed.
Lemma get_vec_reduce_append_over : forall {n m} (T : Matrix n (S m)) (v : Vector n) (i : nat),
WF_Matrix T -> i > m ->
get_vec i (col_append (reduce_col T m) v) = Zero.
Proof. intros.
prep_matrix_equality.
unfold get_vec, col_append, reduce_col.
bdestruct_all; try easy.
rewrite H. easy.
right. lia.
Qed.
Lemma extend_onb_ind_step_part1 : forall {n m} (T : Matrix n (S m)),
WF_Matrix T -> linearly_independent T -> orthonormal (reduce_col T m) ->
orthonormal (col_append (reduce_col T m) (normalize (gram_schmidt_on_T n m T))).
Proof. intros.
split.
- unfold orthogonal.
intros.
bdestruct (m <? i); bdestruct (m <? j); try lia.
+ rewrite get_vec_reduce_append_over; try easy.
unfold inner_product.
rewrite zero_adjoint_eq, Mmult_0_l.
easy.
+ rewrite get_vec_reduce_append_over; try easy.
unfold inner_product.
rewrite zero_adjoint_eq, Mmult_0_l.
easy.
+ rewrite (get_vec_reduce_append_over _ _ j); try easy.
unfold inner_product.
rewrite Mmult_0_r.
easy.
+ bdestruct (i =? m); bdestruct (j =? m); try lia.
* rewrite H5.
rewrite get_vec_reduce_append_hit.
bdestruct (j <? m); bdestruct (m <? j); try lia.
rewrite get_vec_reduce_append_miss; try easy.
rewrite inner_product_comm_conj.
apply Cconj_simplify.
rewrite Cconj_involutive, Cconj_0.
apply inner_product_zero_normalize.
rewrite gram_schmidt_compare.
apply (gram_schmidt_orthogonal (get_vec m T) _ j) in H1; try lia.
rewrite (@get_vec_reduce_col n m j m T) in H1; try lia.
apply H1.
assert (H' : WF_Matrix (get_vec m T)).
{ apply WF_get_vec; easy. }
apply inner_product_zero_iff_zero in H'.
destruct (Ceq_dec (inner_product (get_vec m T) (get_vec m T)) C0); try easy.
apply H' in e.
apply_mat_prop lin_indep_pzf.
apply H10 in H0; try easy.
exists m; split; try lia; easy.
unfold normalize.
apply WF_scale.
apply WF_gs_on_T; easy.
* rewrite H6.
rewrite get_vec_reduce_append_hit.
bdestruct (i <? m); bdestruct (m <? i); try lia.
rewrite get_vec_reduce_append_miss; try easy.
apply inner_product_zero_normalize.
rewrite gram_schmidt_compare.
apply (gram_schmidt_orthogonal (get_vec m T) _ i) in H1; try lia.
rewrite (@get_vec_reduce_col n m i m T) in H1; try lia.
apply H1.
assert (H' : WF_Matrix (get_vec m T)).
{ apply WF_get_vec; easy. }
apply inner_product_zero_iff_zero in H'.
destruct (Ceq_dec (inner_product (get_vec m T) (get_vec m T)) C0); try easy.
apply H' in e.
apply_mat_prop lin_indep_pzf.
apply H10 in H0; try easy.
exists m; split; try lia; easy.
unfold normalize.
apply WF_scale.
apply WF_gs_on_T; easy.
* bdestruct (i <? m); bdestruct (j <? m); try lia.
rewrite get_vec_reduce_append_miss; try easy.
rewrite get_vec_reduce_append_miss; try easy.
unfold orthonormal in H1.
destruct H1 as [H1 _].
unfold orthogonal in H1.
apply (@get_vec_reduce_col n m i m T) in H7.
apply (@get_vec_reduce_col n m j m T) in H8.
apply H1 in H2.
rewrite H7, H8 in H2; easy.
- intros.
bdestruct (i =? m); bdestruct (i <? m); try lia.
+ rewrite H3.
rewrite get_vec_reduce_append_hit.
apply normalized_norm_1.
assert (H' := H).
apply WF_gs_on_T in H'.
apply norm_zero_iff_zero in H'.
destruct (Req_EM_T (norm (gram_schmidt_on_T n m T)) 0%R); try easy.
apply H' in e.
apply gram_schmidt_non_zero in H0; easy.
unfold normalize.
apply WF_scale.
apply WF_gs_on_T; easy.
+ destruct H1 as [_ H1].
rewrite get_vec_reduce_append_miss; try lia.
rewrite <- (@get_vec_reduce_col n m i m T); try lia.
apply H1; lia.
Qed.
Definition delta_T' {n m} (T : Matrix n m) (v : Vector n) (size : nat) : nat -> C :=
fun i => if (i <? size)
then - (proj_coef (get_vec i T) v)
else C0.
Lemma gs_on_T_cols_add : forall {n m1 m2} (T1 : Matrix n m1) (T2 : Matrix n m2) (v : Vector n),
WF_Matrix v ->
smash (col_append T1 (gram_schmidt_on_T n m1 (col_append T1 v))) T2 =
@col_add_many n ((S m1) + m2) m1 (f_to_vec (m1 + m2) (delta_T' T1 v m1))
(smash (col_append T1 v) T2).
Proof. intros.
prep_matrix_equality.
unfold smash, col_append, gram_schmidt_on_T, col_add_many.
bdestruct (y <? S m1); bdestruct (y =? m1); try lia; try easy.
unfold delta_T, delta_T', gen_new_vec, f_to_vec, get_vec, scale.
do 2 rewrite Msum_Csum.
rewrite <- big_sum_extend_r.
bdestruct (m1 =? m1); bdestruct (0 =? 0); try lia.
rewrite Cplus_comm.
apply Cplus_simplify; try lca.
unfold get_vec.
assert (p : S m1 + m2 = m1 + (S m2)). lia.
rewrite p.
rewrite big_sum_sum.
assert (p1 : forall a b : C, b = C0 -> (a + b = a)%C).
intros. rewrite H4. lca.
rewrite p1.
apply big_sum_eq_bounded; intros.
bdestruct (x0 =? m1); bdestruct (x0 <? m1); try lia.
simpl.
bdestruct (x0 <? m1 + m2); try lia.
bdestruct (x0 <? S m1); try lia; easy.
apply (@big_sum_0_bounded C C_is_monoid); intros.
bdestruct (m1 + x0 <? m1 + m2); bdestruct (m1 + x0 <? m1);
try lia; simpl; lca.
Qed.
Lemma smash_scale : forall {n m1 m2} (T1 : Matrix n m1) (T2 : Matrix n m2) (v : Vector n),
smash (col_append T1 (normalize v)) T2 =
col_scale (smash (col_append T1 v) T2) m1 (/ norm v).
Proof. intros.
unfold smash, col_append, normalize, col_scale.
prep_matrix_equality.
bdestruct (y <? S m1); bdestruct (y =? m1); try lia; try easy.
Qed.
Lemma extend_onb_ind_step_part2 : forall {n m1 m2} (T1 : Matrix n m1) (T2 : Matrix n m2)
(v : Vector n),
WF_Matrix T1 -> WF_Matrix T2 -> WF_Matrix v -> v <> Zero ->
linearly_independent (smash (col_append T1 v) T2) ->
linearly_independent (smash (col_append T1
(normalize (gram_schmidt_on_T n m1 (col_append T1 v)))) T2).
Proof. intros.
rewrite smash_scale.
apply_mat_prop lin_indep_scale_invr.
apply H5.
unfold not; intros.
assert (H4' : (norm (gram_schmidt_on_T n m1 (col_append T1 v)) *
/ norm (gram_schmidt_on_T n m1 (col_append T1 v)) =
norm (gram_schmidt_on_T n m1 (col_append T1 v)) * C0)%C).
{ rewrite H6; easy. }
rewrite Cmult_0_r, Cinv_r in H4'.
apply C1_neq_C0; easy.
unfold not; intros.
assert (H5' : WF_Matrix (gram_schmidt_on_T n m1 (col_append T1 v))).
{ apply WF_gs_on_T.
apply WF_col_append; easy. }
apply norm_zero_iff_zero in H5'.
apply RtoC_inj in H7.
rewrite H7 in H5'.
apply (gram_schmidt_non_zero (col_append T1 v)).
apply lin_indep_smash in H3; easy.
apply H5'; lra.
rewrite gs_on_T_cols_add; try easy.
apply_mat_prop lin_indep_add_invr.
apply invr_col_add_col_add_many in H6.
inversion H6; subst.
apply H8; try lia; try easy.
unfold f_to_vec, delta_T'.
bdestruct (m1 <? m1 + m2); bdestruct (m1 <? m1); try lia; easy.
Qed.
Lemma extend_onb_ind_step : forall {n m1 m2} (T1 : Matrix n m1) (T2 : Matrix n m2) (v : Vector n),
WF_Matrix T1 -> WF_Matrix T2 -> WF_Matrix v ->
linearly_independent (smash (col_append T1 v) T2) -> orthonormal T1 ->
exists v1, WF_Matrix v1 /\ orthonormal (col_append T1 v1) /\
linearly_independent (smash (col_append T1 v1) T2).
Proof. intros.
exists (normalize (gram_schmidt_on_T n m1 (col_append T1 v))).
split. unfold normalize.
apply WF_scale.
apply WF_gs_on_T.
apply WF_col_append; try easy.
split.
- apply lin_indep_smash in H2.
assert (H4 := extend_onb_ind_step_part1 (col_append T1 v)).
assert (H' : reduce_col (col_append T1 v) m1 = T1).
{ intros.
prep_matrix_equality.
unfold reduce_col, col_append.
bdestruct (y <? m1); bdestruct (y =? m1);
bdestruct (1 + y =? m1); try lia; try easy.
all : rewrite H; try lia; rewrite H; try lia; lca. }
rewrite H' in H4.
apply H4; try easy.
apply WF_col_append; easy.
- apply extend_onb_ind_step_part2; try easy.
apply lin_indep_smash in H2.
apply_mat_prop lin_indep_pzf.
unfold not; intros.
assert (H' : ~ linearly_independent (col_append T1 v)).
{ apply H5.
exists m1.
split; try lia.
rewrite <- H6.
prep_matrix_equality.
unfold get_vec, col_append.
bdestruct (y =? 0); bdestruct (m1 =? m1); subst; try easy; try lia. }
easy.
Qed.
Lemma extend_onb : forall (n m2 m1 : nat) (T1 : Matrix n (S m1)) (T2 : Matrix n m2),
WF_Matrix T1 -> WF_Matrix T2 ->
linearly_independent (smash T1 T2) -> orthonormal T1 ->
exists T2' : Matrix n m2, WF_Matrix T2' /\ orthonormal (smash T1 T2').
Proof. induction m2 as [| m2'].
- intros.
exists Zero.
split. easy.
rewrite smash_zero; try easy.
rewrite plus_0_r.
apply H2.
- intros.
rewrite (split_col T2) in *.
assert (H3 := (smash_assoc T1 (get_vec 0 T2) (reduce_col T2 0))).
simpl in *.
rewrite <- H3 in H1.
rewrite <- smash_append in H1; try easy.
assert (exists v1, WF_Matrix v1 /\ orthonormal (col_append T1 v1) /\
linearly_independent (smash (col_append T1 v1) (reduce_col T2 0))).
{ apply (extend_onb_ind_step _ _ (get_vec 0 T2)); try easy.
apply WF_reduce_col. lia.
rewrite (split_col T2). easy.
apply WF_get_vec.
rewrite (split_col T2). easy.
assert (add1 : S (m1 + S m2') = S (S m1) + m2'). { lia. }
assert (add2 : S (m1 + 1) = S (S m1)). { lia. }
rewrite add1, add2 in H1.
apply H1. }
destruct H4 as [v [H4 [H5 H6] ] ].
assert (H7 : exists T2' : Matrix n m2',
WF_Matrix T2' /\ orthonormal (smash (smash T1 v) T2')).
{ assert (H'' := (@WF_smash n (S m1) (S O) T1 v)).
assert (H''' : Nat.add (S m1) (S O) = S (S m1)). lia.
apply (IHm2' _ (smash T1 v) (reduce_col T2 0)); try easy.
assert (H' : Nat.add m1 (S O) = S m1). lia.
unfold Nat.add in H'.
rewrite H'.
rewrite H''' in *.
apply H''.
easy. easy.
apply (WF_reduce_col 0 T2); try lia.
rewrite (split_col T2); easy.
assert (add1 : S (Nat.add m1 (S m2')) = S (Nat.add (Nat.add m1 (S O)) m2')). lia.
rewrite add1 in H1.
unfold Nat.add in H1.
unfold Nat.add.
rewrite <- smash_append; try easy.
assert (add2 : Nat.add (S (S m1)) m2' = S (Nat.add (Nat.add m1 (S O)) m2')). lia.
assert (add3 : (S (S m1)) = S (Nat.add m1 (S O))). lia.
rewrite add2, add3 in H6.
unfold Nat.add in H6.
apply H6.
rewrite <- smash_append; try easy.
assert (add4 : S (S m1) = S (Nat.add m1 (S O))). lia.
rewrite add4 in H5.
unfold Nat.add in H5.
apply H5. }
destruct H7.
rewrite smash_assoc in H7.
exists (smash v x).
split.
assert (H' : S m2' = 1 + m2'). lia. rewrite H'.
apply WF_smash; try easy.
assert (add5 : Nat.add (Nat.add (S m1) (S O)) m2' = S (Nat.add m1 (S m2'))). lia.
assert (add6 : (Init.Nat.add (S O) m2') = (S m2')). lia.
rewrite add5, add6 in H7.
apply H7.
apply WF_get_vec.
rewrite (split_col T2).
easy.
Qed.
Lemma get_vec_vec : forall {n} (v : Vector n),
WF_Matrix v -> get_vec 0 v = v.
Proof. intros.
unfold get_vec.
prep_matrix_equality.
bdestruct (y =? 0).
- rewrite H0; easy.
- unfold WF_Matrix in H.
rewrite H; try easy.
right.
bdestruct (y <? 1); try lia.
Qed.
Lemma orthonormal_normalize_v : forall {n} (v : Vector n),
v <> Zero -> WF_Matrix v -> orthonormal (normalize v).
Proof. intros.
split.
unfold orthogonal, inner_product.
intros. destruct i.
+ assert (H' : get_vec j (normalize v) = Zero).
{ prep_matrix_equality.
unfold get_vec, normalize.
bdestruct (y =? 0); try easy.
unfold scale. rewrite H0; try lia; lca. }
rewrite H', Mmult_0_r; easy.
+ assert (H' : get_vec (S i) (normalize v) = Zero).
{ prep_matrix_equality.
unfold get_vec, normalize.
bdestruct (y =? 0); try easy.
unfold scale. rewrite H0; try lia; lca. }
rewrite H', zero_adjoint_eq, Mmult_0_l; easy.
+ intros.
destruct i; try lia.
rewrite get_vec_vec.
apply normalized_norm_1.
unfold not; intros; apply H.
apply norm_zero_iff_zero in H0.
apply H0; easy.
unfold normalize.
auto with wf_db.
Qed.
(* the steps here are a bit confusing, but we use these lemmas to
prove the following useful fact *)
Theorem onb_out_of_v : forall {n} (v : Vector n),
WF_Matrix v -> v <> Zero ->
exists T : Square n, WF_Orthonormal T /\ get_vec 0 T = normalize v.
Proof. intros.
destruct n as [| n].
- assert (H' : v = Zero).