-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixInversion.v
1260 lines (1182 loc) · 43.1 KB
/
MatrixInversion.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 Coq.Lists.List.
Require Import Coq.Setoids.Setoid.
Require Import Coq.Classes.Morphisms.
Require Import Coq.setoid_ring.Ring.
Require Import Coq.setoid_ring.Ring_theory.
Require Import Field_theory.
Require Import Field_tac.
Require Import PeanoNat.
Require Import Arith.
Require Import Omega.
Require Import Matrix.
Require Import MyHelpers.
Section MatrixInversion.
Variable E: MatrixElem.
Variable M: @Matrix E.
Add Field MatrixInversionEtField' : MEfield.
Variable n: nat.
Set Implicit Arguments.
Parameter is_eq_dec : forall x y: MEt, { eq x y } + { ~ eq x y }.
Lemma non_trivial_ring:
e0 <> e1.
Proof.
Print field_theory.
intro.
symmetry in H.
apply MEfield.(F_1_neq_0).
assumption.
Qed.
Lemma normal_field_knowledge:
forall r, r <> e0 -> MEinv r <> e0.
Proof.
intros.
assert (MEinv r *e r = e1) by (field; assumption).
unfold not; intros.
rewrite H1 in H0.
assert (e0 *e r = e0) by field.
rewrite H2 in H0.
assert (e0 <> e1) by (apply non_trivial_ring).
contradiction.
Qed.
Definition I := @I n E M.
Definition e := @e n E M.
Definition row_mul := @row_mul n E M.
Definition row_add_to_row := @row_add_to_row n E M.
Definition swap := @swap n E M.
Definition invertible (M : Mt n n) :=
exists M', M @* M' @= I /\ M' @* M @= I.
Lemma I_is_invertible:
invertible I.
Proof.
exists I.
split; apply I_is_identity.
Qed.
Lemma AB_BA:
forall A B, invertible A -> A @* B @= I -> B @* A @= I.
Proof.
intros.
unfold invertible in H.
inversion H.
inversion H1.
rename x into B'.
assert (B' @* (A @* B) @= B' @* I).
{
setoid_rewrite H0.
reflexivity.
}
rewrite <- mult_assoc in H4.
rewrite H3 in H4.
rewrite I_is_left_identity in H4.
rewrite I_is_right_identity in H4.
rewrite H4.
assumption.
Qed.
Fixpoint GE_elemdown (A: Mt n n) (x: nat) (cur: nat) :=
match cur with
| O => (I, A)
| S cur' =>
let ee := row_add_to_row (n - cur) x (MEopp (Mget A (n - cur) x)) in
let (E', EA') := GE_elemdown (ee @* A) x cur' in
(E' @* ee, EA')
end.
Fixpoint get_first_none_zero (A: Mt n n) (i: nat) (y: nat) :=
match i with
| O => n
| S i' =>
if (is_eq_dec (Mget A (n - i) y) MEzero) then
get_first_none_zero A i' y
else
n - i
end.
Fixpoint GE_stage1 (A: Mt n n) (i: nat) :=
match i with
| O => Some (I, A)
| S i' =>
let r := get_first_none_zero A i (n - i) in
if (r =? n) then
None
else
let A0 := (swap (n - i) r) @* A in
let ee := (row_mul (n - i) (MEinv (Mget A0 (n - i) (n - i)))) in
let (E', EA') := GE_elemdown (ee @* A0) (n - i) (i - 1) in
let ret := GE_stage1 EA' i' in
match ret with
| None => None
| Some (E'', EA'') => Some (E'' @* E' @* ee @* swap (n - i) r, EA'')
end
end.
Fixpoint GE_elemup (A: Mt n n) (x: nat) (i: nat) :=
match i with
| O => (I, A)
| S i' =>
let ee := row_add_to_row i' x (MEopp (Mget A i' x)) in
let (E', EA') := GE_elemup (ee @* A) x i' in
(E' @* ee, EA')
end.
Fixpoint GE_stage2 (A: Mt n n) (i: nat) :=
match i with
| O => (I, A)
| S i' =>
let (E', EA') := GE_elemup (A) i' i' in
let (E'', EA'') := GE_stage2 EA' i' in
(E'' @* E', EA'')
end.
Definition Inversion (A: Mt n n) :=
match GE_stage1 A n with
| None => None
| Some (E, EA) => Some (fst (GE_stage2 EA n) @* E)
end.
Hint Rewrite @Mfill_correct @Melementwise_op_correct @get_element_e @get_element_row_mul @get_element_row_add_to_row @get_element_swap: MMM.
Ltac urgh :=
repeat match goal with
| _ => discriminate
| _ => progress subst
| _ => field
| _ => progress auto
| _ => omega
| _ => progress autorewrite with MMM
| [ |- context[let (_, _) := ?x in _]] => destruct x eqn: ?
| _ => progress elim_bool
| [ |- context[?x <? ?y]] => destruct (x <? y) eqn: ?
| [ |- context[?x <=? ?y]] => destruct (x <=? y) eqn: ?
| [ |- context[match ?x with | _ => _ end]] => destruct (x) eqn: ?
| [H: context[let (_, _) := ?x in _] |- _] => destruct x eqn: ?
| [H: context[?x =? ?y] |- _] => destruct (x =? y) eqn: ?
| [H: context[?x <? ?y] |- _] => destruct (x <? y) eqn: ?
| [H: context[?x <=? ?y] |- _] => destruct (x <=? y) eqn: ?
| [H: context[match ?x with | _ => _ end] |- _] => destruct (x) eqn: ?
end.
Lemma GE_elemdown_correct_1 :
forall A x cur,
x < n -> cur < n - x ->
(fst (GE_elemdown A x cur) @* A) @= snd (GE_elemdown A x cur).
Proof.
intros.
generalize dependent A.
induction cur.
- intros. simpl. apply I_is_identity.
- assert (cur < n - x) by omega. intros.
eapply IHcur in H1.
simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq.
simpl.
rewrite eq in H1. simpl in H1.
rewrite <- mult_assoc in H1.
assumption.
Qed.
Lemma GE_elemdown_correct_keep :
forall A x cur,
x < n -> cur < n - x -> Mget A x x = e1 ->
forall i j, i < n - cur -> j < n -> Mget (snd (GE_elemdown A x cur)) i j = Mget A i j.
Proof.
intros.
generalize dependent A.
generalize dependent i.
generalize dependent j.
induction cur.
- intros.
simpl.
reflexivity.
- intros.
simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq.
simpl.
assert (snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)@[i, j] = (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A)@[i, j]).
{
apply IHcur; auto; try omega.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto.
omega.
}
rewrite eq in H4. simpl in H4.
rewrite H4.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto.
omega.
Qed.
Lemma GE_elemdown_correct_2 :
forall A x cur,
x < n -> cur < n - x -> Mget A x x = e1 ->
forall y, y >= n - cur -> y < n -> Mget (snd (GE_elemdown A x cur)) y x = e0.
Proof.
intros.
generalize dependent A.
generalize dependent y.
induction cur.
- intros.
simpl. omega.
- intros.
destruct (beq_nat y (n - S cur)) eqn: eq; elim_bool.
+ simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq2.
simpl.
assert (m0@[y, x] = (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A)@[y, x]).
{
assert (m0 = snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq2; auto).
rewrite H4.
apply GE_elemdown_correct_keep; auto; try omega.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto. omega.
}
rewrite H4.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
rewrite <- eq0.
rewrite H1.
ring.
+ simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq2.
simpl.
assert (m0 = snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq2; auto).
rewrite H4.
apply IHcur; auto; try omega.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto. omega.
Qed.
Definition lower_left_zeros (A: Mt n n) (L: nat) :=
forall i j,
i < n -> j < n -> j < L -> i > j -> Mget A i j = e0.
Lemma GE_elemdown_correct_keep_0:
forall A x cur,
x < n -> cur < n - x -> Mget A x x = e1 -> lower_left_zeros A x ->
lower_left_zeros (snd (GE_elemdown A x cur)) x.
Proof.
intros.
generalize dependent A.
induction cur.
- intros.
simpl.
assumption.
- intros.
simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq.
simpl.
unfold lower_left_zeros in *.
intros.
destruct (i <? (n - S cur)) eqn: eq2.
+ elim_bool.
replace (m0) with (snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq; auto).
assert (e0 = (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A)@[i, j]).
{
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
rewrite H2; auto.
}
rewrite H7.
apply GE_elemdown_correct_keep; auto; try omega.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
+ destruct (i =? (n - S cur)) eqn: eq3; elim_bool; auto; try omega.
* subst.
replace (m0) with (snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq; auto).
assert (e0 = (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A)@[n - S cur, j]).
{
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
rewrite H2; auto.
replace (A@[x, j]) with e0 by (rewrite H2; auto).
ring.
}
rewrite H7.
apply GE_elemdown_correct_keep; auto; try omega.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
* replace (m0) with (snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq; auto).
apply IHcur; auto; try omega.
--- rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
--- intros.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
rewrite H2; auto.
replace (A@[x, j0]) with e0 by (rewrite H2; auto).
ring.
Qed.
Lemma GE_elemdown_correct_extend_0:
forall A x,
x < n -> Mget A x x = e1 -> lower_left_zeros A x ->
lower_left_zeros (snd (GE_elemdown A x (n - x - 1))) (x + 1).
Proof.
intros.
unfold lower_left_zeros.
intros.
destruct (j =? x) eqn: eq; elim_bool.
- rewrite eq. apply GE_elemdown_correct_2; auto; omega.
- apply GE_elemdown_correct_keep_0; auto; omega.
Qed.
Lemma get_first_none_zero_at_least:
forall A i j, get_first_none_zero A i j >= n - i.
Proof.
intros.
induction i.
- simpl. omega.
- simpl.
destruct (is_eq_dec (A@[n - S i, j]) e0); omega.
Qed.
Lemma get_first_none_zero_at_most:
forall A i j, get_first_none_zero A i j <= n.
Proof.
intros.
induction i.
- simpl. omega.
- simpl.
destruct (is_eq_dec (A@[n - S i, j]) e0); omega.
Qed.
Lemma get_first_none_zero_correct:
forall A i j, get_first_none_zero A i j < n -> A@[get_first_none_zero A i j, j] <> e0.
Proof.
intros.
induction i.
- simpl. simpl in H. omega.
- simpl; urgh.
simpl in H.
urgh.
Qed.
Lemma GE_stage1_correct_1:
forall A i E EA,
i <= n -> GE_stage1 A i = Some (E, EA) ->
E @* A @= EA.
Proof.
intros.
generalize dependent A.
generalize dependent E0.
generalize dependent EA.
induction i; intros.
- simpl in H0. inversion H0; subst.
apply I_is_left_identity.
- unfold GE_stage1 in H0.
fold GE_stage1 in H0.
urgh.
remember (swap (n - S i) (get_first_none_zero A (S i) (n - S i)) @* A) as A0.
remember (row_mul (n - S i) (MEinv (A0@[n - S i, n - S i])) @* A0) as A1.
inversion H0.
replace ((if is_eq_dec (A@[n - S i, n - S i]) e0
then get_first_none_zero A i (n - S i)
else n - S i)) with (get_first_none_zero A (S i) (n - S i)) by (auto).
rewrite mult_assoc.
rewrite <- HeqA0.
assert (m @* A1 @= m0).
{
replace m with (fst (GE_elemdown A1 (n - S i) (S i - 1))) by (rewrite Heqp; auto).
replace m0 with (snd (GE_elemdown A1 (n - S i) (S i - 1))) by (rewrite Heqp; auto).
apply GE_elemdown_correct_1; urgh.
}
destruct (GE_stage1 m0 i) eqn: eq3.
* destruct p.
apply IHi in eq3; try omega.
rewrite mult_assoc.
rewrite mult_assoc.
rewrite <- HeqA1.
rewrite H1.
rewrite <- H3.
inversion Heqo.
rewrite <- H5.
rewrite <- H6.
assumption.
* inversion Heqo.
Qed.
Lemma GE_stage1_correct_keep :
forall A i E EA,
i <= n -> GE_stage1 A i = Some (E, EA) ->
forall x y, x < n - i -> y < n -> Mget EA x y = Mget A x y.
Proof.
intros A i.
generalize dependent A.
induction i; intros.
- simpl in H0.
inversion H0; subst.
reflexivity.
- simpl in H0; urgh.
+
remember (swap (n - S i) (get_first_none_zero A i (n - S i)) @* A) as A0.
remember (row_mul (n - S i) (MEinv (A0@[n - S i, n - S i])) @* A0) as A1; try rewrite <- HeqA0 in *; try rewrite <- HeqA1 in *.
inversion H0.
rewrite <- H5.
rewrite IHi with (A := m0) (EA := m2) (E0 := m1); auto; try omega.
replace (m0) with (snd (GE_elemdown A1 (n - S i) (i - 0))) by (rewrite Heqp; auto).
rewrite GE_elemdown_correct_keep; auto; try omega.
*
apply transitivity with (y0 := A0@[x, y]).
--- rewrite HeqA1.
rewrite get_element_row_mul; urgh;
assert (get_first_none_zero A i (n - S i) <= n) by apply get_first_none_zero_at_most;
omega.
--- rewrite HeqA0.
rewrite get_element_swap; urgh.
assert (get_first_none_zero A i (n - S i) >= n - i) by apply get_first_none_zero_at_least.
omega.
assert (get_first_none_zero A i (n - S i) <= n) by apply get_first_none_zero_at_most.
omega.
* rewrite HeqA1.
rewrite get_element_row_mul; urgh.
apply get_first_none_zero_correct.
assert (get_first_none_zero A i (n - S i) <= n) by apply get_first_none_zero_at_most;
omega.
assert (get_first_none_zero A i (n - S i) <= n) by apply get_first_none_zero_at_most;
omega.
+ remember (swap (n - S i) (n - S i) @* A) as A0; remember (row_mul (n - S i) (MEinv (A0@[n - S i, n - S i])) @* A0) as A1; try rewrite <- HeqA0 in *; try rewrite <- HeqA1 in *.
inversion H0.
rewrite <- H5.
rewrite IHi with (A := m0) (EA := m2) (E0 := m1); auto; try omega.
replace (m0) with (snd (GE_elemdown A1 (n - S i) (i - 0))) by (rewrite Heqp; auto).
rewrite GE_elemdown_correct_keep; auto; try omega.
*
apply transitivity with (y0 := A0@[x, y]).
--- rewrite HeqA1.
rewrite get_element_row_mul; urgh.
--- rewrite HeqA0.
rewrite get_element_swap; urgh.
* rewrite HeqA1.
rewrite get_element_row_mul; urgh.
Qed.
Definition Diag_ones (A: Mt n n) (L: nat) :=
forall i,
i < n -> i < L -> Mget A i i = e1.
Lemma GE_stage1_extend_ones :
forall A i E EA,
i <= n -> Diag_ones A (n - i) -> GE_stage1 A i = Some (E, EA) ->
Diag_ones EA n.
Proof.
intros.
generalize dependent A.
generalize dependent E0.
generalize dependent EA.
induction i; intros; urgh.
- simpl in H1. inversion H1; subst.
replace n with (n - 0) by omega.
assumption.
- unfold Diag_ones.
intros.
unfold GE_stage1 in H1; urgh. fold GE_stage1 in *.
remember (swap (n - S i) (get_first_none_zero A (S i) (n - S i)) @* A) as A0.
remember (row_mul (n - S i) (MEinv (A0@[n - S i, n - S i])) @* A0) as A1; try rewrite <- HeqA0 in *; try rewrite <- HeqA1 in *.
assert (get_first_none_zero A (S i) (n - S i) <= n) by apply get_first_none_zero_at_most.
assert (get_first_none_zero A (S i) (n - S i) >= n - S i) by apply get_first_none_zero_at_least.
assert (Diag_ones m0 (n - i)).
{
unfold Diag_ones; intros.
replace (m0) with (snd (GE_elemdown A1 (n - S i) (S i - 1))) by (rewrite Heqp; auto).
rewrite GE_elemdown_correct_keep; auto; try omega.
+ rewrite HeqA1.
rewrite get_element_row_mul; elim_bool; auto; try omega.
* rewrite eq; field.
rewrite HeqA0. rewrite get_element_swap; urgh.
--- apply get_first_none_zero_correct.
omega.
* rewrite HeqA0.
rewrite get_element_swap; urgh.
apply H0; urgh.
+ rewrite HeqA1; urgh.
apply get_first_none_zero_correct.
omega.
}
apply IHi with (E0:=m1) (EA:=m2) in H6 ; auto; try omega.
inversion H1.
rewrite <- H9.
apply H6; auto.
Qed.
Lemma GE_stage1_extend_zeros :
forall A i E EA,
i <= n -> lower_left_zeros A (n - i) -> GE_stage1 A i = Some (E, EA) ->
lower_left_zeros EA n.
Proof.
intros A i.
generalize dependent A.
induction i; intros; urgh.
- replace n with (n - 0) by omega.
simpl in H1.
inversion H1.
rewrite <- H4.
assumption.
- unfold GE_stage1 in H1; urgh. fold GE_stage1 in *.
remember (swap (n - S i) (get_first_none_zero A (S i) (n - S i)) @* A) as A0.
remember (row_mul (n - S i) (MEinv (A0@[n - S i, n - S i])) @* A0) as A1; try rewrite <- HeqA0 in *; try rewrite <- HeqA1 in *.
assert (get_first_none_zero A (S i) (n - S i) <= n) by apply get_first_none_zero_at_most.
assert (get_first_none_zero A (S i) (n - S i) >= n - S i) by apply get_first_none_zero_at_least.
assert (lower_left_zeros m0 (n - i)).
{
unfold lower_left_zeros.
intros.
replace (m0) with (snd (GE_elemdown A1 (n - S i) (S i - 1))) by (rewrite Heqp; auto).
replace (S i - 1) with (n - (n - S i) - 1) by omega.
apply GE_elemdown_correct_extend_0 with (x := n - S i); urgh.
+
apply get_first_none_zero_correct; urgh.
+ unfold lower_left_zeros; intros.
rewrite get_element_row_mul; urgh.
* replace ( A@[get_first_none_zero A (S i) (n - S i), j0]) with e0 by (rewrite <- H0; auto; omega).
ring.
* rewrite <- H0; auto; omega.
}
apply IHi with (A := m0) (E0 := m1); auto; try omega.
inversion H1.
rewrite <- H7.
assumption.
Qed.
Definition normalized_upper_triangle (A: Mt n n) :=
Diag_ones A n /\ lower_left_zeros A n.
Theorem GE_stage1_correct:
forall A E EA,
GE_stage1 A n = Some (E, EA) ->
E @* A @= EA /\ normalized_upper_triangle EA.
Proof.
intros.
split.
- eapply GE_stage1_correct_1; eauto.
- unfold normalized_upper_triangle.
split.
+ eapply GE_stage1_extend_ones; eauto.
unfold Diag_ones. intros. omega.
+ eapply GE_stage1_extend_zeros; eauto.
unfold lower_left_zeros; intros. omega.
Qed.
Lemma GE_elemup_correct_1 :
forall A x i,
x < n -> i <= x ->
(fst (GE_elemup A x i) @* A) @= snd (GE_elemup A x i).
Proof.
intros.
generalize dependent A.
induction i.
- intros. simpl. apply I_is_identity.
- assert (i <= x) by omega. intros.
eapply IHi in H1.
simpl.
destruct (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i) eqn: eq.
simpl.
rewrite eq in H1. simpl in H1.
rewrite <- mult_assoc in H1.
assumption.
Qed.
Definition upper_right_zeros (A: Mt n n) (L: nat) :=
forall i j,
i < n -> j < n -> j >= n - L -> i < j -> Mget A i j = e0.
Lemma nut_preserve:
forall A x i',
x < n -> i' < x -> normalized_upper_triangle A ->
normalized_upper_triangle ((row_add_to_row i' x (MEopp (Mget A i' x))) @* A).
Proof.
intros.
unfold normalized_upper_triangle.
inversion H1.
unfold Diag_ones in H2. unfold lower_left_zeros in H3.
split.
+ unfold Diag_ones; intros.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
replace (A@[x, i]) with e0 by (rewrite H3; auto; omega).
replace (A@[i, i]) with e1 by (rewrite H2; auto; omega).
ring.
+ unfold lower_left_zeros; intros.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
replace (A@[i, j]) with e0 by (rewrite H3; auto; omega).
replace (A@[x, j]) with e0 by (rewrite H3; auto; omega).
ring.
Qed.
Lemma GE_elemup_correct_2 :
forall A x i,
x < n -> i <= x -> normalized_upper_triangle A
-> normalized_upper_triangle (snd (GE_elemup A x i)).
Proof.
intros.
generalize dependent A.
generalize dependent x.
induction i; intros.
- simpl. assumption.
- simpl.
destruct (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i) eqn: eq.
replace (snd (m @* row_add_to_row i x (MEopp (A@[i, x])), m0)) with (snd (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i)) by (rewrite eq; auto).
apply IHi; auto; try omega.
apply nut_preserve; auto.
Qed.
Lemma GE_elemup_correct_keep :
forall A x i,
x < n -> i <= x -> normalized_upper_triangle A ->
forall i' j, i' < n -> i' >= i -> j < n -> Mget (snd (GE_elemup A x i)) i' j = Mget A i' j.
Proof.
intros.
generalize dependent A.
generalize dependent i.
generalize dependent j.
generalize dependent i'.
generalize dependent x.
induction i.
- intros.
simpl.
reflexivity.
- intros.
simpl.
destruct (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i) eqn: eq.
simpl.
assert (snd (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i)@[i', j] = (row_add_to_row i x (MEopp (A@[i, x])) @* A)@[i', j]).
{
apply IHi; auto; try omega.
apply nut_preserve; auto; try omega.
}
rewrite eq in H5. simpl in H5.
rewrite H5.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto.
omega.
Qed.
Lemma GE_elemup_correct_3 :
forall A x i,
x < n -> i <= x -> normalized_upper_triangle A ->
(forall i0, i0 < i -> (snd (GE_elemup A x i))@[i0, x] = e0).
Proof.
intros.
generalize dependent A.
generalize dependent x.
generalize dependent i.
generalize dependent i0.
induction i; intros.
- simpl. omega.
- simpl.
inversion H1.
unfold Diag_ones in H3.
unfold lower_left_zeros in H4.
destruct (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i) eqn: eq.
replace (snd (m @* row_add_to_row i x (MEopp (A@[i, x])), m0)) with (snd (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i)) by (rewrite eq; auto).
destruct (i0 =? i) eqn: eq2; elim_bool; auto.
+ rewrite GE_elemup_correct_keep; auto; try omega.
* rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
replace (A@[x, x]) with e1 by (rewrite H3; auto; omega).
rewrite eq0.
ring.
* apply nut_preserve; auto; try omega.
+ rewrite IHi; auto; try omega.
apply nut_preserve; auto; try omega.
Qed.
Lemma GE_elemup_correct_4 :
forall A x i L ,
x < n -> i <= x -> L < n - x -> normalized_upper_triangle A -> upper_right_zeros A L ->
upper_right_zeros (snd (GE_elemup A x i)) L.
Proof.
intros.
generalize dependent A.
generalize dependent x.
generalize dependent L.
induction i; intros; try assumption.
simpl.
destruct (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i) eqn: eq.
replace (snd (m @* row_add_to_row i x (MEopp (A@[i, x])), m0)) with (snd (GE_elemup (row_add_to_row i x (MEopp (A@[i, x])) @* A) x i)) by (rewrite eq; auto).
apply IHi; auto; try omega.
- apply nut_preserve; auto; omega.
- unfold upper_right_zeros.
intros.
rewrite get_element_row_add_to_row; auto; try omega.
elim_bool; auto; try omega.
rewrite eq0.
replace (A@[i, j]) with e0 by (rewrite H3; auto; omega).
replace (A@[x, j]) with e0 by (rewrite H3; auto; omega).
ring.
Qed.
Lemma GE_elemup_correct_5:
forall A x,
x < n -> normalized_upper_triangle A -> upper_right_zeros A (n - x - 1) ->
upper_right_zeros (snd (GE_elemup A x x)) (n - x).
Proof.
intros.
unfold upper_right_zeros.
intros.
destruct (j =? x) eqn: eq; elim_bool; auto.
- rewrite eq. apply GE_elemup_correct_3; auto; try omega.
- rewrite GE_elemup_correct_4 with (L := n - x - 1);auto; try omega.
Qed.
Lemma GE_stage2_correct_1:
forall A i,
i <= n ->
fst (GE_stage2 A i) @* A @= snd (GE_stage2 A i).
Proof.
intros.
generalize dependent A.
induction i.
- intros; simpl.
apply I_is_identity.
- intros.
simpl.
destruct (GE_elemup A i i) eqn: eq1.
destruct (GE_stage2 m0 i) eqn: eq2.
simpl.
rewrite mult_assoc.
replace (m) with (fst (GE_elemup A i i)) by (rewrite eq1; auto).
rewrite GE_elemup_correct_1; auto; try omega.
replace (m1) with (fst (GE_stage2 m0 i)) by (rewrite eq2; auto).
replace (m2) with (snd (GE_stage2 m0 i)) by (rewrite eq2; auto).
replace (m0) with (snd (GE_elemup A i i)) by (rewrite eq1; auto).
apply IHi; auto; try omega.
Qed.
Lemma GE_stage2_correct_2:
forall A i,
i <= n -> normalized_upper_triangle A ->
normalized_upper_triangle (snd (GE_stage2 A i)).
Proof.
intros.
generalize dependent A.
induction i.
- intros. simpl. assumption.
- intros; simpl.
destruct (GE_elemup A i i) eqn: eq1.
destruct (GE_stage2 m0 i) eqn: eq2.
simpl.
replace (m2) with (snd (GE_stage2 m0 i)) by (rewrite eq2; auto).
apply IHi; auto; try omega.
replace (m0) with (snd (GE_elemup A i i)) by (rewrite eq1; auto).
apply GE_elemup_correct_2; auto; try omega.
Qed.
Lemma GE_stage2_correct_3:
forall A i,
i <= n -> normalized_upper_triangle A -> upper_right_zeros A (n - i) ->
upper_right_zeros (snd (GE_stage2 A i)) n.
Proof.
intros.
generalize dependent A.
induction i.
- intros; simpl. replace (n) with (n - 0) by omega. assumption.
- intros; simpl.
destruct (GE_elemup A i i) eqn: eq1.
destruct (GE_stage2 m0 i) eqn: eq2.
simpl.
replace (m2) with (snd (GE_stage2 m0 i)) by (rewrite eq2; auto).
apply IHi; auto; try omega.
+ replace (m0) with (snd (GE_elemup A i i)) by (rewrite eq1; auto).
apply GE_elemup_correct_2; auto; try omega.
+ replace (m0) with (snd (GE_elemup A i i)) by (rewrite eq1; auto).
apply GE_elemup_correct_5; auto; try omega.
replace (n - i - 1) with (n - S i) by omega.
assumption.
Qed.
Theorem GE_stage2_correct:
forall A,
normalized_upper_triangle A ->
fst (GE_stage2 A n) @* A @= snd (GE_stage2 A n) /\ snd (GE_stage2 A n) @= I.
Proof.
intros.
split.
- apply GE_stage2_correct_1. auto.
- unfold "@=".
intros.
destruct (j <=? i) eqn: eq; elim_bool; auto; try omega.
+ destruct (j =? i) eqn: eq2; elim_bool; auto; try omega.
* subst.
unfold I. unfold Matrix.I.
rewrite Mfill_correct; elim_bool; auto; try omega.
apply GE_stage2_correct_2; auto.
* unfold I. unfold Matrix.I.
rewrite Mfill_correct; elim_bool; auto; try omega.
apply GE_stage2_correct_2; auto; omega.
+ unfold I. unfold Matrix.I.
rewrite Mfill_correct; elim_bool; auto; try omega.
apply GE_stage2_correct_3; auto; try omega.
unfold upper_right_zeros; intros.
omega.
Qed.
Theorem Inversion_correct:
forall A E,
Inversion A = Some E -> E @* A @= I.
Proof.
intros.
unfold Inversion in H.
destruct (GE_stage1 A n) eqn: eq; try inversion H.
clear H1.
destruct p.
inversion H. clear H.
assert (m @* A @= m0 /\ normalized_upper_triangle m0) by (apply GE_stage1_correct; assumption).
inversion H. clear H.
rewrite mult_assoc.
rewrite H0.
assert ((snd (GE_stage2 m0 n)) @= I) by (apply GE_stage2_correct; auto).
rewrite <- H.
apply GE_stage2_correct.
assumption.
Qed.
Lemma invertible_closed:
forall A B,
invertible A -> invertible B -> invertible (A @* B).
Proof.
intros.
unfold invertible in *.
inversion H.
inversion H0.
exists (x0 @* x).
split.
- rewrite mult_assoc.
assert (B @* (x0 @* x) @= ((B @* x0) @* x)) by (rewrite mult_assoc; reflexivity).
rewrite H3.
inversion H2.
rewrite H4.
rewrite I_is_left_identity.
apply H1.
- rewrite mult_assoc.
assert ((x @* (A @* B)) @= ((x @* A) @* B)) by (rewrite mult_assoc; reflexivity).
rewrite H3.
inversion H1.
rewrite H5.
rewrite I_is_left_identity.
apply H2.
Qed.
Lemma row_mul_invertible:
forall i x,
i < n -> x <> MEzero -> invertible (row_mul i x).
Proof.
intros.
unfold invertible.
exists (row_mul i (MEinv x)).
split.
- unfold Meq.
intros.
rewrite get_element_row_mul; auto; try omega.
destruct (i0 =? i) eqn: eq; elim_bool.
+ unfold row_mul. unfold Matrix.row_mul. rewrite Mfill_correct; urgh.
* unfold I; unfold Matrix.I; urgh.
* unfold I; unfold Matrix.I; urgh.
+ unfold row_mul; unfold Matrix.row_mul; urgh.
* unfold I; unfold Matrix.I; urgh.
* unfold I; unfold Matrix.I; urgh.
- unfold Meq; intros.
rewrite get_element_row_mul; urgh.
+ unfold row_mul; unfold Matrix.row_mul; unfold I; unfold Matrix.I; urgh.
+ unfold row_mul; unfold Matrix.row_mul; unfold I; unfold Matrix.I; urgh.
Qed.
Lemma row_add_to_row_invertible:
forall x y c,
x < n -> y < n -> x <> y -> invertible (row_add_to_row x y c).
Proof.
intros.
unfold invertible.
exists (row_add_to_row x y (MEopp c)).
split; unfold Meq; intros; rewrite get_element_row_add_to_row; urgh; unfold row_add_to_row; unfold Matrix.row_add_to_row; unfold I; unfold Matrix.I; unfold e; unfold Matrix.e; urgh; simpl in *; try inversion eq7; try inversion eq8; try inversion eq4; try field.
Qed.
Lemma swap_invertible:
forall x y,
x < n -> y < n -> invertible (swap x y).
Proof.
intros.
unfold invertible.
exists (swap x y).
split; unfold Meq; intros; unfold I; unfold Matrix.I; unfold swap; unfold Matrix.swap; urgh; simpl; rewrite Mtimes_correct; auto; urgh.
- remember (if j =? x then y else if j =? y then x else j) as t.
apply sum_single with (x0 := t); intros; try rewrite Heqt; unfold I; unfold Matrix.I; unfold e; unfold Matrix.e; urgh.
- apply sum_e0';intros; unfold I; unfold Matrix.I; unfold e; unfold Matrix.e; urgh.
- remember (if j =? x then y else if j =? y then x else j) as t.
apply sum_single with (x0 := t); intros; try rewrite Heqt; unfold I; unfold Matrix.I; unfold e; unfold Matrix.e; urgh.
- apply sum_e0';intros; unfold I; unfold Matrix.I; unfold e; unfold Matrix.e; urgh.
Qed.
Lemma GE_elemdown_preserve_invertibility:
forall A x cur,
x < n -> cur < n - x -> invertible A ->
invertible (snd (GE_elemdown A x cur)).
Proof.
intros.
generalize dependent A.
generalize dependent x.
induction cur; intros.
- simpl. assumption.
- simpl.
destruct (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur) eqn: eq.
simpl.
replace (m0) with (snd (GE_elemdown (row_add_to_row (n - S cur) x (MEopp (A@[n - S cur, x])) @* A) x cur)) by (rewrite eq; auto).
apply IHcur; try omega.
apply invertible_closed; try assumption.
apply row_add_to_row_invertible; omega.
Qed.
Lemma kernal_span:
forall F: (nat -> nat -> MEt), forall i: nat,
(forall j k, j <= i -> k > j -> k < n -> F j k = e0) ->
(forall j, j < i -> F j j = e1) ->
F i i = e0 ->
(exists c: nat -> MEt, forall k, k < n -> F i k = sum i (fun j => (c j) *e (F j k))).
Proof.
intros.
generalize dependent F.
induction i.
- intros.
exists (fun x => e0).
intros.
simpl.
destruct (k =? 0) eqn: eq; urgh.
apply H; omega.
- intros.
assert (exists c: nat -> MEt, forall k: nat, k < n -> (if i <? i then F i k else F (S i) k -e F i k *e F (S i) i) = sum i (fun j => (c j) *e (if j <? i then F j k else F (S i) k -e F i k *e F (S i) i))).
{
apply IHi with (F := (fun x y => if x <? i then F x y else (F (S i) y) -e (F i y *e F (S i) i))).
- intros.
urgh.
assert (i = j) by omega.
rewrite <- H5 in H3.
destruct (k =? i) eqn: eq2; urgh.
assert (F (S j) k = e0).
{
destruct (k =? S j) eqn: eq3; urgh.
rewrite H; try omega; reflexivity.
}
rewrite H5.
rewrite H; try omega.
ring.
- intros.
urgh.
- urgh.
rewrite H0; try omega.
ring.
}
inversion H2.
remember (x) as c'.
clear H2 Heqc' x.
exists (fun x => if x =? i then F (S i) i else c' x).
intros.
apply H3 in H2.
urgh.
rewrite sum_eq with (g := fun x => c' x *e F x k) in H2 .
+ rewrite sum_split with (g := fun x => (if x =? i then e0 else c' x) *e F x k) (h := fun x => (if x =? i then F (S i) i else e0) *e F x k).
* assert (fold_nat (S i)
(fun (acc : MEt) (x : nat) => acc +e (if x =? i then e0 else c' x) *e F x k) e0 = F (S i) k -e F i k *e F (S i) i).
{
simpl.