-
Notifications
You must be signed in to change notification settings - Fork 2
/
euclidean.v
1709 lines (1403 loc) · 61 KB
/
euclidean.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
(* coq-robot (c) 2017 AIST and INRIA. License: LGPL-2.1-or-later. *)
From HB Require Import structures.
From mathcomp Require Import all_ssreflect ssralg ssrint ssrnum rat poly.
From mathcomp Require Import closed_field polyrcf matrix mxalgebra mxpoly zmodp.
From mathcomp Require Import realalg complex fingroup perm.
From mathcomp.analysis Require Import reals forms.
Require Import ssr_ext.
(******************************************************************************)
(* Elements of Euclidean geometry *)
(* *)
(* This file provides elements of Euclidean geometry, with specializations to *)
(* the 3D case. It develops the theory of the dot-product and of the *)
(* cross-product with lemmas such as the double cross-product. It also *)
(* develops the theory of rotation matrices with lemmas such as the *)
(* preservation of the dot-product by orthogonal matrices or a closed formula *)
(* for the characteristic polynomial of a 3x3 matrix. *)
(* *)
(* jacobi_identity == Jacobi identity *)
(* lieAlgebraType R == the type of Lie algebra over R *)
(* lie[x, y] == Lie brackets *)
(* *)
(* u *d w == the dot-product of the vectors u and v, i.e., the only *)
(* component of the 1x1-matrix u * v^T *)
(* norm u == the norm of vector u, i.e., the square root of u *d u *)
(* normalize u == scales vector u to be of unit norm *)
(* A _|_ B == A and B are normal *)
(* 'O[T]_n == the type of orthogonal matrices of size n *)
(* 'SO[T]_n == the type of rotation matrices of size n *)
(* cross M == generalized cross-product *)
(* *)
(* Specializations to the 3D case: *)
(* row2 a b == the row vector [a,b] *)
(* row3 a b c == the row vector [a,b,c] *)
(* col_mx2 u v == specialization of col_mx two row vectors of size 2 *)
(* col_mx3 u v w == specialization of col_mx two row vectors of size 3 *)
(* u *v v == the cross-product of the vectors u and v, defined using *)
(* determinants *)
(* Module rv3LieAlgebra == the space R^3 with the cross-product is a Lie *)
(* algebra *)
(* vaxis_euler M == the vector-axis of the rotation matrix M of Euler's *)
(* theorem *)
(* *)
(******************************************************************************)
Reserved Notation "*d%R".
Reserved Notation "u *d w" (at level 40).
Reserved Notation "*v%R".
Reserved Notation "u *v w" (at level 40).
Reserved Notation "''O[' T ]_ n"
(at level 8, n at level 2, format "''O[' T ]_ n").
Reserved Notation "''SO[' T ]_ n"
(at level 8, n at level 2, format "''SO[' T ]_ n").
Reserved Notation "A _|_ B" (at level 69). (* NB: used to be level 8 *)
Reserved Notation "u _|_ A , B " (A at next level, at level 69,
format "u _|_ A , B ").
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import Order.TTheory GRing.Theory Num.Def Num.Theory.
Local Open Scope ring_scope.
Definition jacobi_identity (T : zmodType) (op : T -> T -> T) := forall x y z,
op x (op y z) + op y (op z x) + op z (op x y) = 0.
Reserved Notation "lie[ t1 , t2 ]" (format "lie[ t1 , t2 ]").
HB.mixin Record isLieAlgebra (R : ringType) L of GRing.Lmodule R L := {
bracket : {bilinear L -> L -> L} ;
liexx : forall x, bracket x x = 0 ;
jacobi : jacobi_identity bracket
}.
#[short(type="lieAlgebraType")]
HB.structure Definition LieAlgebra (R : ringType) :=
{L of isLieAlgebra R L & }.
Notation "lie[ t1 , t2 ]" := (@bracket _ _ t1 t2).
Section liealgebra.
Variables (R : ringType) (G : lieAlgebraType R).
(* Lie brackets are anticommutative *)
Lemma lieC (x y : G) : lie[x, y] = - lie[y, x].
Proof.
apply/eqP; rewrite -subr_eq0 opprK; apply/eqP.
rewrite -[RHS](liexx (x + y)) linearDl 2!linearDr.
by rewrite 2!liexx !(addr0,add0r).
Qed.
End liealgebra.
Section dot_product0.
Variables (R : ringType) (n : nat).
Implicit Types u v w : 'rV[R]_n.
Definition dotmul u v : R := (u *m v^T) ``_ 0.
Local Notation "*d%R" := (@dotmul _).
Local Notation "u *d w" := (dotmul u w).
Lemma dotmulP u v : u *m v^T = (u *d v)%:M.
Proof. by rewrite /dotmul -mx11_scalar. Qed.
Lemma dotmulE u v : u *d v = \sum_k u``_k * v``_k.
Proof. by rewrite [LHS]mxE; apply: eq_bigr=> i; rewrite mxE. Qed.
Lemma dotmul0v v : 0 *d v = 0.
Proof. by rewrite [LHS]mxE big1 // => i; rewrite mxE mul0r. Qed.
Lemma dotmulv0 v : v *d 0 = 0.
Proof. by rewrite /dotmul trmx0 mulmx0 mxE. Qed.
Lemma dotmulDr u b c : u *d (b + c) = u *d b + u *d c.
Proof. by rewrite {1}/dotmul linearD /= mulmxDr mxE. Qed.
Lemma dotmulDl u b c : (b + c) *d u = b *d u + c *d u.
Proof. by rewrite {1}/dotmul mulmxDl mxE. Qed.
Lemma dotmulvN u v : u *d -v = - (u *d v).
Proof. by rewrite /dotmul linearN /= mulmxN mxE. Qed.
Lemma dotmulNv u v : - u *d v = - (u *d v).
Proof. by rewrite /dotmul mulNmx mxE. Qed.
Lemma dotmulBr u b c : u *d (b - c) = u *d b - u *d c.
Proof. by rewrite dotmulDr dotmulvN. Qed.
Lemma dotmulBl u b c : (b - c) *d u = b *d u - c *d u.
Proof. by rewrite dotmulDl dotmulNv. Qed.
Lemma dotmulZv u k v : (k *: u) *d v = k * (u *d v).
Proof. by rewrite /dotmul -scalemxAl mxE. Qed.
Lemma dotmul_delta_mx u i : u *d 'e_i = u``_i.
Proof.
rewrite /dotmul trmx_delta mxE (bigD1 i) //= mxE !eqxx mulr1.
by rewrite big1 ?addr0 // => j jnei; rewrite mxE (negbTE jnei) /= mulr0.
Qed.
Lemma dote2 i j : ('e_i : 'rV[R]_n) *d 'e_j = (i == j)%:R.
Proof. by rewrite dotmul_delta_mx mxE eqxx eq_sym. Qed.
(* Lemma dotmul_eq u v : (forall x, u *d x = v *d x) -> u = v. *)
(* Proof. by move=> uv; apply/rowP => i; rewrite -!dotmul_delta_mx uv. Qed. *)
Lemma mxE_dotmul_row_col m p (M : 'M[R]_(m, n)) (N : 'M[R]_(n, p)) i j :
(M *m N) i j = (row i M) *d (col j N)^T.
Proof. rewrite !mxE dotmulE; apply/eq_bigr => /= k _; by rewrite !mxE. Qed.
Lemma coorE (p : 'rV[R]_n) i : p``_i = p *d 'e_i.
Proof. by rewrite dotmul_delta_mx. Qed.
Lemma colE (v : 'rV[R]_n) j : col j v = 'e_j *m v^T.
Proof.
apply/colP => i; rewrite {i}(ord1 i) !mxE coorE /dotmul mxE.
apply: eq_bigr => /= i _; rewrite !mxE eqxx /=.
case/boolP : (i == j) => /=; by rewrite ?(mulr1,mul1r,mul0r,mulr0).
Qed.
Lemma mxE_dotmul (M : 'M[R]_n) i j : M i j = 'e_j *d row i M.
Proof. by rewrite mxE_col_row /dotmul colE. Qed.
End dot_product0.
Notation "*d%R" := (@dotmul _ _) : ring_scope.
Notation "u *d w" := (dotmul u w) : ring_scope.
Section com_dot_product.
Variables (R : comRingType) (n : nat).
Implicit Types u v : 'rV[R]_n.
Lemma dotmulC u v : u *d v = v *d u.
Proof. by rewrite /dotmul -[_ *m _]trmxK trmx_mul !trmxK mxE. Qed.
Lemma dotmulD u v : (u + v) *d (u + v) = u *d u + (u *d v) *+ 2 + v *d v.
Proof. by rewrite dotmulDr 2!dotmulDl mulr2n !addrA ![v *d _]dotmulC. Qed.
Lemma dotmulvZ u k v : u *d (k *: v) = k * (u *d v).
Proof. by rewrite /dotmul linearZ /= -scalemxAr mxE. Qed.
Lemma dotmul_trmx u M v : u *d (v *m M) = (u *m M^T) *d v.
Proof. by rewrite /dotmul trmx_mul mulmxA. Qed.
End com_dot_product.
Section dotmul_bilinear.
Variables (R : comRingType) (n : nat).
Definition dotmul_rev (v u : 'rV[R]_n) := u *d v.
Canonical rev_dotmul := @RevOp _ _ _ dotmul_rev (@dotmul R n)
(fun _ _ => erefl).
Lemma dotmul_is_linear u : linear (dotmul u : 'rV[R]_n -> R^o).
Proof. move=> /= k v w; by rewrite dotmulDr dotmulvZ. Qed.
HB.instance Definition _ x :=
GRing.isLinear.Build _ _ _ _ (@dotmul R n x) (dotmul_is_linear x).
Lemma dotmul_rev_is_linear v : linear (dotmul_rev v : 'rV[R]_n -> R^o).
Proof. move=> /= k u w; by rewrite /dotmul_rev dotmulDl dotmulZv. Qed.
HB.instance Definition _ v :=
GRing.isLinear.Build _ _ _ _ (@dotmul_rev v) (dotmul_rev_is_linear v).
Lemma dotmul_is_bilinear : bilinear_for
(GRing.Scale.Law.clone _ _ *:%R _) (GRing.Scale.Law.clone _ _ *:%R _)
(@dotmul R n : _ -> _ -> R^o).
Proof.
split => [u'|u] a x y /=.
- by rewrite dotmulDl dotmulZv.
- by rewrite dotmulDr dotmulvZ.
Qed.
HB.instance Definition _ :=
bilinear_isBilinear.Build R
[the lmodType R of 'rV[R]_n] [the lmodType R of 'rV[R]_n]
R^o _ _ (@dotmul R n) dotmul_is_bilinear.
End dotmul_bilinear.
Section dot_product.
Variables (T : realDomainType) (n : nat).
Implicit Types u v w : 'rV[T]_n.
Lemma le0dotmul u : 0 <= u *d u.
Proof. rewrite dotmulE sumr_ge0 // => i _; by rewrite -expr2 sqr_ge0. Qed.
Lemma dotmulvv0 u : (u *d u == 0) = (u == 0).
Proof.
apply/idP/idP; last by move/eqP ->; rewrite dotmul0v.
rewrite dotmulE psumr_eq0; last by move=> i _; rewrite -expr2 sqr_ge0.
move/allP => H; apply/eqP/rowP => i.
apply/eqP; by rewrite mxE -sqrf_eq0 expr2 -(implyTb ( _ == _)) H.
Qed.
End dot_product.
Section norm.
Variables (T : rcfType) (n : nat).
Implicit Types u v : 'rV[T]_n.
Definition norm u := Num.sqrt (u *d u).
Lemma normN u : norm (- u) = norm u.
Proof. by rewrite /norm dotmulNv dotmulvN opprK. Qed.
Lemma norm0 : norm 0 = 0.
Proof. by rewrite /norm dotmul0v sqrtr0. Qed.
Lemma norm_delta_mx i : norm 'e_i = 1.
Proof. by rewrite /norm /dotmul trmx_delta mul_delta_mx mxE !eqxx sqrtr1. Qed.
Lemma norm_ge0 u : norm u >= 0.
Proof. by apply sqrtr_ge0. Qed.
Hint Resolve norm_ge0 : core.
Lemma normr_norm u : `|norm u| = norm u.
Proof. by rewrite ger0_norm. Qed.
Lemma norm_eq0 u : (norm u == 0) = (u == 0).
Proof. by rewrite -sqrtr0 eqr_sqrt // ?dotmulvv0 // le0dotmul. Qed.
Lemma norm_gt0 u : (0 < norm u) = (u != 0).
Proof. by rewrite lt_neqAle norm_ge0 andbT eq_sym norm_eq0. Qed.
Lemma normZ (k : T) u : norm (k *: u) = `|k| * norm u.
Proof.
by rewrite /norm dotmulvZ dotmulZv mulrA sqrtrM -expr2 ?sqrtr_sqr // sqr_ge0.
Qed.
Lemma dotmulvv u : u *d u = norm u ^+ 2.
Proof.
rewrite /norm [_ ^+ _]sqr_sqrtr // dotmulE sumr_ge0 //.
by move=> i _; rewrite sqr_ge0.
Qed.
Lemma polarization_identity v u :
v *d u = 1 / 4%:R * (norm (v + u) ^+ 2 - norm (v - u) ^+ 2).
Proof.
apply: (@mulrI _ 4%:R); first exact: pnatf_unit.
rewrite [in RHS]mulrA div1r divrr ?pnatf_unit // mul1r.
rewrite -2!dotmulvv dotmulD dotmulD mulr_natl (addrC (v *d v)).
rewrite (_ : 4 = 2 + 2)%N // mulrnDr -3![in RHS]addrA; congr (_ + _).
rewrite opprD addrCA [_ + (- _ + _)]addrA subrr add0r.
by rewrite addrC opprD 2!dotmulvN dotmulNv opprK subrK -mulNrn opprK.
Qed.
Lemma sqr_norm u : norm u ^+ 2 = \sum_i u``_i ^+ 2.
Proof. rewrite -dotmulvv dotmulE; apply/eq_bigr => /= i _; by rewrite expr2. Qed.
Lemma mxtrace_tr_mul u : \tr (u^T *m u) = norm u ^+ 2.
Proof.
rewrite /mxtrace sqr_norm; apply/eq_bigr => /= i _; by rewrite mulmx_trE -expr2.
Qed.
Section norm1.
Variable u : 'rV[T]_n.
Hypothesis u1 : norm u = 1.
Lemma norm1_neq0 : u != 0.
Proof. move: u1; rewrite -norm_eq0 => ->; exact: oner_neq0. Qed.
Lemma dotmul1 : u *m u^T = 1.
Proof. by rewrite dotmulP dotmulvv u1 expr1n. Qed.
End norm1.
End norm.
Section normalize.
Variables (T : rcfType) (n : nat).
Implicit Type u v : 'rV[T]_3.
Definition normalize v := (norm v)^-1 *: v.
Lemma normalize0 : normalize 0 = 0.
Proof. by rewrite /normalize scaler0. Qed.
Lemma normalizeN u : normalize (- u) = - normalize u.
Proof. by rewrite /normalize normN scalerN. Qed.
Lemma normalizeI v : norm v = 1 -> normalize v = v.
Proof. by move=> v1; rewrite /normalize v1 invr1 scale1r. Qed.
Lemma norm_normalize v : v != 0 -> norm (normalize v) = 1.
Proof.
move=> v0; rewrite normZ ger0_norm; last by rewrite invr_ge0 // norm_ge0.
by rewrite mulVr // unitfE norm_eq0.
Qed.
Lemma normalize_eq0 v : (normalize v == 0) = (v == 0).
Proof.
apply/idP/idP => [|/eqP ->]; last by rewrite normalize0.
case/boolP : (v == 0) => [//| /norm_normalize].
rewrite -norm_eq0 => -> /negPn; by rewrite oner_neq0.
Qed.
Lemma norm_scale_normalize u : norm u *: normalize u = u.
Proof.
case/boolP : (u == 0) => [/eqP -> {u}|u0]; first by rewrite norm0 scale0r.
by rewrite /normalize scalerA divrr ?scale1r // unitfE norm_eq0.
Qed.
Lemma normalizeZ u (u0 : u != 0) k (k0 : 0 < k) : normalize (k *: u) = normalize u.
Proof.
rewrite {1}/normalize normZ gtr0_norm // invrM ?unitfE ?gt_eqF // ?norm_gt0 //.
by rewrite scalerA -mulrA mulVr ?mulr1 ?unitfE ?gt_eqF.
Qed.
(* NB: not used *)
Lemma dotmul_normalize_norm u : u *d normalize u = norm u.
Proof.
case/boolP : (u == 0) => [/eqP ->{u}|u0]; first by rewrite norm0 dotmul0v.
rewrite -{1}(norm_scale_normalize u) dotmulZv dotmulvv norm_normalize //.
by rewrite expr1n mulr1.
Qed.
Lemma dotmul_normalize u v : (normalize u *d v == 0) = (u *d v == 0).
Proof.
case/boolP : (u == 0) => [/eqP ->|u0]; first by rewrite normalize0.
apply/idP/idP.
rewrite /normalize dotmulZv mulf_eq0 => /orP [|//].
by rewrite invr_eq0 norm_eq0 (negbTE u0).
rewrite /normalize dotmulZv => /eqP ->; by rewrite mulr0.
Qed.
End normalize.
Section normal.
Variable F : fieldType.
Local Notation "A _|_ B" := (A%MS <= kermx B%MS^T)%MS.
Lemma normal_sym n k m (A : 'M[F]_(k, n)) (B : 'M[F]_(m, n)) :
A _|_ B = B _|_ A.
Proof.
rewrite !(sameP sub_kermxP eqP) -{1}[A]trmxK -trmx_mul.
by rewrite -{1}trmx0 (inj_eq (@trmx_inj _ _ _)).
Qed.
Lemma normalNm n k m (A : 'M[F]_(k, n)) (B : 'M[F]_(m, n)) :
(- A) _|_ B = A _|_ B.
Proof. by rewrite eqmx_opp. Qed.
Lemma normalmN n k m (A : 'M[F]_(k, n)) (B : 'M[F]_(m, n)) :
A _|_ (- B) = A _|_ B.
Proof. by rewrite ![A _|_ _]normal_sym normalNm. Qed.
Lemma normalDm n k m p (A : 'M[F]_(k, n)) (B : 'M[F]_(m, n)) (C : 'M[F]_(p, n)) :
(A + B _|_ C) = (A _|_ C) && (B _|_ C).
Proof. by rewrite addsmxE !(sameP sub_kermxP eqP) mul_col_mx col_mx_eq0. Qed.
Lemma normalmD n k m p (A : 'M[F]_(k, n)) (B : 'M[F]_(m, n)) (C : 'M[F]_(p, n)) :
(A _|_ B + C) = (A _|_ B) && (A _|_ C).
Proof. by rewrite ![A _|_ _]normal_sym normalDm. Qed.
Lemma normalvv n (u v : 'rV[F]_n) : (u _|_ v) = (u *d v == 0).
Proof. by rewrite (sameP sub_kermxP eqP) dotmulP fmorph_eq0. Qed.
End normal.
Notation "A _|_ B" := (A%MS <= kermx B%MS^T)%MS.
Notation "u _|_ A , B " := (u _|_ (col_mx A B)).
Section orthogonal_rotation_def.
Variables (n : nat) (T : ringType).
Definition orthogonal_pred := fun M : 'M[T]_n => M *m M^T == 1%:M.
Definition orthogonal := [qualify M : 'M[T]_n | orthogonal_pred M].
Fact orthogonal_key : pred_key orthogonal. Proof. by []. Qed.
Canonical orthogonal_keyed := KeyedQualifier orthogonal_key.
Definition rotation_pred := fun M : 'M[T]_n => (M \is orthogonal) && (\det M == 1).
Definition rotation := [qualify M : 'M[T]_n | rotation_pred M].
Fact rotation_key : pred_key rotation. Proof. by []. Qed.
Canonical rotation_keyed := KeyedQualifier rotation_key.
End orthogonal_rotation_def.
Notation "''O[' T ]_ n" := (@orthogonal n T) : ring_scope.
Notation "''SO[' T ]_ n" := (@rotation n T) : ring_scope.
Section orthogonal_rotation_properties0.
Variables (n' : nat) (T : ringType).
Let n := n'.+1.
Lemma orthogonalE M : (M \is 'O[T]_n) = (M * M^T == 1). Proof. by []. Qed.
Lemma orthogonal1 : 1 \is 'O[T]_n.
Proof. by rewrite orthogonalE trmx1 mulr1. Qed.
Lemma orthogonal_mul_tr M : (M \is 'O[T]_n) -> M *m M^T = 1.
Proof. by move/eqP. Qed.
Lemma orthogonal_oppr_closed : oppr_closed 'O[T]_n.
Proof. by move=> x; rewrite !orthogonalE linearN /= mulNr mulrN opprK. Qed.
HB.instance Definition _ := GRing.isOppClosed.Build _ _ orthogonal_oppr_closed.
Lemma rotation_sub : {subset 'SO[T]_n <= 'O[T]_n}.
Proof. by move=> M /andP []. Qed.
Lemma orthogonalP M :
reflect (forall i j, row i M *d row j M = (i == j)%:R) (M \is 'O[T]_n).
Proof.
apply: (iffP idP) => [|H] /=.
rewrite orthogonalE => /eqP /matrixP H i j.
move/(_ i j) : H; rewrite /dotmul !mxE => <-.
apply eq_bigr => k _; by rewrite !mxE.
rewrite orthogonalE.
apply/eqP/matrixP => i j; rewrite !mxE -H /dotmul !mxE.
apply eq_bigr => k _; by rewrite !mxE.
Qed.
Lemma OSn_On m (P : 'M[T]_n) :
(block_mx (1%:M : 'M_m) 0 0 P \is 'O[T]_(m + n)) = (P \is 'O[T]_n).
Proof.
rewrite !qualifE /orthogonal_pred tr_block_mx trmx1 !trmx0 mulmx_block.
rewrite !(mulmx0, mul0mx, mulmx1, mul1mx, addr0, add0r) scalar_mx_block.
by apply/eqP/eqP => [/eq_block_mx[] |->//].
Qed.
End orthogonal_rotation_properties0.
Lemma SOSn_SOn (T : comRingType) n m (P : 'M[T]_n.+1) :
(block_mx (1%:M : 'M_m) 0 0 P \is 'SO[T]_(m + n.+1)) = (P \is 'SO[T]_n.+1).
Proof. by rewrite qualifE /rotation_pred OSn_On det_lblock det1 mul1r. Qed.
Section orthogonal_rotation_properties.
Variables (n' : nat) (T : comUnitRingType).
Let n := n'.+1.
Lemma orthogonalEinv M : (M \is 'O[T]_n) = (M \is a GRing.unit) && (M^-1 == M^T).
Proof.
rewrite orthogonalE; have [Mu | notMu] /= := boolP (M \in unitmx); last first.
by apply: contraNF notMu => /eqP /mulmx1_unit [].
by rewrite -(inj_eq (@mulrI _ M^-1 _)) ?unitrV // mulr1 mulKr.
Qed.
Lemma orthogonal_unit M : (M \is 'O[T]_n) -> (M \is a GRing.unit).
Proof. by rewrite orthogonalEinv => /andP []. Qed.
Lemma orthogonalV M : (M^T \is 'O[T]_n) = (M \is 'O[T]_n).
Proof.
by rewrite !orthogonalEinv unitmx_tr -trmxV (inj_eq (@trmx_inj _ _ _)).
Qed.
Lemma orthogonal_inv M : M \is 'O[T]_n -> M^-1 = M^T.
Proof. by rewrite orthogonalEinv => /andP [_ /eqP]. Qed.
Lemma orthogonalEC M : (M \is 'O[T]_n) = (M^T * M == 1).
Proof. by rewrite -orthogonalV orthogonalE trmxK. Qed.
Lemma orthogonal_tr_mul M : (M \is 'O[T]_n) -> M^T *m M = 1.
Proof. by rewrite orthogonalEC => /eqP. Qed.
Lemma orthogonal_divr_closed : divr_closed (orthogonal n T).
Proof.
split => [| P Q HP HQ]; first exact: orthogonal1.
rewrite orthogonalE orthogonal_inv // trmx_mul trmxK -mulrA.
by rewrite -orthogonal_inv // mulKr // orthogonal_unit.
Qed.
HB.instance Definition _ := GRing.isDivClosed.Build _ (@orthogonal_pred n T) orthogonal_divr_closed.
Lemma orthogonal_mulr_2closed : GRing.mulr_2closed 'O[T]_n.
Proof.
by move=> /= M N MO NO; rewrite rpredM.
Qed.
(* TODO: useless? *)
(*
Canonical orthogonal_is_mulr_closed := MulrPred orthogonal_divr_closed.
Canonical orthogonal_is_divr_closed := DivrPred orthogonal_divr_closed.
Canonical orthogonal_is_smulr_closed := SmulrPred orthogonal_divr_closed.
Canonical orthogonal_is_sdivr_closed := SdivrPred orthogonal_divr_closed.
*)
Lemma rotationE M : (M \is 'SO[T]_n) = (M \is 'O[T]_n) && (\det M == 1). Proof. by []. Qed.
Lemma rotationV M : (M^T \is 'SO[T]_n) = (M \is 'SO[T]_n).
Proof. by rewrite rotationE orthogonalV det_tr -rotationE. Qed.
Lemma rotation_inv M : M \is 'SO[T]_n -> M^-1 = M^T.
Proof. by rewrite rotationE orthogonalEinv => /andP[/andP[_ /eqP]]. Qed.
Lemma rotation_det M : M \is 'SO[T]_n -> \det M = 1.
Proof. by move=> /andP[_ /eqP]. Qed.
Lemma rotation1 : 1 \is 'SO[T]_n.
Proof. apply/andP; by rewrite orthogonal1 det1. Qed.
Lemma rotation_tr_mul M : (M \is 'SO[T]_n) -> M^T *m M = 1.
Proof. by move=> /rotation_sub /orthogonal_tr_mul. Qed.
Lemma rotation_divr_closed : divr_closed 'SO[T]_n.
Proof.
split => [|P Q Prot Qrot]; first exact: rotation1.
rewrite rotationE rpred_div ?rotation_sub //=.
by rewrite det_mulmx det_inv !rotation_det // divr1.
Qed.
HB.instance Definition _ := GRing.isDivClosed.Build _ (@rotation_pred n T) rotation_divr_closed.
Lemma rotation_mulr_2closed : GRing.mulr_2closed 'SO[T]_n.
Proof.
move=> M N MSO NSO.
rewrite rotationE rpredM//=; last 2 first.
exact: rotation_sub.
exact: rotation_sub.
by rewrite detM rotation_det// rotation_det// mulr1.
Qed.
HB.instance Definition _ := GRing.isMul2Closed.Build _ 'SO[T]_n rotation_mulr_2closed.
(*Canonical rotation_is_mulr_closed := MulrPred rotation_divr_closed.
Canonical rotation_is_divr_closed := DivrPred rotation_divr_closed.*)
Lemma orthogonalPcol M :
reflect (forall i j, (col i M)^T *d (col j M)^T = (i == j)%:R) (M \is 'O[T]_n).
Proof.
apply: (iffP idP) => [MSO i j|H] /=.
- move: (MSO); rewrite -rpredV orthogonal_inv // => /orthogonalP <-.
by rewrite 2!tr_col.
- suff MSO : M^T \is 'O[T]_n.
move/orthogonal_inv: (MSO); rewrite trmxK => <-; by rewrite rpredV.
apply/orthogonalP => i j; by rewrite -H 2!tr_col.
Qed.
End orthogonal_rotation_properties.
Section orthogonal_rotation_properties1.
Variables (n' : nat) (T : realDomainType).
Let n := n'.+1.
Lemma orthogonal_det M : M \is 'O[T]_n -> `|\det M| = 1.
Proof.
move=> /eqP /(congr1 determinant); rewrite detM det_tr det1 => /eqP.
by rewrite sqr_norm_eq1 => /eqP.
Qed.
End orthogonal_rotation_properties1.
Lemma orthogonal2P (T : ringType) M : reflect (M \is 'O[T]_2)
[&& row 0 M *d row 0 M == 1, row 0 M *d row 1 M == 0,
row 1 M *d row 0 M == 0 & row 1 M *d row 1 M == 1].
Proof.
apply (iffP idP) => [/and4P[] /eqP H1 /eqP H2 /eqP H3 /eqP H4|]; last first.
move/orthogonalP => H; by rewrite !H /= !eqxx.
apply/orthogonalP => i j.
case/boolP : (i == 0) => [|/ifnot01P]/eqP->;
by case/boolP : (j == 0) => [|/ifnot01P]/eqP->.
Qed.
(* TODO: move? use *d? *)
Lemma dotmul_conjc_eq0 {T : rcfType} n (v : 'rV[T[i]]_n.+1) :
(v *m map_mx conjc v^T == 0) = (v == 0).
Proof.
apply/idP/idP => [H|/eqP ->]; last by rewrite mul0mx.
have : \sum_(i < n.+1) v``_i * (v``_i)^* = 0.
move/eqP/matrixP : H =>/(_ 0 0).
rewrite !mxE => H; rewrite -{2}H.
apply/eq_bigr => /= i _; by rewrite !mxE.
move/eqP; rewrite psumr_eq0 /= => [/allP K|]; last first.
move=> i _; by rewrite -sqr_normc exprn_ge0.
apply/eqP/rowP => i.
move: (K i); rewrite /index_enum -enumT mem_enum inE => /(_ isT).
rewrite -sqr_normc sqrf_eq0 normr_eq0 => /eqP ->; by rewrite mxE.
Qed.
(* eigenvalues of orthogonal matrices have norm 1 *)
Lemma eigenvalue_O (T : rcfType) n M : M \is 'O[T]_n.+1 -> forall k,
k \in eigenvalue (map_mx (fun x => x%:C%C) M) -> `| k | = 1.
Proof.
move=> MSO /= k.
case/eigenvalueP => v kv v0.
move/(congr1 trmx)/(congr1 (fun x => map_mx conjc x)) : (kv).
rewrite trmx_mul map_mxM linearZ /= map_mxZ map_trmx.
move/(congr1 (fun x => (k *: v) *m x)).
rewrite -{1}kv -mulmxA (mulmxA (map_mx _ M)) (_ : map_mx _ M *m _ = 1%:M); last first.
rewrite (_ : map_mx conjc _ = map_mx (fun x => x%:C%C) M^T); last first.
apply/matrixP => i j; by rewrite !mxE conjc_real.
rewrite orthogonalE in MSO.
by rewrite -map_mxM mulmxE (eqP MSO) map_mx1.
rewrite mul1mx -scalemxAr /= -scalemxAl scalerA => /eqP.
rewrite -subr_eq0 -{1}(scale1r (v *m _)) -scalerBl scaler_eq0 => /orP [].
by rewrite subr_eq0 mulrC -sqr_normc -{1}(expr1n _ 2) eqr_expn2 // ?ler01 // => /eqP.
by rewrite dotmul_conjc_eq0 (negbTE v0).
Qed.
Lemma norm_row_of_O (T : rcfType) n M : M \is 'O[T]_n.+1 -> forall i, norm (row i M) = 1.
Proof.
move=> MSO i.
apply/eqP; rewrite -(@eqr_expn2 _ 2) // ?norm_ge0 // expr1n; apply/eqP.
rewrite -dotmulvv; move/orthogonalP : MSO => /(_ i i) ->; by rewrite eqxx.
Qed.
Lemma dot_row_of_O (T : ringType) n M : M \is 'O[T]_n.+1 -> forall i j,
row i M *d row j M = (i == j)%:R.
Proof. by move/orthogonalP. Qed.
Lemma norm_col_of_O (T : rcfType) n M : M \is 'O[T]_n.+1 -> forall i, norm (col i M)^T = 1.
Proof.
move=> MSO i.
apply/eqP.
rewrite -(@eqr_expn2 _ 2) // ?norm_ge0 // expr1n -dotmulvv tr_col dotmulvv.
by rewrite norm_row_of_O ?expr1n // orthogonalV.
Qed.
Lemma orth_preserves_sqr_norm (T : comRingType) n M : M \is 'O[T]_n.+1 ->
{mono (fun u => u *m M) : x / x *d x}.
Proof.
move=> HM u; rewrite dotmul_trmx -mulmxA (_ : M *m _ = 1%:M) ?mulmx1 //.
by move: HM; rewrite orthogonalE => /eqP.
Qed.
Lemma orth_preserves_dotmul {T : numDomainType} n (f : 'M[T]_n.+1) :
{mono (fun u => u *m f) : x y / x *d y} <-> f \is 'O[T]_n.+1.
Proof.
split => H.
apply/orthogonalP => i j.
by rewrite 2!rowE H dotmul_delta_mx mxE eqxx /= eq_sym.
move=> u v.
have := orth_preserves_sqr_norm H (u + v).
rewrite mulmxDl dotmulD.
rewrite dotmulD.
rewrite orth_preserves_sqr_norm // (orth_preserves_sqr_norm H v) //.
move/(congr1 (fun x => x - v *d v)).
rewrite -!addrA subrr 2!addr0.
move/(congr1 (fun x => - (u *d u) + x)).
rewrite !addrA (addrC (- (u *d u))) subrr 2!add0r.
rewrite -2!mulr2n => /eqP.
by rewrite eqr_pmuln2r // => /eqP.
Qed.
Lemma orth_preserves_norm (T : rcfType) n M : M \is 'O[T]_n.+1 ->
{mono (fun u => u *m M) : x / norm x }.
Proof. move=> HM v; by rewrite /norm (proj2 (orth_preserves_dotmul M) HM). Qed.
Lemma Oij_ub (T : rcfType) n (M : 'M[T]_n.+1) : M \is 'O[T]_n.+1 -> forall i j, `| M i j | <= 1.
Proof.
move=> /norm_row_of_O MO i j; rewrite leNgt; apply/negP => abs.
move: (MO i) => /(congr1 (fun x => x ^+ 2)); apply/eqP.
rewrite gt_eqF // sqr_norm (bigD1 j) //= !mxE -(addr0 (1 ^+ 2)) ltr_le_add //.
by rewrite -(sqr_normr (M _ _)) ltr_expn2r.
rewrite sumr_ge0 // => k ij; by rewrite sqr_ge0.
Qed.
Lemma O_tr_idmx (T : rcfType) n (M : 'M[T]_n.+1) : M \is 'O[T]_n.+1 -> \tr M = n.+1%:R -> M = 1.
Proof.
move=> MO; move: (MO) => /norm_row_of_O MO' tr3.
have Mdiag : forall i, M i i = 1.
move=> i; apply/eqP/negPn/negP => Mii; move: tr3; apply/eqP.
rewrite lt_eqF // /mxtrace.
rewrite (bigD1 i) //=.
rewrite (eq_bigr (fun i : 'I_n.+1 => M (inord i) (inord i))); last first.
by move=> j _; congr (M _ _); apply val_inj => /=; rewrite inordK.
rewrite -(big_mkord [pred x : nat | x != i] (fun i => M (inord i) (inord i))).
rewrite -[in n.+1%:R](card_ord n.+1) -sum1_card (bigD1 i) //= natrD.
rewrite ltr_le_add //; first by rewrite lt_neqAle Mii /= ler_norml1 // Oij_ub.
rewrite [in X in _ <= X](@big_morph _ _ _ 0 (fun x y => x + y)%R) //; last first.
by move=> x y; rewrite natrD.
rewrite -(big_mkord [pred x : nat | x != i] (fun i => 1)).
apply ler_sum => j ji; by rewrite ler_norml1 // Oij_ub.
apply/matrixP => i j; rewrite !mxE.
case/boolP : (i == j) => [/eqP ->|ij]; first by move : Mdiag => /(_ j).
move: (MO' i) => /(congr1 (fun x => x ^+ 2)).
rewrite expr1n sqr_norm (bigD1 i) //= mxE.
move: Mdiag => /(_ i) -> /eqP.
rewrite expr1n addrC eq_sym -subr_eq subrr eq_sym psumr_eq0 /=; last first.
by move=> *; rewrite sqr_ge0.
by move/allP => /(_ j (mem_index_enum _)); rewrite eq_sym ij implyTb mxE sqrf_eq0 => /eqP.
Qed.
Section Crossproduct.
Variable (R : comRingType) (n' : nat).
Let n := n'.+1.
Definition cross (u : 'M[R]_(n', n)) : 'rV_n :=
\row_(k < n) \det (col_mx (@delta_mx _ 1%N _ 0 k) u).
Lemma cross_multilinear (A B C : 'M_(n',n)) (i0 : 'I_n') (b c : R) :
row i0 A = b *: row i0 B + c *: row i0 C ->
row' i0 B = row' i0 A ->
row' i0 C = row' i0 A -> cross A = b *: cross B + c *: cross C.
Proof.
move=> rABC rBA rCA; apply/rowP=> k; rewrite !mxE.
have bumpD (i k1 : 'I_n') : bump (bump 0 i0) i = (1 + k1)%N -> i0 != k1.
move=> Bi; apply/eqP => i0Ek1; move: Bi; rewrite -i0Ek1.
rewrite /bump !add1n; case: ltnP => [u0Li He|iLi0 He].
by rewrite -He leqNgt ltnS leqnn in u0Li.
by rewrite -ltnS -He ltnn in iLi0.
apply: (@determinant_multilinear _ _ _ _ _ (fintype.lift 0 i0)).
-apply/rowP => i; rewrite !mxE; case: fintype.splitP; first by do 2 case.
move=> k1 H; rewrite (_ : k1 = i0).
by move/rowP : rABC => /(_ i); rewrite !mxE.
apply/val_eqP/eqP=> /=.
by rewrite /= /bump !add1n in H; case: H.
- apply/matrixP => i j; rewrite !mxE /=; case: fintype.splitP => // k1 /= H1.
have /unlift_some[k2 k2E _] := bumpD i k1 H1.
by move/matrixP : rBA => /(_ k2 j); rewrite !mxE k2E.
apply/matrixP => i j; rewrite !mxE /=; case: fintype.splitP => // k1 /= H1.
have /unlift_some[k2 k2E _] := bumpD i k1 H1.
by move/matrixP : rCA => /(_ k2 j); rewrite !mxE k2E.
Qed.
Lemma dot_cross (u : 'rV[R]_n) (V : 'M[R]_(n', n)) :
u *d (cross V) = \det (col_mx u V).
Proof.
rewrite dotmulE (expand_det_row _ 0); apply: eq_bigr => k _; rewrite !mxE /=.
case: fintype.splitP => j //=; rewrite ?ord1 //= => _ {j}; congr (_ * _).
rewrite (expand_det_row _ 0) (bigD1 k) //= big1 ?addr0; last first.
move=> i neq_ik; rewrite !mxE; case: fintype.splitP=> //= j.
by rewrite ord1 mxE (negPf neq_ik) mul0r.
rewrite !mxE; case: fintype.splitP => //= j _; rewrite ord1 !mxE !eqxx mul1r.
rewrite !expand_cofactor; apply: eq_bigr => s s0; congr (_ * _).
apply: eq_bigr => i; rewrite !mxE.
by case: fintype.splitP => //= j'; rewrite ord1 {j'} -val_eqE => /= ->.
Qed.
End Crossproduct.
Section Crossproduct_fieldType.
Variable (F : fieldType) (n' : nat).
Let n := n'.+1.
Lemma cross_normal (A : 'M[F]_(n', n)) : A _|_ cross A.
Proof.
apply/rV_subP => v /submxP [M ->]; rewrite normalvv dot_cross; apply/det0P.
exists (row_mx (- 1) M); rewrite ?row_mx_eq0 ?oppr_eq0 ?oner_eq0 //.
by rewrite mul_row_col mulNmx mul1mx addNr.
Qed.
End Crossproduct_fieldType.
Section row2.
Variable R : ringType.
Definition row2 (a b : R) : 'rV[R]_2 :=
\row_p [eta \0 with 0 |-> a, 1 |-> b] p.
Lemma row2_of_row (M : 'M[R]_2) i : row i M = row2 (M i 0) (M i 1).
Proof. by apply/rowP=> j; rewrite !mxE /=; case: ifPn=> [|/ifnot01P]/eqP->. Qed.
End row2.
Section row3.
Variable R : ringType.
Implicit Types a b c : R.
Definition row3 a b c : 'rV[R]_3 :=
\row_p [eta \0 with 0 |-> a, 1 |-> b, 2%:R |-> c] p.
Lemma row3K (u : 'rV[R]_3) : u = row3 u``_0 u``_1 u``_(2%:R).
Proof. by apply/row3P/and3P; split; rewrite !mxE. Qed.
Lemma col_row3 a b c i : col i (row3 a b c) = ((row3 a b c) ``_ i)%:M.
Proof. by apply/rowP => k; rewrite (ord1 k) !mxE /= mulr1n. Qed.
Lemma row_mx_colE n (M : 'M[R]_(n, 3)) :
row_mx (col 0 M) (row_mx (col 1 M) (col 2%:R M)) = M.
Proof.
rewrite -[in RHS](@hsubmxK _ n 1 2 M) (_ : lsubmx _ = col 0 M); last first.
apply/colP => i; rewrite !mxE /= (_ : lshift 2 0 = 0) //; exact/val_inj.
rewrite (_ : rsubmx _ = row_mx (col 1 M) (col 2%:R M)) //.
set a := rsubmx _; rewrite -[in LHS](@hsubmxK _ n 1 1 a); congr row_mx.
apply/colP => i; rewrite !mxE /= (_ : rshift 1 _ = 1) //; exact/val_inj.
apply/colP => i; rewrite !mxE /= (_ : rshift 1 (rshift 1 0) = 2%:R) //.
exact/val_inj.
Qed.
Lemma row3E a b c : row3 a b c = row_mx a%:M (row_mx b%:M c%:M).
Proof. by rewrite -[LHS]row_mx_colE !col_row3 !mxE. Qed.
Lemma row_row3 n (M : 'M[R]_(n, 3)) i : row i M = row3 (M i 0) (M i 1) (M i 2%:R).
Proof.
by apply/rowP=> k; rewrite !mxE /=; case: ifPn=>[|/ifnot0P/orP[]]/eqP->.
Qed.
Lemma row3N a b c : - row3 a b c = row3 (- a) (- b) (- c).
Proof.
apply/rowP => i; rewrite !mxE /= ; case: ifPn; rewrite ?opprB // => ?.
by case: ifPn; rewrite ?opprB // => ?; case: ifPn; rewrite ?opprB // oppr0.
Qed.
Lemma row3Z a b c k : k *: row3 a b c = row3 (k * a) (k * b) (k * c).
Proof.
apply/rowP => i; rewrite !mxE /=.
case: ifPn => // ?; case: ifPn => // ?; case: ifPn => // ?; by Simp.r.
Qed.
Lemma row3D a b c a' b' c' :
row3 a b c + row3 a' b' c' = row3 (a + a') (b + b') (c + c').
Proof.
rewrite 3!row3E (add_row_mx a%:M) (add_row_mx b%:M).
rewrite -(scalemx1 _ a) -(scalemx1 _ a') -(scalemx1 _ b) -(scalemx1 _ b').
rewrite -(scalemx1 _ c) -(scalemx1 _ c'); by do 3! rewrite -scalerDl scalemx1.
Qed.
Lemma row30 : row3 0 0 0 = 0 :> 'rV[R]_3.
Proof. by apply/rowP => a; rewrite !mxE /=; do 3 case: ifPn => //. Qed.
Lemma row3_proj (u : 'rV[R]_3) :
u = row3 (u``_0) 0 0 + row3 0 (u``_1) 0 + row3 0 0 (u``_2%:R).
Proof.
rewrite 2!row3D !(addr0,add0r); apply/rowP => k; by rewrite -row_row3 mxE.
Qed.
Lemma e0row : 'e_0 = row3 1 0 0.
Proof.
by apply/rowP=> i; rewrite !mxE /=; case: ifPn=> //;
rewrite ifnot0=> /orP[]/eqP ->.
Qed.
Lemma e1row : 'e_1 = row3 0 1 0.
Proof.
by apply/rowP => i; rewrite !mxE /=; case: ifPn => [/eqP -> //|];
rewrite ifnot0=> /orP[]/eqP ->.
Qed.
Lemma e2row : 'e_2%:R = row3 0 0 1.
Proof.
by apply/rowP => i; rewrite !mxE /=; case: ifPn => [/eqP -> //|];
rewrite ifnot0=> /orP[]/eqP ->.
Qed.
Lemma row3e0 a : row3 a 0 0 = a *: 'e_0.
Proof. by rewrite e0row row3Z mulr1 mulr0. Qed.
Lemma row3e1 a : row3 0 a 0 = a *: 'e_1.
Proof. by rewrite e1row row3Z mulr1 mulr0. Qed.
Lemma row3e2 a : row3 0 0 a = a *: 'e_2%:R.
Proof. by rewrite e2row row3Z mulr1 mulr0. Qed.
End row3.
Lemma norm_row3z (T : rcfType) (z : T) : norm (row3 0 0 z) = `|z|.
Proof. by rewrite /norm dotmulE sum3E !mxE /= ?(mul0r,add0r) sqrtr_sqr. Qed.
Section col_mx2.
Variable (T : ringType).
Implicit Types (u v : 'rV[T]_2) (M : 'M[T]_2).
Definition col_mx2 u v := \matrix_(i < 2) [eta \0 with 0 |-> u, 1 |-> v] i.
Lemma eq_col_mx2 a a' b b' c c' d d' :
col_mx2 (row2 a b) (row2 c d) = col_mx2 (row2 a' b') (row2 c' d') ->
[/\ a = a', b = b', c = c' & d = d'].
Proof.
move/matrixP => H; split; by [
move/(_ 0 0) : H; rewrite !mxE | move/(_ 0 1) : H; rewrite !mxE |
move/(_ 1 0) : H; rewrite !mxE | move/(_ 1 1) : H; rewrite !mxE].
Qed.
Lemma col_mx2_rowE M : M = col_mx2 (row 0 M) (row 1 M).
Proof.
apply/row_matrixP => i; by rewrite rowK /=; case: ifPn => [|/ifnot01P]/eqP->.
Qed.
Lemma mul_col_mx2 n (c1 c2 : 'cV[T]_n) u v :
row_mx c1 c2 *m col_mx2 u v =
row_mx (c1 *m u``_0%:M + c2 *m v``_0%:M) (c1 *m u``_1%:M + c2 *m v``_1%:M).
Proof.
suff -> : col_mx2 u v = @block_mx _ 1 1 1 1 u``_0%:M u``_1%:M v``_0%:M v``_1%:M.
by rewrite (mul_row_block c1 c2 u``_0%:M).
apply/matrixP => a b; case/boolP : (a == 0) => a0.
- case/boolP : (b == 0) => b0.
+ rewrite (eqP a0) (eqP b0) !mxE /= split1 unlift_none //=.
by rewrite !mxE split1 unlift_none /= !mxE eqxx mulr1n.
+ have /eqP b1 : b == 1 by rewrite -ifnot01.
rewrite b1 (eqP a0) [in LHS]mxE /=.
transitivity ((block_mx u``_0%:M u``_1%:M v``_0%:M v``_1%:M)
(lshift 1 0) (rshift 1 0)); last by f_equal; exact/val_inj.
by rewrite block_mxEur mxE eqxx mulr1n.
- have a1 : a == 1 by rewrite -ifnot01.
case/boolP : (b == 0) => b0.
+ rewrite (eqP a1) (eqP b0) [in LHS]mxE /=.
transitivity ((block_mx u``_0%:M u``_1%:M v``_0%:M v``_1%:M)
(rshift 1 0) (lshift 1 0)); last by f_equal; exact/val_inj.
by rewrite block_mxEdl mxE eqxx mulr1n.
+ have /eqP b1 : b == 1 by rewrite -ifnot01.
rewrite (eqP a1) b1 [in LHS]mxE /=.
transitivity ((block_mx u``_0%:M u``_1%:M v``_0%:M v``_1%:M)
(rshift 1 0) (rshift 1 0)); last by f_equal; exact/val_inj.
by rewrite block_mxEdr mxE eqxx mulr1n.
Qed.
End col_mx2.
Section col_mx3.
Variable (T : ringType).
Implicit Types (u v w : 'rV[T]_3) (M : 'M[T]_3).
Definition col_mx3 u v w :=
\matrix_(i < 3) [eta \0 with 0 |-> u, 1 |-> v, 2%:R |-> w] i.
Lemma trmx_col_mx3_row3 (a b c e f g h i j : T) :
(col_mx3 (row3 a b c) (row3 e f g) (row3 h i j))^T =
col_mx3 (row3 a e h) (row3 b f i) (row3 c g j).
Proof. by apply/matrix3P/and9P; split; rewrite !mxE. Qed.
Lemma col_mx3_row M : col_mx3 (row 0 M) (row 1 M) (row 2%:R M) = M.
Proof.
by apply/row_matrixP=> i; rewrite rowK /=; case: ifPn=> [|/ifnot0P/orP[]]/eqP->.
Qed.
Lemma mulmx_row3_col3 a b c u v w :
row3 a b c *m col_mx3 u v w = a *: u + b *: v + c *: w.
Proof. apply/rowP => n; by rewrite !mxE sum3E !mxE. Qed.
Lemma col_mx3E u v w : col_mx3 u v w = col_mx u (col_mx v w).
Proof.
rewrite -[LHS]col_mx3_row; apply/row_matrixP => i; rewrite !rowK /=.
case: ifPn => [|/ifnot0P/orP[]]/eqP->.
- by rewrite (_ : 0 = @lshift 1 _ 0) ?(@rowKu _ 1) ?row_id //; exact: val_inj.
- rewrite (_ : 1 = @rshift 1 _ 0) ?(@rowKd _ 1); last exact: val_inj.
by rewrite (_ : 0 = @lshift 1 _ 0) ?(@rowKu _ 1) ?row_id //; exact: val_inj.
- rewrite (_ : 2%:R = @rshift 1 _ 1) ?(@rowKd _ 1); last exact: val_inj.
by rewrite (_ : 1 = @rshift 1 1 0) ?(@rowKd _ 1) ?row_id //; exact: val_inj.
Qed.
Lemma row'_col_mx3 (i : 'I_3) (u v w : 'rV[T]_3) :
row' i (col_mx3 u v w) = [eta \0 with
0 |-> \matrix_(k < 2) [eta \0 with 0 |-> v, 1 |-> w] k,
1 |-> \matrix_(k < 2) [eta \0 with 0 |-> u, 1 |-> w] k,
2%:R |-> \matrix_(k < 2) [eta \0 with 0 |-> u, 1 |-> v] k] i.
Proof.
case: i => [[|[|[|?]]]] ?; apply/matrixP=> [] [[|[|[|?]]]] ? j;
by rewrite !mxE.
Qed.
Lemma col_mx3_perm_12 u v w : xrow 1 2%:R (col_mx3 u v w) = col_mx3 u w v.
Proof.
apply/matrixP => -[[|[|[] //]] ?] [[|[|[] //]] ?]; by rewrite !mxE permE.
Qed.
Lemma col_mx3_perm_01 u v w : xrow 0 1 (col_mx3 u v w) = col_mx3 v u w.
Proof.
apply/matrixP => -[[|[|[] //]] ?] [[|[|[] //]] ?]; by rewrite !mxE permE.
Qed.