Mercurial > hg > octave-terminal
changeset 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 | 6622772a0add |
files | scripts/ChangeLog scripts/general/cart2pol.m scripts/general/cart2sph.m scripts/general/pol2cart.m scripts/general/sph2cart.m |
diffstat | 5 files changed, 191 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2010-06-03 Rik <octave@nomad.inbox5.com> + + * general/cart2pol.m, general/cart2sph.m, general/pol2cart.m, + general/sph2cart.m: Add option to operate on column matrix of + coordinates. + 2010-06-03 Rik <octave@nomad.inbox5.com> * general/arrayfun.m, general/cart2pol.m, general/cart2sph.m,
--- a/scripts/general/cart2pol.m +++ b/scripts/general/cart2pol.m @@ -19,10 +19,21 @@ ## -*- 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{}) +## ## Transform Cartesian to polar or cylindrical coordinates. -## @var{x}, @var{y} (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 @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} +## 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{theta}, @var{phi} (, @var{z})). ## @seealso{pol2cart, cart2sph, sph2cart} ## @end deftypefn @@ -31,24 +42,41 @@ function [theta, r, z] = cart2pol (x, y, z) - if (nargin < 2 || nargin > 3) - error ("cart2pol: number of arguments must be 2 or 3"); - endif - - if (nargin == 2 && nargout > 2) - error ("cart2pol: number of output arguments must not be greater than number of input arguments"); + if (nargin < 1 || nargin > 3) + print_usage (); endif - if ((ismatrix (x) && ismatrix (y) && (nargin == 2 || ismatrix (z))) - && (size_equal (x, y) || isscalar (x) || isscalar (y)) - && (nargin == 2 || size_equal (x, z) || isscalar (x) || isscalar (z)) - && (nargin == 2 || size_equal (y, z) || isscalar (y) || isscalar (z))) - - theta = atan2 (y, x); - r = sqrt (x .^ 2 + y .^ 2); + if (nargin == 1) + 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); + else + error ("cart2pol: matrix input must have 2 or 3 columns [X, Y (, Z)]"); + endif + elseif (nargin == 2) + if (! ((ismatrix (x) && ismatrix (y)) + && (size_equal (x, y) || isscalar (x) || isscalar (y)))) + error ("cart2pol: arguments must be matrices of same size, or scalar"); + endif + elseif (nargin == 3) + if (! ((ismatrix (x) && ismatrix (y) && ismatrix (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)))) + error ("cart2pol: arguments must be matrices of same size, or scalar"); + endif + endif - else - error ("cart2pol: arguments must be matrices of same size, or scalar"); + theta = atan2 (y, x); + r = sqrt (x .^ 2 + y .^ 2); + + if (nargout <= 1) + theta = [theta, r, z]; endif endfunction @@ -103,3 +131,13 @@ %! assert (r, 0); %! assert (z, z2); +%!test +%! C = [0, 0; 1, 1; 2, 2]; +%! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)]; +%! assert (cart2pol (C), P, sqrt(eps)); + +%!test +%! C = [0, 0, 0; 1, 1, 1; 2, 2, 2]; +%! P = [0, 0, 0; pi/4, sqrt(2), 1; pi/4, 2*sqrt(2), 2]; +%! assert (cart2pol (C), P, sqrt(eps)); +
--- a/scripts/general/cart2sph.m +++ b/scripts/general/cart2sph.m @@ -17,13 +17,22 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z}) +## @deftypefn {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z}) +## @deftypefnx {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{C}) +## @deftypefnx {Function File} {@var{S} =} cart2sph (@dots{}) ## Transform Cartesian to spherical coordinates. -## @var{x}, @var{y} and @var{z} must be the same shape, or scalar. +## ## @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)}. -## @seealso{pol2cart, cart2pol, sph2cart} +## @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{s} where each row represents one spherical coordinate +## (@var{theta}, @var{phi}, @var{r}). +## @seealso{sph2cart, cart2pol, pol2cart} ## @end deftypefn ## Author: Kai Habel <kai.habel@gmx.de> @@ -31,21 +40,33 @@ function [theta, phi, r] = cart2sph (x, y, z) - if (nargin != 3) + if (nargin != 1 && nargin != 3) print_usage (); endif - if ((ismatrix (x) && ismatrix (y) && ismatrix (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 (nargin == 1) + if (ismatrix (x) && columns (x) == 3) + z = x(:,3); + y = x(:,2); + x = x(:,1); + else + error ("cart2sph: matrix input must have 3 columns [X, Y, Z]"); + endif + elseif (nargin == 3) + if (! ((ismatrix (x) && ismatrix (y) && ismatrix (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)))) + error ("cart2sph: X, Y, Z must be matrices of the same size, or scalar"); + endif + endif - theta = atan2 (y, x); - phi = atan2 (z, sqrt (x .^ 2 + y .^ 2)); - r = sqrt (x .^ 2 + y .^ 2 + z .^ 2); + theta = atan2 (y, x); + phi = atan2 (z, sqrt (x .^ 2 + y .^ 2)); + r = sqrt (x .^ 2 + y .^ 2 + z .^ 2); - else - error ("cart2sph: arguments must be matrices of same size, or scalar"); + if (nargout <= 1) + theta = [theta, phi, r]; endif endfunction @@ -86,3 +107,7 @@ %! assert (p, [0, 0, 0]); %! assert (r, [0, 1, 2] * sqrt(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 +++ b/scripts/general/pol2cart.m @@ -17,13 +17,23 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{x}, @var{y}] =} pol2cart (@var{theta}, @var{r}) +## @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{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}, @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). -## @seealso{cart2pol, cart2sph, sph2cart} +## If called with a single matrix argument then each row of @var{p} +## represents the polar/(cylindrical) coordinate (@var{x}, @var{y} (, @var{z})). +## +## If only a single return argument is requested then return a matrix +## @var{C} where each row represents one Cartesian coordinate +## (@var{x}, @var{y} (, @var{z})). +## @seealso{cart2pol, sph2cart, cart2sph} ## @end deftypefn ## Author: Kai Habel <kai.habel@gmx.de> @@ -31,24 +41,41 @@ function [x, y, z] = pol2cart (theta, r, z) - if (nargin < 2 || nargin > 3) - error ("pol2cart: number of arguments must be 2 or 3"); - endif - - if (nargin == 2 && nargout > 2) - error ("pol2cart: number of output arguments must not be greater than number of input arguments"); + if (nargin < 1 || nargin > 3) + print_usage (); endif - if ((ismatrix (theta) && ismatrix (r) && (nargin == 2 || ismatrix (z))) - && (size_equal (theta, r) || isscalar (theta) || isscalar (r)) - && (nargin == 2 || size_equal (theta, z) || isscalar (theta) || isscalar (z)) - && (nargin == 2 || size_equal (r, z) || isscalar (r) || isscalar (z))) + if (nargin == 1) + if (ismatrix (theta) && (columns (theta) == 2 || columns (theta) == 3)) + if (columns (theta) == 3) + z = theta(:,3); + else + z = []; + endif + r = theta(:,2); + theta = theta(:,1); + else + error ("pol2car: matrix input must have 2 or 3 columns [THETA, R (, Z)]"); + endif + elseif (nargin == 2) + if (! ((ismatrix (theta) && ismatrix (r)) + && (size_equal (theta, r) || isscalar (theta) || isscalar (r)))) + error ("pol2cart: arguments must be matrices of same size, or scalar"); + endif + elseif (nargin == 3) + if (! ((ismatrix (theta) && ismatrix (r) && ismatrix (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)))) + error ("pol2cart: arguments must be matrices of same size, or scalar"); + endif + endif - x = cos (theta) .* r; - y = sin (theta) .* r; + x = r .* cos (theta); + y = r .* sin (theta); - else - error ("pol2cart: arguments must be matrices of same size, or scalar"); + if (nargout <= 1) + x = [x, y, z]; endif endfunction @@ -103,3 +130,13 @@ %! assert (y, [0, 0, 0] / sqrt(2), eps); %! assert (z, z2); +%!test +%! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)]; +%! C = [0, 0; 1, 1; 2, 2]; +%! assert (pol2cart(P), C, sqrt(eps)); + +%!test +%! P = [0, 0, 0; pi/4, sqrt(2), 1; pi/4, 2*sqrt(2), 2]; +%! C = [0, 0, 0; 1, 1, 1; 2, 2, 2]; +%! assert (pol2cart(P), C, sqrt(eps)); +
--- a/scripts/general/sph2cart.m +++ b/scripts/general/sph2cart.m @@ -17,13 +17,22 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r}) +## @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{}) ## Transform spherical to Cartesian coordinates. -## @var{x}, @var{y} and @var{z} must be the same shape, or scalar. +## ## @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)}. -## @seealso{pol2cart, cart2pol, cart2sph} +## @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} +## represents the spherical coordinate (@var{theta}, @var{phi}, @var{r}). +## +## If only a single return argument is requested then return a matrix +## @var{C} where each row represents one Cartesian coordinate +## (@var{x}, @var{y}, @var{z}). +## @seealso{cart2sph, pol2cart, cart2pol} ## @end deftypefn ## Author: Kai Habel <kai.habel@gmx.de> @@ -31,21 +40,33 @@ function [x, y, z] = sph2cart (theta, phi, r) - if (nargin != 3) + if (nargin != 1 && nargin != 3) print_usage (); endif - if ((ismatrix (theta) && ismatrix (phi) && ismatrix (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 (nargin == 1) + if (ismatrix (theta) && columns (theta) == 3) + r = theta(:,3); + phi = theta(:,2); + theta = theta(:,1); + else + error ("sph2cart: matrix input must have 3 columns [THETA, PHI, R]"); + endif + elseif (nargin == 3) + if (! ((ismatrix (theta) && ismatrix (phi) && ismatrix (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)))) + error ("sph2cart: THETA, PHI, and R must be matrices of the same size, or scalar"); + endif + endif - x = r .* cos (phi) .* cos (theta); - y = r .* cos (phi) .* sin (theta); - z = r .* sin (phi); + x = r .* cos (phi) .* cos (theta); + y = r .* cos (phi) .* sin (theta); + z = r .* sin (phi); - else - error ("sph2cart: arguments must be matrices of same size, or scalar"); + if (nargout <= 1) + x = [x, y, z]; endif endfunction @@ -86,3 +107,8 @@ %! assert (y, [0, 1, 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); +