]> Creatis software - CreaPhase.git/blob - octave_packages/quaternion-2.0.0/@quaternion/log.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / quaternion-2.0.0 / @quaternion / log.m
1 ## Copyright (C) 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{qlog} =} log (@var{q})
18 ## Logarithmus naturalis of a quaternion.
19 ## @end deftypefn
20
21 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
22 ## Created: November 2011
23 ## Version: 0.1
24
25 function q = log (q)
26
27   if (nargin != 1)
28     print_usage ();
29   endif
30
31   normq = abs (q);
32   normv = normv (q);
33   acossq = acos (q.w ./ normq);
34
35   q.w = log (normq);    
36   q.x = (q.x ./ normv) .* acossq;
37   q.y = (q.y ./ normv) .* acossq;
38   q.z = (q.z ./ normv) .* acossq;
39   
40   ## FIXME: q = quaternion (2, 3, 4, 5)
41   ##        p = log (exp (q))
42   ##        p.v is wrong, probably somehow related to acos
43   ## NOTE:  p = exp (log (q)) is calculated correctly
44   ## NOTE:  qtfm 1.9 returns the same "wrong" result
45
46 endfunction
47
48
49 %!shared A, B
50 %! Aw = [2, 6, 10; 14, 18, 22];
51 %! Ax = [3, 7, 11; 15, 19, 23];
52 %! Ay = [4, 8, 12; 16, 20, 24];
53 %! Az = [5, 9, 13; 17, 21, 25];
54 %! A = quaternion (Aw, Ax, Ay, Az);
55 %!
56 %! B = exp (log (A));
57 %!
58 %!assert (A.w, B.w, 1e-4);
59 %!assert (A.x, B.x, 1e-4);
60 %!assert (A.y, B.y, 1e-4);
61 %!assert (A.z, B.z, 1e-4);