]> Creatis software - CreaPhase.git/blob - octave_packages/quaternion-2.0.0/@quaternion/quaternion.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / quaternion-2.0.0 / @quaternion / quaternion.m
1 ## Copyright (C) 2010, 2011   Lukas F. Reichlin
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
21 ##
22 ## @example
23 ## q = w + x*i + y*j + z*k
24 ## @end example
25 ##
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
30 ## assumed.
31 ##
32 ## @strong{Example}
33 ## @example
34 ## @group
35 ## octave:1> q = quaternion (2)
36 ## q = 2 + 0i + 0j + 0k
37 ## 
38 ## octave:2> q = quaternion (3, 4, 5)
39 ## q = 0 + 3i + 4j + 5k
40 ## 
41 ## octave:3> q = quaternion (2, 3, 4, 5)
42 ## q = 2 + 3i + 4j + 5k
43 ## @end group
44 ## @end example
45 ## @example
46 ## @group
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)
52 ## q.w =
53 ##     2    6   10
54 ##    14   18   22
55 ## 
56 ## q.x =
57 ##     3    7   11
58 ##    15   19   23
59 ## 
60 ## q.y =
61 ##     4    8   12
62 ##    16   20   24
63 ## 
64 ## q.z =
65 ##     5    9   13
66 ##    17   21   25
67 ## 
68 ## octave:9> 
69 ## @end group
70 ## @end example
71 ##
72 ## @end deftypefn
73
74 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
75 ## Created: May 2010
76 ## Version: 0.2
77
78 function q = quaternion (a, b, c, d)
79
80   switch (nargin)
81     case 1
82       if (isa (a, "quaternion"))        # quaternion (q)
83         q = a;
84         return;
85       elseif (is_real_array (a))        # quaternion (w)
86         b = c = d = zeros (size (a));
87       else
88         print_usage ();
89       endif
90     case 3                              # quaternion (x, y, z)
91       d = c;
92       c = b;
93       b = a;
94       a = zeros (size (a));
95     case 4                              # quaternion (w, x, y, z)
96       ## nothing to do here, just prevent case "otherwise"
97     otherwise
98       print_usage ();
99   endswitch
100
101   if (! is_real_array (a, b, c, d))
102     error ("quaternion: arguments must be real matrices");
103   endif
104
105   if (! size_equal (a, b, c, d));
106     error ("quaternion: arguments must have identical sizes");
107   endif
108
109   q = class (struct ("w", a, "x", b, "y", c, "z", d), "quaternion");
110
111 endfunction