forked from inQWIRE/QuantumLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenSubspaces.v
5618 lines (5315 loc) · 224 KB
/
GenSubspaces.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Permutation.
Require Export Summation.
Require Export GenVecSet.
(* Problems of current Subspaces.v:
1. Use of deprecated definitions and lemmas such as col_append, row_append
2. Names use obsolete terms such as col_vec
*)
Declare Scope subspaces_scope.
Delimit Scope subspaces_scope with SS.
Open Scope subspaces_scope.
Module SubspacesOverField
(FM : FieldModule).
Include VecSetOverField FM.
Ltac Fsimpl := repeat (repeat rewrite Gmult_plus_distr_l;
repeat rewrite Gmult_plus_distr_r;
repeat rewrite Gmult_assoc;
repeat rewrite Gmult_1_l;
repeat rewrite Gmult_1_r;
repeat rewrite Gmult_0_l;
repeat rewrite Gmult_0_r;
repeat rewrite Gplus_assoc;
repeat rewrite Gplus_0_l;
repeat rewrite Gplus_0_r).
Program Instance F_is_module_space : Module_Space F F :=
{ Vscale := Gmult }.
Next Obligation. field. Qed.
Next Obligation. field. Qed.
Next Obligation. field. Qed.
Lemma cons_conc : forall (X : Type) (x : X) (ls : list X),
x :: ls = [x] ++ ls.
Proof. reflexivity. Qed.
Lemma nth_helper : forall {X : Type} (n : nat) (ls : list X) (x : X),
(n < length ls)%nat -> skipn n ls = [nth n ls x] ++ skipn (S n) ls.
Proof. induction n as [| n'].
- destruct ls. easy. easy.
- intros. destruct ls.
assert (H' : forall (n : nat), (S n < 0)%nat -> False). { nia. }
apply H' in H. easy.
rewrite skipn_cons.
assert (H'' : nth (S n') (x0 :: ls) x = nth n' ls x). { easy. }
rewrite H''.
rewrite (IHn' ls x).
easy.
simpl in H.
assert (H''' : forall (n m : nat), (S m < S n)%nat -> (m < n)%nat). { nia. }
apply H''' in H.
easy.
Qed.
Lemma Fopp_opp : forall (a b : F), -a = b <-> a = -b. Proof. split; intros; [rewrite <- H | rewrite H]; ring. Qed.
Lemma Fplus_opp_l : forall c : F, - c + c = 0. Proof. intros; ring. Qed.
Lemma Fplus_opp_r : forall c : F, c + - c = 0. Proof. intros; ring. Qed.
Lemma Fplus_inj_r : forall (c c1 c2 : F),
c1 = c2 -> c1 + c = c2 + c.
Proof. intros. rewrite H. reflexivity. Qed.
Lemma Fplus_inj_l : forall (c c1 c2 : F),
c1 = c2 -> c + c1 = c + c2.
Proof. intros. rewrite H. reflexivity. Qed.
Lemma Fplus_inv_r : forall (c c1 c2 : F),
c1 + c = c2 + c -> c1 = c2.
Proof. intros. apply Fplus_inj_r with (c:=-c) in H.
rewrite <- ! Gplus_assoc in H.
rewrite ! Fplus_opp_r in H.
rewrite ! Gplus_0_r in H.
assumption.
Qed.
Lemma Fplus_inv_l : forall (c c1 c2 : F),
c + c1= c + c2 -> c1 = c2.
Proof. intros. apply Fplus_inj_l with (c:=-c) in H.
rewrite ! Gplus_assoc in H.
rewrite ! Fplus_opp_l in H.
rewrite ! Gplus_0_l in H.
assumption.
Qed.
Lemma Fplus_zero_iff_equals_minus : forall (c1 c2 : F),
c1 + c2 = 0 <-> c1 = -c2.
Proof. split.
- intro. apply Fplus_inj_r with (c := -c2) in H.
rewrite <- ! Gplus_assoc in H.
rewrite Fplus_opp_r in H.
rewrite Gplus_0_l, Gplus_0_r in H.
assumption.
- intro. rewrite H. rewrite Fplus_opp_l. reflexivity.
Qed.
Lemma F_inj_l : forall (c x y : F), x = y -> (c * x = c * y)%G.
Proof. intros. rewrite H. easy. Qed.
Lemma F_inv_l : forall (c x y : F), c <> 0%G -> (c * x = c * y)%G -> x = y.
Proof. intros. apply F_inj_l with (c:=/c) in H0. rewrite ! Gmult_assoc in H0.
rewrite Finv_l in H0. 2: apply F_field_theory.
all : try rewrite ! Gmult_1_l in H0; assumption.
Qed.
Lemma F_inj_r : forall (c x y : F), x = y -> (x * c = y * c)%G.
Proof. intros. rewrite H. easy. Qed.
Lemma F_inv_r : forall (c x y : F), c <> 0%G -> (x * c = y * c)%G -> x = y.
Proof. intros. apply F_inj_r with (c:=/c) in H0. rewrite <- ! Gmult_assoc in H0.
replace (c * / c)%G with (/ c * c)%G in H0 by (rewrite Gmult_comm; auto).
rewrite Finv_l in H0. 2: apply F_field_theory.
all: try rewrite ! Gmult_1_r in H0; assumption.
Qed.
Lemma Gneg1_neq_0 : (- 1%G) <> 0%G.
Proof. intro H.
assert (forall a b : F, a = b -> - a = - b)%G. { intros. rewrite H0. auto. }
apply H0 in H. rewrite Gopp_involutive in H.
replace (- 0)%G with 0%G in H by ring.
contradict H.
apply G1_neq_0.
Qed.
Lemma Fopp_mult_distr_r : forall c1 c2 : F, - (c1 * c2) = c1 * - c2.
Proof. intros; ring. Qed.
Lemma Fopp_mult_distr_l : forall c1 c2 : F, - (c1 * c2) = - c1 * c2.
Proof. intros; ring. Qed.
Lemma Fplus_simplify : forall (a b c d : F),
a = b -> c = d -> (a + c = b + d)%G.
Proof. intros. rewrite H, H0; easy. Qed.
Lemma Fmult_simplify : forall (a b c d : F),
a = b -> c = d -> (a * c = b * d)%G.
Proof. intros. rewrite H, H0; easy. Qed.
Lemma Mmult_eq_l : forall {m n o : nat} (A A' : GenMatrix m n) (B : GenMatrix n o), A = A' -> A × B = A' × B.
Proof. intros. rewrite H. easy. Qed.
Lemma Mmult_eq_r : forall {m n o : nat} (A : GenMatrix m n) (B B' : GenMatrix n o), B = B' -> A × B = A × B'.
Proof. intros. rewrite H. easy. Qed.
Lemma Mscale_inj : forall {m n} (A B : GenMatrix m n) (c : F), A = B -> (c .* A = c .* B)%GM.
Proof. intros m n A B c H. rewrite H. easy. Qed.
Lemma Mscale_inv : forall {m n} (A B : GenMatrix m n) (c : F), c <> 0%G -> (c .* A = c .* B)%GM -> A = B.
Proof. intros m n A B c H H0. apply Mscale_inj with (c:= /c) in H0.
rewrite ! Mscale_assoc in H0. rewrite Finv_l in H0; try easy.
rewrite ! Mscale_1_l in H0. easy.
apply F_field_theory. auto.
Qed.
Lemma Mmult_double_side : forall {i j k : nat} {m1 m1' : GenMatrix i j} {m2 m2' : GenMatrix j k} ,
m1 = m1' -> m2 = m2' -> m1 × m2 = m1' × m2'.
Proof. intros. rewrite H, H0. reflexivity. Qed.
Lemma Mplus_inj_l : forall {j k : nat} (m m1 m2 : GenMatrix j k),
m1 = m2 -> m .+ m1 = m .+ m2.
Proof. intros. rewrite H. reflexivity. Qed.
Lemma Mplus_inj_r : forall {j k : nat} (m m1 m2 : GenMatrix j k),
m1 = m2 -> m1 .+ m = m2 .+ m.
Proof. intros. rewrite H. reflexivity. Qed.
Lemma Mplus_double_side : forall {j k : nat} {m1 m1' m2 m2' : GenMatrix j k} ,
m1 = m1' -> m2 = m2' -> m1 .+ m2 = m1' .+ m2'.
Proof. intros. rewrite H, H0. reflexivity. Qed.
Lemma Mplus_opp_r : forall {j k : nat} (m : GenMatrix j k),
WF_GenMatrix m -> m .+ - 1%G .* m = Zero.
Proof. intros. lgma. Qed.
Lemma Mplus_opp_l : forall {j k : nat} (m : GenMatrix j k),
WF_GenMatrix m -> - 1%G .* m .+ m = Zero.
Proof. intros. lgma. Qed.
Lemma Mplus_inv_r : forall {j k : nat} (m m1 m2 : GenMatrix j k),
WF_GenMatrix m -> m1 .+ m = m2 .+ m -> m1 = m2.
Proof. intros. apply Mplus_inj_r with (m := - 1%G .* m) in H0.
rewrite ! GMplus_assoc in H0.
rewrite ! Mplus_opp_r in H0; auto.
rewrite ! GMplus_0_r in H0.
assumption.
Qed.
Lemma Mplus_inv_l : forall {j k : nat} (m m1 m2 : GenMatrix j k),
WF_GenMatrix m -> m .+ m1 = m .+ m2 -> m1 = m2.
Proof. intros. apply Mplus_inj_l with (m := - 1%G .* m) in H0.
rewrite <- ! GMplus_assoc in H0.
rewrite ! Mplus_opp_l in H0; auto.
rewrite ! GMplus_0_l in H0.
assumption.
Qed.
Lemma Mplus_zero_iff_equals_minus : forall {j k : nat} (m1 m2 : GenMatrix j k),
WF_GenMatrix m2 -> (m1 .+ m2 = Zero <-> m1 = - 1%G .* m2).
Proof. intros. split.
- intro. apply Mplus_inj_r with (m := - 1%G .* m2) in H0.
rewrite GMplus_assoc in H0.
rewrite Mplus_opp_r in H0; auto.
rewrite GMplus_0_l, GMplus_0_r in H0.
assumption.
- intro. rewrite H0. lgma.
Qed.
Lemma big_sum_permutation : forall (A : Type) (m : nat) (d : A) (l1 l2 : list A) (f : A -> F),
Permutation l1 l2 -> (m >= length l1)%nat ->
Σ (fun y : nat => f (nth y l1 d)) m = Σ (fun y : nat => f (nth y l2 d)) m.
Proof. intros.
gen m.
induction H.
- simpl. easy.
- intros. simpl in *.
destruct m; try easy.
rewrite <- ! big_sum_extend_l.
rewrite IHPermutation.
easy. lia.
- intros.
destruct m; try easy.
destruct m.
simpl in *.
lia.
rewrite <- ! big_sum_extend_l.
simpl.
ring.
- intros.
rewrite IHPermutation1; try easy.
rewrite IHPermutation2; try easy.
rewrite Permutation_length with (l' := l') in H1; auto.
Qed.
Lemma get_col_vec : forall {n} (v : GenVector n),
WF_GenMatrix v -> get_col v 0 = v.
Proof. intros.
unfold get_col.
prep_genmatrix_equality.
bdestruct (y =? 0).
- rewrite H0; easy.
- unfold WF_GenMatrix in H.
rewrite H; try easy.
right.
bdestruct (y <? 1); try lia.
Qed.
Lemma linearly_independent_dimensions : forall {n m : nat} (A : GenMatrix n m),
WF_GenMatrix A -> linearly_independent A -> (m <= n)%nat.
Proof. intros n m A H H0.
bdestruct (n <? m)%nat; auto.
contradict H0.
apply lindep_implies_not_linindep.
apply gt_dim_lindep; auto.
Qed.
Definition list_vector_to_matrix {n} (l : list (GenVector n)) : GenMatrix n (length l) := (fun r c : nat => (nth c l (@Zero n 1)) r 0%nat).
Check list_vector_to_matrix.
Definition matrix_column_choose {n m} (indices_list : list nat) (M : GenMatrix n m) : GenMatrix n (length indices_list) := list_vector_to_matrix (map (fun i : nat => get_col M i) indices_list).
Definition vector_row_choose {n} (indices_list : list nat) (v : GenVector n) : GenVector (length indices_list) := (fun r c : nat => v (nth r indices_list n) c).
Lemma WF_GenMatrix_matrix_column_choose_indices_list_I_n: forall (n : nat) (indices_list : list nat),
WF_GenMatrix (matrix_column_choose indices_list (@I n)).
Proof. intros.
unfold WF_GenMatrix.
intros.
unfold matrix_column_choose.
unfold list_vector_to_matrix.
assert (Zero = get_col (@I n) n).
{ unfold get_col.
do 2 (apply functional_extensionality; intros).
bdestruct_all; try easy.
unfold I.
bdestruct_all; try easy. }
rewrite H0.
rewrite map_nth with (d := n).
unfold get_col.
bdestruct_all.
destruct H0; unfold I; bdestruct_all; simpl; try easy.
rewrite nth_overflow in H2; lia.
Qed.
Lemma WF_GenMatrix_matrix_column_choose_indices_list : forall {n m : nat} (indices_list : list nat) (M : GenMatrix n m), WF_GenMatrix M -> WF_GenMatrix (matrix_column_choose indices_list M).
Proof. intros.
unfold WF_GenMatrix.
intros.
unfold matrix_column_choose.
unfold list_vector_to_matrix.
assert (Zero = get_col M m).
{ unfold get_col.
do 2 (apply functional_extensionality; intros).
bdestruct_all; try easy.
unfold WF_GenMatrix in H0.
assert ((x0 >= n)%nat \/ (m >= m)%nat). { right. lia. }
specialize (H x0 m H2).
rewrite H.
trivial. }
rewrite H1.
rewrite map_nth with (d := m).
unfold get_col.
bdestruct_all.
destruct H0.
- unfold WF_GenMatrix in H.
assert ((x >= n)%nat \/ ((nth y indices_list m) >= m)%nat). { left. assumption. }
specialize (H x (nth y indices_list m) H3).
assumption.
- rewrite nth_overflow.
2 : lia.
unfold WF_GenMatrix in H0.
assert ((x >= n)%nat \/ (m >= m)%nat). { right. lia. }
specialize (H x m H3).
assumption.
Qed.
Lemma WF_GenMatrix_vector_row_choose_indices_list : forall {n : nat} (indices_list : list nat) (v : GenVector n), WF_GenMatrix v -> WF_GenMatrix (vector_row_choose indices_list v).
Proof. intros.
unfold WF_GenMatrix.
intros.
unfold vector_row_choose.
destruct H0.
- rewrite nth_overflow.
2 : lia.
unfold WF_GenMatrix in H0.
assert ((n >= n)%nat \/ (y >= 1)%nat). { left. lia. }
specialize (H n y H1).
apply H.
- unfold WF_GenMatrix in H0.
assert (((nth x indices_list n) >= n)%nat \/ (y >= 1)%nat). { right. assumption. }
specialize (H (nth x indices_list n) y H1).
apply H.
Qed.
Hint Resolve WF_GenMatrix_matrix_column_choose_indices_list_I_n WF_GenMatrix_matrix_column_choose_indices_list WF_GenMatrix_vector_row_choose_indices_list : wf_db.
(** subspace of the form { v | P v } **)
Definition subspace {n : nat} (P : GenVector n -> Prop) : Prop :=
(forall (v : GenVector n), P v -> WF_GenMatrix v) /\
P Zero /\
(forall (v w : GenVector n), P v -> P w -> P (v .+ w)) /\
(forall (v : GenVector n) (c : F), P v -> P (c .* v)).
Lemma WF_subspace : forall {n : nat} {P : GenVector n -> Prop} {v : GenVector n},
subspace P -> P v -> WF_GenMatrix v.
Proof. intros n P v H0 H1. destruct H0 as [WFP [PZero [Psum Pscale]]].
auto.
Qed.
Hint Resolve WF_subspace : wf_db.
Lemma matrix_column_choose_original : forall {n m : nat} (A : GenMatrix n m),
WF_GenMatrix A -> matrix_column_choose (List.seq 0 m) A = A.
Proof. intros n m A H0.
unfold matrix_column_choose, list_vector_to_matrix.
unfold WF_GenMatrix in H0.
prep_genmatrix_equality.
assert (@Zero n 1 = (get_col A m)).
{ unfold get_col.
prep_genmatrix_equality.
bdestruct_all; trivial.
rewrite H0; trivial.
lia. }
bdestruct (x <? n)%nat.
- bdestruct (y <? m)%nat.
+ rewrite H.
rewrite map_nth with (d := m).
rewrite seq_nth; trivial.
+ rewrite nth_overflow.
* rewrite H0; trivial.
lia.
* rewrite map_length.
rewrite seq_length.
assumption.
- bdestruct (y <? m)%nat.
+ rewrite H.
rewrite map_nth with (d := m).
rewrite seq_nth; trivial.
+ rewrite nth_overflow.
* rewrite H0; trivial.
lia.
* rewrite map_length.
rewrite seq_length.
assumption.
Qed.
Lemma subspace_closed_under_linear_combinations : forall {n m : nat} {P : GenVector n -> Prop} (M : GenMatrix n m) (a : GenVector m), WF_GenMatrix a -> subspace P -> (forall (i : nat), (i < m)%nat -> P (get_col M i)) -> P (M × a).
Proof. intros n m P M a H0 H1 H2.
induction m.
- unfold GMmult. simpl.
unfold subspace in H1.
destruct H1 as [WFP [PZero [Psum Pscale]]].
assumption.
- assert (M × a = (matrix_column_choose (List.seq 0 m) M) × (vector_row_choose (List.seq 0 m) a) .+ (a m 0%nat) .* (get_col M m)).
{ unfold GMmult.
unfold scale.
unfold matrix_column_choose, list_vector_to_matrix.
unfold vector_row_choose.
unfold get_col.
unfold GMplus.
simpl.
do 2 (apply functional_extensionality; intros).
unfold WF_GenMatrix in *.
bdestruct (x0 <? 1)%nat.
- bdestruct_all.
subst.
f_equal.
2 : apply Gmult_comm.
rewrite seq_length.
apply big_sum_eq_bounded.
intros.
rewrite seq_nth.
2 : assumption.
simpl.
f_equal.
rewrite nth_indep with (d' := (fun i0 x1 y : nat => if (y =? 0)%nat then M x1 i0 else 0) (S m)).
2 : rewrite map_length;
rewrite seq_length;
assumption.
rewrite map_nth with (d := S m).
bdestruct_all.
rewrite seq_nth; trivial.
- remember H0 as H0'. clear HeqH0'.
remember H0 as H0''. clear HeqH0''.
assert ((m >= S m)%nat \/ (x0 >= 1)%nat). { right. assumption. }
specialize (H0 m x0 H3).
rewrite H0. rewrite Gmult_0_r, Gplus_0_r.
bdestruct_all.
rewrite Gmult_0_r, Gplus_0_r.
f_equal.
2 : symmetry; apply seq_length.
apply functional_extensionality; intros.
assert ((x1 >= S m)%nat \/ (x0 >= 1)%nat). { right. assumption. }
specialize (H0' x1 x0 H5).
rewrite H0'.
rewrite Gmult_0_r.
assert ((nth x1 (List.seq 0 m) (S m) >= S m)%nat \/ (x0 >= 1)%nat). { right. assumption. }
specialize (H0'' (nth x1 (List.seq 0 m) (S m)) x0 H6).
rewrite H0''.
rewrite Gmult_0_r.
reflexivity. }
rewrite H.
remember H1 as H1'.
clear HeqH1'.
unfold subspace in H1.
destruct H1 as [WFP [PZero [Psum Pscale]]].
apply Psum.
+ rewrite ! seq_length.
apply IHm.
* pose (WF_GenMatrix_vector_row_choose_indices_list (List.seq 0 m) a).
rewrite ! seq_length in w; auto.
* intros i0 H1.
assert (get_col (matrix_column_choose (List.seq 0 m) M) i0 = get_col M i0).
{ unfold matrix_column_choose, list_vector_to_matrix.
unfold get_col.
do 2 (apply functional_extensionality; intros).
bdestruct_all; trivial.
subst.
rewrite nth_indep with (d' := (fun i1 x0 y : nat => if (y =? 0)%nat then M x0 i1 else 0) (S m)).
2 : rewrite map_length;
rewrite seq_length;
assumption.
rewrite map_nth with (d := S m).
bdestruct_all.
rewrite seq_nth; trivial. }
setoid_rewrite H3.
auto.
+ apply Pscale.
auto.
Qed.
Definition span {n m : nat} (M : GenMatrix n m) (u : GenVector n) : Prop := (exists (a : GenVector m), WF_GenMatrix a /\ u = M × a).
Lemma span_is_subspace : forall (n m : nat) (M : GenMatrix n m),
WF_GenMatrix M -> subspace (span M).
Proof. intros n m M H0.
repeat constructor.
- intros v H1.
unfold span in H1.
destruct H1 as [a [WFa vMa]].
rewrite vMa.
auto with wf_db.
- exists Zero.
split; auto with wf_db.
rewrite GMmult_0_r.
reflexivity.
- intros v w H1 H2.
unfold span in *.
destruct H1 as [a [WFa vMa]].
destruct H2 as [b [WFb wMb]].
exists (a .+ b).
split; auto with wf_db.
subst.
rewrite GMmult_plus_distr_l.
reflexivity.
- intros v c H1.
unfold span in *.
destruct H1 as [a [WFa vMa]].
exists (c .* a).
split; auto with wf_db.
subst.
rewrite Mscale_mult_dist_r; auto.
Qed.
(* Lemma 19 Suppose V is a vector space, u1,u2,...,un are vectors in V, and v ∈ sp{u1,u2,...,un}. Then
sp{u1,u2,...,un,v} = sp{u1,u2,...,un}. *)
Lemma equal_span_col_append : forall {n m : nat} (M : GenMatrix n m) (v u : GenVector n),
span M u -> span (col_append M v) u.
Proof. intros n m M v u H0.
unfold span in *.
destruct H0 as [a [H0 H1]].
exists (fun (r c : nat) => if r <? m then a r c else 0%G).
split.
- unfold WF_GenMatrix.
intros x y H2.
unfold WF_GenMatrix in H0.
rewrite H0.
bdestruct_all; reflexivity.
lia.
- rewrite H1.
unfold col_append, col_wedge.
unfold GMmult.
prep_genmatrix_equality.
simpl.
bdestruct_all.
rewrite Gmult_0_r, Gplus_0_r.
apply big_sum_eq_bounded.
intros.
bdestruct_all.
reflexivity.
Qed.
(* Lemma 19 Suppose V is a vector space, u1,u2,...,un are vectors in V, and v ∈ sp{u1,u2,...,un}. Then
sp{u1,u2,...,un,v} = sp{u1,u2,...,un}. *)
Lemma equal_span_col_append_inv : forall {n m : nat} (M : GenMatrix n m) (v : GenVector n), span M v -> (forall (u : GenVector n), span (col_append M v) u -> span M u).
Proof. intros n m M v H u H0.
unfold span in *.
do 2 destruct H.
do 2 destruct H0.
rewrite H1 in H2.
rewrite H2.
unfold GMmult in H2.
(** Σ_{i = 0}^{i = m-1} M_i x0_i + x0_m * Σ_{i = 0}^{i = m-1} M_i x_i
= Σ_{i = 0}^{i = m-1} M_i (x0_i + x0_m * x_i) **)
(** (fun (r c : nat) => (big_sum (fun (i : nat) => M r i * ((x0 i c) + (x0 m c) * (x i c))) m)). **)
exists (fun (r c : nat) => if (r <? m) then ((x0 r c) + (x0 m c) * (x r 0%nat)) else 0%G).
split.
- unfold WF_GenMatrix.
intros x1 y H3.
destruct H3; bdestruct_all; trivial.
remember H0 as H0'. clear HeqH0'.
unfold WF_GenMatrix in H0, H0'.
assert ((x1 >= S m)%nat \/ (y >= 1)%nat). { right. lia. }
specialize (H0 x1 y H5).
rewrite H0.
assert ((m >= S m)%nat \/ (y >= 1)%nat). { right. lia. }
specialize (H0' m y H6).
rewrite H0'.
ring.
- unfold col_append, col_wedge.
unfold GMmult.
do 2 (apply functional_extensionality; intros).
simpl.
bdestruct_all.
assert ( Σ (fun y : nat => M x1 y * (if y <? m then x0 y x2 + x0 m x2 * x y 0%nat else 0)) m
= Σ (fun y : nat => M x1 y * (x0 y x2 + x0 m x2 * x y 0%nat)) m).
{ apply big_sum_eq_bounded.
intros x3 H5.
bdestruct_all.
reflexivity. }
rewrite H5.
replace (fun y : nat => M x1 y * (x0 y x2 + x0 m x2 * x y 0%nat))
with (fun y : nat => M x1 y * x0 y x2 + (M x1 y * x y 0%nat)* x0 m x2)
by (apply functional_extensionality; intros; ring).
assert (Σ (fun y : nat => M x1 y * x0 y x2 + M x1 y * x y 0%nat * x0 m x2) m
= Σ (fun y : nat => M x1 y * x0 y x2) m + Σ (fun y : nat => M x1 y * x y 0%nat) m * x0 m x2).
{ setoid_rewrite big_sum_plus.
simpl.
f_equal.
rewrite @big_sum_mult_r with (R := F) (H := R0) (H0 := R1) (H1 := R2) (H2 := R3).
simpl.
reflexivity. }
rewrite H6.
f_equal.
apply big_sum_eq_bounded.
intros.
bdestruct_all.
reflexivity.
Qed.
(* Lemma 19 Suppose V is a vector space, u1,u2,...,un are vectors in V, and v ∈ sp{u1,u2,...,un}. Then
sp{u1,u2,...,un,v} = sp{u1,u2,...,un}. *)
Lemma equal_span_reduce_col_inv : forall {n m : nat} (M : GenMatrix n (S m)) (i : nat),
(i < S m)%nat -> (forall (u : GenVector n), span (reduce_col M i) u -> span M u).
Proof. intros n m M i H u H0.
unfold span in *.
destruct H0 as [a [H0 H0']].
exists (fun r c => if (r <? i)%nat then (a r c) else if (r =? i)%nat then 0%G else (a (r-1)%nat c)).
split.
- unfold WF_GenMatrix in *.
intros.
rewrite ! H0;
bdestruct_all; trivial;
lia.
- rewrite H0'.
unfold reduce_col.
prep_genmatrix_equality.
unfold GMmult.
replace m with (i + (m - i))%nat at 1 by lia.
rewrite @big_sum_sum with (H := R0) (m := i) (n := (m - i)%nat).
replace (S m) with (i + ((S m) - i))%nat at 1 by lia.
rewrite @big_sum_sum with (H := R0) (m := i) (n := ((S m) - i)%nat).
f_equal.
+ apply big_sum_eq_bounded.
intros.
bdestruct_all.
reflexivity.
+ replace ((S m) - i)%nat with (S (m - i))%nat by lia.
rewrite <- big_sum_extend_l.
bdestruct_all.
rewrite Gmult_0_r, Gplus_0_l.
apply big_sum_eq_bounded.
intros.
bdestruct_all.
replace (1 + (i + x0))%nat with (i + S x0)%nat by lia.
replace (i + S x0 - 1)%nat with (i + x0)%nat by lia.
reflexivity.
Qed.
(* Lemma 19 Suppose V is a vector space, u1,u2,...,un are vectors in V, and v ∈ sp{u1,u2,...,un}. Then
sp{u1,u2,...,un,v} = sp{u1,u2,...,un}. *)
Lemma equal_span_reduce_col : forall {n m : nat} (M : GenMatrix n (S m)) (i : nat),
(i < S m)%nat -> span (reduce_col M i) (get_col M i) ->
(forall (u : GenVector n), span M u -> span (reduce_col M i) u).
Proof. intros n m M i H H0 u H1.
unfold span in *.
destruct H0 as [a [H0 H0']].
destruct H1 as [b [H1 H1']].
(* get_col i M = reduce_col M i × a
=> M_i = (Σ_{k=0}^{k=i-1} M_k a_k) + (Σ_{k=i+1}^{k=m} M_k a_{k-1})
u = M × b = Σ_{k=0}^{k=m} M_k b_k
= (Σ_{k=0}^{k=i-1} M_k b_k) + M_i b_i + (Σ_{k=i+1}^{k=m} M_k b_k)
= (Σ_{k=0}^{k=i-1} M_k b_k)
+ ((Σ_{k=0}^{k=i-1} M_k a_k) + (Σ_{k=i+1}^{k=m} M_k a_{k-1})) b_i
+ (Σ_{k=i+1}^{k=m} M_k b_k)
= (Σ_{k=0}^{k=i-1} M_k (b_i a_k + b_k)) + (Σ_{k=i+1}^{k=m} M_k (b_i a_{k-1} + b_k))
u = reduce_col M i × c = (Σ_{k=0}^{k=i-1} M_k c_k) + (Σ_{k=i+1}^{k=m} M_k c_{k-1})
c = ((b i 0%nat) .* a) .+ (reduce_row i b) *)
exists (((b i 0%nat) .* a) .+ (reduce_row b i)).
split.
- auto with wf_db.
- rewrite H1'.
rewrite GMmult_plus_distr_l.
rewrite Mscale_mult_dist_r; auto.
rewrite <- H0'.
unfold get_col, reduce_row, reduce_col.
unfold GMmult, scale, GMplus.
prep_genmatrix_equality.
bdestruct_all.
+ subst.
replace (S m) with (i + (S (m - i)))%nat by lia.
rewrite @big_sum_sum with (H := R0) (m := i) (n := (S (m - i))%nat).
rewrite <- big_sum_extend_l.
simpl.
setoid_rewrite Gplus_comm at 1.
rewrite <- ! Gplus_assoc.
f_equal.
* replace (i + 0)%nat with i by lia.
ring.
* rewrite Gplus_comm at 1.
replace m with (i + (m - i))%nat at 2 by lia.
rewrite @big_sum_sum with (H := R0) (m := i) (n := (m - i)%nat).
simpl.
f_equal.
-- apply big_sum_eq_bounded.
intros.
bdestruct_all.
ring.
-- apply big_sum_eq_bounded.
intros.
bdestruct_all.
replace (i + S x0)%nat with (S (i + x0)) by lia.
reflexivity.
+ assert ((fun y0 : nat => M x y0 * b y0 y)
=
(fun _ : nat => 0%G)).
{ apply functional_extensionality; intros.
unfold WF_GenMatrix in H1.
rewrite H1; try ring; lia. }
rewrite H3.
simpl.
rewrite Gmult_0_r, Gplus_0_l, Gplus_0_r.
apply big_sum_eq_bounded.
intros.
unfold WF_GenMatrix in H1.
rewrite ! H1.
bdestruct_all; ring.
all : lia.
Qed.
Lemma last_in_list : forall {A : Type} (d : A) (l : list A),
l <> [] -> In (last l d) l.
Proof. intros A d l H0.
apply app_removelast_last with (d := d) in H0.
rewrite <- nth_middle with (a := (last l d)) (d := d) (l := removelast l) (l' := []).
rewrite <- H0.
apply nth_In.
rewrite H0.
rewrite removelast_last.
rewrite app_length.
simpl.
lia.
Qed.
Definition col_insert_front {n m : nat} (M : GenMatrix n m) (v : GenVector n) : GenMatrix n (S m) :=
fun r c => if (c =? 0)%nat then v r 0%nat else M r (c - 1)%nat.
Lemma WF_GenMatrix_col_insert_front : forall {n m : nat} (M : GenMatrix n m) (v : GenVector n),
WF_GenMatrix M -> WF_GenMatrix v -> WF_GenMatrix (col_insert_front M v).
Proof. intros n m M v H0 H1.
unfold col_insert_front.
unfold WF_GenMatrix in *.
intros.
bdestruct_all.
- rewrite H1; trivial.
lia.
- rewrite H0; trivial.
lia.
Qed.
Hint Resolve WF_GenMatrix_col_insert_front : wf_db.
(* # ~12 *)
(** Theorem 24 Let V be a vector space over a field F, and let u1,u2,...,un be vectors in V , where n ≥ 2. Then {u1, u2, . . . , un} is linearly dependent if and only if at least one of u1, u2, . . . , un can be written as a linear combination of the remaining n − 1 vectors. **)
(* proves the "only if" part of theorem 24
Lemma lin_dep_gen_elem : forall {m n} (T : GenMatrix n (S m)),
WF_GenMatrix T -> linearly_dependent T ->
(exists i, i < (S m) /\
(exists v : GenVector m, WF_GenMatrix v /\
@GMmult n m 1 (reduce_col T i) v = (-C1) .* (get_col i T))).
*)
Lemma span_linearly_dependent_col_insert_front : forall {m n} (M : GenMatrix n m) (v : GenVector n),
WF_GenMatrix M -> span M v -> linearly_dependent (col_insert_front M v).
Proof. intros m n M v H0 H1.
unfold linearly_dependent.
unfold span in H1.
destruct H1 as [a [H1 H2]].
exists (fun r c => if (r =? 0)%nat
then if (c =? 0)%nat
then (- 1%G)
else 0%G
else a (r - 1)%nat c).
split.
- unfold WF_GenMatrix.
intros.
bdestruct_all; trivial.
unfold WF_GenMatrix in H1.
rewrite H1; trivial; lia.
- split.
+ intro H3.
apply f_equal_inv with (x := 0%nat) in H3.
apply f_equal_inv with (x := 0%nat) in H3.
unfold Zero in H3. simpl in H3.
contradict H3.
apply Gneg1_neq_0.
+ unfold col_insert_front.
unfold GMmult.
prep_genmatrix_equality.
rewrite <- big_sum_extend_l.
bdestruct_all.
* subst.
simpl.
unfold GMmult.
assert (@Zero n 1 x 0%nat
= Σ (fun y : nat => M x y * a y 0%nat) m * - 1%G + Σ (fun y : nat => M x y * a y 0%nat) m).
{ unfold Zero. ring. }
rewrite H2.
apply Fplus_inj_l.
apply big_sum_eq_bounded.
intros.
replace (x0 - 0)%nat with x0 by lia.
reflexivity.
* rewrite Gmult_0_r, Gplus_0_l.
unfold Zero.
simpl.
rewrite big_sum_0_bounded; trivial.
intros.
unfold WF_GenMatrix in H1.
rewrite H1; try ring; lia.
Qed.
Lemma span_linearly_dependent_col_append : forall {m n} (M : GenMatrix n m) (v : GenVector n),
WF_GenMatrix M -> span M v -> linearly_dependent (col_append M v).
Proof. intros m n M v H0 H1.
unfold linearly_dependent.
unfold span in H1.
destruct H1 as [a [H1 H2]].
exists (fun r c => if (r =? m)%nat
then if (c =? 0)%nat
then (- 1%G)
else 0%G
else a r c).
split.
- unfold WF_GenMatrix.
intros.
bdestruct_all; trivial.
unfold WF_GenMatrix in H1.
rewrite H1; trivial; lia.
- split.
+ intro H3.
apply f_equal_inv with (x := m) in H3.
apply f_equal_inv with (x := 0%nat) in H3.
simpl in H3.
replace (m =? m)%nat with true in H3 by (rewrite Nat.eqb_refl; reflexivity).
unfold Zero in H3.
contradict H3.
apply Gneg1_neq_0.
+ unfold col_append, col_wedge.
unfold GMmult.
prep_genmatrix_equality.
rewrite <- big_sum_extend_r.
bdestruct_all.
* subst.
simpl.
unfold GMmult.
assert (H2 : @Zero n 1 x 0%nat
= Σ (fun y : nat => M x y * a y 0%nat) m + Σ (fun y : nat => M x y * a y 0%nat) m * - 1%G ).
{ unfold Zero. ring. }
rewrite H2.
apply Fplus_inj_r.
apply big_sum_eq_bounded.
intros.
bdestruct_all.
reflexivity.
* rewrite Gmult_0_r, Gplus_0_r.
unfold Zero.
rewrite big_sum_0_bounded; trivial.
intros.
bdestruct_all.
unfold WF_GenMatrix in H1.
rewrite H1; try ring; lia.
Qed.
Lemma linearly_dependent_linear_combination : forall {n m : nat} (M : GenMatrix n m), (m > 1)%nat -> WF_GenMatrix M -> linearly_dependent M -> (exists (i : nat) (a : GenVector (m-1)), (i < m)%nat /\ WF_GenMatrix a /\ get_col M i = (matrix_column_choose ((List.seq 0 i) ++ (List.seq (i+1) (m - i - 1)%nat)) M) × a).
Proof. intros n m M H0 H1 H2.
unfold linearly_dependent in H2.
destruct H2 as [u [H2 [H3 H4]]].
apply nonzero_vec_nonzero_elem in H3; trivial.
destruct H3 as [i H3].
exists i.
bdestruct (i <? m).
- exists (fun r c : nat => if r <? i then (- (/ (u i 0%nat)) * (u r c))%G else (- (/ (u i 0%nat)) * (u (r+1)%nat c))%G).
split.
+ assumption.
+ split.
* unfold WF_GenMatrix in *.
intros x y H6.
destruct H6; bdestruct_all.
-- assert (H8 : (x+1 >= m)%nat \/ (y >= 1)%nat). { left. lia. }
specialize (H2 (x+1)%nat y H8).
rewrite H2.
ring.
-- assert (H8 : (x >= m)%nat \/ (y >= 1)%nat). { right. lia. }
specialize (H2 x y H8).
rewrite H2.
ring.
-- assert (H8 : (x+1 >= m)%nat \/ (y >= 1)%nat). { right. lia. }
specialize (H2 (x+1)%nat y H8).
rewrite H2.
ring.
* unfold GMmult in *.
unfold matrix_column_choose, list_vector_to_matrix.
unfold get_col.
do 2 (apply functional_extensionality; intros).
apply f_equal_inv with (x := x) in H4.
apply f_equal_inv with (x := x0) in H4.
unfold Zero in H4.
bdestruct_all.
-- assert (H7 : @Zero n 1%nat = (fun i0 x1 y0 : nat => if (y0 =? 0)%nat then M x1 i0 else 0) m).
{ do 2 (apply functional_extensionality; intros).
unfold Zero.
bdestruct_all; trivial.
unfold WF_GenMatrix in H1.
assert (H8 : (x1 >= n)%nat \/ (m >= m)%nat). { right. lia. }
specialize (H1 x1 m H8).
rewrite H1.
reflexivity. }
rewrite H7.
assert (H8 : (fun y : nat =>
nth y
(map (fun i0 x1 y0 : nat => if (y0 =? 0)%nat then M x1 i0 else 0)
(List.seq 0 i ++ List.seq (i + 1) (m - i - 1)))
(fun x1 y0 : nat => if (y0 =? 0)%nat then M x1 m else 0) x 0%nat *
(if y <? i then - / u i 0%nat * u y x0 else - / u i 0%nat * u (y + 1)%nat x0))
=
(fun y : nat =>
(- / u i 0%nat)%G * ((M x (nth y (List.seq 0 i ++ List.seq (i + 1) (m - i - 1)) m)) *
(if y <? i then u y x0 else u (y + 1)%nat x0)))).
{ apply functional_extensionality; intros.
rewrite map_nth with (d := m).
bdestruct_all; ring. }
setoid_rewrite H8.
setoid_rewrite <- @big_sum_scale_l with (H7 := F_is_module_space).
simpl.
apply Gmult_cancel_l with (a := (- u i 0%nat)%G); auto.
++ intro H9.
rewrite Fopp_opp in H9.
replace (- 0%G) with 0%G in H9 by ring.
contradiction.
++ rewrite Gmult_assoc.
replace (- u i 0%nat * - / u i 0%nat)%G with 1%G.
** rewrite Gmult_1_l .
rewrite Gmult_comm.
rewrite <- Fopp_mult_distr_r.
apply Fplus_inv_r with (c := (M x i * u i 0%nat)%G).
replace (- (M x i * u i 0%nat) + M x i * u i 0%nat)%G with 0%G by ring.
rewrite <- H4 at 1.
assert (H9 : Σ
(fun x1 : nat =>
M x (nth x1 (List.seq 0 i ++ List.seq (i + 1) (m - i - 1)) m) *
(if x1 <? i then u x1 x0 else u (x1 + 1)%nat x0))
(length (List.seq 0 i ++ List.seq (i + 1) (m - i - 1))) +
M x i * u i 0%nat
=
Σ
(fun x1 : nat =>
M x (nth x1 (List.seq 0 i ++ List.seq (i + 1) (m - i - 1) ++ [i]) m) *
(if (x1 =? m-1)%nat then u i 0%nat else
(if x1 <? i then u x1 x0 else u (x1 + 1)%nat x0)))
(length (List.seq 0 i ++ List.seq (i + 1) (m - i - 1) ++ [i]))).
{ rewrite app_assoc.
setoid_rewrite app_length at 2.
simpl.
Search ((?x + 1)%nat = Datatypes.S ?x).
setoid_rewrite Nat.add_1_r at 6.
rewrite <- @big_sum_extend_r with (H := R0).
simpl.
assert (H9 : (length (List.seq 0 i ++ List.seq (i + 1) (m - i - 1))) = (m-1)%nat).
{ rewrite app_length.
rewrite ! seq_length.
lia. }
rewrite ! H9.
bdestruct_all.
rewrite <- H9 at 3.
rewrite nth_middle with (a := i) (l' := []).
f_equal.
apply big_sum_eq_bounded.
intros x1 H12.
bdestruct_all.
- setoid_rewrite app_nth1 at 2.
+ reflexivity.
+ rewrite app_length.
rewrite ! seq_length.
lia.
- setoid_rewrite app_nth1 at 2.
+ reflexivity.
+ rewrite app_length.
rewrite ! seq_length.
lia. }
rewrite H9.
rewrite ! app_length.
rewrite ! seq_length.
simpl.
replace (i + (m - i - 1 + 1))%nat with m by lia.
assert (H10 : Σ (fun y : nat => M x y * u y x0) m
=
Σ (fun y : nat => M x (nth y (List.seq 0 m) m) *
u (nth y (List.seq 0 m) m) x0) m).
{ apply big_sum_eq_bounded.
intros x1 H10.
rewrite seq_nth. simpl.
ring.