1 ## Copyright (C) 2010, 2011 Lukas F. Reichlin
3 ## This program is free software: you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation, either version 3 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{q} =} quaternion (@var{w})
18 ## @deftypefnx {Function File} {@var{q} =} quaternion (@var{x}, @var{y}, @var{z})
19 ## @deftypefnx {Function File} {@var{q} =} quaternion (@var{w}, @var{x}, @var{y}, @var{z})
20 ## Constructor for quaternions - create or convert to quaternion.
23 ## q = w + x*i + y*j + z*k
26 ## Arguments @var{w}, @var{x}, @var{y} and @var{z} can be scalars or
27 ## matrices, but they must be real and of equal size. If scalar part
28 ## @var{w} or components @var{x}, @var{y} and @var{z} of the vector
29 ## part are not specified, zero matrices of appropriate size are
35 ## octave:1> q = quaternion (2)
36 ## q = 2 + 0i + 0j + 0k
38 ## octave:2> q = quaternion (3, 4, 5)
39 ## q = 0 + 3i + 4j + 5k
41 ## octave:3> q = quaternion (2, 3, 4, 5)
42 ## q = 2 + 3i + 4j + 5k
47 ## octave:4> w = [2, 6, 10; 14, 18, 22];
48 ## octave:5> x = [3, 7, 11; 15, 19, 23];
49 ## octave:6> y = [4, 8, 12; 16, 20, 24];
50 ## octave:7> z = [5, 9, 13; 17, 21, 25];
51 ## octave:8> q = quaternion (w, x, y, z)
74 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
78 function q = quaternion (a, b, c, d)
82 if (isa (a, "quaternion")) # quaternion (q)
85 elseif (is_real_array (a)) # quaternion (w)
86 b = c = d = zeros (size (a));
90 case 3 # quaternion (x, y, z)
95 case 4 # quaternion (w, x, y, z)
96 ## nothing to do here, just prevent case "otherwise"
101 if (! is_real_array (a, b, c, d))
102 error ("quaternion: arguments must be real matrices");
105 if (! size_equal (a, b, c, d));
106 error ("quaternion: arguments must have identical sizes");
109 q = class (struct ("w", a, "x", b, "y", c, "z", d), "quaternion");