]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/cart2pol.m
update packages
[CreaPhase.git] / octave_packages / m / general / cart2pol.m
1 ## Copyright (C) 2000-2012 Kai Habel
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{x}, @var{y})
21 ## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{x}, @var{y}, @var{z})
22 ## @deftypefnx {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{c})
23 ## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{c})
24 ## @deftypefnx {Function File} {@var{p} =} cart2pol (@dots{})
25 ##
26 ## Transform Cartesian to polar or cylindrical coordinates.
27 ##
28 ## @var{theta} describes the angle relative to the positive x-axis.
29 ## @var{r} is the distance to the z-axis @w{(0, 0, z)}.
30 ## @var{x}, @var{y} (and @var{z}) must be the same shape, or scalar.
31 ## If called with a single matrix argument then each row of @var{c}
32 ## represents the Cartesian coordinate (@var{x}, @var{y} (, @var{z})).
33 ##
34 ## If only a single return argument is requested then return a matrix
35 ## @var{p} where each row represents one polar/(cylindrical) coordinate
36 ## (@var{theta}, @var{phi} (, @var{z})).
37 ## @seealso{pol2cart, cart2sph, sph2cart}
38 ## @end deftypefn
39
40 ## Author: Kai Habel <kai.habel@gmx.de>
41 ## Adapted-by: jwe
42
43 function [theta, r, z] = cart2pol (x, y, z)
44
45   if (nargin < 1 || nargin > 3)
46     print_usage ();
47   endif
48
49   if (nargin == 1)
50     if (ismatrix (x) && (columns (x) == 2 || columns (x) == 3))
51       if (columns (x) == 3)
52         z = x(:,3);
53       else
54         z = [];
55       endif
56       y = x(:,2);
57       x = x(:,1);
58     else
59       error ("cart2pol: matrix input must have 2 or 3 columns [X, Y (, Z)]");
60     endif
61   elseif (nargin == 2)
62     if (! ((ismatrix (x) && ismatrix (y))
63             && (size_equal (x, y) || isscalar (x) || isscalar (y))))
64       error ("cart2pol: arguments must be matrices of same size, or scalar");
65     endif
66   elseif (nargin == 3)
67     if (! ((ismatrix (x) && ismatrix (y) && ismatrix (z))
68             && (size_equal (x, y) || isscalar (x) || isscalar (y))
69             && (size_equal (x, z) || isscalar (x) || isscalar (z))
70             && (size_equal (y, z) || isscalar (y) || isscalar (z))))
71       error ("cart2pol: arguments must be matrices of same size, or scalar");
72     endif
73   endif
74
75   theta = atan2 (y, x);
76   r = sqrt (x .^ 2 + y .^ 2);
77
78   if (nargout <= 1)
79     theta = [theta, r, z];
80   endif
81
82 endfunction
83
84 %!test
85 %! x = [0, 1, 2];
86 %! y = 0;
87 %! [t, r] = cart2pol (x, y);
88 %! assert (t, [0, 0, 0]);
89 %! assert (r, x);
90
91 %!test
92 %! x = [0, 1, 2];
93 %! y = [0, 1, 2];
94 %! [t, r] = cart2pol (x, y);
95 %! assert (t, [0, pi/4, pi/4], sqrt(eps));
96 %! assert (r, sqrt(2)*[0, 1, 2], sqrt(eps));
97
98 %!test
99 %! x = [0, 1, 2];
100 %! y = [0, 1, 2];
101 %! z = [0, 1, 2];
102 %! [t, r, z2] = cart2pol (x, y, z);
103 %! assert (t, [0, pi/4, pi/4], sqrt(eps));
104 %! assert (r, sqrt(2)*[0, 1, 2], sqrt(eps));
105 %! assert (z, z2);
106
107 %!test
108 %! x = [0, 1, 2];
109 %! y = 0;
110 %! z = 0;
111 %! [t, r, z2] = cart2pol (x, y, z);
112 %! assert (t, [0, 0, 0], eps);
113 %! assert (r, x, eps);
114 %! assert (z, z2);
115
116 %!test
117 %! x = 0;
118 %! y = [0, 1, 2];
119 %! z = 0;
120 %! [t, r, z2] = cart2pol (x, y, z);
121 %! assert (t, [0, 1, 1]*pi/2, eps);
122 %! assert (r, y, eps);
123 %! assert (z, z2);
124
125 %!test
126 %! x = 0;
127 %! y = 0;
128 %! z = [0, 1, 2];
129 %! [t, r, z2] = cart2pol (x, y, z);
130 %! assert (t, 0);
131 %! assert (r, 0);
132 %! assert (z, z2);
133
134 %!test
135 %! C = [0, 0; 1, 1; 2, 2];
136 %! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)];
137 %! assert (cart2pol (C), P, sqrt(eps));
138
139 %!test
140 %! C = [0, 0, 0; 1, 1, 1; 2, 2, 2];
141 %! P = [0, 0, 0; pi/4, sqrt(2), 1; pi/4, 2*sqrt(2), 2];
142 %! assert (cart2pol (C), P, sqrt(eps));
143