-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquaternion.v
1464 lines (1186 loc) · 51.9 KB
/
quaternion.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.
Require Import NsatzTactic.
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 Require Import interval reals trigo.
Require Import ssr_ext euclidean vec_angle frame rot.
From mathcomp.analysis Require Import forms.
Require Import extra_trigo.
(******************************************************************************)
(* Quaternions *)
(* *)
(* This file develops the theory of quaternions. It defines the type of *)
(* quaternions and the type of unit quaternions and show that quaternions *)
(* form a ZmodType, a RingType, a LmodType, a UnitRingType. It also defines *)
(* polar coordinates and dual quaternions. *)
(* *)
(* quat R == type of quaternions over the ringType R *)
(* x%:q == quaternion with scalar part x and vector part 0 *)
(* x \is realq == the quaternion x has no vector part *)
(* u%:v == pure quaternion (or vector quaternion) with scalar part 0 *)
(* and vector part u *)
(* x \is pureq == the quaternion x has no scalar part *)
(* `i, `j, `k == basic quaternions *)
(* x.1 == scalar part of the quaternion x *)
(* x.2 == vector part of the quaternion x *)
(* x^*q == conjugate of quaternion x *)
(* normq x == norm of the quaternion x *)
(* uquat R == type of unit quaternions, i.e., quaternions with norm 1 *)
(* conjugation x == v |-> x v x^* *)
(* *)
(* Polar coordinates: *)
(* polar_of_quat a == polar coordinates of the quaternion a *)
(* quat_of_polar a u == quaternion corresponding to the polar coordinates *)
(* angle a and vector u *)
(* quat_rot x == snd \o conjugation x (rotation of angle 2a about *)
(* vector v where a,v are the polar coordinates of x, *)
(* a unit quaternion *)
(* Dual numbers: *)
(* dual R == the type of dual numbers over a ringType R *)
(* x.1 == left part of the dual number x *)
(* x.2 == right part of the dual number x *)
(* Dual numbers are equipped with a structure of ZmodType, RingType, and of *)
(* LmodType when R is a ringType, of Com/UnitRingType when R is a *)
(* Com/UnitRingType. *)
(* *)
(* Dual quaternions: *)
(* x +ɛ* y == dual number formed by x and y *)
(* dquat == type of dual quaternions *)
(* x \is puredq == the dual quaternion x is pure *)
(* a \is dnum == a has no vector part *)
(* x^*dq == conjugate of dual quaternion x *)
(* *)
(******************************************************************************)
Reserved Notation "x %:q" (at level 2, format "x %:q").
Reserved Notation "x %:v" (at level 2, format "x %:v").
Reserved Notation "x '_i'" (at level 1, format "x '_i'").
Reserved Notation "x '_j'" (at level 1, format "x '_j'").
Reserved Notation "x '_k'" (at level 1, format "x '_k'").
Reserved Notation "'`i'".
Reserved Notation "'`j'".
Reserved Notation "'`k'".
Reserved Notation "x '^*q'" (at level 2, format "x '^*q'").
Reserved Notation "r *`i" (at level 3).
Reserved Notation "r *`j" (at level 3).
Reserved Notation "r *`k" (at level 3).
Reserved Notation "x +ɛ* y"
(at level 40, left associativity, format "x +ɛ* y").
Reserved Notation "x -ɛ* y"
(at level 40, left associativity, format "x -ɛ* y").
Reserved Notation "x '^*d'" (at level 2, format "x '^*d'").
Reserved Notation "x '^*dq'" (at level 2, format "x '^*dq'").
Declare Scope quat_scope.
Declare Scope dual_scope.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Local Open Scope ring_scope.
Import Order.TTheory GRing.Theory Num.Def Num.Theory.
(* TODO: overrides forms.v *)
Notation "u '``_' i" := (u (@GRing.zero _) i) : ring_scope.
Section quaternion0.
Variable R : ringType.
Record quat := mkQuat {quatl : R ; quatr : 'rV[R]_3 }.
Implicit Types x y : quat.
Local Notation "x %:q" := (mkQuat x 0).
Local Notation "x %:v" := (mkQuat 0 x).
Local Notation "'`i'" := ('e_0)%:v.
Local Notation "'`j'" := ('e_1)%:v.
Local Notation "'`k'" := ('e_2%:R)%:v.
Local Notation "x '_i'" := ((x.2)``_0).
Local Notation "x '_j'" := ((x.2)``_1).
Local Notation "x '_k'" := ((x.2)``_(2%:R : 'I_3)).
Coercion pair_of_quat x := let: mkQuat x1 x2 := x in (x1, x2).
Let quat_of_pair (a : R * 'rV[R]_3) := let: (a1, a2) := a in mkQuat a1 a2.
Lemma quat_of_pairK : cancel pair_of_quat quat_of_pair.
Proof. by case. Qed.
HB.instance Definition _ := Equality.copy quat (can_type quat_of_pairK).
HB.instance Definition _ := Choice.copy quat (can_type quat_of_pairK).
Lemma eq_quat x y : (x == y) = (x.1 == y.1) && (x.2 == y.2).
Proof.
case: x y => [? ?] [? ?] /=.
apply/idP/idP => [/eqP [ -> ->]|/andP[/eqP -> /eqP -> //]]; by rewrite !eqxx.
Qed.
Definition addq x y := nosimpl (mkQuat (x.1 + y.1) (x.2 + y.2)).
Lemma addqC : commutative addq.
Proof. move=> *; congr mkQuat; by rewrite addrC. Qed.
Lemma addqA : associative addq.
Proof. move=> *; congr mkQuat; by rewrite addrA. Qed.
Lemma add0q : left_id 0%:q addq.
Proof. case=> *; by rewrite /addq /= 2!add0r. Qed.
Definition oppq x := nosimpl (mkQuat (- x.1) (- x.2)).
Lemma addNq : left_inverse 0%:q oppq addq.
Proof. move=> *; congr mkQuat; by rewrite addNr. Qed.
HB.instance Definition _ := @GRing.isZmodule.Build quat _ _ _ addqA addqC add0q addNq.
Lemma addqE x y : x + y = addq x y. Proof. by []. Qed.
Lemma oppqE x : - x = oppq x. Proof. by []. Qed.
Local Notation "r *`i" := (mkQuat 0 (r *: 'e_0)).
Local Notation "r *`j" := (mkQuat 0 (r *: 'e_1)).
Local Notation "r *`k" := (mkQuat 0 (r *: 'e_2%:R)).
Lemma quatE x : x = x.1%:q + x.2%:v.
Proof. by apply/eqP; rewrite eq_quat /=; Simp.r. Qed.
Lemma quatrE x : x.2%:v = x _i *`i + x _j *`j + x _k *`k.
Proof. by apply/eqP; rewrite eq_quat /=; Simp.r; rewrite -vec3E. Qed.
Lemma quat_scalarE (r s : R) : (r%:q == s%:q) = (r == s).
Proof. by apply/idP/idP => [/eqP[] ->|/eqP -> //]. Qed.
Lemma quat_realN (r : R) : (- r)%:q = - (r%:q).
Proof. by rewrite oppqE /oppq /= oppr0. Qed.
Lemma quat_vectN (u : 'rV[R]_3) : (- u)%:v = - (u%:v).
Proof. by rewrite oppqE /oppq /= oppr0. Qed.
Lemma quat_realD (r s : R) : (r + s)%:q = r%:q + s%:q.
Proof. by rewrite addqE /addq /= add0r. Qed.
Lemma quat_vectD (u v : 'rV[R]_3) : (u + v)%:v = u%:v + v%:v.
Proof. by rewrite addqE /addq /= addr0. Qed.
Lemma quat_realB (r s : R) : (r - s)%:q = r%:q - s%:q.
Proof. by rewrite quat_realD quat_realN. Qed.
Lemma quat_vectB (u v : 'rV[R]_3) : (u - v)%:v = u%:v - v%:v.
Proof. by rewrite quat_vectD quat_vectN. Qed.
Definition pureq := [qualify x : quat | x.1 == 0].
Fact pureq_key : pred_key pureq. Proof. by []. Qed.
Canonical pureq_keyed := KeyedQualifier pureq_key.
Definition realq := [qualify x : quat | x.2 == 0].
Fact realq_key : pred_key realq. Proof. by []. Qed.
Canonical realq_keyed := KeyedQualifier realq_key.
End quaternion0.
Delimit Scope quat_scope with quat.
Local Open Scope quat_scope.
Notation "r %:q" := (mkQuat r 0) : quat_scope.
Notation "u %:v" := (mkQuat 0 u) : quat_scope.
Notation "'`i'" := ('e_0)%:v : quat_scope.
Notation "'`j'" := ('e_1)%:v : quat_scope.
Notation "'`k'" := ('e_2%:R)%:v : quat_scope.
Notation "x '_i'" := ((x.2)``_0) : quat_scope.
Notation "x '_j'" := ((x.2)``_1) : quat_scope.
Notation "x '_k'" := ((x.2)``_(2%:R : 'I_3)) : quat_scope.
Notation "r *`i" := (mkQuat 0 (r *: 'e_0)) : quat_scope.
Notation "r *`j" := (mkQuat 0 (r *: 'e_1)) : quat_scope.
Notation "r *`k" := (mkQuat 0 (r *: 'e_2%:R)) : quat_scope.
Arguments pureq {R}.
Section quaternion.
Variable R : comRingType.
Implicit Types x y : quat R.
Definition mulq x y := nosimpl
(mkQuat (x.1 * y.1 - x.2 *d y.2) (x.1 *: y.2 + y.1 *: x.2 + x.2 *v y.2)).
Lemma mulqA : associative mulq.
Proof.
move=> [a a'] [b b'] [c c']; congr mkQuat => /=.
- rewrite mulrDr mulrDl mulrA -!addrA; congr (_ + _).
rewrite mulrN !dotmulDr !dotmulDl !opprD !addrA dot_crossmulC; congr (_ + _).
rewrite addrC addrA; congr (_ + _ + _).
by rewrite mulrC dotmulvZ mulrN.
by rewrite dotmulZv.
by rewrite dotmulvZ dotmulZv.
- rewrite 2![in LHS]scalerDr 1![in RHS]scalerDl scalerA.
rewrite -4![in LHS]addrA -3![in RHS]addrA; congr (_ + _).
rewrite [in RHS]scalerDr [in RHS]addrCA
-[in RHS]addrA -[in LHS]addrA; congr (_ + _).
by rewrite scalerA mulrC -scalerA.
rewrite [in RHS]scalerDr [in LHS]scalerDl [in LHS]addrCA
-[in RHS]addrA -addrA; congr (_ + _).
by rewrite scalerA mulrC.
rewrite (addrC (a *: _)) linearD /= (addrC (a' *v _)) linearD /=.
rewrite -![in LHS]addrA ![in LHS]addrA (addrC (- _ *: a'))
-![in LHS]addrA; congr (_ + _).
by rewrite linearZ.
rewrite [in RHS](@lieC _ (vec3 R)) /= linearD /= opprD [in RHS]addrCA
![in LHS]addrA addrC -[in LHS]addrA.
congr (_ + _); first by rewrite linearZ /= (@lieC _ (vec3 R)) scalerN.
rewrite addrA addrC linearD /= opprD [in RHS]addrCA; congr (_ + _).
by rewrite !linearZ /= (@lieC _ (vec3 R)).
rewrite 2!double_crossmul opprD opprK
[in RHS]addrC addrA; congr (_ + _); last first.
by rewrite scaleNr.
by rewrite dotmulC scaleNr; congr (_ + _); rewrite dotmulC.
Qed.
Lemma mul1q : left_id 1%:q mulq.
Proof.
case=> a a'; rewrite /mulq /=; congr mkQuat; Simp.r => /=.
by rewrite dotmul0v subr0.
by rewrite linear0l addr0.
Qed.
Lemma mulq1 : right_id 1%:q mulq.
Proof.
case=> a a'; rewrite /mulq /=; congr mkQuat; Simp.r => /=.
by rewrite dotmulv0 subr0.
by rewrite linear0r addr0.
Qed.
Lemma mulqDl : left_distributive mulq (@addq R).
Proof.
move=> [a a'] [b b'] [c c']; rewrite /mulq /=; congr mkQuat => /=.
by rewrite [in RHS]addrCA 2!addrA -mulrDl (addrC a) dotmulDl opprD addrA.
rewrite scalerDl -!addrA; congr (_ + _).
rewrite [in RHS](addrCA (a' *v c')) [in RHS](addrCA (c *: a')); congr (_ + _).
rewrite scalerDr -addrA; congr (_ + _).
rewrite addrCA; congr (_ + _).
by rewrite (@lieC _ (vec3 R))/= linearD /= (@lieC _ (vec3 R)) opprD opprK (@lieC _ (vec3 R) b').
Qed.
Lemma mulqDr : right_distributive mulq (@addq R).
Proof.
move=> [a a'] [b b'] [c c']; rewrite /mulq /=; congr mkQuat => /=.
rewrite mulrDr -!addrA; congr (_ + _).
rewrite addrCA; congr (_ + _).
by rewrite dotmulDr opprD.
rewrite scalerDr -!addrA; congr (_ + _).
rewrite [in RHS](addrCA (a' *v b')) [in RHS](addrCA (b *: a')); congr (_ + _).
rewrite scalerDl -addrA; congr (_ + _).
by rewrite addrCA linearD.
Qed.
Lemma oneq_neq0 : 1%:q != 0 :> quat R.
Proof. apply/eqP => -[]; apply/eqP. exact: oner_neq0. Qed.
HB.instance Definition _ := @GRing.Zmodule_isRing.Build (quat R) _ _ mulqA mul1q mulq1 mulqDl mulqDr oneq_neq0.
Lemma mulqE x y : x * y = mulq x y. Proof. by []. Qed.
Lemma realq_comm x y : x \is realq R -> x * y = y * x.
Proof.
rewrite qualifE; move: x y => [x1 x2] [y1 y2] /= /eqP->.
congr mkQuat => /=; first by rewrite dotmul0v dotmulv0 mulrC.
by rewrite scaler0 !linear0 add0r addr0 -/(crossmulr _ _) linear0 addr0.
Qed.
Lemma realq_real (r : R) : r%:q \is realq R.
Proof. by rewrite qualifE. Qed.
Lemma realqE x : x \is realq R -> x = (x.1)%:q.
Proof. by rewrite qualifE; case: x => [x1 x2] /= /eqP->. Qed.
Lemma quat_realM (r s : R) : (r * s)%:q = r%:q * s%:q.
Proof. by congr mkQuat; rewrite /= (dotmul0v, linear0l); Simp.r. Qed.
Lemma iiN1 : `i * `i = -1.
Proof. by congr mkQuat; rewrite (dote2, @liexx _ (vec3 R)) /=; Simp.r. Qed.
Lemma ijk : `i * `j = `k.
Proof. by congr mkQuat; rewrite /= (dote2, vecij); Simp.r. Qed.
Lemma ikNj : `i * `k = - `j.
Proof. by congr mkQuat; rewrite /= (dote2, vecik); Simp.r. Qed.
Lemma jiNk : `j * `i = - `k.
Proof. by congr mkQuat; rewrite /= (dote2, vecji); Simp.r. Qed.
Lemma jjN1 : `j * `j = -1.
Proof. by congr mkQuat; rewrite /= (dote2, @liexx _ (vec3 R)); Simp.r. Qed.
Lemma jkNi : `j * `k = `i.
Proof. by congr mkQuat; rewrite /= ?(dote2, vecjk) //; Simp.r. Qed.
Lemma kij : `k * `i = `j.
Proof. by congr mkQuat; rewrite /= (dote2, vecki); Simp.r. Qed.
Lemma kjNi : `k * `j = - `i.
Proof. by congr mkQuat; rewrite /= (dote2, veckj); Simp.r. Qed.
Lemma kkN1 : `k * `k = -1.
Proof. by congr mkQuat; rewrite /= (dote2, @liexx _ (vec3 R)); Simp.r. Qed.
Definition scaleq k x := mkQuat (k * x.1) (k *: x.2).
Lemma scaleqA r s x : scaleq r (scaleq s x) = scaleq (r * s) x.
Proof.
rewrite /scaleq /=; congr mkQuat; by [rewrite mulrA | rewrite scalerA].
Qed.
Lemma scaleq1 : left_id 1 scaleq.
Proof.
by move=> q; rewrite /scaleq mul1r scale1r; apply/eqP; rewrite eq_quat /= !eqxx.
Qed.
Lemma scaleqDr : @right_distributive R (quat R) scaleq +%R.
Proof. move=> a b c; by rewrite /scaleq /= mulrDr scalerDr. Qed.
Lemma scaleqDl x : {morph (scaleq^~ x : R -> quat R) : r s / r + s}.
Proof. by move=> r s; rewrite /scaleq mulrDl /= scalerDl; congr mkQuat. Qed.
HB.instance Definition _ := @GRing.Zmodule_isLmodule.Build _ (quat R) scaleq scaleqA scaleq1 scaleqDr scaleqDl.
Lemma scaleqE (k : R) x : k *: x = k *: x.1%:q + k *: x.2%:v.
Proof. by apply/eqP; rewrite eq_quat /=; Simp.r. Qed.
Lemma quat_realZ (k : R) (r : R) : (k * r)%:q = k *: r%:q.
Proof. by congr mkQuat; rewrite scaler0. Qed.
Lemma quat_vectZ (k : R) (u : 'rV[R]_3) : (k *: u)%:v = k *: u%:v.
Proof. by congr mkQuat; rewrite /= mulr0. Qed.
Lemma quatAl k x y : k *: (x * y) = k *: x * y.
Proof.
case: x y => [x1 x2] [y1 y2]; apply/eqP.
rewrite !mulqE /mulq /= scaleqE /= eq_quat /=.
apply/andP; split; first by Simp.r; rewrite mulrBr mulrA dotmulZv.
apply/eqP; Simp.r; rewrite 2!scalerDr scalerA -2!addrA; congr (_ + _).
by rewrite linearZl_LR /=; congr (_ + _); rewrite scalerA mulrC -scalerA.
Qed.
HB.instance Definition _ := @GRing.Lmodule_isLalgebra.Build R (quat R) quatAl.
Lemma quatAr k x y : k *: (x * y) = x * (k *: y).
Proof.
case: x y => [x1 x2] [y1 y2]; apply/eqP.
rewrite !mulqE /mulq /= scaleqE /= eq_quat /=.
apply/andP; split; first by Simp.r; rewrite /= mulrBr mulrCA mulrA dotmulvZ.
apply/eqP; Simp.r; rewrite 2!scalerDr !scalerA.
rewrite (mulrC k); congr (_ + _ + _).
by rewrite linearZr_LR/=.
Qed.
HB.instance Definition _ := @GRing.Lalgebra_isAlgebra.Build _ (quat R) quatAr.
Lemma quat_algE r : r%:q = r%:A.
Proof. by apply/eqP; rewrite eq_quat //=; Simp.r. Qed.
Definition conjq x := nosimpl (mkQuat x.1 (- x.2)).
Local Notation "x '^*q'" := (conjq x).
Lemma conjq_def x : x^*q = mkQuat x.1 (- x.2).
Proof. by case: x. Qed.
Lemma conjq_linear : linear conjq.
Proof.
move=> k /= x y; rewrite !conjq_def /= scaleqE addqE /addq /=; Simp.r.
by rewrite linearN /= linearD.
Qed.
HB.instance Definition _ := @GRing.isLinear.Build _ (quat R) _ _ conjq conjq_linear.
Lemma conjqI x : (x ^*q) ^*q = x.
Proof. by rewrite conjq_def; case: x => x1 x2 /=; rewrite opprK. Qed.
Lemma conjq0 : (0%:v)^*q = 0.
Proof. by rewrite conjq_def oppr0. Qed.
Lemma conjq_comm x : x^*q * x = x * x^*q.
Proof.
apply/eqP; rewrite eq_quat /=.
do ! rewrite (linearNl,linearNr,(@liexx _ (vec3 R)),dotmulvN,dotmulNv,subr0,opprK,
scaleNr,scalerN,eqxx) /=.
by rewrite addrC.
Qed.
Lemma conjq_addMC x y : x * y + (x * y)^*q = y * x + (y * x) ^*q.
Proof.
case: x => x1 x2; case: y => y1 y2; congr mkQuat => /=.
by rewrite [y1 * _]mulrC [y2 *d _]dotmulC.
rewrite !opprD !addrA [_ + x1 *:y2]addrC -!addrA; congr (_ + (_ + _)).
by rewrite [LHS]addrC [RHS]addrC !addrA !subrK addrC.
Qed.
Lemma realq_conjD x : x + x^*q \is realq R.
Proof. by case: x => x1 x2; rewrite addqE /addq /= subrr qualifE. Qed.
Lemma realq_conjM x : x * x^*q \is realq R.
Proof.
case: x => [x1 x2]; rewrite mulqE /mulq /= scalerN linearN /=.
rewrite opprK [- _ + _]addrC subrr add0r.
rewrite linearN/= (@liexx _ (vec3 R)) oppr0.
by rewrite qualifE.
Qed.
Lemma conjq_comm2 x y : y^*q * x + x^*q * y = x * y^*q + y * x^*q.
Proof.
apply: (addIr (x * x ^*q + y * y ^*q)).
rewrite [RHS]addrAC !addrA -mulrDr -[RHS]addrA -mulrDr -mulrDl -linearD /=.
rewrite addrC !addrA -conjq_comm -mulrDr -addrA -conjq_comm -mulrDr -mulrDl.
by rewrite -linearD /= [y + x]addrC conjq_comm.
Qed.
Lemma conjqM x y : (x * y)^*q = y^*q * x^*q.
Proof.
case: x y => [x1 x2] [y1 y2] /=.
rewrite 2!conjq_def /= mulqE /mulq /= mulrC dotmulC dotmulvN dotmulNv opprK;
congr mkQuat.
rewrite 2!opprD.
rewrite (addrC (- (x1 *: y2))).
congr (_ + _ + _).
- by rewrite scalerN.
- by rewrite scalerN.
- rewrite linearN/= (@lieC _ (vec3 R) x2)/= opprK.
rewrite -(@lieC _ (vec3 R) x2)/= linearN/=.
by rewrite (@lieC _ (vec3 R) x2)/= opprK.
Qed.
Lemma quat_realC (r : R) : (r%:q)^*q = r%:q.
Proof. by congr mkQuat; rewrite /= oppr0. Qed.
Lemma quat_vectC (u : 'rV_3) : (u%:v)^*q = -(u%:v).
Proof. by congr mkQuat; rewrite /= oppr0. Qed.
End quaternion.
Arguments pureq {R}.
Arguments realq {R}.
Notation "x '^*q'" := (conjq x) : quat_scope.
Section quaternion1.
Variable R : realType.
Implicit Types x y : quat R.
Definition sqrq x := x.1 ^+ 2 + norm (x.2) ^+ 2.
Lemma sqrq0 : sqrq 0 = 0. Proof. by rewrite /sqrq norm0 expr0n add0r. Qed.
Lemma sqrq_ge0 x : 0 <= sqrq x. Proof. by rewrite addr_ge0 // sqr_ge0. Qed.
Lemma sqrq_eq0 x : (sqrq x == 0) = (x == 0).
Proof.
rewrite /sqrq paddr_eq0 ?sqr_ge0// !sqrf_eq0 norm_eq0 -xpair_eqE.
by rewrite -surjective_pairing.
Qed.
Lemma sqrqN x : sqrq (- x) = sqrq x.
Proof. by rewrite /sqrq /= normN sqrrN. Qed.
Lemma sqrq_conj x : sqrq (x ^*q) = sqrq x.
Proof. by rewrite /sqrq normN. Qed.
Lemma conjqP x : x * x^*q = (sqrq x)%:q.
Proof.
rewrite /mulq /=; congr mkQuat.
by rewrite /= dotmulvN dotmulvv opprK -expr2.
by rewrite scalerN addNr add0r linearNr (@liexx _ (vec3 R)) oppr0.
Qed.
Lemma conjqZ k x : (k *: x) ^*q = k *: x ^*q.
Proof. by congr mkQuat; rewrite /= scalerN. Qed.
Lemma conjqN (u : 'rV[R]_3) : (- u%:v)^*q = - u%:v^*q.
Proof. by rewrite 2!conjq_def /= opprK oppr0 quat_vectN opprK. Qed.
Lemma pureq_conj x : (x \is pureq) = (x + x^*q == 0).
Proof.
case: x => x1 x2; rewrite qualifE /=; apply/idP/idP=> [/eqP->{x1}|].
by rewrite conjq_def /= quat_vectN subrr.
by rewrite conjq_def /= => /eqP[/eqP]; rewrite -mulr2n (mulrn_eq0 x1 2).
Qed.
Lemma conjqE x :
x^*q = - (1 / 2%:R) *: (x + `i * x * `i + `j * x * `j + `k * x * `k).
Proof.
apply/eqP; rewrite eq_quat; apply/andP; split; apply/eqP.
rewrite [in LHS]/= scaleqE /=.
rewrite !(mul0r,mulr0,addr0) scale0r !add0r !dotmulDl.
rewrite dotmulZv dotmulvv normeE expr1n mulr1 dotmulC
dot_crossmulC (@liexx _ (vec3 R)) dotmul0v addr0.
rewrite subrr add0r dotmulZv dotmulvv normeE expr1n mulr1
dotmulC dot_crossmulC (@liexx _ (vec3 R)) .
rewrite dotmul0v addr0 dotmulZv dotmulvv normeE expr1n mulr1
opprD addrA dotmulC dot_crossmulC.
rewrite (@liexx _ (vec3 R)) dotmul0v subr0 -opprD mulrN mulNr
opprK -mulr2n -(mulr_natl x.1) mulrA.
by rewrite div1r mulVr ?mul1r // unitfE pnatr_eq0.
rewrite /= !(mul0r,scale0r,add0r,addr0).
rewrite [_ *v 'e_0](@lieC _ (vec3 R)) /= ['e_0 *v _]linearD /= ['e_0 *v _]linearZ /= (@liexx _ (vec3 R)) .
rewrite scaler0 add0r double_crossmul dotmulvv normeE expr1n scale1r.
rewrite [_ *v 'e_1](@lieC _ (vec3 R)) /= ['e_1 *v _]linearD /= ['e_1 *v _]linearZ /= (@liexx _ (vec3 R)) .
rewrite scaler0 add0r double_crossmul dotmulvv normeE expr1n scale1r.
rewrite [_ *v 'e_2%:R](@lieC _ (vec3 R)) /= ['e_2%:R *v _]linearD /=
['e_2%:R *v _]linearZ /= (@liexx _ (vec3 R)).
rewrite scaler0 add0r double_crossmul dotmulvv normeE expr1n scale1r.
rewrite [X in _ = - _ *: X](_ : _ = 2%:R *:x.2).
by rewrite scalerA mulNr div1r mulVr ?unitfE ?pnatr_eq0 // scaleN1r.
rewrite !opprB (addrCA _ x.2) addrA -mulr2n scaler_nat -[RHS]addr0 -3!addrA;
congr (_ + _).
do 3 rewrite (addrCA _ x.2).
do 2 rewrite addrC -!addrA.
rewrite -opprB (scaleNr _ 'e_0) opprK -mulr2n addrA -mulr2n.
rewrite addrC addrA -opprB scaleNr opprK -mulr2n.
rewrite opprD.
rewrite (addrCA (- _ *: 'e_2%:R)).
rewrite -opprB scaleNr opprK -mulr2n.
rewrite -!mulNrn.
rewrite addrA.
rewrite -opprD.
rewrite -mulr2n.
rewrite -mulNrn.
rewrite -3!mulrnDl -scaler_nat.
apply/eqP; rewrite scalemx_eq0 pnatr_eq0 /=.
rewrite addrA addrC eq_sym -subr_eq add0r opprB opprD 2!opprK.
rewrite !['e__ *d _]dotmulC !dotmul_delta_mx /=.
rewrite addrA.
by rewrite -vec3E.
Qed.
Lemma conjq_scalar x : x.1%:q = (1 / 2%:R) *: (x + x^*q).
Proof.
case: x => x1 x2.
rewrite /conjq /= addqE /addq /= subrr quat_realD scalerDr -scalerDl.
by rewrite -splitr scale1r.
Qed.
Lemma conjq_vector x : x.2%:v = (1 / 2%:R) *: (x - x^*q).
Proof.
case: x => x1 x2.
rewrite /conjq /= addqE /addq /= subrr opprK.
rewrite quat_vectD scalerDr -scalerDl.
by rewrite -splitr scale1r.
Qed.
Definition invq a := (1 / sqrq a) *: (a ^*q).
Definition unitq : pred (quat R) := [pred a | a != 0%:q].
Lemma mulVq : {in unitq, left_inverse 1 invq (@mulq R)}.
Proof.
move=> a; rewrite inE /= => a0.
rewrite /invq -mulqE -quatAl conjq_comm conjqP.
by rewrite -quat_realZ mul1r mulVf // sqrq_eq0.
Qed.
Lemma mulqV : {in unitq, right_inverse 1 invq (@mulq R)}.
Proof.
move=> a; rewrite inE /= => a0.
by rewrite /invq -mulqE -quatAr conjqP -quat_realZ mul1r mulVf // sqrq_eq0.
Qed.
Lemma quat_integral x y : (x * y == 0) = ((x == 0) || (y == 0)).
Proof.
case: (x =P 0) => [->|/eqP xNZ] /=; first by rewrite mul0r eqxx.
apply/eqP/eqP => [xyZ|->]; last by rewrite mulr0.
by rewrite -[y]mul1r -(@mulVq x) // -mulrA xyZ mulr0.
Qed.
Lemma unitqP x y : y * x = 1 /\ x * y = 1 -> unitq x.
Proof.
move=> [ba1 ab1]; rewrite /unitq inE; apply/eqP => x0.
move/esym: ab1; rewrite x0 mul0r.
apply/eqP; exact: oneq_neq0.
Qed.
Lemma invq0id : {in [predC unitq], invq =1 id}.
Proof.
move=> a; rewrite !inE negbK => /eqP ->.
by rewrite /invq /= conjq0 scaler0.
Qed.
HB.instance Definition _ := @GRing.Ring_hasMulInverse.Build (quat R) _ _ mulVq mulqV unitqP invq0id.
Lemma invqE x : x^-1 = invq x. Proof. by done. Qed.
Definition normq x := Num.sqrt (sqrq x).
Lemma normq0 : normq 0 = 0.
Proof. by rewrite /normq /sqrq expr0n /= norm0 add0r expr0n sqrtr0. Qed.
Lemma normqc x : normq x^*q = normq x.
Proof. by rewrite /normq /sqrq /= normN. Qed.
Lemma normqE x : (normq x ^+ 2)%:q = x^*q * x.
Proof.
rewrite -normqc /normq sqr_sqrtr; last by rewrite /sqrq addr_ge0 // sqr_ge0.
by rewrite -conjqP conjqI.
Qed.
Lemma normq_ge0 x : 0 <= normq x.
Proof. by apply sqrtr_ge0. Qed.
Lemma normq_eq0 x : (normq x == 0) = (x == 0).
Proof. by rewrite /normq -{1}sqrtr0 eqr_sqrt ?sqrq_ge0// sqrq_eq0. Qed.
Lemma normq_vector (u : 'rV[R]_3) : normq u%:v = norm u.
Proof.
by rewrite /normq /sqrq /= expr0n add0r sqrtr_sqr ger0_norm ?norm_ge0.
Qed.
Lemma normqM x y : normq (x * y) = normq x * normq y.
Proof.
apply/eqP; rewrite -(@eqr_expn2 _ 2) // ?normq_ge0 //; last first.
by rewrite mulr_ge0 // normq_ge0.
rewrite -quat_scalarE normqE conjqM -mulrA (mulrA x^*q) -normqE.
rewrite quat_algE mulr_algl -scalerAr exprMn quat_realM.
by rewrite (normqE y) -mulr_algl quat_algE.
Qed.
Lemma normqZ (k : R) x : normq (k *: x) = `|k| * normq x.
Proof.
by rewrite /normq /sqrq /= normZ 2!exprMn sqr_normr -mulrDr sqrtrM ?sqr_ge0 //
sqrtr_sqr.
Qed.
Lemma normqV x : normq (x^-1) = normq x / sqrq x.
Proof.
rewrite invqE /invq normqZ ger0_norm; last first.
by rewrite divr_ge0 // ?ler01 // /sqrq addr_ge0 // sqr_ge0.
by rewrite normqc mulrC mul1r.
Qed.
Definition normQ x := (normq x)%:q.
Lemma normQ_eq0 x : (normQ x == 0) = (x == 0).
Proof. by rewrite /normQ quat_scalarE normq_eq0. Qed.
Definition normalizeq x : quat R := 1 / normq x *: x.
Lemma normalizeq1 x : x != 0 -> normq (normalizeq x) = 1.
Proof.
move=> x0; rewrite /normalizeq normqZ normrM normr1 mul1r normrV; last first.
by rewrite unitfE normq_eq0.
by rewrite ger0_norm ?normq_ge0 // mulVr // unitfE normq_eq0.
Qed.
Definition lequat x y := (x.2 == y.2) && (x.1 <= y.1).
Lemma lequat_normD x y : lequat (normQ (x + y)) (normQ x + normQ y).
Proof.
rewrite /lequat /= add0r eqxx andTb /normq /sqrq !sqr_norm !sum3E /= !mxE.
pose X := nth 0 [:: x.1; x _i; x _j; x _k].
pose Y := nth 0 [:: y.1; y _i; y _j; y _k].
suff: Num.sqrt (\sum_(i < 4) (X i + Y i)^+2) <=
Num.sqrt (\sum_(i < 4) (X i) ^+ 2) + Num.sqrt (\sum_(i < 4) (Y i) ^+ 2).
by rewrite !sum4E /X /Y /= !addrA.
have sqr_normE (x1 : R) : `|x1| ^+2 = x1 ^+ 2.
by case: (ltrgt0P x1); rewrite ?sqrrN // => ->.
have ler_mul_norm (x1 y1 : R) : x1 * y1 <= `|x1| * `|y1|.
rewrite {1}(numEsign x1) {1}(numEsign y1) mulrAC mulrA -signr_addb.
rewrite -mulrA [_ * `|x1|]mulrC.
case: (_ (+) _); rewrite !(expr0, expr1, mulNr, mul1r) //.
rewrite -subr_gte0 opprK -mulr2n; apply: mulrn_wge0.
by apply: mulr_ge0; apply: normr_ge0.
apply: le_trans (_ : Num.sqrt (\sum_(i < 4) (`|X i| + `|Y i|) ^+ 2) <= _).
rewrite -ler_sqr ?nnegrE; try by apply: sqrtr_ge0.
rewrite !sqr_sqrtr; try by apply: sumr_ge0 => i _; apply: sqr_ge0.
apply: ler_sum => i _; rewrite !sqrrD.
by rewrite !sqr_normE; do ! apply: ler_add => //.
rewrite -ler_sqr ?nnegrE; last 2 first.
- by apply: sqrtr_ge0.
- by apply: addr_ge0; apply: sqrtr_ge0.
rewrite [in X in _ <= X]sqrrD !sqr_sqrtr;
try by apply: sumr_ge0 => i ; rewrite sqr_ge0.
under eq_bigr do rewrite sqrrD.
rewrite 2!big_split /= sumrMnl.
under [X in _ <= X + _ + _]eq_bigr do rewrite -sqr_normE.
under [X in _ <= _ + _ X * _ *+2 + _]eq_bigr do rewrite -sqr_normE.
under [X in _ <= _ + _ + X]eq_bigr do rewrite -sqr_normE.
under [X in _ <= _ + _ * _ X *+2 + _]eq_bigr do rewrite -sqr_normE.
do 2 (apply: ler_add => //); rewrite ler_muln2r /=.
rewrite -ler_sqr ?nnegrE; last 2 first.
- by apply: sumr_ge0 => i _; apply: mulr_ge0; apply: normr_ge0.
- by apply: mulr_ge0; apply: sqrtr_ge0.
rewrite exprMn !sqr_sqrtr; last 2 first.
- by apply: sumr_ge0=> i _; apply: sqr_ge0.
- by apply: sumr_ge0=> i _; apply: sqr_ge0.
(* This is Cauchy Schwartz *)
rewrite -[_ <= _]orFb -[false]/(2 == 0)%nat -ler_muln2r.
pose u := \sum_(i < 4) \sum_(j < 4) (`|X i| * `|Y j| - `|X j| * `|Y i|) ^+ 2.
set z1 := \sum_(i < _) _; set z2 := \sum_(i < _) _; set z3 := \sum_(i < _) _.
suff ->: z2 * z3 *+ 2 = z1 ^+ 2 *+ 2 + u.
rewrite -{1}(addr0 (_ *+ 2)).
apply: ler_add => //.
by apply: sumr_ge0 => i _; apply: sumr_ge0 => j _; apply: sqr_ge0.
under [X in _ = _ + X]eq_bigr do
(under eq_bigr do (rewrite sqrrB !exprMn); rewrite !(sumrN, big_split));
rewrite !(sumrN, big_split, addrA) /=.
under eq_bigr do rewrite -mulr_sumr; rewrite -mulr_suml -/z2 -/z3.
have mswap (a b c d : R) : a * b * (c * d) = (c * b) * (a * d).
by rewrite mulrC mulrAC [a * _]mulrC !mulrA.
under [X in _ = _ - (X + _) + _]eq_bigr do
(under eq_bigr do rewrite mswap; rewrite -mulr_suml);
rewrite -mulr_sumr -expr2 -/z1.
under [X in _ = _ - (_ + X) + _]eq_bigr do
(under eq_bigr do rewrite mswap; rewrite -mulr_suml);
rewrite -mulr_sumr -expr2 -/z1 -mulr2n.
under [X in _ = _ + X]eq_bigr do rewrite -mulr_suml;
rewrite -mulr_sumr -/z2 -/z3.
by rewrite [_ *+ 2 + _]addrC addrK mulr2n.
Qed.
Definition ltquat x y := (x.2 == y.2) && (x.1 < y.1).
Lemma ltquat0_add x y : ltquat 0 x -> ltquat 0 y -> ltquat 0 (x + y).
Proof.
case: x => x0 x1; case: y => y0 y1; rewrite /ltquat /=.
move=> /andP[/eqP<- x0P] /andP[/eqP<- u0P] /=.
by rewrite addr0 eqxx addr_gt0.
Qed.
Lemma ge0_lequat_total x y :
lequat 0 x -> lequat 0 y -> lequat x y || lequat y x.
Proof.
case: x => x0 x1; case: y => y0 y1; rewrite /lequat /=.
move=> /andP[/eqP<- x0P] /andP[/eqP<- y0P] /=.
case: (lerP x0 y0); rewrite eqxx //=.
by apply: ltW.
Qed.
Lemma normQM x y : normQ (x * y) = normQ x * normQ y.
Proof. by rewrite {1}/normQ normqM quat_realM. Qed.
Lemma lequat_def x y : lequat x y = (normQ (y - x) == y - x).
Proof.
case: x => x0 x1; case: y => y0 y1; rewrite /normQ /normq /sqrq /=.
apply/idP/idP.
rewrite /lequat /=.
case/andP => /eqP<- x0Ly0.
apply/eqP; congr mkQuat; rewrite ?subrr ?expr0n ?addr0 //=.
rewrite norm0 expr0n addr0 sqrtr_sqr.
by apply/eqP; rewrite eqr_norm_id subr_ge0.
case/eqP => /eqP H H1.
move: (sym_equal H1) H => /subr0_eq->.
rewrite /lequat /= eqxx /=.
by rewrite subrr norm0 expr0n addr0 sqrtr_sqr eqr_norm_id subr_ge0.
Qed.
Lemma ltquat_def x y : ltquat x y = (y != x) && lequat x y.
Proof.
case: x => x0 x1; case: y => y0 y1 /=.
apply/andP/and3P => [[/eqP<- x0Ly0] | ].
split=> //.
by apply/negP; case/eqP => y0E; rewrite y0E // ltxx in x0Ly0.
by apply: ltW.
rewrite eq_quat negb_and /= => [] [/orP[y0Dx0 | y1Dx1] x1Ey1 x0Ly0];
split => //.
by rewrite lt_neqAle eq_sym y0Dx0.
by rewrite eq_sym x1Ey1 in y1Dx1.
Qed.
Fail Definition quat_POrderedMixin :=
NumMixin lequat_normD ltquat0_add eq0_normQ ge0_lequat_total
normQM lequat_def ltquat_def.
Fail Canonical Structure quat_numDomainType :=
NumDomainType _ quat_POrderedMixin.
Definition uquat := [qualify x : quat R | normq x == 1].
Fact uquat_key : pred_key uquat. Proof. by []. Qed.
Canonical uquat_keyed := KeyedQualifier uquat_key.
Lemma uquatE x : (x \is uquat) = (sqrq x == 1).
Proof. by rewrite qualifE /normq -{1}sqrtr1 eqr_sqrt // ?sqrq_ge0// ler01. Qed.
Lemma muluq_proof x y : x \is uquat -> y \is uquat -> x * y \is uquat.
Proof. by rewrite 3!qualifE => /eqP Hq /eqP Hp; rewrite normqM Hq Hp mulr1. Qed.
Lemma invq_uquat x : x \is uquat -> x^-1 = x^*q.
Proof.
by rewrite uquatE => /eqP Hq; rewrite invqE /invq Hq invr1 mul1r scale1r.
Qed.
Lemma invuq_proof x : x \is uquat -> normq (x^-1) == 1.
Proof. by move=> ux; rewrite invq_uquat // normqc. Qed.
Lemma cos_atan_uquat x : x \is uquat -> x \isn't pureq ->
let a := atan (norm x.2 / x.1) in cos a ^+ 2 = x.1 ^+ 2.
Proof.
move=> ux q00 a.
rewrite cos_atan exprMn [x.1 ^-1 ^+2]exprVn.
have /divff <- : x.1 ^+ 2 !=0 by rewrite sqrf_eq0.
rewrite -mulrDl.
rewrite uquatE /sqrq in ux; rewrite (eqP ux) mul1r.
by rewrite -exprVn sqrtr_sqr normfV invrK sqr_normr.
Qed.
Lemma sin_atan_uquat x : x \is uquat -> x \isn't pureq ->
let a := atan (norm x.2 / x.1) in sin a ^+ 2 = norm x.2 ^+ 2.
Proof.
move=> ux q00 a.
rewrite /a sqr_sin_atan.
have /divrr <- : x.1 ^+ 2 \in GRing.unit by rewrite unitfE sqrf_eq0.
rewrite uquatE /sqrq in ux.
rewrite expr_div_n -mulrDl.
by rewrite (eqP ux) mul1r invrK -mulrA mulVr ?mulr1 // unitrX // unitfE.
Qed.
End quaternion1.
Arguments uquat {R}.
Section conjugation.
Variable R : realType.
Implicit Types (x : quat R) (u : 'rV[R]_3).
Definition conjugation x u : quat R := x * u%:v * x^*q.
Lemma conjugation_is_pure x u : conjugation x u \is pureq.
Proof.
rewrite pureq_conj /conjugation conjqM conjqI conjqM mulrA -mulrDl -mulrDr.
by have := pureq_conj u%:v; rewrite qualifE /= eqxx => /esym/eqP ->; Simp.r.
Qed.
Lemma conjugationE x u : conjugation x u =
((x.1 ^+ 2 - norm x.2 ^+ 2) *: u +
((x.2 *d u) *: x.2) *+ 2 +
(x.1 *: (x.2 *v u)) *+ 2)%:v.
Proof.
case: x => x1 x2 /=; rewrite /conjugation /= /conjq /= mulqE /mulq /=.
rewrite mulr0 scale0r addr0 add0r; congr mkQuat.
rewrite dotmulvN opprK dotmulDl (dotmulC (_ *v _) x2) dot_crossmulC.
by rewrite (@liexx _ (vec3 R)) dotmul0v addr0 dotmulZv mulNr mulrC dotmulC addrC subrr.
rewrite scalerDr scalerA -expr2 addrCA scalerBl -!addrA; congr (_ + _).
rewrite [in X in _ + X = _]linearN /= (@lieC _ (vec3 R) _ x2)/= linearD /= opprK.
rewrite linearZ /= (addrA (x1 *: _ )) -mulr2n.
rewrite [in LHS]addrCA 2![in RHS]addrA [in RHS]addrC; congr (_ + _).
rewrite scalerN scaleNr opprK -addrA addrCA; congr (_ + _).
by rewrite double_crossmul [in RHS]addrC dotmulvv.
Qed.
Lemma conjugation_uquat x : x \is uquat -> conjugation x x.2 = (x.2)%:v.
Proof.
rewrite uquatE /sqrq => /eqP xu.
rewrite conjugationE (@liexx _ (vec3 R)) scaler0 mul0rn addr0 dotmulvv scalerBl mulr2n addrA.
by rewrite subrK -scalerDl xu scale1r.
Qed.
Lemma conjugation_axis x k : x \is uquat ->
conjugation x (k *: x.2) = (k *: x.2)%:v.
Proof.
move=> xu; rewrite /conjugation quat_vectZ -scalerAr -scalerAl.
by rewrite -/(conjugation x x.2) conjugation_uquat.
Qed.
Lemma norm_conjugation x u : x \is uquat -> normq (conjugation x u) = norm u.
Proof.
rewrite qualifE => /eqP x1; rewrite /conjugation 2!normqM normqc x1; Simp.r.
by rewrite normq_vector.
Qed.
End conjugation.
Section polar_coordinates.
Variable R : realType.
Implicit Types (x : quat R) (v : 'rV[R]_3) (a : R).
Definition quat_of_polar a v := mkQuat (cos a) (sin a *: v).
Lemma quat_of_polar01 : quat_of_polar 0 'e_1 = 1%:q.
Proof. by rewrite /quat_of_polar /= cos0 sin0 scale0r. Qed.
Lemma quat_of_polarpi1 : quat_of_polar pi 'e_1 = (-1)%:q.
Proof. by rewrite /quat_of_polar cospi sinpi scale0r. Qed.
Lemma quat_of_polarpihalf v : quat_of_polar (pi / 2%:R) v = v%:v.
Proof. by rewrite /quat_of_polar cos_pihalf sin_pihalf scale1r. Qed.
Lemma uquat_of_polar a v (v1 : norm v = 1) : quat_of_polar a v \is uquat.
Proof.
by rewrite uquatE /quat_of_polar /sqrq /= normZ v1 mulr1 sqr_normr cos2Dsin2.
Qed.
Definition quat_rot x v : 'rV[R]_3 := (conjugation x v).2.
Lemma conjugation_quat_of_polar_axis v a : norm v = 1 ->
quat_rot (quat_of_polar a v) v = v.
Proof.
move=> v1.
rewrite /quat_rot conjugationE /= normZ exprMn v1 expr1n mulr1 sqr_normr.
rewrite dotmulZv dotmulvv v1 expr1n mulr1 linearZl_LR (@liexx _ (vec3 R)) /= 2!scaler0 mul0rn.
rewrite addr0 scalerA -expr2 mulr2n scalerBl addrA subrK -scalerDl cos2Dsin2.
by rewrite scale1r.
Qed.
Local Open Scope frame_scope.
Lemma conjugation_quat_of_polar_frame_j (f : frame R) a :
quat_rot (quat_of_polar a f~i) f~j =
cos (a *+ 2) *: f~j + sin (a *+ 2) *: f~k.
Proof.
rewrite /quat_rot conjugationE /= normZ noframe_norm mulr1 sqr_normr dotmulZv.
have v0 : f~i != 0 by rewrite -norm_eq0 noframe_norm oner_neq0.
rewrite (noframe_idotj f) mulr0 scale0r mul0rn addr0 linearZl_LR /=.
rewrite (frame_icrossj f) scalerA [in RHS]mulr2n cosD sinD -!expr2.
by congr (_ + _); rewrite (mulrC (sin a)) -mulr2n -scalerMnl.
Qed.
Lemma conjugation_quat_of_polar_frame_k (f : frame R) a :
quat_rot (quat_of_polar a f~i) f~k =
- sin (a *+ 2) *: f~j + cos (a *+ 2) *: f~k.
Proof.
rewrite /quat_rot conjugationE /= normZ noframe_norm mulr1 sqr_normr dotmulZv.
have v0 : f~i != 0 by rewrite -norm_eq0 noframe_norm oner_neq0.
rewrite (noframe_idotk f) mulr0 scale0r mul0rn addr0 linearZl_LR /=.
rewrite (frame_icrossk f) 2!scalerN scalerA sinD cosD -!expr2 addrC scaleNr.
by congr (_ + _); rewrite (mulrC (sin a)) -mulr2n -scalerMnl mulNrn.
Qed.
Definition polar_of_quat x : (R * 'rV_3)%type :=
if x.2 == 0 then
if x.1 == 1 then (0, 'e_1) else (pi, 'e_1)
else if x.1 == 0 then (pi / 2%:R, x.2) else
let: u := normalize x.2 in
let: a := atan (norm x.2 / x.1) in
if 0 < x.1 then (a, u) else (a + pi, u).
Lemma polar_of_quat0 : polar_of_quat 0 = (pi, 'e_1).
Proof. by rewrite /polar_of_quat eqxx eq_sym oner_eq0. Qed.
Lemma norm_polar_of_quat x : x \is uquat -> norm (polar_of_quat x).2 = 1.
Proof.
case: x => a0 a1; rewrite /= qualifE /polar_of_quat /normq /sqrq /=.
have [/eqP ->|a10] := ifPn; first by case: ifPn; rewrite norm_delta_mx.
case: (sgzP a0) => [-> /eqP| |]; try by rewrite norm_normalize.
by rewrite expr0n add0r sqrtr_sqr ger0_norm // norm_ge0.
Qed.
Lemma polar_of_quatK x : x \is uquat ->
quat_of_polar (polar_of_quat x).1 (polar_of_quat x).2 = x.
Proof.
case: x => a0 a1; rewrite /= qualifE /polar_of_quat /normq /sqrq /=.
have [->|/eqP a1N u1] := a1 =P 0.
rewrite norm0 expr0n addr0 sqrtr_sqr; have [?/eqP->|?|_] := ltrgt0P a0.
- by rewrite eqxx quat_of_polar01.
- by rewrite eqr_oppLR => /eqP ->; rewrite eqrNxx oner_eq0 quat_of_polarpi1.
- by rewrite eq_sym oner_eq0.
move: u1; have [-> _|a0P /eqP u1 |a0N /eqP u1] := sgzP a0.
- by rewrite quat_of_polarpihalf.
- congr mkQuat.
by rewrite cos_atan sqrtr_1sqr2 ?gt_eqF// gtr0_norm// invrK.
rewrite sin_atan sqrtr_1sqr2 ?gt_eqF// gtr0_norm// invrK -mulrA.
by rewrite mulVf ?gt_eqF// mulr1 norm_scale_normalize.
- congr mkQuat.
rewrite cosDpi cos_atan sqrtr_1sqr2 ?lt_eqF// invrK ltr0_norm//.
by rewrite opprK.
rewrite sinDpi sin_atan sqrtr_1sqr2// ?lt_eqF// ltr0_norm// 2!invrN mulrN.
by rewrite invrK opprK -mulrA mulVf ?lt_eqF// mulr1 norm_scale_normalize.
Qed.
Lemma quat_rot_is_linear x : linear (quat_rot x).
Proof.
move=> k u v; rewrite /quat_rot !conjugationE.
rewrite scalerDr scalerA (mulrC _ k) -scalerA.
rewrite 2![in RHS]scalerDr -2![in LHS]addrA -3![in RHS]addrA; congr (_ + _).
rewrite [in RHS]addrA [in RHS]addrCA -[in RHS]addrA; congr (_ + _).
rewrite dotmulDr scalerDl mulrnDl -addrA addrCA; congr (_ + _).
rewrite dotmulvZ -scalerA scalerMnr -addrA; congr (_ + _).
rewrite linearD /= scalerDr mulrnDl; congr (_ + _).
by rewrite linearZ /= scalerA mulrC -scalerA -scalerMnr.
Qed.
HB.instance Definition _ x := @GRing.isLinear.Build _ _ _ _ _ (quat_rot_is_linear x).
Lemma quat_rot_isRot_polar v a : norm v = 1 ->
isRot (a *+2) v [linear of quat_rot (quat_of_polar a v)].
Proof.