-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquaternion.m
1444 lines (1255 loc) · 46.2 KB
/
quaternion.m
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
classdef quaternion
% The "quaternion" class used to represent quaternions.
%
% NOTES:
% To get more information on this class type "doc quaternion" into the
% command window.
%
% The following websites are useful to learn more about quaternions:
% http://en.wikipedia.org/wiki/Quaternion
% http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
% http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions
%
% NECESSARY FILES AND/OR PACKAGES: TODO: Add necessary files
% +somePackage, someFile.m
%
% SEE ALSO: TODO: Add see alsos
% relatedFunction1 | relatedFunction2
%
% AUTHOR:
% Rowland O'Flaherty (www.rowlandoflaherty.com)
%
% VERSION:
% Created 17-NOV-2012
%-------------------------------------------------------------------------------
%% Properties ------------------------------------------------------------------
properties (GetAccess = public, SetAccess = private, Hidden = true)
r % (1 x 1 number) Magnitude of real part of quaternion.
i % (1 x 1 number) Magnitude of i part of quaternion.
j % (1 x 1 number) Magnitude of j part of quaternion.
k % (1 x 1 number) Magnitdue of k part of quaternion.
end
properties (Dependent = true)
real % (1 x 1 number) Real part of quaternion.
imag % (3 x 1 number) Imaginary part of quaternion.
quat % (4 x 1 number) Quaternion as a vector.
rot % (3 x 3 number) Closest rotation matrix from quaternion.
euler % (3 x 1 number) Euler angles from quaternion.
roll % (1 x 1 number) Euler roll angle from quaternion.
pitch % (1 x 1 number) Euler pitch angle from quaternion.
yaw % (1 x 1 number) Euler yaw angle from quaternion.
axis % (3 x 1 number) Axis of rotation from quaternion.
end
%% Constructor -----------------------------------------------------------------
methods
function quaternionObj = quaternion(arg1,arg2,arg3,arg4)
% Constructor function for the "quaternion" class.
%
% SYNTAX:
% quaternionObj = quaternion(r,i,j,k)
% quaternionObj = quaternion(quat)
% quaternionObj = quaternion(rot)
% quaternionObj = quaternion(euler)
% quaternionObj = quaternion(axis,angle)
%
% INPUTS:
% r - (1 x 1 number)
% Real part of quaternion.
%
% i - (1 x 1 number)
% Imaginary i part of quaternion.
%
% j - (1 x 1 number)
% Imaginary j part of quaternion.
%
% k - (1 x 1 number)
% Imaginary k part of quaternion.
%
% quat - (4 x 1 number)
% The new quaternion will be created with quaternion
% component representation.
%
% rot - (3 x 3 number)
% The new quaternion will be created with a rotation matrix
% representation. Must be an element of SO(3).
%
% euler - (1 x 3 number)
% The new quaternion will be created with a Euler angles
% representation.
%
% axis,angle - (3 x 1 number),(1 x 1 number)
% The new quaternion will be created with a Euler axis/angle
% representation. The angle is in radians and the the axis is
% unit vector. If only an axis is provided the angle equals
% the norm of the axis.
%
% OUTPUTS:
% quaternionObj - (1 x 1 quaternion object)
% A new instance of the "quaternion" class.
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(0,4)
switch nargin
case 0 % Default
r = 1; i = 0; j = 0; k = 0; %#ok<*PROP>
case 1 % Quaternion Vector, Rotation Matrix, or Euler Angles
assert(isnumeric(arg1) && isreal(arg1),...
'quaternion:arg1',...
'Input arguments must be real numbers.')
switch numel(arg1)
case 4 % Quaternion
arg1 = arg1(:)';
r = arg1(1); i = arg1(2); j = arg1(3); k = arg1(4);
case 9 % Rotation Matrix
arg1 = reshape(arg1(:),3,3);
q = quaternion.rot2quat(arg1);
r = q.r; i = q.i; j = q.j; k = q.k;
case 3 % Euler Angles or Axis
if size(arg1,1) == 1 % Euler Angles
q = quaternion.euler2quat(arg1);
else % Axis
q = quaternion.axis2quat(arg1);
end
r = q.r; i = q.i; j = q.j; k = q.k;
otherwise
error('quaternion:arg1',...
'Invalid representation of a rotation.')
end
case 2
q = quaternion.axis2quat(arg1,arg2);
r = q.r; i = q.i; j = q.j; k = q.k;
case 3
error('quanternion:narginchk',...
'Invalid number of input arguments.')
case 4
r = arg1; i = arg2; j = arg3; k = arg4;
end
% Assign properties
quaternionObj.r = r;
quaternionObj.i = i;
quaternionObj.j = j;
quaternionObj.k = k;
end
end
%-------------------------------------------------------------------------------
%% Property Methods ------------------------------------------------------------
methods
function quaternionObj = set.real(quaternionObj,real)
assert(isnumeric(real) && isreal(real) && numel(real) == 1,...
'quaternion:set:real',...
'Property "real" must be set to a 1 x 1 real number.')
quaternionObj.r = real;
end
function real = get.real(quaternionObj)
real = quaternionObj.r;
end
function quaternionObj = set.imag(quaternionObj,imag)
assert(isnumeric(imag) && isreal(imag) && numel(imag) == 3,...
'quaternion:set:imag',...
'Property "imag" must be set to a 3 x 1 real number.')
quaternionObj.i = imag(1);
quaternionObj.j = imag(2);
quaternionObj.k = imag(3);
end
function imag = get.imag(quaternionObj)
imag(1,1) = quaternionObj.i;
imag(2,1) = quaternionObj.j;
imag(3,1) = quaternionObj.k;
end
function quaternionObj = set.quat(quaternionObj,quat)
assert(isnumeric(quat) && isreal(quat) && numel(quat) == 4,...
'quaternion:set:quat',...
'Property "quat" must be set to a 4 x 1 real number.')
quaternionObj.r = quat(1);
quaternionObj.i = quat(2);
quaternionObj.j = quat(3);
quaternionObj.k = quat(4);
end
function quat = get.quat(quaternionObj)
quat(1,1) = quaternionObj.r;
quat(2,1) = quaternionObj.i;
quat(3,1) = quaternionObj.j;
quat(4,1) = quaternionObj.k;
end
function quaternionObj = set.rot(quaternionObj,rot)
q = quaternion.rot2quat(rot);
quaternionObj.quat = q.quat;
end
function rot = get.rot(quaternionObj)
rot = quaternion.quat2rot(quaternionObj.quat);
end
function quaternionObj = set.euler(quaternionObj,euler)
q = quaternion.euler2quat(euler);
quaternionObj.quat = q.quat;
end
function euler = get.euler(quaternionObj)
euler = quaternion.quat2euler(quaternionObj.quat);
end
function quaternionObj = set.roll(quaternionObj,roll)
e = quaternionObj.euler;
q = quaternion.euler2quat([roll e(2) e(3)]);
quaternionObj.quat = q.quat;
end
function roll = get.roll(quaternionObj)
e = quaternionObj.euler;
roll = e(1);
end
function quaternionObj = set.pitch(quaternionObj,pitch)
e = quaternionObj.euler;
q = quaternion.euler2quat([e(1) pitch e(3)]);
quaternionObj.quat = q.quat;
end
function pitch = get.pitch(quaternionObj)
e = quaternionObj.euler;
pitch = e(2);
end
function quaternionObj = set.yaw(quaternionObj,yaw)
e = quaternionObj.euler;
q = quaternion.euler2quat([e(1) e(2) yaw]);
quaternionObj.quat = q.quat;
end
function yaw = get.yaw(quaternionObj)
e = quaternionObj.euler;
yaw = e(3);
end
function quaternionObj = set.axis(quaternionObj,axis)
q = quaternion.axis2quat(axis);
quaternionObj.quat = q.quat;
end
function axis = get.axis(quaternionObj)
axis = quaternion.quat2axis(quaternionObj.quat);
end
end
%-------------------------------------------------------------------------------
%% General Methods -------------------------------------------------------------
methods (Access = public)
function disp(x)
% The "disp" method overloads Matlab's built in "disp" function for
% quaternions.
%
% SYNTAX:
% disp(x)
%
% INPUTS:
% x - (quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
windowSize = get(0,'CommandWindowSize');
charPerLine = windowSize(1);
switch get(0,'Format')
case 'short'
space = ' ';
flag = '%.4f';
charPerCol = 39;
case 'long'
space = ' ';
flag = '%.15f';
charPerCol = 39;
case 'shortE'
space = ' ';
flag = '%.4e';
charPerCol = 39;
case 'longE'
space = ' ';
flag = '%.15e';
charPerCol = 39;
case 'shortG'
space = ' ';
flag = '%.4g';
charPerCol = 39;
case 'longG'
space = ' ';
flag = '%.15g';
charPerCol = 39;
otherwise
error('quaternion:disp:format',...
'Format type "%s" has not been implemented yet for the quaternion class.',get(0,'Format'));
end
colPerLine = floor(charPerLine/charPerCol);
dim = length(size(x));
if dim <= 2
rowMax = size(x,1);
colMax = size(x,2);
blockMax = ceil(colMax/colPerLine);
colEnd = 0;
for b = 1:blockMax
colStart = colEnd + 1;
colEnd = min(colStart + colPerLine - 1,colMax);
if blockMax ~= 1
if colStart ~= colEnd
fprintf(' Columns %d through %d\n\n',colStart,colEnd);
else
fprintf(' Column %d\n\n',colStart);
end
end
for r = 1:rowMax
for c = colStart:colEnd
fprintf([space flag],x(r,c).r);
if x(r,c).i == 0; x(r,c).i = 0; end
if x(r,c).i >= 0
fprintf([' + ' flag 'i'],x(r,c).i);
else
fprintf([' - ' flag 'i'],abs(x(r,c).i));
end
if x(r,c).j == 0; x(r,c).j = 0; end
if x(r,c).j >= 0
fprintf([' + ' flag 'j'],x(r,c).j);
else
fprintf([' - ' flag 'j'],abs(x(r,c).j));
end
if x(r,c).k == 0; x(r,c).k = 0; end
if x(r,c).k >= 0
fprintf([' + ' flag 'k'],x(r,c).k);
else
fprintf([' - ' flag 'k'],abs(x(r,c).k));
end
end
fprintf('\n');
end
fprintf('\n');
end
else
error('quaternion:disp:dim',...
'Functionality to display quaternions with dimension greater than two has not been created yet.')
end
end
function a = plus(a,b)
% The "plus" method overloads Matlab's built in "plus" (i.e. + )
% function for quaternions.
%
% SYNTAX:
% c = a + b
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% c - (1 x 1 quaternion)
% An instance of the "quaternion" class that is the
% sum of "a" added to "b".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:plus:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
assert(isa(b,'quaternion') && numel(b) == 1,...
'quaternion:plus:b',...
'Input argument "b" must be a 1 x 1 "quaternion" object.')
a1 = a.r; b1 = a.i; c1 = a.j; d1 = a.k;
a2 = b.r; b2 = b.i; c2 = b.k; d2 = b.k;
a.r = a1 + a2;
a.i = b1 + b2;
a.j = c1 + c2;
a.k = d1 + d2;
end
function a = minus(a,b)
% The "minus" method overloads Matlab's built in "minus" (i.e. - )
% function for quaternions.
%
% SYNTAX:
% c = a - b
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% c - (1 x 1 quaternion)
% An instance of the "quaternion" class that is the
% difference between "a" and "b".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:minus:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
assert(isa(b,'quaternion') && numel(b) == 1,...
'quaternion:minus:b',...
'Input argument "b" must be a 1 x 1 "quaternion" object.')
a1 = a.r; b1 = a.i; c1 = a.j; d1 = a.k;
a2 = b.r; b2 = b.k; c2 = b.j; d2 = b.k;
a.r = a1 - a2;
a.i = b1 - b2;
a.j = c1 - c2;
a.k = d1 - d2;
end
function a = times(a,b)
% The "times" method overloads Matlab's built in "times" (i.e. .* )
% function for quaternions.
%
% SYNTAX:
% c = a .* b
%
% INPUTS:
% a - (1 x N scaler or quaternion or 3 x N vector)
% First argument in product equation.
%
% b - (1 x N scaler or quaternion or 3 x N vector)
% Second argument in product equation.
%
% OUTPUTS:
% c - (1 x N quaternion or 3 x N vector)
% Product of equation.
%
% NOTES:
% This method works for:
% quaternion .* quaternion = quaternion -- elementwise Hamiltonian product
% scaler .* quaternion or quaternion .* scaler = quaternion -- elementwise scaler product
% quaternion * vector (3 x 1) = vector (3 x 1) -- elementwise rotational product
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(isa(a,'quaternion') || (isnumeric(a) && isreal(a) && isvector(a)),...
'quaternion:times:a',...
'Input argument "a" must be a real scaler, vector or "quaternion" vector object.')
[M,N] = size(a);
assert((isa(b,'quaternion') && isequal(size(b),[M,N])) || (isnumeric(b) && isreal(b) && (isequal(size(b),[M,N]) || (M == 1 && isequal(size(b),[3,N])))),...
'quaternion:times:b',...
'Input argument "b" must be a %d x %d real matrix, 3 x %d matrix, or %d x %d "quaternion" vector object.',M,N,N,M,N)
if M == 1 && size(b,1) == 3
c = nan(3,N);
for i = 1:N
c(:,i) = a(i) * b(:,i);
end
a = c;
else
for i = 1:numel(b)
a(i) = a(i) * b(i);
end
end
end
function a = mtimes(a,b)
% The "mtimes" method overloads Matlab's built in "mtimes" (i.e. *
% ) function for quaternions.
%
% SYNTAX:
% c = a * b
%
% INPUTS:
% a - (1 x 1 scaler or quaternion)
% First argument in product equation.
%
% b - (1 x 1 scaler or quaternion or 3 x 1 vector)
% Second argument in product equation.
%
% OUTPUTS:
% c - (1 x 1 quaternion or 3 x 1 vector)
% Product of equation.
%
% NOTES:
% This method works for:
% quaternion * quaternion = quaternion -- Hamiltonian product
% scaler * quaternion or quaternion * scaler = quaternion -- scaler product
% quaternion * vector (3 x 1) = vector (3 x 1) -- rotational product
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert((isa(a,'quaternion') && numel(a) == 1) || (isnumeric(a) && isreal(a) && numel(a) == 1),...
'quaternion:mtimes:a',...
'Input argument "a" must be a real scaler or 1 x 1 "quaternion" object.')
assert((isa(b,'quaternion') && numel(b) == 1) || (isnumeric(b) && isreal(b) && (numel(b) == 1 || isequal(size(b),[3,1]))),...
'quaternion:mtimes:b',...
'Input argument "b" must be a real scaler, 3 x 1 vector, or 1 x 1 "quaternion" object.')
if isa(a,'quaternion')
r1 = a.r; i1 = a.i; j1 = a.j; k1 = a.k;
end
if isa(b,'quaternion')
r2 = b.r; i2 = b.i; j2 = b.j; k2 = b.k;
end
if isa(a,'quaternion') && isa(b,'quaternion')
a.r = r1*r2 - i1*i2 - j1*j2 - k1*k2;
a.i = r1*i2 + i1*r2 + j1*k2 - k1*j2;
a.j = r1*j2 - i1*k2 + j1*r2 + k1*i2;
a.k = r1*k2 + i1*j2 - j1*i2 + k1*r2;
elseif isa(a,'quaternion')
if numel(b) == 1
a.r = r1*b;
a.i = i1*b;
a.j = j1*b;
a.k = k1*b;
else
r2 = 0; i2 = b(1); j2 = b(2); k2 = b(3);
a3 = r1*r2 - i1*i2 - j1*j2 - k1*k2;
b3 = r1*i2 + i1*r2 + j1*k2 - k1*j2;
c3 = r1*j2 - i1*k2 + j1*r2 + k1*i2;
d3 = r1*k2 + i1*j2 - j1*i2 + k1*r2;
a4 = a.r; b4 = -a.i; c4 = -a.j; d4 = -a.k;
a = nan(3,1);
a(1) = a3*b4 + b3*a4 + c3*d4 - d3*c4;
a(2) = a3*c4 - b3*d4 + c3*a4 + d3*b4;
a(3) = a3*d4 + b3*c4 - c3*b4 + d3*a4;
end
else
b.r = r2*a;
b.i = i2*a;
b.j = j2*a;
b.k = k2*a;
a = b;
end
end
function a = mrdivide(a,b)
% The "mrdivide" method overloads Matlab's built in "mrdivide"
% (i.e. / ) function for quaternions.
%
% SYNTAX:
% c = a / b
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% b - (1 x 1 scaler)
% Scaler divisor.
%
% OUTPUTS:
% c - (1 x 1 quaternion)
% An instance of the "quaternion" class that is the
% quotient of "a" divided by "b".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:mrdivide:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
assert(isnumeric(b) && isreal(b) && numel(b) == 1,...
'quaternion:mrdivide:b',...
'Input argument "b" must be a 1 x 1 real scaler value.')
a.r = a.r/b;
a.i = a.i/b;
a.j = a.j/b;
a.k = a.k/b;
end
function q = conj(q)
% The "conj" method overloads Matlab's built in "conj" function for
% quaternions.
%
% SYNTAX:
% b = conj(a)
%
% INPUTS:
% a - (M x N x ... quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% b - (M x N x ... quaternion)
% An instance of the "quaternion" class that is elementwise the
% conjucate of "a".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
% Elementwise conjucate
for i = 1:numel(q)
q(i).i = -q(i).i;
q(i).j = -q(i).j;
q(i).k = -q(i).k;
end
end
function r = ctranspose(q)
% The "conj" method overloads Matlab's built in "ctranspose" (i.e.
% ' )function for quaternions.
%
% SYNTAX:
% b = a'
%
% INPUTS:
% a - (M x N quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% b - (M x N quaternion)
% An instance of the "quaternion" class that is the conjucate
% transpose of "a".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
% Check arguments for errors
assert(numel(size(q)) == 2,...
'quaternion:norm:a',...
'Input argument "a" must have dimension <= 2.')
% Conjucate transpose
[M,N] = size(q);
r = reshape(q,N,M);
for i = 1:M
for j = 1:N
r(j,i) = conj(q(i,j));
end
end
end
function value = norm(a)
% The "norm" method overloads Matlab's built in "norm" function for
% quaternions.
%
% SYNTAX:
% value = norm(a)
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% value - (1 x 1 number)
% The norm of the quaternion "a".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:norm:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
a1 = a.r; b1 = a.i; c1 = a.j; d1 = a.k;
value = norm([a1 b1 c1 d1]);
end
function a = unit(a)
% The "unit" method outputs the corresponding unit quaternion to
% the quaternion "a" or known as the versor.
%
% SYNTAX:
% b = quat(a)
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class that is the
% unit quaternion to "a".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:unit:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
a = a/norm(a);
end
function b = inv(a)
% The "inv" method overloads Matlab's built in "inv" function for
% quaternions.
%
% SYNTAX:
% b = inv(a)
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class that is the
% inverse quaternion to "a".
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:inv:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
b = conj(a)/norm(a)^2;
end
function TF = eq(a,b)
% The "eq" method overloads Matlab's built in "eq" (i.e. == )
% function for quaternions.
%
% SYNTAX:
% a == b
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% TF - (1 x 1 logical)
% True if all of the components of the quaternions "a" and "b"
% equal each other.
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:eq:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
assert(numel(b) == 1,...
'quaternion:eq:b',...
'Input argument "b" must be a 1 x 1 "quaternion" object.')
TF = (a.r == b.r & a.i == b.i & a.j == b.j & a.k == b.k);
end
function TF = ne(a,b)
% The "ne" method overloads Matlab's built in "ne" (i.e. ~= )
% function for quaternions.
%
% SYNTAX:
% a ~= b
%
% INPUTS:
% a - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% b - (1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% TF - (1 x 1 logical)
% True if any of the components of the quaternions "a" and "b"
% do not equal each other.
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(2,2)
% Check arguments for errors
assert(numel(a) == 1,...
'quaternion:eq:a',...
'Input argument "a" must be a 1 x 1 "quaternion" object.')
assert(numel(b) == 1,...
'quaternion:eq:b',...
'Input argument "b" must be a 1 x 1 "quaternion" object.')
TF = (a.r ~= b.r | a.i ~= b.i | a.j ~= b.j | a.k ~= b.k);
end
function logic = isnan(x)
% The "isnan" method overloads Matlab's built in "isnan" function for
% quaternions.
%
% SYNTAX:
% isnan(x)
%
% INPUTS:
% x - ( M x N x ... quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% logic - (M x N x ... logical)
% A maxtrix of logical the same size of "x", which are true
% if any part of the quaternion "x" is equal to a NaN.
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check number of arguments
narginchk(1,1)
d = size(x);
n = numel(x);
logic = zeros(d);
for i = 1:n
logic(i) = any(isnan([x(i).r x(i).i x(i).j x(i).k]));
end
end
function val = sum(q,dim,~)
% The "sum" method overloads Matlab's built in "sum" function for
% quaternions.
%
% SYNTAX:
% sum(q,dim)
%
% INPUTS:
% q - ( M x N x ... quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% val - ( A x B x ... quaternion)
% Sum of quaternions.
%
% NOTES:
%
%-----------------------------------------------------------------------
d = size(q);
if prod(d) == 1
val = q;
return;
end
if nargin < 2; dim = find(d > 1,1,'first'); end
r = sum(reshape([q.r],d),dim);
i = sum(reshape([q.i],d),dim);
j = sum(reshape([q.j],d),dim);
k = sum(reshape([q.k],d),dim);
val = quaternion(r,i,j,k);
end
function val = var(q,w,dim)
% The "var" method overloads Matlab's built in "var" function for
% quaternions.
%
% SYNTAX:
% var(q,w,dim)
%
% INPUTS:
% q - ( M x N x ... quaternion)
% An instance of the "quaternion" class.
%
% w - ( length must equal dim )
% Weight vector
%
% OUTPUTS:
% val - ( A x B x ... quaternion)
% Variance of quaternions.
%
% NOTES:
% This needs to be verified that it is correct!!!
%
%-----------------------------------------------------------------------
d = size(q);
if prod(d) == 1
val = quaternion(0,0,0,0);
return;
end
if nargin < 2; w = 0; end
if nargin < 3; dim = find(d > 1,1,'first'); end
r = var(reshape([q.r],d),w,dim);
i = var(reshape([q.i],d),w,dim);
j = var(reshape([q.j],d),w,dim);
k = var(reshape([q.k],d),w,dim);
val = quaternion(r,i,j,k);
end
function M = matProdInertial(q)
% The "matProdInertial" method returns the matrix G for computing
% quaternion time derivative for an angular velocity in the
% inertial frame.
%
% SYNTAX:
% M = q.matProdInertial
%
% INPUTS:
% q - ( 1 x 1 quaternion)
% An instance of the "quaternion" class.
%
% OUTPUTS:
% M - ( 3 x 4)
% Matrix product for inertial frame angular velocity.
%
% NOTES:
%
%-----------------------------------------------------------------------
% Check arguments for errors
assert(numel(q) == 1,...
'quaternion:eq:q',...
'Input argument "q" must be a 1 x 1 "quaternion" object.')