[37] | 1 | function q = plus(l, r) |
---|
| 2 | % + Plus. |
---|
| 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 | % Three cases have to be handled: |
---|
| 11 | % |
---|
| 12 | % l is a quaternion, r is not, |
---|
| 13 | % r is a quaternion, l is not, |
---|
| 14 | % l and r are both quaternions. |
---|
| 15 | % |
---|
| 16 | % The complication here is that we can't call ispure() or s() etc |
---|
| 17 | % for a parameter that is not a quaternion, so we have to handle |
---|
| 18 | % these cases differently. |
---|
| 19 | |
---|
| 20 | if isa(l, 'quaternion') & isa(r, 'quaternion') |
---|
| 21 | |
---|
| 22 | % The scalar part could be empty, and we have to handle this |
---|
| 23 | % specially, because [] + x is [] and not x, for some reason. |
---|
| 24 | |
---|
| 25 | if ispure(l) & ispure(r) |
---|
| 26 | q = quaternion( x(l) + x(r), y(l) + y(r), z(l) + z(r)); |
---|
| 27 | elseif ispure(l) |
---|
| 28 | q = quaternion( s(r), x(l) + x(r), y(l) + y(r), z(l) + z(r)); |
---|
| 29 | elseif ispure(r) |
---|
| 30 | q = quaternion(s(l), x(l) + x(r), y(l) + y(r), z(l) + z(r)); |
---|
| 31 | else |
---|
| 32 | q = quaternion(s(l) + s(r), x(l) + x(r), y(l) + y(r), z(l) + z(r)); |
---|
| 33 | end |
---|
| 34 | |
---|
| 35 | elseif isa(l, 'quaternion') & isa(r, 'numeric') |
---|
| 36 | |
---|
| 37 | if ispure(l) |
---|
| 38 | q = quaternion( r, x(l), y(l), z(l)); |
---|
| 39 | else |
---|
| 40 | q = quaternion(s(l) + r, x(l), y(l), z(l)); |
---|
| 41 | end |
---|
| 42 | |
---|
| 43 | elseif isa(l, 'numeric') & isa(r, 'quaternion') |
---|
| 44 | |
---|
| 45 | if ispure(r) |
---|
| 46 | q = quaternion(l , x(r), y(r), z(r)); |
---|
| 47 | else |
---|
| 48 | q = quaternion(l + s(r), x(r), y(r), z(r)); |
---|
| 49 | end |
---|
| 50 | |
---|
| 51 | else |
---|
| 52 | error('Unhandled parameter types in function +/plus') |
---|
| 53 | end |
---|
| 54 | |
---|
| 55 | |
---|