changeset 22432:1e88747417e6

Allow mixed scalar/array inputs to cart2pol, cart2sph, pol2cart, sph2cart (bug #48950). * cart2pol.m: Use common_size() to simplify input validation and bring inputs, including scalars, to a common size. Don't concatenate z values into output if z is empty. Update BIST tests. * cart2sph.m: Use common_size() to simplify input validation and bring inputs, including scalars, to a common size. Add new BIST test. * pol2cart.m: Use common_size() to simplify input validation and bring inputs, including scalars, to a common size. Don't concatenate z values into output if z is empty. Update BIST tests. * sph2cart.m: Use common_size() to simplify input validation and bring inputs, including scalars, to a common size. Add new BIST test.
author Rik <rik@octave.org>
date Fri, 02 Sep 2016 12:44:17 -0700
parents 475b4cd1b789
children 4c7a66badaa1
files scripts/general/cart2pol.m scripts/general/cart2sph.m scripts/general/pol2cart.m scripts/general/sph2cart.m
diffstat 4 files changed, 66 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/cart2pol.m	Fri Sep 02 09:55:07 2016 -0700
+++ b/scripts/general/cart2pol.m	Fri Sep 02 12:44:17 2016 -0700
@@ -59,15 +59,19 @@
     y = x(:,2);
     x = x(:,1);
   elseif (nargin == 2)
-    if (! ((isnumeric (x) && isnumeric (y))
-            && (size_equal (x, y) || isscalar (x) || isscalar (y))))
+    if (! isnumeric (x) || ! isnumeric (y))
+      error ("cart2pol: X, Y must be numeric arrays of the same size, or scalar");
+    endif
+    [err, x, y] = common_size (x, y);
+    if (err)
       error ("cart2pol: X, Y must be numeric arrays of the same size, or scalar");
     endif
   elseif (nargin == 3)
-    if (! ((isnumeric (x) && isnumeric (y) && isnumeric (z))
-            && (size_equal (x, y) || isscalar (x) || isscalar (y))
-            && (size_equal (x, z) || isscalar (x) || isscalar (z))
-            && (size_equal (y, z) || isscalar (y) || isscalar (z))))
+    if (! isnumeric (x) || ! isnumeric (y) || ! isnumeric (z))
+      error ("cart2pol: X, Y, Z must be numeric arrays of the same size, or scalar");
+    endif
+    [err, x, y, z] = common_size (x, y, z);
+    if (err)
       error ("cart2pol: X, Y, Z must be numeric arrays of the same size, or scalar");
     endif
   endif
@@ -76,7 +80,11 @@
   r = sqrt (x .^ 2 + y .^ 2);
 
   if (nargout <= 1)
-    theta = [theta(:), r(:), z(:)];
+    if (isempty (z))
+      theta = [theta(:), r(:)];
+    else
+      theta = [theta(:), r(:), z(:)];
+    endif
   endif
 
 endfunction
@@ -103,7 +111,7 @@
 %! [t, r, z2] = cart2pol (x, y, z);
 %! assert (t, [0, pi/4, pi/4], sqrt (eps));
 %! assert (r, sqrt (2)*[0, 1, 2], sqrt (eps));
-%! assert (z, z2);
+%! assert (z2, z);
 
 %!test
 %! x = [0, 1, 2];
@@ -112,7 +120,7 @@
 %! [t, r, z2] = cart2pol (x, y, z);
 %! assert (t, [0, 0, 0], eps);
 %! assert (r, x, eps);
-%! assert (z, z2);
+%! assert (z2, [0, 0, 0]);
 
 %!test
 %! x = 0;
@@ -121,16 +129,16 @@
 %! [t, r, z2] = cart2pol (x, y, z);
 %! assert (t, [0, 1, 1]*pi/2, eps);
 %! assert (r, y, eps);
-%! assert (z, z2);
+%! assert (z2, [0, 0, 0]);
 
 %!test
 %! x = 0;
 %! y = 0;
 %! z = [0, 1, 2];
 %! [t, r, z2] = cart2pol (x, y, z);
-%! assert (t, 0);
-%! assert (r, 0);
-%! assert (z, z2);
+%! assert (t, [0, 0, 0]);
+%! assert (r, [0, 0, 0]);
+%! assert (z2, z);
 
 %!test
 %! C = [0, 0; 1, 1; 2, 2];
--- a/scripts/general/cart2sph.m	Fri Sep 02 09:55:07 2016 -0700
+++ b/scripts/general/cart2sph.m	Fri Sep 02 12:44:17 2016 -0700
@@ -55,10 +55,11 @@
     y = x(:,2);
     x = x(:,1);
   else
-    if (! ((isnumeric (x) && isnumeric (y) && isnumeric (z))
-            && (size_equal (x, y) || isscalar (x) || isscalar (y))
-            && (size_equal (x, z) || isscalar (x) || isscalar (z))
-            && (size_equal (y, z) || isscalar (y) || isscalar (z))))
+    if (! isnumeric (x) || ! isnumeric (y) || ! isnumeric (z))
+      error ("cart2sph: X, Y, Z must be numeric arrays of the same size, or scalar");
+    endif
+    [err, x, y, z] = common_size (x, y, z);
+    if (err)
       error ("cart2sph: X, Y, Z must be numeric arrays of the same size, or scalar");
     endif
   endif
@@ -111,6 +112,15 @@
 %! assert (r, [0, 1, 2] * sqrt (2));
 
 %!test
+%! x = 0;
+%! y = 0;
+%! z = [0, 1, 2];
+%! [t, p, r] = cart2sph (x, y, z);
+%! assert (t, [0, 0, 0]);
+%! assert (p, [0, 1, 1] * pi/2);
+%! assert (r, [0, 1, 2]);
+
+%!test
 %! C = [0, 0, 0; 1, 0, 1; 2, 0, 2];
 %! S = [0, 0, 0; 0, pi/4, sqrt(2); 0, pi/4, 2*sqrt(2)];
 %! assert (cart2sph (C), S, eps);
--- a/scripts/general/pol2cart.m	Fri Sep 02 09:55:07 2016 -0700
+++ b/scripts/general/pol2cart.m	Fri Sep 02 12:44:17 2016 -0700
@@ -59,15 +59,19 @@
     r = theta(:,2);
     theta = theta(:,1);
   elseif (nargin == 2)
-    if (! ((isnumeric (theta) && isnumeric (r))
-            && (size_equal (theta, r) || isscalar (theta) || isscalar (r))))
+    if (! isnumeric (theta) || ! isnumeric (r))
+      error ("pol2cart: THETA, R must be numeric arrays of the same size, or scalar");
+    endif
+    [err, theta, r] = common_size (theta, r);
+    if (err)
       error ("pol2cart: THETA, R must be numeric arrays of the same size, or scalar");
     endif
   elseif (nargin == 3)
-    if (! ((isnumeric (theta) && isnumeric (r) && isnumeric (z))
-            && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
-            && (size_equal (theta, z) || isscalar (theta) || isscalar (z))
-            && (size_equal (r, z) || isscalar (r) || isscalar (z))))
+    if (! isnumeric (theta) || ! isnumeric (r) || ! isnumeric (z))
+      error ("pol2cart: THETA, R, Z must be numeric arrays of the same size, or scalar");
+    endif
+    [err, theta, r, z] = common_size (theta, r, z);
+    if (err)
       error ("pol2cart: THETA, R, Z must be numeric arrays of the same size, or scalar");
     endif
   endif
@@ -76,7 +80,11 @@
   y = r .* sin (theta);
 
   if (nargout <= 1)
-    x = [x(:), y(:), z(:)];
+    if (isempty (z))
+      x = [x(:), y(:)];
+    else
+      x = [x(:), y(:), z(:)];
+    endif
   endif
 
 endfunction
@@ -103,7 +111,7 @@
 %! [x, y, z2] = pol2cart (t, r, z);
 %! assert (x, [0, 1, 2], sqrt (eps));
 %! assert (y, [0, 1, 2], sqrt (eps));
-%! assert (z, z2);
+%! assert (z2, z);
 
 %!test
 %! t = 0;
@@ -112,7 +120,7 @@
 %! [x, y, z2] = pol2cart (t, r, z);
 %! assert (x, [0, 1, 2], sqrt (eps));
 %! assert (y, [0, 0, 0], sqrt (eps));
-%! assert (z, z2);
+%! assert (z2, z);
 
 %!test
 %! t = [1, 1, 1]*pi/4;
@@ -121,7 +129,7 @@
 %! [x, y, z2] = pol2cart (t, r, z);
 %! assert (x, [1, 1, 1] / sqrt (2), eps);
 %! assert (y, [1, 1, 1] / sqrt (2), eps);
-%! assert (z, z2);
+%! assert (z2, z);
 
 %!test
 %! t = 0;
@@ -130,7 +138,7 @@
 %! [x, y, z2] = pol2cart (t, r, z);
 %! assert (x, [1, 2, 3], eps);
 %! assert (y, [0, 0, 0] / sqrt (2), eps);
-%! assert (z, z2);
+%! assert (z2, [1, 1, 1]);
 
 %!test
 %! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)];
