comparison scripts/general/sph2cart.m @ 8533:fb1b87ea4af9

Permit scalars when transforming coordinates.
author Ben Abbott <bpabbott@mac.com>
date Sat, 17 Jan 2009 17:32:11 -0500
parents a1dbe9d80eee
children eb63fbe60fab
comparison
equal deleted inserted replaced
8532:4d884a016846 8533:fb1b87ea4af9
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r}) 20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r})
21 ## Transform spherical to cartesian coordinates. 21 ## Transform spherical to cartesian coordinates.
22 ## @var{x}, @var{y} and @var{z} must be of same shape. 22 ## @var{x}, @var{y} and @var{z} must be of same shape, or scalar.
23 ## @var{theta} describes the angle relative to the x-axis. 23 ## @var{theta} describes the angle relative to the x-axis.
24 ## @var{phi} is the angle relative to the xy-plane. 24 ## @var{phi} is the angle relative to the xy-plane.
25 ## @var{r} is the distance to the origin (0, 0, 0). 25 ## @var{r} is the distance to the origin (0, 0, 0).
26 ## @seealso{pol2cart, cart2pol, cart2sph} 26 ## @seealso{pol2cart, cart2pol, cart2sph}
27 ## @end deftypefn 27 ## @end deftypefn
28 28
29 ## Author: Kai Habel <kai.habel@gmx.de> 29 ## Author: Kai Habel <kai.habel@gmx.de>
30 ## Adapted-by: jwe 30 ## Adapted-by: jwe
31 31
32 function [X, Y, Z] = sph2cart (Theta, Phi, R) 32 function [x, y, z] = sph2cart (theta, phi, r)
33 33
34 if (nargin != 3) 34 if (nargin != 3)
35 print_usage (); 35 print_usage ();
36 endif 36 endif
37 37
38 if ((! (ismatrix (Theta) && ismatrix (Phi) && ismatrix (R))) 38 if ((ismatrix (theta) && ismatrix (phi) && ismatrix (r))
39 || (! size_equal (Theta, Phi)) 39 && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi))
40 || (! size_equal (Theta, R))) 40 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
41 error ("sph2cart: arguments must be matrices of same size"); 41 && (size_equal (phi, r) || isscalar (phi) || isscalar (r)))
42
43 x = r .* cos (phi) .* cos (theta);
44 y = r .* cos (phi) .* sin (theta);
45 z = r .* sin (phi);
46
47 else
48 error ("sph2cart: arguments must be matrices of same size, or scalar");
42 endif 49 endif
43 50
44 X = R .* cos (Phi) .* cos (Theta); 51 endfunction
45 Y = R .* cos (Phi) .* sin (Theta);
46 Z = R .* sin (Phi);
47 52
48 endfunction 53 %!test
54 %! t = [0, 0, 0];
55 %! p = [0, 0, 0];
56 %! r = [0, 1, 2];
57 %! [x, y, z] = sph2cart (t, p, r);
58 %! assert (x, r);
59 %! assert (y, [0, 0, 0]);
60 %! assert (z, [0, 0, 0]);
61
62 %!test
63 %! t = 0;
64 %! p = [0, 0, 0];
65 %! r = [0, 1, 2];
66 %! [x, y, z] = sph2cart (t, p, r);
67 %! assert (x, r);
68 %! assert (y, [0, 0, 0]);
69 %! assert (z, [0, 0, 0]);
70
71 %!test
72 %! t = [0, 0, 0];
73 %! p = 0;
74 %! r = [0, 1, 2];
75 %! [x, y, z] = sph2cart (t, p, r);
76 %! assert (x, r);
77 %! assert (y, [0, 0, 0]);
78 %! assert (z, [0, 0, 0]);
79
80 %!test
81 %! t = [0, 0.5, 1]*pi;
82 %! p = [0, 0, 0];
83 %! r = 1;
84 %! [x, y, z] = sph2cart (t, p, r);
85 %! assert (x, [1, 0, -1], eps);
86 %! assert (y, [0, 1, 0], eps);
87 %! assert (z, [0, 0, 0], eps);
88