changeset 17585:36b9fa789d8e

Overhaul polar, spherical, cartesian conversion routiens when nargout == 1. * scripts/general/cart2pol.m: Use @var{P} (capital) in docstring. Initialize z to []. Return 3-column matrix when nargout == 1. Modify %!test to check this behavior. * scripts/general/cart2sph.m: Use @var{C} (capital) in docstring. Return 3-column matrix when nargout == 1. Modify %!test to check this behavior. scripts/general/pol2cart.m: Use @var{P} (capital) in docstring. Use 'C' in %!test to refer to single Cartesian matrix of coordinates. scripts/general/sph2cart.m: Use @var{C} (capital) in docstring. Return 3-column matrix when nargout == 1. Modify %!test to check this behavior.
author Rik <rik@octave.org>
date Sun, 06 Oct 2013 21:01:10 -0700
parents 6278085742b7
children 8334144458a4
files scripts/general/cart2pol.m scripts/general/cart2sph.m scripts/general/pol2cart.m scripts/general/sph2cart.m
diffstat 4 files changed, 31 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/cart2pol.m	Sun Oct 06 20:35:08 2013 -0700
+++ b/scripts/general/cart2pol.m	Sun Oct 06 21:01:10 2013 -0700
@@ -19,20 +19,20 @@
 ## -*- texinfo -*-
 ## @deftypefn  {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{x}, @var{y})
 ## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{x}, @var{y}, @var{z})
-## @deftypefnx {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{c})
-## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{c})
-## @deftypefnx {Function File} {@var{p} =} cart2pol (@dots{})
+## @deftypefnx {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{C})
+## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{C})
+## @deftypefnx {Function File} {@var{P} =} cart2pol (@dots{})
 ##
 ## Transform Cartesian to polar or cylindrical coordinates.
 ##
 ## @var{theta} describes the angle relative to the positive x-axis.
 ## @var{r} is the distance to the z-axis @w{(0, 0, z)}.
-## @var{x}, @var{y} (and @var{z}) must be the same shape, or scalar.
-## If called with a single matrix argument then each row of @var{c}
+## @var{x}, @var{y} (, and @var{z}) must be the same shape, or scalar.
+## If called with a single matrix argument then each row of @var{C}
 ## represents the Cartesian coordinate (@var{x}, @var{y} (, @var{z})).
 ##
 ## If only a single return argument is requested then return a matrix
-## @var{p} where each row represents one polar/(cylindrical) coordinate
+## @var{P} where each row represents one polar/(cylindrical) coordinate
 ## (@var{theta}, @var{phi} (, @var{z})).
 ## @seealso{pol2cart, cart2sph, sph2cart}
 ## @end deftypefn
@@ -40,7 +40,7 @@
 ## Author: Kai Habel <kai.habel@gmx.de>
 ## Adapted-by: jwe
 
-function [theta, r, z] = cart2pol (x, y, z)
+function [theta, r, z] = cart2pol (x, y, z = [])
 
   if (nargin < 1 || nargin > 3)
     print_usage ();
@@ -50,8 +50,6 @@
     if (ismatrix (x) && (columns (x) == 2 || columns (x) == 3))
       if (columns (x) == 3)
         z = x(:,3);
-      else
-        z = [];
       endif
       y = x(:,2);
       x = x(:,1);
@@ -76,7 +74,7 @@
   r = sqrt (x .^ 2 + y .^ 2);
 
   if (nargout <= 1)
-    theta = [theta, r, z];
+    theta = [theta(:), r(:), z(:)];
   endif
 
 endfunction
@@ -92,9 +90,9 @@
 %!test
 %! x = [0, 1, 2];
 %! y = [0, 1, 2];
-%! [t, r] = cart2pol (x, y);
-%! assert (t, [0, pi/4, pi/4], sqrt (eps));
-%! assert (r, sqrt (2)*[0, 1, 2], sqrt (eps));
+%! P = cart2pol (x, y);
+%! assert (P(:,1), [0; pi/4; pi/4], sqrt (eps));
+%! assert (P(:,2), sqrt (2)*[0; 1; 2], sqrt (eps));
 
 %!test
 %! x = [0, 1, 2];
--- a/scripts/general/cart2sph.m	Sun Oct 06 20:35:08 2013 -0700
+++ b/scripts/general/cart2sph.m	Sun Oct 06 21:01:10 2013 -0700
@@ -26,11 +26,11 @@
 ## @var{phi} is the angle relative to the xy-plane.
 ## @var{r} is the distance to the origin @w{(0, 0, 0)}.
 ## @var{x}, @var{y}, and @var{z} must be the same shape, or scalar.
-## If called with a single matrix argument then each row of @var{c}
+## If called with a single matrix argument then each row of @var{C}
 ## represents the Cartesian coordinate (@var{x}, @var{y}, @var{z}).
 ##
 ## If only a single return argument is requested then return a matrix
