comparison scripts/general/cart2sph.m @ 10688:7357e37f34fa

coordinate transforms: add option to operate on column matrix of coordinates.
author Rik <octave@nomad.inbox5.com>
date Tue, 08 Jun 2010 21:47:05 -0700
parents a8ce6bdecce5
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
10687:a8ce6bdecce5 10688:7357e37f34fa
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z}) 20 ## @deftypefn {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z})
21 ## @deftypefnx {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{C})
22 ## @deftypefnx {Function File} {@var{S} =} cart2sph (@dots{})
21 ## Transform Cartesian to spherical coordinates. 23 ## Transform Cartesian to spherical coordinates.
22 ## @var{x}, @var{y} and @var{z} must be the same shape, or scalar. 24 ##
23 ## @var{theta} describes the angle relative to the positive x-axis. 25 ## @var{theta} describes the angle relative to the positive x-axis.
24 ## @var{phi} is the angle relative to the xy-plane. 26 ## @var{phi} is the angle relative to the xy-plane.
25 ## @var{r} is the distance to the origin @w{(0, 0, 0)}. 27 ## @var{r} is the distance to the origin @w{(0, 0, 0)}.
26 ## @seealso{pol2cart, cart2pol, sph2cart} 28 ## @var{x}, @var{y}, and @var{z} must be the same shape, or scalar.
29 ## If called with a single matrix argument then each row of @var{c}
30 ## represents the Cartesian coordinate (@var{x}, @var{y}, @var{z}).
31 ##
32 ## If only a single return argument is requested then return a matrix
33 ## @var{s} where each row represents one spherical coordinate
34 ## (@var{theta}, @var{phi}, @var{r}).
35 ## @seealso{sph2cart, cart2pol, pol2cart}
27 ## @end deftypefn 36 ## @end deftypefn
28 37
29 ## Author: Kai Habel <kai.habel@gmx.de> 38 ## Author: Kai Habel <kai.habel@gmx.de>
30 ## Adapted-by: jwe 39 ## Adapted-by: jwe
31 40
32 function [theta, phi, r] = cart2sph (x, y, z) 41 function [theta, phi, r] = cart2sph (x, y, z)
33 42
34 if (nargin != 3) 43 if (nargin != 1 && nargin != 3)
35 print_usage (); 44 print_usage ();
36 endif 45 endif
37 46
38 if ((ismatrix (x) && ismatrix (y) && ismatrix (z)) 47 if (nargin == 1)
39 && (size_equal (x, y) || isscalar (x) || isscalar (y)) 48 if (ismatrix (x) && columns (x) == 3)
40 && (size_equal (x, z) || isscalar (x) || isscalar (z)) 49 z = x(:,3);
41 && (size_equal (y, z) || isscalar (y) || isscalar (z))) 50 y = x(:,2);
51 x = x(:,1);
52 else
53 error ("cart2sph: matrix input must have 3 columns [X, Y, Z]");
54 endif
55 elseif (nargin == 3)
56 if (! ((ismatrix (x) && ismatrix (y) && ismatrix (z))
57 && (size_equal (x, y) || isscalar (x) || isscalar (y))
58 && (size_equal (x, z) || isscalar (x) || isscalar (z))
59 && (size_equal (y, z) || isscalar (y) || isscalar (z))))
60 error ("cart2sph: X, Y, Z must be matrices of the same size, or scalar");
61 endif
62 endif
42 63
43 theta = atan2 (y, x); 64 theta = atan2 (y, x);
44 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2)); 65 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2));
45 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2); 66 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
46 67
47 else 68 if (nargout <= 1)
48 error ("cart2sph: arguments must be matrices of same size, or scalar"); 69 theta = [theta, phi, r];
49 endif 70 endif
50 71
51 endfunction 72 endfunction
52 73
53 %!test 74 %!test
84 %! [t, p, r] = cart2sph (x, y, z); 105 %! [t, p, r] = cart2sph (x, y, z);
85 %! assert (t, [0, 1, 1] * pi/4); 106 %! assert (t, [0, 1, 1] * pi/4);
86 %! assert (p, [0, 0, 0]); 107 %! assert (p, [0, 0, 0]);
87 %! assert (r, [0, 1, 2] * sqrt(2)); 108 %! assert (r, [0, 1, 2] * sqrt(2));
88 109
110 %!test
111 %! C = [0, 0, 0; 1, 0, 1; 2, 0, 2];
112 %! S = [0, 0, 0; 0, pi/4, sqrt(2); 0, pi/4, 2*sqrt(2)];
113 %! assert (cart2sph(C), S, eps);