-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquaternion.m
2647 lines (2550 loc) · 97 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
% classdef quaternion, implements quaternion mathematics and 3D rotations
%
% Properties (SetAccess = protected):
% e(4,1) components, basis [1; i; j; k]: e(1) + i*e(2) + j*e(3) + k*e(4)
% i*j=k, j*i=-k, j*k=i, k*j=-i, k*i=j, i*k=-j, i*i = j*j = k*k = -1
%
% Constructors:
% q = quaternion scalar zero quaternion, q.e = [0;0;0;0]
% q = quaternion(x) x is a matrix size [4,s1,s2,...] or [s1,4,s2,...],
% q is size [s1,s2,...], q(i1,i2,...).e = ...
% x(1:4,i1,i2,...) or x(i1,1:4,i2,...).'
% q = quaternion(v) v is a matrix size [3,s1,s2,...] or [s1,3,s2,...],
% q is size [s1,s2,...], q(i1,i2,...).e = ...
% [0;v(1:3,i1,i2,...)] or [0;v(i1,1:3,i2,...).']
% q = quaternion(c) c is a complex matrix size [s1,s2,...],
% q is size [s1,s2,...], q(i1,i2,...).e = ...
% [real(c(i1,i2,...));imag(c(i1,i2,...));0;0]
% q = quaternion(x1,x2) x1,x2 are matrices size [s1,s2,...] or scalars,
% q(i1,i2,...).e = [x1(i1,i2,...);x2(i1,i2,...);0;0]
% q = quaternion(v1,v2,v3) v1,v2,v3 matrices size [s1,s2,...] or scalars,
% q(i1,i2,...).e = [0;v1(i1,i2,...);v2(i1,i2,...);...
% v3(i1,i2,...)]
% q = quaternion(x1,x2,x3,x4) x1,x2,x3,x4 matrices size [s1,s2,...] or scalars,
% q(i1,i2,...).e = [x1(i1,i2,...);x2(i1,i2,...);...
% x3(i1,i2,...);x4(i1,i2,...)]
%
% Quaternion array constructor methods:
% q = quaternion.complexmatrix(Q)
% construct quaternions from complex 2x2 matrices
% q = quaternion.eye(N) quaternion NxN identity matrix
% q = quaternion.nan(siz) q(:).e = [NaN;NaN;NaN;NaN]
% q = quaternion.ones(siz) q(:).e = [1;0;0;0]
% q = quaternion.rand(siz) uniform random quaternions, NOT normalized
% to 1, 0 <= q.e(1) <= 1, -1 <= q.e(2:4) <= 1
% q = quaternion.randRot(siz) random quaternions uniform in rotation space
% q = quaternion.zeros(siz) q(:).e = [0;0;0;0]
%
% Rotation constructor methods (all lower case):
% q = quaternion.angleaxis(angle,axis)
% angle is an array in radians, axis is an array
% of vectors size [3,s1,s2,...] or [s1,3,s2,...],
% q is size [s1,s2,...], quaternions normalized to 1
% equivalent to rotations about axis by angle
% q = quaternion.eulerangles(axes,angles) or
% q = quaternion.eulerangles(axes,ang1,ang2,ang3)
% axes is a string array or cell string array,
% '123' = 'xyz' = 'XYZ' = 'ijk', etc.,
% angles is an array of Euler angles in radians,
% size [3,s1,s2,...] or [s1,3,s2,...], or
% (ang1, ang2, ang3) are arrays or scalars of
% Euler angles in radians, q is size
% [s1,s2,...], quaternions normalized to 1
% equivalent to Euler Angle rotations
% q = quaternion.integrateomega(t,w,odeoptions) or
% q = quaternion.integrateomega(t,omega,axis,odeoptions)
% integrate angular velocities over time
% q = quaternion.modifiedrodrigues(mrp)
% quaternions from Modified Rodrigues parameters
% q = quaternion.rodrigues(rp)
% quaternions from Rodrigues parameters
% q = quaternion.rotateutov(u,v,dimu,dimv)
% quaternions normalized to 1 that rotate 3
% element vectors u into the directions of 3
% element vectors v
% q = quaternion.rotationmatrix(R)
% R is an array of rotation or Direction Cosine
% Matrices size [3,3,s1,s2,...] with det(R) == 1,
% q(i1,i2,...) = quaternions normalized to 1,
% equivalent to R(1:3,1:3,i1,i2,...)
%
% Rotation methods (Mixed Case):
% [angle,axis] = AngleAxis(q) angles in radians, unit vector rotation axes
% equivalent to q
% qd = Derivative(q,w) quaternion derivatives, w are 3 component
% angular velocity vectors, qd = 0.5*q*quaternion(w)
% angles = EulerAngles(q,axes) angles are 3 Euler angles equivalent to q, axes
% are strings or cell strings, '123' = 'xyz', etc.
% q1 = Integral(q0,t,w,odeoptions) or
% q1 = Integral(q0,t,omega,axis,odeoptions)
% integrate angular velocities to quaternions
% mrp = ModifiedRodrigues(q) Modified Rodrigues parameters equivalent to q
% [omega,axis] = OmegaAxis(q,t,dim)
% instantaneous angular velocities and rotation axes
% PlotRotation(q,interval) plot columns of rotation matrices of q,
% pause interval between figure updates in seconds
% [q1,w1,t1] = PropagateEulerEq(q0,w0,I,t,@torque,odeoptions)
% Euler equation numerical propagator, see
% help quaternion.PropagateEulerEq
% rp = Rodrigues(q) Rodrigues parameters equivalent to q
% Tp = RotateTensor(q,T) rotations q converted to rotation matrices R,
% acting on 3x3 tensors T: Tp = R * T * R.'
% vp = RotateVector(q,v,dim) vp are 3 component vectors, rotations q acting
% on vectors v, uses rotation matrix multiplication
% vp = RotateVectorQ(q,v,dim) vp are 3 component vectors, rotations q acting
% on vectors v, uses quaternion multiplication,
% RotateVector is 7 times faster than RotateVectorQ
% R = RotationMatrix(q) 3x3 rotation matrices equivalent to q
%
% Note:
% In all rotation operations, the rotations operate from left to right on
% 3x1 column vectors and create rotated vectors, not representations of
% those vectors in rotated coordinate systems.
% For Euler angles, '123' means rotate the vector about x first, about y
% second, about z third, i.e.:
% vp = rotate(z,angle(3)) * rotate(y,angle(2)) * rotate(x,angle(1)) * v
%
% Ordinary methods:
% n = abs(q) quaternion norm, n = sqrt( sum( q.e.^2 ))
% q3 = bsxfun(func,q1,q2) binary singleton expansion of operation func
% c = complex(q) complex( real(q), imag(q) )
% Q = ComplexMatrix(q) convert quaternions into complex 2x2 matrices
% qc = conj(q) quaternion conjugate, qc.e =
% [q.e(1);-q.e(2);-q.e(3);-q.e(4)]
% qt = ctranspose(q) qt = q'; quaternion conjugate transpose,
% 2-D (or scalar) q only
% qp = cumprod(q,dim) cumulative quaternion array product over
% dimension dim
% qs = cumsum(q,dim) cumulative quaternion array sum over dimension dim
% qd = diff(q,ord,dim) quaternion array difference, order ord, over
% dimension dim
% ans = display(q) 'q = ( e(1) ) + i( e(2) ) + j( e(3) ) + k( e(4) )'
% d = dot(q1,q2) quaternion element dot product, d = dot(q1.e,q2.e)
% d = double(q) d = q.e; if size(q) == [s1,s2,...], size(d) ==
% [4,s1,s2,...]
% l = eq(q1,q2) quaternion equality, l = all( q1.e == q2.e )
% l = equiv(q1,q2,tol) quaternion rotational equivalence, within
% tolerance tol, l = (q1 == q2) | (q1 == -q2)
% qe = exp(q) quaternion exponential, v = q.e(2:4), qe.e =
% exp(q.e(1))*[cos(|v|);v.*sin(|v|)./|v|]
% ei = imag(q) imaginary e(2) components
% qi = interp1(t,q,ti,method) interpolate quaternion array
% qi = inverse(q) quaternion inverse, qi = conj(q)./norm(q).^2,
% q .* qi = qi .* q = 1 for q ~= 0
% l = isequal(q1,q2,...) true if equal sizes and values
% l = isequaln(q1,q2,...) true if equal including NaNs
% l = isequalwithequalnans(q1,q2,...) true if equal including NaNs
% l = isfinite(q) true if all( isfinite( q.e ))
% l = isinf(q) true if any( isinf( q.e ))
% l = isnan(q) true if any( isnan( q.e ))
% ej = jmag(q) e(3) components
% ek = kmag(q) e(4) components
% q3 = ldivide(q1,q2) quaternion left division, q3 = q1 \. q2 =
% inverse(q1) *. q2
% ql = log(q) quaternion logarithm, v = q.e(2:4), ql.e =
% [log(|q|);v.*acos(q.e(1)./|q|)./|v|]
% q3 = minus(q1,q2) quaternion subtraction, q3 = q1 - q2
% q3 = mldivide(q1,q2) left division only defined for scalar q1
% qp = mpower(q,p) quaternion matrix power, qp = q^p, p scalar
% integer >= 0, q square quaternion matrix
% q3 = mrdivide(q1,q2) right division only defined for scalar q2
% q3 = mtimes(q1,q2) 2-D matrix quaternion multiplication, q3 = q1 * q2
% l = ne(q1,q2) quaternion inequality, l = ~all( q1.e == q2.e )
% n = norm(q) quaternion norm, n = sqrt( sum( q.e.^2 ))
% [q,n] = normalize(q) make quaternion norm == 1, unless q == 0,
% n = matrix of previous norms
% q3 = plus(q1,q2) quaternion addition, q3 = q1 + q2
% qp = power(q,p) quaternion power, qp = q.^p
% qp = prod(q,dim) quaternion array product over dimension dim
% qp = product(q1,q2) quaternion product of scalar quaternions,
% qp = q1 .* q2, noncommutative
% q3 = rdivide(q1,q2) quaternion right division, q3 = q1 ./ q2 =
% q1 .* inverse(q2)
% er = real(q) real e(1) components
% qs = slerp(q0,q1,t) quaternion spherical linear interpolation
% qr = sqrt(q) qr = q.^0.5, square root
% qs = sum(q,dim) quaternion array sum over dimension dim
% q3 = times(q1,q2) matrix component quaternion multiplication,
% q3 = q1 .* q2, noncommutative
% qm = uminus(q) quaternion negation, qm = -q
% qp = uplus(q) quaternion unitary plus, qp = +q
% ev = vector(q) vector e(2:4) components
%
% Author:
% Mark Tincknell, MIT LL, 29 July 2011, revised 25 June 2015
properties (SetAccess = protected)
e = zeros(4,1);
end % properties
% Array constructors
methods
function q = quaternion( varargin ) % (constructor)
perm = [];
sqz = false;
switch nargin
case 0 % nargin == 0
q.e = zeros(4,1);
return;
case 1 % nargin == 1
siz = size( varargin{1} );
nel = prod( siz );
if nel == 0
q = quaternion.empty;
return;
elseif isa( varargin{1}, 'quaternion' )
q = varargin{1};
return;
elseif (nel == 1) || ~isreal( varargin{1}(:) )
for iel = nel : -1 : 1
q(iel).e = chop( [real(varargin{1}(iel)); ...
imag(varargin{1}(iel)); ...
0; ...
0] );
end
q = reshape( q, siz );
return;
end
[arg4, dim4, perm4] = finddim( varargin{1}, 4 );
if dim4 > 0
siz(dim4) = 1;
nel = prod( siz );
if dim4 > 1
perm = perm4;
else
sqz = true;
end
for iel = nel : -1 : 1
q(iel).e = chop( arg4(:,iel) );
end
else
[arg3, dim3, perm3] = finddim( varargin{1}, 3 );
if dim3 > 0
siz(dim3) = 1;
nel = prod( siz );
if dim3 > 1
perm = perm3;
else
sqz = true;
end
for iel = nel : -1 : 1
q(iel).e = chop( [0; arg3(:,iel)] );
end
else
error( 'quaternion:constructor:nargin1', ...
'Invalid input' );
end
end
case 2 % nargin == 2
% real-imaginary only (no j or k) inputs
na = cellfun( 'prodofsize', varargin );
[nel, jel] = max( na );
if ~all( (na == 1) | (na == nel) )
error( 'quaternion:constructor:nargin2', ...
'All inputs must be singletons or have the same number of elements' );
end
siz = size( varargin{jel} );
for iel = nel : -1 : 1
q(iel).e = chop( [varargin{1}(min(iel,na(1))); ...
varargin{2}(min(iel,na(2))); ...
0;
0] );
end
case 3 % nargin == 3
% vector inputs (no real, only i, j, k)
na = cellfun( 'prodofsize', varargin );
[nel, jel] = max( na );
if ~all( (na == 1) | (na == nel) )
error( 'quaternion:constructor:nargin3', ...
'All inputs must be singletons or have the same number of elements' );
end
siz = size( varargin{jel} );
for iel = nel : -1 : 1
q(iel).e = chop( [0; ...
varargin{1}(min(iel,na(1))); ...
varargin{2}(min(iel,na(2))); ...
varargin{3}(min(iel,na(3)))] );
end
otherwise % nargin >= 4
na = cellfun( 'prodofsize', varargin );
[nel, jel] = max( na );
if ~all( (na == 1) | (na == nel) )
error( 'quaternion:constructor:nargin4', ...
'All inputs must be singletons or have the same number of elements' );
end
siz = size( varargin{jel} );
for iel = nel : -1 : 1
q(iel).e = chop( [varargin{1}(min(iel,na(1))); ...
varargin{2}(min(iel,na(2))); ...
varargin{3}(min(iel,na(3))); ...
varargin{4}(min(iel,na(4)))] );
end
end % switch nargin
if nel == 0
q = quaternion.empty;
end
q = reshape( q, siz );
if ~isempty( perm )
q = ipermute( q, perm );
end
if sqz
q = squeeze( q );
end
end % quaternion (constructor)
% Ordinary methods
function n = abs( q )
n = q.norm;
end % abs
function q3 = bsxfun( func, q1, q2 )
% function q3 = bsxfun( func, q1, q2 )
% Binary Singleton Expansion for quaternion arrays. Apply the element by
% element binary operation specified by the function handle func to arrays
% q1 and q2. All dimensions of q1 and q2 must either agree or be length 1.
% Inputs:
% func function handle (e.g. @plus) of quaternion function or operator
% q1(n1) quaternion array
% q2(n2) quaternion array
% Output:
% q3(n3) quaternion array of function or operator outputs
% size(q3) = max( size(q1), size(q2) )
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
s1 = size( q1 );
s2 = size( q2 );
nd1 = length( s1 );
nd2 = length( s2 );
s1 = [s1, ones(1,nd2-nd1)];
s2 = [s2, ones(1,nd1-nd2)];
if ~all( (s1 == s2) | (s1 == 1) | (s2 == 1) )
error( 'quaternion:bsxfun:notconformable', ...
'Non-singleton dimensions of q1 and q2 must match each other' );
end
c1 = num2cell( s1 );
c2 = num2cell( s2 );
s3 = max( s1, s2 );
nd3 = length( s3 );
n3 = prod( s3 );
q3 = quaternion.nan( s3 );
for i3 = 1 : n3
[ix3{1:nd3}] = ind2sub( s3, i3 );
ix1 = cellfun( @min, ix3, c1, 'UniformOutput', false );
ix2 = cellfun( @min, ix3, c2, 'UniformOutput', false );
q3(i3) = func( q1(ix1{:}), q2(ix2{:}) );
end
end % bsxfun
function c = complex( q )
c = complex( real( q ), imag( q ));
end % complex
function qc = conj( q )
d = double( q );
qc = reshape( quaternion( d(1,:), -d(2,:), -d(3,:), -d(4,:) ), ...
size( q ));
end % conj
function qt = ctranspose( q )
qt = transpose( q.conj );
end % ctranspose
function qp = cumprod( q, dim )
% function qp = cumprod( q, dim )
% cumulative quaternion array product, dim defaults to first dimension of
% length > 1
if isempty( q )
qp = q;
return;
end
if (nargin < 2) || isempty( dim )
[q, dim, perm] = finddim( q, -2 );
elseif dim > 1
ndm = ndims( q );
perm = [ dim : ndm, 1 : dim-1 ];
q = permute( q, perm );
end
qp = q;
for is = 2 : size(q,1)
qp(is,:) = qp(is-1,:) .* q(is,:);
end
if dim > 1
qp = ipermute( qp, perm );
end
end % cumprod
function qs = cumsum( q, dim )
% function qs = cumsum( q, dim )
% cumulative quaternion array sum, dim defaults to first dimension of
% length > 1
if isempty( q )
qs = q;
return;
end
if (nargin < 2) || isempty( dim )
[q, dim, perm] = finddim( q, -2 );
elseif dim > 1
ndm = ndims( q );
perm = [ dim : ndm, 1 : dim-1 ];
q = permute( q, perm );
end
qs = q;
for is = 2 : size(q,1)
qs(is,:) = qs(is-1,:) + q(is,:);
end
if dim > 1
qs = ipermute( qs, perm );
end
end % cumsum
function qd = diff( q, ord, dim )
% function qd = diff( q, ord, dim )
% quaternion array difference, ord is the order of difference (default = 1)
% dim defaults to first dimension of length > 1
if isempty( q )
qd = q;
return;
end
if (nargin < 2) || isempty( ord )
ord = 1;
end
if ord <= 0
qd = q;
return;
end
if (nargin < 3) || isempty( dim )
[q, dim, perm] = finddim( q, -2 );
elseif dim > 1
ndm = ndims( q );
perm = [ dim : ndm, 1 : dim-1 ];
q = permute( q, perm );
end
siz = size( q );
if siz(1) <= 1
qd = quaternion.empty;
return;
end
qd = quaternion.zeros( [(siz(1)-1), siz(2:end)] );
for is = 1 : siz(1)-1
qd(is,:) = q(is+1,:) - q(is,:);
end
ord = ord - 1;
if ord > 0
qd = diff( qd, ord, 1 );
end
if dim > 1
qd = ipermute( qd, perm );
end
end % diff
function display( q ) %#ok<DISPLAY>
if ~isequal( get(0,'FormatSpacing'), 'compact' )
disp(' ');
end
if isempty( q )
fprintf( '%s \t= ([]) + i([]) + j([]) + k([])\n', inputname(1) )
return;
end
siz = size( q );
nel = [1 cumprod( siz )];
ndm = length( siz );
for iel = 1 : nel(end)
if nel(end) == 1
sub = '';
else
sub = ')';
jel = iel - 1;
for idm = ndm : -1 : 1
idx = floor( jel / nel(idm) ) + 1;
sub = [',' int2str(idx) sub]; %#ok<AGROW>
jel = rem( jel, nel(idm) );
end
sub(1) = '(';
end
fprintf( '%s%s \t= (%-12.5g) + i(%-12.5g) + j(%-12.5g) + k(%-12.5g)\n', ...
inputname(1), sub, q(iel).e )
end
end % display
function d = dot( q1, q2 )
% function d = dot( q1, q2 )
% quaternion element dot product: d = dot( q1.e, q2.e ), using binary
% singleton expansion of quaternion arrays
% dn = dot( q1, q2 )/( norm(q1) * norm(q2) ) is the cosine of the angle in
% 4D space between 4D vectors q1.e and q2.e
d = squeeze( sum( bsxfun( @times, double( q1 ), double( q2 )), 1 ));
end % dot
function d = double( q )
siz = size( q );
d = reshape( [q.e], [4 siz] );
d = chop( d );
end % double
function l = eq( q1, q2 )
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
si1 = size( q1 );
si2 = size( q2 );
ne1 = prod( si1 );
ne2 = prod( si2 );
if (ne1 == 0) || (ne2 == 0)
l = logical([]);
return;
elseif ne1 == 1
siz = si2;
elseif ne2 == 1
siz = si1;
elseif isequal( si1, si2 )
siz = si1;
else
error( 'quaternion:eq:baddims', ...
'Matrix dimensions must agree' );
end
l = bsxfun( @eq, [q1.e], [q2.e] );
l = reshape( all( l, 1 ), siz );
end % eq
function l = equiv( q1, q2, tol )
% function l = equiv( q1, q2, tol )
% quaternion rotational equivalence, within tolerance tol,
% l = (q1 == q2) | (q1 == -q2)
% optional argument tol (default = eps) sets tolerance for difference
% from exact equality
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
if (nargin < 3) || isempty( tol )
tol = eps;
end
si1 = size( q1 );
si2 = size( q2 );
ne1 = prod( si1 );
ne2 = prod( si2 );
if (ne1 == 0) || (ne2 == 0)
l = logical([]);
return;
elseif ne1 == 1
siz = si2;
elseif ne2 == 1
siz = si1;
elseif isequal( si1, si2 )
siz = si1;
else
error( 'quaternion:equiv:baddims', ...
'Matrix dimensions must agree' );
end
dm = chop( bsxfun( @minus, [q1.e], [q2.e] ), tol );
dp = chop( bsxfun( @plus, [q1.e], [q2.e] ), tol );
l = all( (dm == 0) | (dp == 0), 1 );
l = reshape( l, siz );
end % equiv
function qe = exp( q )
% function qe = exp( q )
% quaternion exponential, v = q.e(2:4),
% qe.e = exp(q.e(1))*[cos(|v|);v.*sin(|v|)./|v|]
d = double( q );
siz = size( d );
od = ones( 1, ndims( q ));
vn = reshape( sqrt( sum( d(2:4,:).^2, 1 )), [1 siz(2:end)] );
cv = cos( vn );
sv = sin( vn );
n0 = vn ~= 0;
sv(n0) = sv(n0) ./ vn(n0);
sv = repmat( sv, [3, od] );
ex = repmat( reshape( exp( d(1,:) ), [1 siz(2:end)] ), [4, od] );
de = ex .* [ cv; sv .* reshape( d(2:4,:), [3 siz(2:end)] )];
qe = reshape( quaternion( de(1,:), de(2,:), de(3,:), de(4,:) ), ...
size( q ));
end % exp
function ei = imag( q )
siz = size( q );
d = double( q );
ei = reshape( d(2,:), siz );
end % imag
function qi = interp1( varargin )
% function qi = interp1( t, q, ti, method ) or
% qi = q.interp1( t, ti, method ) or
% qi = interp1( q, ti, method )
% Interpolate quaternion array. If q are orientation quaternions (i.e.
% normalized to 1), then -q is equivalent to q, and the sign of q to use as
% the second knot of the interpolation is chosen by which ever is closer to
% the first knot. Extrapolation (i.e. ti < min(t) or ti > max(t)) gives
% qi = quaternion.nan.
% Inputs:
% t(nt) array of ordinates (e.g. times); if t is not provided t=1:nt
% q(nt,nq) quaternion array
% ti(ni) array of query (interpolation) points, t(1) <= ti <= t(end)
% method [OPTIONAL] 'slerp' or 'linear'; default = 'slerp'
% Output:
% qi(ni,nq) interpolated quaternion array
nna = nnz( ~cellfun( @ischar, varargin ));
im = 4;
if isa( varargin{1}, 'quaternion' )
q = varargin{1};
siq = size( q );
if nna == 2
if size( q, 1 ) == 1
t = (1 : siq(2)).';
else
t = (1 : siq(1)).';
end
ti = varargin{2}(:);
im = 3;
elseif isempty( varargin{2} )
if size( q, 1 ) == 1
t = (1 : siq(2)).';
else
t = (1 : siq(1)).';
end
ti = varargin{3}(:);
else
t = varargin{2}(:);
ti = varargin{3}(:);
end
elseif isa( varargin{2}, 'quaternion' )
t = varargin{1}(:);
q = varargin{2};
ti = varargin{3}(:);
siq = size( q );
else
error( 'quaternion:interp1:notq', ...
'Input q must be a quaterion' );
end
neq = prod( siq );
if neq == 0
qi = quaternion.empty;
return;
end
nt = numel( t );
if siq(1) == nt
dim = 1;
else
[q, dim, perm] = finddim( q, nt );
if dim == 0
error( 'quaternion:interp1:badt', ...
'q must have a dimension the same size as t' );
end
end
iNf = interp1( t, (1:nt).', ti );
iN = max( 1, min( nt-1, floor( iNf )));
jN = max( 2, min( nt, ceil( iNf )));
iNm = repmat( iNf - iN, [1, neq / nt] );
% If q are orientation quaternions (i.e. all normalized to 1), then -q
% represents the same rotation. Pick the sign of +/-q that has the closest
% dot product to use as the second knot of the interpolation.
qj = q(jN,:);
if all( abs( norm( q(:) ) - 1 ) <= eps(16) )
qd = dot( q(iN,:), qj );
lq = qd < -qd;
qj(lq) = -qj(lq);
end
if (length( varargin ) >= im) && ...
(strncmpi( 'linear', varargin{im}, length( varargin{im} )))
qi = (1 - iNm) .* q(iN,:) + iNm .* qj;
else
qi = slerp( q(iN,:), qj, iNm );
end
if length( siq ) > 2
sin = siq;
sin(dim) = numel( ti );
sin = circshift( sin, [0, 1-dim] );
qi = reshape( qi, sin );
end
if dim > 1
qi = ipermute( qi, perm );
end
end % interp1
function qi = inverse( q )
% function qi = inverse( q )
% quaternion inverse, qi = conj(q)/norm(q)^2, q*qi = qi*q = 1 for q ~= 0
if isempty( q )
qi = q;
return;
end
d = double( q );
d(2:4,:) = -d(2:4,:);
n2 = repmat( sum( d.^2, 1 ), [4, ones(1,ndims(d)-1 )] );
ne0 = n2 ~= 0;
di = Inf( size( d ));
di(ne0) = d(ne0) ./ n2(ne0);
qi = reshape( quaternion( di(1,:), di(2,:), di(3,:), di(4,:) ), ...
size( q ));
end % inverse
function l = isequal( q1, varargin )
% function l = isequal( q1, q2, ... )
nar = numel( varargin );
if nar == 0
error( 'quaternion:isequal:noargs', ...
'Not enough input arguments' );
end
l = false;
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
si1 = size( q1 );
for iar = 1 : nar
si2 = size( varargin{iar} );
if (length( si1 ) ~= length( si2 )) || ...
~all( si1 == si2 )
return;
else
if ~isa( varargin{iar}, 'quaternion' )
q2 = quaternion( ...
real(varargin{iar}), imag(varargin{iar}), 0, 0 );
else
q2 = varargin{iar};
end
if ~isequal( [q1.e], [q2.e] )
return;
end
end
end
l = true;
end % isequal
function l = isequaln( q1, varargin )
% function l = isequaln( q1, q2, ... )
nar = numel( varargin );
if nar == 0
error( 'quaternion:isequaln:noargs', ...
'Not enough input arguments' );
end
l = false;
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
si1 = size( q1 );
for iar = 1 : nar
si2 = size( varargin{iar} );
if (length( si1 ) ~= length( si2 )) || ...
~all( si1 == si2 )
return;
else
if ~isa( varargin{iar}, 'quaternion' )
q2 = quaternion( ...
real(varargin{iar}), imag(varargin{iar}), 0, 0 );
else
q2 = varargin{iar};
end
if ~isequaln( [q1.e], [q2.e] )
return;
end
end
end
l = true;
end % isequaln
function l = isequalwithequalnans( q1, varargin )
% function l = isequalwithequalnans( q1, q2, ... )
nar = numel( varargin );
if nar == 0
error( 'quaternion:isequalwithequalnans:noargs', ...
'Not enough input arguments' );
end
l = false;
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
si1 = size( q1 );
for iar = 1 : nar
si2 = size( varargin{iar} );
if (length( si1 ) ~= length( si2 )) || ...
~all( si1 == si2 )
return;
else
if ~isa( varargin{iar}, 'quaternion' )
q2 = quaternion( ...
real(varargin{iar}), imag(varargin{iar}), 0, 0 );
else
q2 = varargin{iar};
end
if ~isequalwithequalnans( [q1.e], [q2.e] ) %#ok<FPARK>
return;
end
end
end
l = true;
end % isequalwithequalnans
function l = isfinite( q )
% function l = isfinite( q ), l = all( isfinite( q.e ))
d = [q.e];
l = reshape( all( isfinite( d ), 1 ), size( q ));
end % isfinite
function l = isinf( q )
% function l = isinf( q ), l = any( isinf( q.e ))
d = [q.e];
l = reshape( any( isinf( d ), 1 ), size( q ));
end % isinf
function l = isnan( q )
% function l = isnan( q ), l = any( isnan( q.e ))
d = [q.e];
l = reshape( any( isnan( d ), 1 ), size( q ));
end % isnan
function ej = jmag( q )
siz = size( q );
d = double( q );
ej = reshape( d(3,:), siz );
end % jmag
function ek = kmag( q )
siz = size( q );
d = double( q );
ek = reshape( d(4,:), siz );
end % kmag
function q3 = ldivide( q1, q2 )
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
si1 = size( q1 );
si2 = size( q2 );
ne1 = prod( si1 );
ne2 = prod( si2 );
if (ne1 == 0) || (ne2 == 0)
q3 = quaternion.empty;
return;
elseif ~isequal( si1, si2 ) && (ne1 ~= 1) && (ne2 ~= 1)
error( 'quaternion:ldivide:baddims', ...
'Matrix dimensions must agree' );
end
for iel = max( ne1, ne2 ) : -1 : 1
q3(iel) = product( q1(min(iel,ne1)).inverse, ...
q2(min(iel,ne2)) );
end
if ne2 > ne1
q3 = reshape( q3, si2 );
else
q3 = reshape( q3, si1 );
end
end % ldivide
function ql = log( q )
% function ql = log( q )
% quaternion logarithm, v = q.e(2:4), ql.e = [log(|q|);v.*acos(q.e(1)./|q|)./|v|]
% logarithm of negative real quaternions is ql.e = [log(|q|);pi;0;0]
d = double( q );
d2 = d.^2;
siz = size( d );
od = ones( 1, ndims( q ));
[vn,qn] = deal( zeros( [1 siz(2:end)] ));
vn(:) = sqrt( sum( d2(2:4,:), 1 ));
qn(:) = sqrt( sum( d2(1:4,:), 1 ));
lq = log( qn );
d1 = reshape( d(1,:), [1 siz(2:end)] );
nq = qn ~= 0;
d1(nq) = d1(nq) ./ qn(nq);
ac = acos( d1 );
nv = vn ~= 0;
ac(nv) = ac(nv) ./ vn(nv);
ac = reshape( repmat( ac, [3, od] ), 3, [] );
va = reshape( d(2:4,:) .* ac, [3 siz(2:end)] );
nn = (d1 < 0) & (vn == 0);
va(1,nn)= pi;
dl = [ lq; va ];
ql = reshape( quaternion( dl(1,:), dl(2,:), dl(3,:), dl(4,:) ), ...
size( q ));
end % log
function q3 = minus( q1, q2 )
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
si1 = size( q1 );
si2 = size( q2 );
ne1 = prod( si1 );
ne2 = prod( si2 );
if (ne1 == 0) || (ne2 == 0)
q3 = quaternion.empty;
return;
elseif ne1 == 1
siz = si2;
elseif ne2 == 1
siz = si1;
elseif isequal( si1, si2 )
siz = si1;
else
error( 'quaternion:minus:baddims', ...
'Matrix dimensions must agree' );
end
d3 = bsxfun( @minus, [q1.e], [q2.e] );
q3 = quaternion( d3(1,:), d3(2,:), d3(3,:), d3(4,:) );
q3 = reshape( q3, siz );
end % minus
function q3 = mldivide( q1, q2 )
% function q3 = mldivide( q1, q2 ), left division only defined for scalar q1
if numel( q1 ) > 1
error( 'quaternion:mldivide:undefined', ...
'Left matix division undefined for quaternion arrays' );
end
q3 = ldivide( q1, q2 );
end % mldivide
function qp = mpower( q, p )
% function qp = mpower( q, p ), quaternion matrix power
siq = size( q );
neq = prod( siq );
nep = numel( p );
if neq == 1
qp = power( q, p );
return;
elseif isa( p, 'quaternion' )
error( 'quaternion:mpower:undefined', ...
'Quaternion as matrix exponent is not defined' );
end
if (neq == 0) || (nep == 0)
qp = quaternion.empty;
return;
elseif (nep > 1) || (mod( p, 1 ) ~= 0) || (p < 0) || ...
(numel( siq ) > 2) || (siq(1) ~= siq(2))
error( 'quaternion:mpower:notconformable', ...
'Inputs must be a scalar non-negative integer power and a square quaternion matrix' );
elseif p == 0
qp = quaternion.eye( siq(1) );
return;
end
qp = q;
for ip = 2 : p
qp = qp * q;
end
end % mpower
function q3 = mrdivide( q1, q2 )
% function q3 = mrdivide( q1, q2 ), right division only defined for scalar q2
if numel( q2 ) > 1
error( 'quaternion:mrdivide:undefined', ...
'Right matix division undefined for quaternion arrays' );
end
q3 = rdivide( q1, q2 );
end % mrdivide
function q3 = mtimes( q1, q2 )
% function q3 = mtimes( q1, q2 )
% q3 = matrix quaternion product of 2-D conformable quaternion matrices q1
% and q2
if ~isa( q1, 'quaternion' )
q1 = quaternion( real(q1), imag(q1), 0, 0 );
end
if ~isa( q2, 'quaternion' )
q2 = quaternion( real(q2), imag(q2), 0, 0 );
end
si1 = size( q1 );
si2 = size( q2 );
ne1 = prod( si1 );
ne2 = prod( si2 );
if (ne1 == 1) || (ne2 == 1)
q3 = times( q1, q2 );
return;
end
if (length( si1 ) ~= 2) || (length( si2 ) ~= 2)
error( 'quaternion:mtimes:not2D', ...
'Input arguments must be 2-D' );
end
if si1(2) ~= si2(1)
error( 'quaternion:mtimes:notconformable', ...
'Inner matrix dimensions must agree' );
end
q3 = repmat( quaternion, [si1(1) si2(2)] );
for i1 = 1 : si1(1)
for i2 = 1 : si2(2)
for i3 = 1 : si1(2)
q3(i1,i2) = q3(i1,i2) + product( q1(i1,i3), q2(i3,i2) );
end
end
end
end % mtimes
function l = ne( q1, q2 )
l = ~eq( q1, q2 );
end % ne
function n = norm( q )
n = shiftdim( sqrt( sum( double( q ).^2, 1 )), 1 );
end % norm
function [q, n] = normalize( q )
% function [q, n] = normalize( q )