[37] | 1 | function q = times(l, r) |
---|
| 2 | % .* Array multiply. |
---|
| 3 | % (Quaternion overloading of standard Matlab function.) |
---|
| 4 | |
---|
| 5 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
| 6 | % See the file : Copyright.m for further details. |
---|
| 7 | |
---|
| 8 | error(nargchk(2, 2, nargin)), error(nargoutchk(0, 1, nargout)) |
---|
| 9 | |
---|
| 10 | if isa(l, 'quaternion') & isa(r, 'quaternion') |
---|
| 11 | |
---|
| 12 | % Both arguments are quaternions. There are four cases to handle, |
---|
| 13 | % dependent on whether the arguments are pure or full. |
---|
| 14 | |
---|
| 15 | if ispure(l) & ispure(r) |
---|
| 16 | q = -dot(l, r) + cross(l, r); |
---|
| 17 | elseif ispure(l) |
---|
| 18 | q = l .* s(r) + + l .* v(r); |
---|
| 19 | elseif ispure(r) |
---|
| 20 | q = s(l) .* r + v(l) .* r; |
---|
| 21 | else |
---|
| 22 | q = s(l) .* s(r) + v(l) .* s(r) + s(l) .* v(r) + v(l) .* v(r); |
---|
| 23 | end |
---|
| 24 | |
---|
| 25 | else |
---|
| 26 | % One of the arguments is not a quaternion. If it is numeric, we can handle it, |
---|
| 27 | % and we must because the code above requires us to multiply scalar parts by |
---|
| 28 | % vector parts using a recursive call to this function. |
---|
| 29 | |
---|
| 30 | if isa(l, 'quaternion') & isa(r, 'numeric') |
---|
| 31 | if ispure(l) |
---|
| 32 | q = quaternion( x(l) .* r, y(l) .* r, z(l) .* r); |
---|
| 33 | else |
---|
| 34 | q = quaternion(s(l) .* r, x(l) .* r, y(l) .* r, z(l) .* r); |
---|
| 35 | end |
---|
| 36 | elseif isa(l, 'numeric') & isa(r, 'quaternion') |
---|
| 37 | if ispure(r) |
---|
| 38 | q = quaternion( l .* x(r), l .* y(r), l .* z(r)); |
---|
| 39 | else |
---|
| 40 | q = quaternion(l .* s(r), l .* x(r), l .* y(r), l .* z(r)); |
---|
| 41 | end |
---|
| 42 | else |
---|
| 43 | error('Multiplication of a quaternion by a non-numeric is not implemented.') |
---|
| 44 | end |
---|
| 45 | end |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|