@@ -182,3 +190,4 @@
 %!error <numeric arrays of the same size> pol2cart ([1,2,3], [1,2,3], {1,2,3})
 %!error <numeric arrays of the same size> pol2cart (ones (3,3,3), 1, ones (3,2,3))
 %!error <numeric arrays of the same size> pol2cart (ones (3,3,3), ones (3,2,3), 1)
+
--- a/scripts/general/sph2cart.m	Fri Sep 02 09:55:07 2016 -0700
+++ b/scripts/general/sph2cart.m	Fri Sep 02 12:44:17 2016 -0700
@@ -55,10 +55,11 @@
     phi = theta(:,2);
     theta = theta(:,1);
   else
-    if (! ((isnumeric (theta) && isnumeric (phi) && isnumeric (r))
-            && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi))
-            && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
-            && (size_equal (phi, r) || isscalar (phi) || isscalar (r))))
+    if (! isnumeric (theta) || ! isnumeric (phi) || ! isnumeric (r))
+      error ("sph2cart: THETA, PHI, R must be numeric arrays of the same size, or scalar");
+    endif
+    [err, theta, phi, r] = common_size (theta, phi, r);
+    if (err)
       error ("sph2cart: THETA, PHI, R must be numeric arrays of the same size, or scalar");
     endif
   endif
@@ -111,6 +112,12 @@
 %! assert (z, [0, 0, 0], eps);
 
 %!test
+%! [x, y, z] = sph2cart ([0 0 0], 0, 1);
+%! assert (x, [1, 1, 1], eps);
+%! assert (y, [0, 0, 0], eps);
+%! assert (z, [0, 0, 0], eps);
+
+%!test
 %! S = [ 0, 0, 1; 0.5*pi, 0, 1; pi, 0, 1];
 %! C = [ 1, 0, 0; 0, 1, 0; -1, 0, 0];
 %! assert (sph2cart (S), C, eps);