-## @var{s} where each row represents one spherical coordinate
+## @var{S} where each row represents one spherical coordinate
 ## (@var{theta}, @var{phi}, @var{r}).
 ## @seealso{sph2cart, cart2pol, pol2cart}
 ## @end deftypefn
@@ -66,7 +66,7 @@
   r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
 
   if (nargout <= 1)
-    theta = [theta, phi, r];
+    theta = [theta(:), phi(:), r(:)];
   endif
 
 endfunction
@@ -85,10 +85,10 @@
 %! x = 0;
 %! y = [0, 1, 2];
 %! z = [0, 1, 2];
-%! [t, p, r] = cart2sph (x, y, z);
-%! assert (t, [0, 1, 1] * pi/2, eps);
-%! assert (p, [0, 1, 1] * pi/4, eps);
-%! assert (r, [0, 1, 2] * sqrt (2), eps);
+%! S = cart2sph (x, y, z);
+%! assert (S(:,1), [0; 1; 1] * pi/2, eps);
+%! assert (S(:,2), [0; 1; 1] * pi/4, eps);
+%! assert (S(:,3), [0; 1; 2] * sqrt (2), eps);
 
 %!test
 %! x = [0, 1, 2];
--- a/scripts/general/pol2cart.m	Sun Oct 06 20:35:08 2013 -0700
+++ b/scripts/general/pol2cart.m	Sun Oct 06 21:01:10 2013 -0700
@@ -19,15 +19,15 @@
 ## -*- texinfo -*-
 ## @deftypefn  {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{theta}, @var{r})
 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{theta}, @var{r}, @var{z})
-## @deftypefnx {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{p})
-## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{p})
+## @deftypefnx {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{P})
+## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{P})
 ## @deftypefnx {Function File} {@var{C} =} pol2cart (@dots{})
 ## Transform polar or cylindrical to Cartesian coordinates.
 ##
 ## @var{theta}, @var{r}, (and @var{z}) must be the same shape, or scalar.
 ## @var{theta} describes the angle relative to the positive x-axis.
 ## @var{r} is the distance to the z-axis (0, 0, z).
-## If called with a single matrix argument then each row of @var{p}
+## If called with a single matrix argument then each row of @var{P}
 ## represents the polar/(cylindrical) coordinate (@var{theta}, @var{r} (,
 ## @var{z})).
 ##
@@ -90,9 +90,9 @@
 %!test
 %! t = [0, 1, 1] * pi/4;
 %! r = sqrt (2) * [0, 1, 2];
-%! cart = pol2cart (t, r);
-%! assert (cart(:,1), [0; 1; 2], sqrt (eps));
-%! assert (cart(:,2), [0; 1; 2], sqrt (eps));
+%! C = pol2cart (t, r);
+%! assert (C(:,1), [0; 1; 2], sqrt (eps));
+%! assert (C(:,2), [0; 1; 2], sqrt (eps));
 
 %!test
 %! t = [0, 1, 1] * pi/4;
--- a/scripts/general/sph2cart.m	Sun Oct 06 20:35:08 2013 -0700
+++ b/scripts/general/sph2cart.m	Sun Oct 06 21:01:10 2013 -0700
@@ -19,14 +19,14 @@
 ## -*- texinfo -*-
 ## @deftypefn  {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r})
 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{S})
-## @deftypefnx {Function File} {C =} sph2cart (@dots{})
+## @deftypefnx {Function File} {@var{C} =} sph2cart (@dots{})
 ## Transform spherical to Cartesian coordinates.
 ##
 ## @var{theta} describes the angle relative to the positive x-axis.
 ## @var{phi} is the angle relative to the xy-plane.
 ## @var{r} is the distance to the origin @w{(0, 0, 0)}.
 ## @var{theta}, @var{phi}, and @var{r} must be the same shape, or scalar.
-## If called with a single matrix argument then each row of @var{s}
+## If called with a single matrix argument then each row of @var{S}
 ## represents the spherical coordinate (@var{theta}, @var{phi}, @var{r}).
 ##
 ## If only a single return argument is requested then return a matrix
@@ -66,7 +66,7 @@
   z = r .* sin (phi);
 
   if (nargout <= 1)
-    x = [x, y, z];
+    x = [x(:), y(:), z(:)];
   endif
 
 endfunction
@@ -85,10 +85,10 @@
 %! t = 0;
 %! p = [0, 0, 0];
 %! r = [0, 1, 2];
-%! [x, y, z] = sph2cart (t, p, r);
-%! assert (x, r);
-%! assert (y, [0, 0, 0]);
-%! assert (z, [0, 0, 0]);
+%! C = sph2cart (t, p, r);
+%! assert (C(:,1), r(:));
+%! assert (C(:,2), [0; 0; 0]);
+%! assert (C(:,3), [0; 0; 0]);
 
 %!test
 %! t = [0, 0, 0];