1 | function q = quaternion(a0, a1, a2, a3) |
---|
2 | % QUATERNION Construct quaternions from components. |
---|
3 | % Accepts the following possible arguments, which may be scalars, vectors or matrices: |
---|
4 | % |
---|
5 | % No argument - returns an empty quaternion scalar, vector or matrix. |
---|
6 | % A quaternion argument - returns the argument unmodified. |
---|
7 | % A non-quaternion argument - returns the argument in the scalar part and supplies |
---|
8 | % a zero vector part. |
---|
9 | % Two arguments - returns a quaternion, provided the first argument |
---|
10 | % is numeric and the second is a pure quaternion. |
---|
11 | % Three arguments - returns a pure quaternion scalar, vector or matrix, |
---|
12 | % with an empty scalar part. |
---|
13 | % Four arguments - returns a full quaternion scalar, vector or matrix. |
---|
14 | |
---|
15 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
16 | % See the file : Copyright.m for further details. |
---|
17 | |
---|
18 | % Quaternions are represented as (private) structures with four fields. |
---|
19 | |
---|
20 | error(nargchk(0, 4, nargin)), error(nargoutchk(0, 1, nargout)) |
---|
21 | |
---|
22 | switch nargin |
---|
23 | case 0 |
---|
24 | |
---|
25 | % Construct an empty quaternion. |
---|
26 | |
---|
27 | q = class(compose([],[],[],[]), 'quaternion'); |
---|
28 | |
---|
29 | case 1 |
---|
30 | |
---|
31 | if isa(a0, 'quaternion') |
---|
32 | q = a0; % a0 is already a quaternion, so return it. |
---|
33 | else |
---|
34 | % a0 is not a quaternion. |
---|
35 | if isnumeric(a0) |
---|
36 | zero = a0 - a0; % Construct zeros of the same type and size as a0. Note 1. |
---|
37 | q = class(compose(a0, zero, zero, zero), 'quaternion'); |
---|
38 | else |
---|
39 | error('Cannot construct a quaternion with a non-numeric in the scalar part.'); |
---|
40 | end |
---|
41 | end |
---|
42 | |
---|
43 | case 2 |
---|
44 | |
---|
45 | if any(size(a0) ~= size(a1)) |
---|
46 | error('Arguments must have the same dimensions') |
---|
47 | end |
---|
48 | |
---|
49 | if isnumeric(a0) && isa(a1, 'quaternion') |
---|
50 | if ispure(a1) |
---|
51 | q = class(compose(a0, x(a1), y(a1), z(a1)), 'quaternion'); |
---|
52 | else |
---|
53 | error('The second argument must be a pure quaternion.'); |
---|
54 | end |
---|
55 | else |
---|
56 | error('First argument must be numeric and the second must be a quaternion.'); |
---|
57 | end |
---|
58 | |
---|
59 | case 3 |
---|
60 | |
---|
61 | % Construct a pure quaternion. |
---|
62 | |
---|
63 | s0 = size(a0); |
---|
64 | if any(s0 ~= size(a1)) || any(s0 ~= size(a2)) |
---|
65 | error('Arguments must have the same dimensions') |
---|
66 | end |
---|
67 | |
---|
68 | q = class(compose(a0, a1, a2), 'quaternion'); |
---|
69 | |
---|
70 | case 4 % Return a full quaternion. |
---|
71 | |
---|
72 | s0 = size(a0); |
---|
73 | if any(s0 ~= size(a1)) || any(s0 ~= size(a2)) || any(s0 ~= size(a3)) |
---|
74 | error('Arguments must have the same dimensions') |
---|
75 | end |
---|
76 | |
---|
77 | q = class(compose(a0, a1, a2, a3), 'quaternion'); |
---|
78 | |
---|
79 | end |
---|
80 | |
---|
81 | % Note 1 |
---|
82 | % |
---|
83 | % At the point where zeros are constructed by subtracting a0 from a0, there |
---|
84 | % is a minor issue with NaNs and Infinities. If the function is called with |
---|
85 | % quaternion(NaN), the result will have NaN in the scalar and vector parts. |
---|
86 | % Similarly with Inf, since Inf-Inf yields NaN. This doesn't seem an |
---|
87 | % important issue to resolve for the moment. Steve Sangwine 31 May 2005. |
---|