forked from coq-community/corn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixClass.v
1322 lines (1082 loc) · 55.4 KB
/
matrixClass.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
(*
Copyright © 2009 Valentin Blot
Permission is hereby granted, free of charge, to any person obtaining a copy of
this proof and associated documentation files (the "Proof"), to deal in
the Proof without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Proof, and to permit persons to whom the Proof is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Proof.
THE PROOF IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE PROOF OR THE USE OR OTHER DEALINGS IN THE PROOF.
*)
Require Import ssreflect ssrbool ssrfun eqtype ssrnat seq choice fintype.
Require Import finfun bigops ssralg groups perm zmodp morphisms.
Require Import Ring RingClass.
Require Import bigopsClass.
Require Import Setoid Morphisms.
Notation " x === y " := (Equivalence.equiv x y) (at level 70, no associativity).
Open Scope signature_scope.
Set Implicit Arguments.
Unset Strict Implicit.
Import Prenex Implicits.
Reserved Notation "''M_' n" (at level 8, n at level 2, format "''M_' n").
Reserved Notation "''M_' ( n )" (at level 8, only parsing).
Reserved Notation "''M_' ( m , n )" (at level 8, format "''M_' ( m , n )").
Reserved Notation "\matrix_ ( i , j ) E"
(at level 36, E at level 36, i, j at level 50,
format "\matrix_ ( i , j ) E").
Reserved Notation "\matrix_ ( i < m , j < n ) E"
(at level 36, E at level 36, i, m, j, n at level 50,
format "\matrix_ ( i < m , j < n ) E").
Reserved Notation "\matrix_ ( i , j < n ) E"
(at level 36, E at level 36, i, j, n at level 50,
format "\matrix_ ( i , j < n ) E").
Reserved Notation "x %:M" (at level 8, format "x %:M").
Reserved Notation "-m A" (at level 35, right associativity).
Reserved Notation "A +m B" (at level 50, left associativity).
Reserved Notation "A -m B" (at level 50, left associativity).
Reserved Notation "x *m: A" (at level 40, left associativity).
Reserved Notation "A *m B" (at level 40, left associativity).
Reserved Notation "A ^T" (at level 8).
Reserved Notation "\tr A" (at level 10, A at level 8, format "\tr A").
Reserved Notation "\det A" (at level 10, A at level 8, format "\det A").
Reserved Notation "\adj A" (at level 10, A at level 8, format "\adj A").
Delimit Scope matrix_scope with M.
Open Local Scope matrix_scope.
Definition setoid_cancel {A : Type} {B : Type} `{Equivalence A aeq} (f : A -> B) g :=
forall x, g (f x) === x.
Section MatrixDef.
Variable R : Type.
Variables m n : nat.
Definition matrix : Type := 'I_m -> 'I_n -> R.
End MatrixDef.
Notation "''M_' n" := (matrix _ n n) : type_scope.
Notation "''M_' ( n )" := 'M_n (only parsing) : type_scope.
Notation "''M_' ( m , n )" := (matrix _ m n) : type_scope.
Notation "\matrix_ ( i < m , j < n ) E" :=
(fun (i : 'I_m) (j : 'I_n) => E) (only parsing).
Notation "\matrix_ ( i , j < n ) E" :=
(\matrix_(i < n, j < n) E) (only parsing).
Notation "\matrix_ ( i , j ) E" := (\matrix_(i < _, j < _) E).
Section Slicing.
Context `{r_st : Equivalence R req}.
Definition mx_row m n i0 (A : 'M_(m, n)) :=
\matrix_(i < 1, j < n) (A i0 j : R).
Global Instance mx_row_morph m n i0 : Proper (Equivalence.equiv==>Equivalence.equiv) (@mx_row m n i0).
Proof. by move=> m n i0 A B eqAB i; apply eqAB. Qed.
Definition mx_col m n j0 (A : 'M_(m, n)) :=
\matrix_(i < m, j < 1) (A i j0 : R).
Global Instance mx_col_morph m n i0 : Proper (Equivalence.equiv==>Equivalence.equiv) (@mx_col m n i0).
Proof. by move=> m n i0 A B eqAB i j; apply eqAB. Qed.
Definition mx_row' m n i0 (A : 'M_(m, n)) :=
\matrix_(i, j) (A (lift i0 i) j : R).
Global Instance mx_row'_morph m n i0 : Proper (Equivalence.equiv==>Equivalence.equiv) (@mx_row' m n i0).
Proof. by move=> m n i0 A B eqAB i; apply eqAB. Qed.
Definition mx_col' m n j0 (A : 'M_(m, n)) :=
\matrix_(i, j) (A i (lift j0 j) : R).
Global Instance mx_col'_morph m n i0 : Proper (Equivalence.equiv==>Equivalence.equiv) (@mx_col' m n i0).
Proof. by move=> m n i0 A B eqAB i j; apply eqAB. Qed.
Definition rswap m n i1 i2 (A : 'M_(m, n)) :=
\matrix_(i, j) (A (tperm i1 i2 i) j : R).
Global Instance rswap_morph m n i1 i2 : Proper (Equivalence.equiv==>Equivalence.equiv) (@rswap m n i1 i2).
Proof. by move=> m n i1 i2 A B eqAB i; apply eqAB. Qed.
Definition cswap m n i1 i2 (A : 'M_(m, n)) :=
\matrix_(i, j) (A i (tperm i1 i2 j) : R).
Global Instance cswap_morph m n i1 i2 : Proper (Equivalence.equiv==>Equivalence.equiv) (@cswap m n i1 i2).
Proof. by move=> m n i1 i2 A B eqAB i j; apply eqAB. Qed.
Definition trmx m n (A : 'M_(m, n)) := \matrix_(i, j) (A j i : R).
Global Instance trmx_morph m n : Proper (Equivalence.equiv==>Equivalence.equiv) (@trmx m n).
Proof. by move=> m n A B eqAB i j; apply eqAB. Qed.
Lemma trmxK : forall m n, setoid_cancel (@trmx m n) (@trmx n m).
Proof. by move=> m n A i j; rewrite/trmx; reflexivity. Qed.
Lemma trmx_inj : forall m n (A B : 'M_(m, n)), trmx A === trmx B -> A === B.
Proof. by rewrite/trmx=> m n A B eqtr i j; apply eqtr. Qed.
Notation "A ^T" := (trmx A).
Lemma trmx_row : forall m n i0 (A : 'M_(m, n)),
(mx_row i0 A)^T === mx_col i0 A^T.
Proof. by rewrite/trmx/mx_row/mx_col=> m n i0 A i j; reflexivity. Qed.
Lemma trmx_row' : forall m n i0 (A : 'M_(m, n)),
(mx_row' i0 A)^T === mx_col' i0 A^T.
Proof. by rewrite/trmx/mx_row/mx_col=> m n i0 A i j; reflexivity. Qed.
Lemma trmx_col : forall m n j0 (A : 'M_(m, n)),
(mx_col j0 A)^T === mx_row j0 A^T.
Proof. by rewrite/trmx/mx_row/mx_col=> m n i0 A i j; reflexivity. Qed.
Lemma trmx_col' : forall m n j0 (A : 'M_(m, n)),
(mx_col' j0 A)^T === mx_row' j0 A^T.
Proof. by rewrite/trmx/mx_row/mx_col=> m n i0 A i j; reflexivity. Qed.
Lemma trmx_cswap : forall m n (A : 'M_(m, n)) i1 i2,
(cswap i1 i2 A)^T === rswap i1 i2 A^T.
Proof. by rewrite/trmx/rswap/cswap=> m n A i1 i2 i j; case tpermP; reflexivity. Qed.
Lemma trmx_rswap : forall m n (A : 'M_(m, n)) i1 i2,
(rswap i1 i2 A)^T === cswap i1 i2 A^T.
Proof. by rewrite/trmx/rswap/cswap=> m n A i1 i2 i j; case tpermP; reflexivity. Qed.
Lemma mx_row_id : forall n (A : 'M_(1, n)), mx_row ord0 A === A.
Proof. by move=> n A i j; (have -> : ord0 = i by rewrite (ord1 i); apply ord_inj => //); reflexivity. Qed.
Lemma mx_row_eq : forall m1 m2 n i1 i2 (A1 : 'M_(m1, n)) (A2 : 'M_(m2, n)),
mx_row i1 A1 === mx_row i2 A2 -> A1 i1 === A2 i2.
Proof.
rewrite/mx_row => m1 m2 n i1 i2 A1 A2 eqA1A2 j.
by apply eqA1A2; apply (@Ordinal 1 0).
Qed.
Lemma mx_row'_eq : forall m n i0 (A B : 'M_(m, n)),
mx_row' i0 A === mx_row' i0 B -> {in predC1 i0, forall i, A i === B i}.
Proof.
move=> m n i0 A B eqAB i; rewrite /mx_row' inE /= eq_sym.
by case/unlift_some=> i' -> _; apply: eqAB.
Qed.
Section CutPaste.
Variables m n1 n2 : nat.
(* The shape of the (dependent) width parameter of the type of A *)
(* determines where the cut is made! *)
Definition lcutmx (A : 'M_(m, n1 + n2)):=
\matrix_(i < m, j < n1) (A i (lshift n2 j) : R).
Global Instance lcutmx_morph : Proper (Equivalence.equiv==>Equivalence.equiv) lcutmx.
Proof. by move=> A B eqAB i j; apply eqAB. Qed.
Definition rcutmx (A : 'M_(m, n1 + n2)) :=
\matrix_(i < m, j < n2) (A i (rshift n1 j) : R).
Global Instance rcutmx_morph : Proper (Equivalence.equiv==>Equivalence.equiv) rcutmx.
Proof. by move=> A B eqAB i j; apply eqAB. Qed.
Definition pastemx (A1 : 'M_(m, n1)) (A2 : 'M_(m, n2)) :=
\matrix_(i < m, j < n1 + n2)
(match split j with inl j1 => A1 i j1 | inr j2 => A2 i j2 end : R).
Global Instance pastemx_morph : Proper (Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv) pastemx.
Proof.
rewrite/pastemx=> A1 B1 eqAB1 A2 B2 eqAB2 i j; case: (splitP j)=> j' _; first by apply eqAB1.
by apply eqAB2.
Qed.
Lemma pastemxEl : forall A1 A2 i j, pastemx A1 A2 i (lshift n2 j) === A1 i j.
Proof. by rewrite/pastemx=> A1 A2 i j; rewrite (unsplitK (inl _ _)); reflexivity. Qed.
Lemma pastemxEr : forall A1 A2 i j, pastemx A1 A2 i (rshift n1 j) === A2 i j.
Proof. by rewrite/pastemx=> A1 A2 i j; rewrite (unsplitK (inr _ _)); reflexivity. Qed.
Lemma pastemxKl : forall A1 A2, lcutmx (pastemx A1 A2) === A1.
Proof. by move=> A1 A2 i j; rewrite /lcutmx; rewrite -> pastemxEl; reflexivity. Qed.
Lemma pastemxKr : forall A1 A2, rcutmx (pastemx A1 A2) === A2.
Proof. by move=> A1 A2 i j; rewrite /rcutmx; rewrite -> pastemxEr; reflexivity. Qed.
Lemma cutmxK : forall A, pastemx (lcutmx A) (rcutmx A) === A.
Proof.
move=> A i j.
rewrite/pastemx/lcutmx/rcutmx.
case: splitP; case=> /= k kprf eqk.
by have <- : j = lshift n2 (Ordinal kprf); [apply ord_inj; apply eqk | reflexivity].
by have <- : j = rshift n1 (Ordinal kprf); [apply ord_inj; apply eqk | reflexivity].
Qed.
End CutPaste.
Lemma mx_row_paste : forall m n1 n2 i0 (A1 : 'M_(m, n1)) (A2 : 'M_(m, n2)),
mx_row i0 (pastemx A1 A2) === pastemx (mx_row i0 A1) (mx_row i0 A2).
Proof. by reflexivity. Qed.
Lemma mx_row'_paste : forall m n1 n2 i0 (A1 : 'M_(m, n1)) (A2 : 'M_(m, n2)),
mx_row' i0 (pastemx A1 A2) === pastemx (mx_row' i0 A1) (mx_row' i0 A2).
Proof. by reflexivity. Qed.
Lemma mx_col_lshift : forall m n1 n2 j1 (A1 : 'M_(m, n1)) A2,
mx_col (lshift n2 j1) (pastemx A1 A2) === mx_col j1 A1.
Proof. by rewrite/mx_col=> m n1 n2 j1 A1 A2 i j; rewrite -> pastemxEl; reflexivity. Qed.
Lemma mx_col_rshift : forall m n1 n2 j2 A1 (A2 : 'M_(m, n2)),
mx_col (rshift n1 j2) (pastemx A1 A2) === mx_col j2 A2.
Proof. by rewrite/mx_col=> m n1 n2 j1 A1 A2 i j; rewrite -> pastemxEr; reflexivity. Qed.
Lemma mx_col'_lshift : forall m n1 n2 j1 (A1 : 'M_(m, n1.+1)) A2,
mx_col' (lshift n2 j1) (pastemx A1 A2) === pastemx (mx_col' j1 A1) A2.
Proof.
move=> m n1 n2 j1 A1 A2 i /= j.
case: (splitP j) => j' def_j'.
have -> : j = lshift n2 j' by apply ord_inj; apply def_j'.
rewrite -> pastemxEl; rewrite /mx_col'.
have -> : lift (lshift n2 j1) (lshift n2 j') = lshift n2 (lift j1 j'); last by rewrite -> pastemxEl; reflexivity.
by apply ord_inj.
have -> : j = rshift n1 j' by apply ord_inj; apply def_j'.
rewrite -> pastemxEr; rewrite /mx_col'.
have -> : lift (lshift n2 j1) (rshift n1 j') = (rshift n1.+1 j'); last by rewrite -> pastemxEr; reflexivity.
apply ord_inj => /=.
rewrite/bump //= addSnnS addnS -def_j' -(addn1 j) addnC.
have -> : j1 <= j=>//.
rewrite def_j' {def_j'}.
by apply leq_trans with n1; [apply j1 | apply leq_addr].
Qed.
Lemma mx_col'_rcast : forall n1 n2, 'I_n2 -> (n1 + n2.-1)%N === (n1 + n2).-1.
Proof. by move=> n1 n2 [j]; move/ltn_predK <-; rewrite addnS. Qed.
(*Lemma paste_mx_col' : forall m n1 n2 j2 A1 (A2 : 'M_(m, n2)),
pastemx A1 (mx_col' j2 A2)
=== eq_rect _ (matrix R m) (mx_col' (rshift n1 j2) (pastemx A1 A2))
_ (esym (mx_col'_rcast n1 j2)).
Proof.
move=> m n1 n2 j2 A1 A2; apply/matrixP=> i /= j; rewrite mxE.
case: splitP => j' def_j'; case: (n1 + n2.-1)%N / (esym _) => /= in j def_j' *.
rewrite mxE -(pastemxEl _ A2); congr (pastemx _ _ _); apply: ord_inj.
by rewrite /= def_j' /bump leqNgt ltn_addr.
rewrite 2!mxE -(pastemxEr A1); congr (pastemx _ _ _ _); apply: ord_inj => /=.
by rewrite def_j' /bump leq_add2l addnCA.
Qed.
Lemma mx_col'_rshift : forall m n1 n2 j2 A1 (A2 : 'M_(m, n2)),
mx_col' (rshift n1 j2) (pastemx A1 A2)
= eq_rect _ (matrix R m) (pastemx A1 (mx_col' j2 A2))
_ (mx_col'_rcast n1 j2).
Proof.
move=> m n1 n2 j2 A1 A2; rewrite paste_mx_col'.
by case: _.-1 / (mx_col'_rcast n1 j2) {A1 A2}(mx_col' _ _).
Qed.*)
Section Block.
Variables m1 m2 n1 n2 : nat.
Definition block_mx Aul Aur All Alr : 'M_(m1 + m2, n1 + n2) :=
(pastemx (pastemx Aul Aur)^T (pastemx All Alr)^T)^T.
Global Instance block_mx_morph : Proper (Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv) block_mx.
Proof.
rewrite/block_mx=> Aul1 Aul2 eqAul Aur1 Aur2 eqAur All1 All2 eqAll Alr1 Alr2 eqAlr i j.
by apply pastemx_morph; apply trmx_morph; apply pastemx_morph.
Qed.
Section CutBlock.
Variable A : matrix R (m1 + m2) (n1 + n2).
Definition ulsubmx := lcutmx (lcutmx A^T)^T.
Definition ursubmx := rcutmx (lcutmx A^T)^T.
Definition llsubmx := lcutmx (rcutmx A^T)^T.
Definition lrsubmx := rcutmx (rcutmx A^T)^T.
Lemma submxK : block_mx ulsubmx ursubmx llsubmx lrsubmx === A.
Proof.
rewrite/block_mx/ulsubmx/ursubmx/llsubmx/lrsubmx.
rewrite -> !cutmxK => i j.
rewrite/rcutmx/lcutmx/pastemx/trmx.
case: splitP => i' eqii'.
by have -> : lshift m2 i' = i; [apply ord_inj|reflexivity].
by have -> : rshift m1 i' = i; [apply ord_inj|reflexivity].
Qed.
End CutBlock.
Section PasteBlock.
Variables (Aul : matrix R m1 n1) (Aur : matrix R m1 n2).
Variables (All : matrix R m2 n1) (Alr : matrix R m2 n2).
Let A := block_mx Aul Aur All Alr.
Lemma block_mxEul : forall i j, A (lshift m2 i) (lshift n2 j) === Aul i j.
Proof. by move=> i j; rewrite /A /block_mx /trmx; rewrite -> !pastemxEl; reflexivity. Qed.
Lemma block_mxKul : ulsubmx A === Aul.
Proof. by move=> i j; rewrite /A /block_mx /ulsubmx /lcutmx /trmx; rewrite -> !pastemxEl; reflexivity. Qed.
Lemma block_mxEur : forall i j, A (lshift m2 i) (rshift n1 j) === Aur i j.
Proof. by move=> i j; rewrite /A /block_mx /trmx; rewrite -> pastemxEl, pastemxEr; reflexivity. Qed.
Lemma block_mxKur : ursubmx A === Aur.
Proof. by move=> i j; rewrite /A /block_mx /ursubmx /lcutmx /rcutmx /trmx; rewrite -> pastemxEl, pastemxEr; reflexivity. Qed.
Lemma block_mxEll : forall i j, A (rshift m1 i) (lshift n2 j) === All i j.
Proof. by move=> i j; rewrite /A /block_mx /trmx; rewrite -> pastemxEr, pastemxEl; reflexivity. Qed.
Lemma block_mxKll : llsubmx A === All.
Proof. by move=> i j; rewrite /A /block_mx /llsubmx /lcutmx /rcutmx /trmx; rewrite -> pastemxEr, pastemxEl; reflexivity. Qed.
Lemma block_mxElr : forall i j, A (rshift m1 i) (rshift n1 j) === Alr i j.
Proof. by move=> i j; rewrite /A /block_mx /trmx; rewrite -> !pastemxEr; reflexivity. Qed.
Lemma block_mxKlr : lrsubmx A === Alr.
Proof. by move=> i j; rewrite /A /block_mx /lrsubmx /lcutmx /rcutmx /trmx; rewrite -> !pastemxEr; reflexivity. Qed.
End PasteBlock.
End Block.
Section TrBlock.
Variables m1 m2 n1 n2 : nat.
Section TrCut.
Variable A : matrix R (m1 + m2) (n1 + n2).
Lemma trmx_ulsub : (ulsubmx A)^T === ulsubmx A^T.
Proof. by move => i j /=; reflexivity. Qed.
Lemma trmx_ursub : (ursubmx A)^T === llsubmx A^T.
Proof. by move => i j /=; reflexivity. Qed.
Lemma trmx_llsub : (llsubmx A)^T === ursubmx A^T.
Proof. by move => i j /=; reflexivity. Qed.
Lemma trmx_lrsub : (lrsubmx A)^T === lrsubmx A^T.
Proof. by move => i j /=; reflexivity. Qed.
End TrCut.
Lemma trmx_block : forall (Aul : 'M_(m1, n1)) Aur All (Alr : 'M_(m2, n2)),
(block_mx Aul Aur All Alr)^T ===
block_mx Aul^T All^T Aur^T Alr^T.
Proof.
move=> Aul Aur All Alr.
pose (block_mx Aul Aur All Alr).
rewrite -/m.
rewrite <- (block_mxKul Aul Aur All Alr).
rewrite <- (block_mxKll Aul Aur All Alr) at 2.
rewrite <- (block_mxKur Aul Aur All Alr) at 3.
rewrite <- (block_mxKlr Aul Aur All Alr) at 4.
by rewrite -> trmx_ulsub, trmx_llsub, trmx_ursub, trmx_lrsub, submxK; reflexivity.
Qed.
End TrBlock.
End Slicing.
Notation "A ^T" := (trmx A).
Prenex Implicits lcutmx rcutmx ulsubmx ursubmx llsubmx lrsubmx.
(* Definition of operations for matrices over a ring *)
Section MatrixOpsDef.
Context `{r_ring : Ring}.
Add Ring r_r : r_rt (setoid r_st r_ree, preprocess [unfold Equivalence.equiv]).
Notation "0" := rO.
Notation "1" := rI.
Notation "x + y" := (radd x y).
Notation "x * y " := (rmul x y).
Notation "x - y " := (rsub x y).
Notation "- x" := (ropp x).
Notation "\sum_ ( <- r | P ) F" := (\big[radd/0]_(<- r | P) F).
Notation "\sum_ ( i <- r | P ) F" := (\big[radd/0]_(i <- r | P) F).
Notation "\sum_ ( i <- r ) F" := (\big[radd/0]_(i <- r) F).
Notation "\sum_ ( m <= i < n | P ) F" := (\big[radd/0]_(m <= i < n | P) F).
Notation "\sum_ ( m <= i < n ) F" := (\big[radd/0]_(m <= i < n) F).
Notation "\sum_ ( i | P ) F" := (\big[radd/0]_(i | P) F).
Notation "\sum_ i F" := (\big[radd/0]_i F).
Notation "\sum_ ( i : t | P ) F" := (\big[radd/0]_(i : t | P) F) (only parsing).
Notation "\sum_ ( i : t ) F" := (\big[radd/0]_(i : t) F) (only parsing).
Notation "\sum_ ( i < n | P ) F" := (\big[radd/0]_(i < n | P) F).
Notation "\sum_ ( i < n ) F" := (\big[radd/0]_(i < n) F).
Notation "\sum_ ( i \in A | P ) F" := (\big[radd/0]_(i \in A | P) F).
Notation "\sum_ ( i \in A ) F" := (\big[radd/0]_(i \in A) F).
Notation "\prod_ ( <- r | P ) F" := (\big[rmul/1]_(<- r | P) F).
Notation "\prod_ ( i <- r | P ) F" := (\big[rmul/1]_(i <- r | P) F).
Notation "\prod_ ( i <- r ) F" := (\big[rmul/1]_(i <- r) F).
Notation "\prod_ ( m <= i < n | P ) F" := (\big[rmul/1]_(m <= i < n | P) F).
Notation "\prod_ ( m <= i < n ) F" := (\big[rmul/1]_(m <= i < n) F).
Notation "\prod_ ( i | P ) F" := (\big[rmul/1]_(i | P) F).
Notation "\prod_ i F" := (\big[rmul/1]_i F).
Notation "\prod_ ( i : t | P ) F" := (\big[rmul/1]_(i : t | P) F) (only parsing).
Notation "\prod_ ( i : t ) F" := (\big[rmul/1]_(i : t) F) (only parsing).
Notation "\prod_ ( i < n | P ) F" := (\big[rmul/1]_(i < n | P) F).
Notation "\prod_ ( i < n ) F" := (\big[rmul/1]_(i < n) F).
Notation "\prod_ ( i \in A | P ) F" := (\big[rmul/1]_(i \in A | P) F).
Notation "\prod_ ( i \in A ) F" := (\big[rmul/1]_(i \in A) F).
Existing Instance radd_morph.
Existing Instance rmul_morph.
Existing Instance rsub_morph.
Existing Instance ropp_morph.
Existing Instance radd_assoc.
Existing Instance radd_comm.
Existing Instance radd_left_unit.
Existing Instance rmul_assoc.
Existing Instance rmul_comm.
Existing Instance rmul_left_unit.
Existing Instance rmul_left_zero.
Existing Instance radd_rmul_left_distr.
Section ZmodOps.
(* The Zmodule structure *)
Variables m n : nat.
Implicit Types A B C : matrix R m n.
Definition null_mx := \matrix_(i < m, j < n) (0 : R).
Definition oppmx A := \matrix_(i < m, j < n) (- A i j).
Definition addmx A B := \matrix_(i < m, j < n) (A i j + B i j).
Definition scalemx x A := \matrix_(i < m, j < n) (x * A i j).
Global Instance addmx_morph : Proper (Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv) addmx.
Proof. by move=> A A' eqAA' B B' eqBB' i j; rewrite/addmx; setoid_rewrite eqAA'; setoid_rewrite eqBB'; reflexivity. Qed.
Global Instance oppmx_morph : Proper (Equivalence.equiv==>Equivalence.equiv) oppmx.
Proof. by move=> A A' eqAA' i j ; rewrite/oppmx; setoid_rewrite eqAA'; reflexivity. Qed.
Global Instance scalemx_morph : Proper (Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv) scalemx.
Proof. by move=> A A' eqAA' B B' eqBB' i j ; rewrite/scalemx; setoid_rewrite eqAA'; setoid_rewrite eqBB'; reflexivity. Qed.
Lemma summxE : forall I r (P : pred I) (E : I -> 'M_(m, n)) i j,
(\big[addmx/null_mx]_(k <- r | P k) E k) i j === \sum_(k <- r | P k) E k i j.
Proof.
move=> I r P E i j.
apply: (big_morph (phi:=fun A => A i j)) => [A B||].
by rewrite/addmx; ring.
by rewrite/null_mx; ring.
apply radd_morph.
Qed.
(* Vector space structure... pending the definition *)
Notation "'0m" := null_mx.
Notation "-m A" := (oppmx A).
Notation "A +m B" := (addmx A B).
Notation "A -m B" := (addmx A (oppmx B)).
Notation "x *m: A" := (scalemx x A).
Lemma scale0mx : forall A, 0 *m: A === '0m.
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> A i j; ring. Qed.
Lemma scalemx0 : forall x, x *m: '0m === '0m.
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x i j; ring. Qed.
Lemma scale1mx : forall A, 1 *m: A === A.
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> A i j; ring. Qed.
Lemma scaleNmx : forall x A, (- x) *m: A === -m (x *m: A).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x A i j; ring. Qed.
Lemma scalemxN : forall x A, x *m: (-m A) === -m (x *m: A).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x A i j; ring. Qed.
Lemma scalemx_addl : forall x y A, (x + y) *m: A === (x *m: A) +m (y *m: A).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x y A i j; ring. Qed.
Lemma scalemx_addr : forall x A B, x *m: (A +m B) === (x *m: A) +m (x *m: B).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x A B i j; ring. Qed.
Lemma scalemx_subl : forall x y A, (x - y) *m: A === (x *m: A) -m (y *m: A).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x y A i j; ring. Qed.
Lemma scalemx_subr : forall x A B, x *m: (A -m B) === (x *m: A) -m (x *m: B).
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x A B i j; ring. Qed.
Lemma scalemxA : forall x y A, x *m: (y *m: A) === (x * y) *m: A.
Proof. by rewrite/null_mx/addmx/oppmx/scalemx=> x y A i j; ring. Qed.
(* Basis... *)
Definition delta_mx i0 j0 :=
\matrix_(i < m, j < n) (if ((i == i0) && (j == j0)) then 1 else 0).
Lemma matrix_sum_delta : forall A,
A === \big[addmx/null_mx]_(i < m) \big[addmx/null_mx]_(j < n) (A i j *m: delta_mx i j).
Proof.
move=> A i j.
setoid_rewrite summxE.
setoid_rewrite summxE.
setoid_rewrite (bigD1 (j:=i))=>//=.
setoid_rewrite (big1 (P:=fun i0 => i0 != i))=>[|i0 Hi0].
setoid_rewrite (bigD1 (j:=j))=>//=.
setoid_rewrite (big1 (P:=fun i0 => i0 != j))=>[|i0 Hi0].
by rewrite/delta_mx/scalemx !eq_refl/=; ring.
rewrite/delta_mx/scalemx !eq_refl/= eq_sym; move:Hi0.
by case/negbRL=>->/=; ring.
apply (big1 (P:=fun _ => true) (F:=fun k => (A i0 k *m: delta_mx i0 k) i j))=>i1 _.
rewrite/delta_mx/scalemx eq_sym/=; move:Hi0.
by case/negbRL=>->/=; ring.
Qed.
End ZmodOps.
Notation "'0m" := (@null_mx _ _).
Notation "-m A" := (oppmx A).
Notation "A +m B" := (addmx A B).
Notation "A -m B" := (addmx A (oppmx B)).
Notation "x *m: A" := (scalemx x A).
Lemma trmx0 : forall (m n : nat), (@null_mx m n)^T === @null_mx n m.
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma trmx_add : forall m n (A B : 'M_(m, n)), (A +m B)^T === A^T +m B^T.
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma trmx_scale : forall m n a (A : 'M_(m, n)), (a *m: A)^T === a *m: A^T.
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma mx_row0 : forall m n i0, mx_row i0 (@null_mx m n) === (@null_mx 1 n).
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma mx_col0 : forall m n j0, mx_col j0 (@null_mx m n) === (@null_mx m 1).
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma mx_row'0 : forall m n i0, mx_row' i0 (@null_mx m n) === (@null_mx m.-1 n).
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma mx_col'0 : forall m n i0, mx_col' i0 (@null_mx m n) === (@null_mx m n.-1).
Proof. by move=> m n; rewrite/trmx/null_mx/addmx/oppmx/scalemx; reflexivity. Qed.
Lemma pastemx0 : forall m n1 n2,
pastemx (@null_mx m n1) (@null_mx m n2) === (@null_mx m (n1 + n2)).
Proof. by move=> m n1 n2 i j; rewrite/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; reflexivity. Qed.
Lemma addmx_paste : forall m n1 n2 (A1 B1 : 'M_(m, n1)) (A2 B2 : 'M_(m, n2)),
pastemx A1 A2 +m pastemx B1 B2 === pastemx (A1 +m B1) (A2 +m B2).
Proof. by move=> m n1 n2 iA1 B1 A2 B2 i j; rewrite/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; reflexivity. Qed.
Lemma scalemx_paste : forall m n1 n2 a (A1 : 'M_(m, n1)) (A2 : 'M_(m, n2)),
a *m: pastemx A1 A2 === pastemx (a *m: A1) (a *m: A2).
Proof. by move=> m n1 n2 a A1 A2 i j; rewrite/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; reflexivity. Qed.
Lemma block_mx0 : forall m1 m2 n1 n2,
block_mx (@null_mx m1 n1) (@null_mx m1 n2) (@null_mx m2 n1) (@null_mx m2 n2) === @null_mx (m1 + m2) (n1 + n2).
Proof. by move=> m1 m2 n1 n2 i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; case: split; reflexivity. Qed.
Lemma addmx_block : forall m1 m2 n1 n2 (Aul Bul : 'M_(m1, n1)) (Aur Bur : 'M_(m1, n2)) (All Bll : 'M_(m2, n1)) (Alr Blr : 'M_(m2, n2)),
block_mx Aul Aur All Alr +m block_mx Bul Bur Bll Blr
=== block_mx (Aul +m Bul) (Aur +m Bur) (All +m Bll) (Alr +m Blr).
Proof. by move=> m1 m2 n1 n2 Aul Bul Aur Bur All Bll Alr Blr i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; case: split; reflexivity. Qed.
Lemma scalemx_block : forall m1 m2 n1 n2 a (Aul : 'M_(m1, n1)) (Aur : 'M_(m1, n2)) (All : 'M_(m2, n1)) (Alr : 'M_(m2, n2)),
a *m: block_mx Aul Aur All Alr
=== block_mx (a *m: Aul) (a *m: Aur) (a *m: All) (a *m: Alr).
Proof. by move=> m1 m2 n1 n2 a Aul Aur All Alr i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx; case: split; case: split; reflexivity. Qed.
(* The graded ring structure *)
Definition scalar_mx n x := \matrix_(i , j < n) (if i == j then x else 0).
Global Instance scalar_mx_morph n : Morphism (Equivalence.equiv==>Equivalence.equiv) (@scalar_mx n).
Proof. by rewrite/scalar_mx=>n x y eqxy i j; case:(i==j); [apply eqxy | reflexivity]. Qed.
Definition mulmx m n p (A : 'M_(m, n)) (B : 'M_(n, p)) :=
\matrix_(i < m, k < p) \big [radd/0]_(j < n) (A i j * B j k).
Global Instance mulmx_morph m n p : Morphism (Equivalence.equiv==>Equivalence.equiv==>Equivalence.equiv) (@mulmx m n p).
Proof.
move=> m n p A A' eqAA' B B' eqBB' i k.
rewrite/mulmx; apply eq_bigr=> j _.
by setoid_rewrite eqAA'; setoid_rewrite eqBB'; reflexivity.
Qed.
Notation "x %:M" := (@scalar_mx _ x).
Notation "A *m B" := (mulmx A B).
Lemma scalar_mx0 : forall n, 0 %:M === @null_mx n n.
Proof. by move=> n i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx/scalar_mx/mulmx; case: eqP=> _; ring. Qed.
Lemma scalar_mx_opp : forall (n : nat) a, (- a)%:M === -m (@scalar_mx n a).
Proof. by move=> n a i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx/scalar_mx/mulmx; case: eqP=> _; ring. Qed.
Lemma scalar_mx_add : forall n a b, @scalar_mx n (a + b) === a%:M +m b%:M.
Proof. by move=> n a b i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx/scalar_mx/mulmx; case: eqP=> _; ring. Qed.
Lemma mulmx_scalar : forall m n a (A : 'M_(m, n)), (a%:M) *m A === a *m: A.
Proof.
move=> m n a A i j; rewrite/block_mx/pastemx/trmx/null_mx/addmx/oppmx/scalemx/scalar_mx/mulmx.
setoid_rewrite (bigD1 (j:=i))=>//.
setoid_rewrite big1=>[|i'/=]; first by case: eqP=>ii'//; ring.
by rewrite/is_true eq_sym; move/negbRL=>->/=; ring.
Qed.
Lemma scalar_mx_mul : forall n a b, @scalar_mx n (a * b) === a%:M *m b%:M.
Proof. by move=> n a b; rewrite -> mulmx_scalar; rewrite /scalar_mx /scalemx=> i j; by case (i==j); ring. Qed.
Lemma trmx_scalar : forall n a, (a%:M)^T === @scalar_mx n a.
Proof. by move=> n a i j; rewrite/trmx/null_mx/addmx/oppmx/scalemx/scalar_mx/mulmx eq_sym; reflexivity. Qed.
Lemma mul1mx : forall m n (A : 'M_(m, n)), 1%:M *m A === A.
Proof. by move=> m n A; rewrite -> mulmx_scalar, scale1mx; reflexivity. Qed.
Lemma mulmx_addl : forall m n p (A1 A2 : 'M_(m, n)) (B : 'M_(n, p)),
(A1 +m A2) *m B === A1 *m B +m A2 *m B.
Proof.
move=> m n p A1 A2 B i k; rewrite /addmx /mulmx.
setoid_rewrite <- big_split.
by apply eq_bigr=> j _; ring.
Qed.
Lemma scalemx_add : forall n a1 a2, @scalar_mx n (a1 + a2) === a1%:M +m a2%:M.
Proof. by move=> n a1 a2 i j; rewrite/scalar_mx/addmx; case: (i==j); ring. Qed.
Lemma scalemxAl : forall m n p a (A : 'M_(m, n)) (B : 'M_(n, p)),
a *m: (A *m B) === (a *m: A) *m B.
Proof.
move=> m n p a A B i k.
rewrite/scalemx/mulmx.
setoid_rewrite (big_distrr).
apply eq_bigr => j _; ring.
Qed.
Lemma mul0mx : forall m n p (A : 'M_(n, p)), '0m *m A === @null_mx m p.
Proof. by move=> m n p A i k; rewrite/mulmx/null_mx; apply (big1 (P:=fun _ => true) (F:=fun j => 0 * A j k))=> j _; ring. Qed.
Lemma mulmx0 : forall m n p (A : 'M_(m, n)), A *m '0m === @null_mx m p.
Proof. by move=> m n p A i k; rewrite/mulmx/null_mx; apply (big1 (P:=fun _ => true) (F:=fun j => A i j * 0))=> j _; ring. Qed.
Lemma mulmx1 : forall m n (A : 'M_(m, n)), A *m 1%:M === A.
Proof.
move=> m n A i k; rewrite/mulmx/scalar_mx.
setoid_rewrite (bigD1 (j:=k))=>//.
setoid_rewrite big1=> [| j/=]; first by rewrite eq_refl; ring.
by rewrite/is_true; move/negbRL=>->/=; ring.
Qed.
Lemma mulmx_addr : forall m n p (A : 'M_(m, n)) (B1 B2 : 'M_(n, p)),
A *m (B1 +m B2) === A *m B1 +m A *m B2.
Proof. by move=> m n p A B1 B2 i k; rewrite/mulmx/addmx; setoid_rewrite <- big_split; apply eq_bigr=> j _; ring. Qed.
Lemma mulmxA : forall m n p q (A : 'M_(m, n)) (B : 'M_(n, p)) (C : 'M_(p, q)),
A *m (B *m C) === A *m B *m C.
Proof.
move=> m n p q A B C i l; rewrite/mulmx.
setoid_rewrite big_distrr; setoid_rewrite big_distrl.
setoid_rewrite (exchange_big predT predT (fun j k => A i j * (B j k * C k l))).
by apply eq_bigr=> j _; apply eq_bigr=> k _; ring.
Qed.
Definition perm_mx n (s : 'S_n) :=
\matrix_(i, j) (if s i == j then 1 else 0).
Definition tperm_mx n i1 i2 := @perm_mx n (tperm i1 i2).
Lemma trmx_perm : forall n (s : 'S_n), (perm_mx s)^T === perm_mx s^-1.
Proof. by move=> n s i j; rewrite /trmx /perm_mx (canF_eq (permK _)) eq_sym; reflexivity. Qed.
Lemma trmx_tperm : forall n i1 i2, (@tperm_mx n i1 i2)^T === tperm_mx i1 i2.
Proof. by move=> n i1 i2; rewrite /tperm_mx; rewrite -> trmx_perm, tpermV; reflexivity. Qed.
Lemma mulmx_perm : forall n (s t : 'S_n),
perm_mx s *m perm_mx t === perm_mx (s * t).
Proof.
move=> n s t i j; rewrite/mulmx/perm_mx.
setoid_rewrite (bigD1 (j:=s i))=>//=.
setoid_rewrite (big1 (P:=fun k => k != s i))=>[|k]; first by rewrite eq_refl permM; ring.
by rewrite eq_sym; move/negbTE => ->; ring.
Qed.
Lemma mul_tperm_mx : forall m n (A : 'M_(m, n)) i1 i2,
(tperm_mx i1 i2) *m A === rswap i1 i2 A.
Proof.
move=> m n' A i1 i2 i j.
rewrite /mulmx /tperm_mx /perm_mx /rswap.
setoid_rewrite (bigD1 (j:=tperm i1 i2 i))=>//=.
setoid_rewrite (big1 (P:=fun k => k != tperm i1 i2 i))=>[|k]; first by rewrite eq_refl; ring.
by rewrite eq_sym; move/negbTE => ->; ring.
Qed.
Lemma perm_mx1 : forall n, perm_mx 1 === @scalar_mx n 1.
Proof. by move=> n i j; rewrite /perm_mx /scalar_mx perm1; reflexivity. Qed.
(* The trace, in 1/4 line. *)
Definition mx_trace n (A : 'M_n) := \sum_(i < n) A i i.
Notation "'\tr' A" := (mx_trace A).
Lemma mx_trace0 : forall n, \tr ('0m : 'M_n) === 0.
Proof. by move=> n; apply (big1 (I:=ordinal_finType n))=> i _; reflexivity. Qed.
Lemma mx_trace_scale : forall n a (A : 'M_n), \tr (a *m: A) === a * \tr A.
Proof. by move=> n a A; rewrite/mx_trace; setoid_rewrite (big_distrr (I:=ordinal_finType n)); apply eq_bigr => i _; reflexivity. Qed.
Notation "a *+ n" := (iter n (radd a) rO).
Lemma mx_trace_scalar : forall n a, \tr (a%:M : 'M_n) === a *+ n.
Proof. by move=> n a; rewrite <- big_const_ord; apply eq_bigr=> i _; rewrite/scalar_mx eq_refl; reflexivity. Qed.
Lemma mx_trace_add : forall n A B, \tr (A +m B : 'M_n) === \tr A + \tr B.
Proof. by move=> n A B; rewrite/mx_trace/addmx; apply big_split. Qed.
Lemma mx_trace_tr : forall n (A : 'M_n), \tr A^T === \tr A.
Proof. by move=> n A; apply eq_bigr=> i _; reflexivity. Qed.
Lemma mx_trace_block : forall n1 n2 Aul Aur All Alr,
\tr (block_mx Aul Aur All Alr : 'M_(n1 + n2)) === \tr Aul + \tr Alr.
Proof.
move=> n1 n2 Aul Aur All Alr; rewrite /mx_trace; setoid_rewrite big_split_ord => /=.
apply radd_morph; apply eq_bigr=> i _; [rewrite -> block_mxEul | rewrite -> block_mxElr]; reflexivity.
Qed.
Lemma mulmx_paste : forall m n p1 p2 (A : 'M_(m, n)) (B1 : 'M_(n, p1)) (B2 : 'M_(n, p2)),
A *m (pastemx B1 B2) === pastemx (A *m B1) (A *m B2).
Proof. by move=> m n p1 p2 A B1 B2 i k; rewrite/pastemx/mulmx; case defk: (split k) => [k1 | k2]; apply eq_bigr=> j _; reflexivity. Qed.
Lemma dotmx_paste : forall m n1 n2 p A1 A2 B1 B2,
(pastemx A1 A2 : 'M_(m, n1 + n2)) *m (pastemx B1 B2 : 'M_(p, n1 + n2))^T
=== A1 *m B1^T +m A2 *m B2^T.
Proof.
move=> m n1 n2 p A1 A2 B1 B2 i k; rewrite/mulmx/addmx/trmx; setoid_rewrite big_split_ord.
by apply radd_morph; apply eq_bigr=> j _; [rewrite -> pastemxEl, pastemxEl | rewrite -> pastemxEr, pastemxEr]; reflexivity.
Qed.
End MatrixOpsDef.
Notation "'0m" := (@null_mx _ _ _ _ _ _ _ _ _ _ _ _).
Notation "-m A" := (oppmx A).
Notation "A +m B" := (addmx A B).
Notation "A -m B" := (addmx A (oppmx B)).
Notation "x *m: A" := (scalemx x A).
Notation "x %:M" := (scalar_mx x).
Notation "A *m B" := (mulmx A B).
Notation "'\tr' A" := (mx_trace A).
Section TrMul.
Context `{r_ring : Ring}.
Notation "0" := rO.
Notation "1" := rI.
Notation "x + y" := (radd x y).
Notation "x * y " := (rmul x y).
Notation "x - y " := (rsub x y).
Notation "- x" := (ropp x).
Notation "\sum_ ( <- r | P ) F" := (\big[radd/0]_(<- r | P) F).
Notation "\sum_ ( i <- r | P ) F" := (\big[radd/0]_(i <- r | P) F).
Notation "\sum_ ( i <- r ) F" := (\big[radd/0]_(i <- r) F).
Notation "\sum_ ( m <= i < n | P ) F" := (\big[radd/0]_(m <= i < n | P) F).
Notation "\sum_ ( m <= i < n ) F" := (\big[radd/0]_(m <= i < n) F).
Notation "\sum_ ( i | P ) F" := (\big[radd/0]_(i | P) F).
Notation "\sum_ i F" := (\big[radd/0]_i F).
Notation "\sum_ ( i : t | P ) F" := (\big[radd/0]_(i : t | P) F) (only parsing).
Notation "\sum_ ( i : t ) F" := (\big[radd/0]_(i : t) F) (only parsing).
Notation "\sum_ ( i < n | P ) F" := (\big[radd/0]_(i < n | P) F).
Notation "\sum_ ( i < n ) F" := (\big[radd/0]_(i < n) F).
Notation "\sum_ ( i \in A | P ) F" := (\big[radd/0]_(i \in A | P) F).
Notation "\sum_ ( i \in A ) F" := (\big[radd/0]_(i \in A) F).
Notation "\prod_ ( <- r | P ) F" := (\big[rmul/1]_(<- r | P) F).
Notation "\prod_ ( i <- r | P ) F" := (\big[rmul/1]_(i <- r | P) F).
Notation "\prod_ ( i <- r ) F" := (\big[rmul/1]_(i <- r) F).
Notation "\prod_ ( m <= i < n | P ) F" := (\big[rmul/1]_(m <= i < n | P) F).
Notation "\prod_ ( m <= i < n ) F" := (\big[rmul/1]_(m <= i < n) F).
Notation "\prod_ ( i | P ) F" := (\big[rmul/1]_(i | P) F).
Notation "\prod_ i F" := (\big[rmul/1]_i F).
Notation "\prod_ ( i : t | P ) F" := (\big[rmul/1]_(i : t | P) F) (only parsing).
Notation "\prod_ ( i : t ) F" := (\big[rmul/1]_(i : t) F) (only parsing).
Notation "\prod_ ( i < n | P ) F" := (\big[rmul/1]_(i < n | P) F).
Notation "\prod_ ( i < n ) F" := (\big[rmul/1]_(i < n) F).
Notation "\prod_ ( i \in A | P ) F" := (\big[rmul/1]_(i \in A | P) F).
Notation "\prod_ ( i \in A ) F" := (\big[rmul/1]_(i \in A) F).
Existing Instance radd_morph.
Existing Instance rmul_morph.
Existing Instance rsub_morph.
Existing Instance ropp_morph.
Existing Instance radd_assoc.
Existing Instance radd_comm.
Existing Instance radd_left_unit.
Existing Instance rmul_assoc.
Existing Instance rmul_comm.
Existing Instance rmul_left_unit.
Existing Instance rmul_left_zero.
Existing Instance radd_rmul_left_distr.
Add Ring r_r2 : r_rt (setoid r_st r_ree, preprocess [unfold Equivalence.equiv]).
Existing Instance addmx_morph.
Existing Instance oppmx_morph.
Existing Instance mulmx_morph.
Existing Instance trmx_morph.
Existing Instance pastemx_morph.
Existing Instance block_mx_morph.
Lemma trmx_mul_rev : forall m n p (A : matrix R m n) (B : matrix R n p),
(A *m B)^T === B^T *m A^T.
Proof. by move=> m n p A B k i; rewrite/trmx; apply eq_bigr=> j _; ring. Qed.
Lemma mulmx_block : forall m1 m2 n1 n2 p1 p2 (Aul : matrix R m1 n1) Aur All Alr Bul Bur Bll Blr,
(block_mx Aul Aur All Alr : 'M_(m1 + m2, n1 + n2))
*m (block_mx Bul Bur Bll Blr : 'M_(n1 + n2, p1 + p2))
=== block_mx (Aul *m Bul +m Aur *m Bll) (Aul *m Bur +m Aur *m Blr)
(All *m Bul +m Alr *m Bll) (All *m Bur +m Alr *m Blr).
Proof.
move=> m1 m2 n1 n2 p1 p2 Aul Aur All Alr Bul Bur Bll Blr/=; rewrite <- (trmxK (_ *m _)).
rewrite -> trmx_mul_rev, (trmx_block Aul); rewrite /block_mx; rewrite -> (trmxK (pastemx _ _)), dotmx_paste, <- !addmx_paste.
by rewrite -> !trmx_add, (trmxK _), (trmxK _), <- addmx_paste, !mulmx_paste, <- !trmx_mul_rev, !mulmx_paste; reflexivity.
Qed.
Lemma mul_mx_tperm : forall m n (A : matrix R m n) i1 i2,
A *m (tperm_mx i1 i2) === cswap i1 i2 A.
Proof.
move=> m n A i1 i2; apply: trmx_inj.
by rewrite -> trmx_mul_rev, trmx_tperm, mul_tperm_mx, trmx_cswap; reflexivity.
Qed.
End TrMul.
Section ComMatrix.
Context `{r_ring : Ring}.
Notation "0" := rO.
Notation "1" := rI.
Notation "x + y" := (radd x y).
Notation "x * y " := (rmul x y).
Notation "x - y " := (rsub x y).
Notation "- x" := (ropp x).
Notation "\sum_ ( <- r | P ) F" := (\big[radd/0]_(<- r | P) F).
Notation "\sum_ ( i <- r | P ) F" := (\big[radd/0]_(i <- r | P) F).
Notation "\sum_ ( i <- r ) F" := (\big[radd/0]_(i <- r) F).
Notation "\sum_ ( m <= i < n | P ) F" := (\big[radd/0]_(m <= i < n | P) F).
Notation "\sum_ ( m <= i < n ) F" := (\big[radd/0]_(m <= i < n) F).
Notation "\sum_ ( i | P ) F" := (\big[radd/0]_(i | P) F).
Notation "\sum_ i F" := (\big[radd/0]_i F).
Notation "\sum_ ( i : t | P ) F" := (\big[radd/0]_(i : t | P) F) (only parsing).
Notation "\sum_ ( i : t ) F" := (\big[radd/0]_(i : t) F) (only parsing).
Notation "\sum_ ( i < n | P ) F" := (\big[radd/0]_(i < n | P) F).
Notation "\sum_ ( i < n ) F" := (\big[radd/0]_(i < n) F).
Notation "\sum_ ( i \in A | P ) F" := (\big[radd/0]_(i \in A | P) F).
Notation "\sum_ ( i \in A ) F" := (\big[radd/0]_(i \in A) F).
Notation "\prod_ ( <- r | P ) F" := (\big[rmul/1]_(<- r | P) F).
Notation "\prod_ ( i <- r | P ) F" := (\big[rmul/1]_(i <- r | P) F).
Notation "\prod_ ( i <- r ) F" := (\big[rmul/1]_(i <- r) F).
Notation "\prod_ ( m <= i < n | P ) F" := (\big[rmul/1]_(m <= i < n | P) F).
Notation "\prod_ ( m <= i < n ) F" := (\big[rmul/1]_(m <= i < n) F).
Notation "\prod_ ( i | P ) F" := (\big[rmul/1]_(i | P) F).
Notation "\prod_ i F" := (\big[rmul/1]_i F).
Notation "\prod_ ( i : t | P ) F" := (\big[rmul/1]_(i : t | P) F) (only parsing).
Notation "\prod_ ( i : t ) F" := (\big[rmul/1]_(i : t) F) (only parsing).
Notation "\prod_ ( i < n | P ) F" := (\big[rmul/1]_(i < n | P) F).
Notation "\prod_ ( i < n ) F" := (\big[rmul/1]_(i < n) F).
Notation "\prod_ ( i \in A | P ) F" := (\big[rmul/1]_(i \in A | P) F).
Notation "\prod_ ( i \in A ) F" := (\big[rmul/1]_(i \in A) F).
Existing Instance radd_morph.
Existing Instance rmul_morph.
Existing Instance rsub_morph.
Existing Instance ropp_morph.
Existing Instance radd_assoc.
Existing Instance radd_comm.
Existing Instance radd_left_unit.
Existing Instance rmul_assoc.
Existing Instance rmul_comm.
Existing Instance rmul_left_unit.
Existing Instance rmul_left_zero.
Existing Instance radd_rmul_left_distr.
Add Ring r_r3 : r_rt (setoid r_st r_ree, preprocess [unfold Equivalence.equiv]).
Existing Instance addmx_morph.
Existing Instance oppmx_morph.
Existing Instance mulmx_morph.
Existing Instance trmx_morph.
Existing Instance pastemx_morph.
Existing Instance block_mx_morph.
Lemma trmx_mul : forall m n p (A : matrix R m n) (B : 'M_(n, p)),
(A *m B)^T === B^T *m A^T.
Proof.
move=> m n p A B; rewrite -> trmx_mul_rev; rewrite /mulmx=> k i.
by apply (eq_bigr (I:=ordinal_finType n)) => j _; reflexivity.
Qed.
Lemma scalemxAr : forall m n p a (A : matrix R m n) (B : 'M_(n, p)),
a *m: (A *m B) === A *m (a *m: B).
Proof.
move=> m n p a A B; apply trmx_inj.
by rewrite -> trmx_scale, !trmx_mul, trmx_scale, scalemxAl; reflexivity.
Qed.
Lemma scalar_mx_comm : forall (n : pos_nat) a (A : matrix R n n),
A *m (a%:M) === (a%:M) *m A.
Proof.
move=> n a A; apply: trmx_inj; rewrite -> trmx_mul, trmx_scalar.
by rewrite -> !mulmx_scalar, trmx_scale; reflexivity.
Qed.
Lemma mx_trace_mulC : forall m n (A : matrix R m n) B,
\tr (A *m B) === \tr (B *m A).
Proof.
move=> m n A B; transitivity (\sum_(i < m) \sum_(j < n) A i j * B j i).
by apply eq_bigr; reflexivity.
setoid_rewrite (exchange_big (I:=ordinal_finType m)); apply eq_bigr => i _.
by apply eq_bigr => j _; ring.
Qed.
Local Notation "x ^+ n" := (iter n (rmul x) 1).
(* The determinant, in one line. *)
Definition determinant n (A : matrix R n n) :=
\big[radd/0]_(s : 'S_n) ((-(1:R)) ^+ s * \prod_(i < n) A i (s i)).
Global Instance determinant_morph n : Morphism (Equivalence.equiv==>Equivalence.equiv) (@determinant n).
Proof.
move=> n A B eqAB; rewrite /determinant; apply eq_bigr => s _.
apply rmul_morph; first by reflexivity.
by apply eq_bigr => i _; apply eqAB.
Qed.
Notation "'\det' A" := (determinant A).
Definition cofactor n A (i j : 'I_n) : R :=
(-(1:R)) ^+ (i + j) * \det (mx_row' i (mx_col' j A)).
Definition adjugate n A := \matrix_(i, j < n) (cofactor A j i : R).
Lemma determinant_multilinear : forall n (A B C : 'M_n) i0 b c,
mx_row i0 A === b *m: mx_row i0 B +m c *m: mx_row i0 C ->
mx_row' i0 B === mx_row' i0 A ->
mx_row' i0 C === mx_row' i0 A ->
\det A === b * \det B + c * \det C.
Proof.
move=> n A B C i0 b c; rewrite <- (mx_row_id (_ +m _)); move/mx_row_eq=> ABC.
move/mx_row'_eq=> BA; move/mx_row'_eq=> CA; rewrite/determinant.
setoid_rewrite (big_distrr _ b); setoid_rewrite (big_distrr _ c).
rewrite <- big_split; apply eq_bigr => s _ /=.
have Heq : forall x y z, req (b * (z * x) + c * (z * y)) (z * (b * x + c * y)) by move=> x y z; ring.
rewrite -> Heq.
apply rmul_morph; first by reflexivity.
setoid_rewrite (bigD1 (j:=i0))=>//=.
rewrite -> (ABC _).
rewrite/mx_row/addmx/scalemx.
transitivity ((b * B i0 (s i0)) * \prod_(i < n | i != i0) A i (s i)
+ c * (C i0 (s i0) * \prod_(i < n | i != i0) A i (s i))).
set tmp := reducebig _ _ _ _ _; ring.
apply radd_morph.
ring_simplify; apply rmul_morph; first by reflexivity.
by apply eq_bigr => i neq; symmetry; apply BA.
ring_simplify; apply rmul_morph; first by reflexivity.
by apply eq_bigr => i neq; symmetry; apply CA.
Qed.
Lemma alternate_determinant : forall n (A : 'M_n) i1 i2,
i1 != i2 -> A i1 === A i2 -> \det A === 0.
Proof.
move=> n A i1 i2 Di12 A12; pose r := 'I_n.
pose t := tperm i1 i2; pose tr s := (t * s)%g.