Mercurial > octave-nkf
comparison scripts/general/pol2cart.m @ 17584:6278085742b7
pol2cart.m: Fix bug with z-input and only 1 output (bug #40184)
* scripts/general/pol2cart.m: Correct docstring which had copy/paste
error from cart2pol. Initialize z matrix to []. Return 3-column
matrix (x,y,z) when only one outptu argument is requested. Change
%!test to catch this behavior.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 06 Oct 2013 20:35:08 -0700 |
parents | e424cdb2ef96 |
children | 36b9fa789d8e |
comparison
equal
deleted
inserted
replaced
17583:4cb05034f1c6 | 17584:6278085742b7 |
---|---|
26 ## | 26 ## |
27 ## @var{theta}, @var{r}, (and @var{z}) must be the same shape, or scalar. | 27 ## @var{theta}, @var{r}, (and @var{z}) must be the same shape, or scalar. |
28 ## @var{theta} describes the angle relative to the positive x-axis. | 28 ## @var{theta} describes the angle relative to the positive x-axis. |
29 ## @var{r} is the distance to the z-axis (0, 0, z). | 29 ## @var{r} is the distance to the z-axis (0, 0, z). |
30 ## If called with a single matrix argument then each row of @var{p} | 30 ## If called with a single matrix argument then each row of @var{p} |
31 ## represents the polar/(cylindrical) coordinate (@var{x}, @var{y} (, @var{z})). | 31 ## represents the polar/(cylindrical) coordinate (@var{theta}, @var{r} (, |
32 ## @var{z})). | |
32 ## | 33 ## |
33 ## If only a single return argument is requested then return a matrix | 34 ## If only a single return argument is requested then return a matrix |
34 ## @var{C} where each row represents one Cartesian coordinate | 35 ## @var{C} where each row represents one Cartesian coordinate |
35 ## (@var{x}, @var{y} (, @var{z})). | 36 ## (@var{x}, @var{y} (, @var{z})). |
36 ## @seealso{cart2pol, sph2cart, cart2sph} | 37 ## @seealso{cart2pol, sph2cart, cart2sph} |
37 ## @end deftypefn | 38 ## @end deftypefn |
38 | 39 |
39 ## Author: Kai Habel <kai.habel@gmx.de> | 40 ## Author: Kai Habel <kai.habel@gmx.de> |
40 ## Adapted-by: jwe | 41 ## Adapted-by: jwe |
41 | 42 |
42 function [x, y, z] = pol2cart (theta, r, z) | 43 function [x, y, z] = pol2cart (theta, r, z = []) |
43 | 44 |
44 if (nargin < 1 || nargin > 3) | 45 if (nargin < 1 || nargin > 3) |
45 print_usage (); | 46 print_usage (); |
46 endif | 47 endif |
47 | 48 |
48 if (nargin == 1) | 49 if (nargin == 1) |
49 if (ismatrix (theta) && (columns (theta) == 2 || columns (theta) == 3)) | 50 if (ismatrix (theta) && (columns (theta) == 2 || columns (theta) == 3)) |
50 if (columns (theta) == 3) | 51 if (columns (theta) == 3) |
51 z = theta(:,3); | 52 z = theta(:,3); |
52 else | |
53 z = []; | |
54 endif | 53 endif |
55 r = theta(:,2); | 54 r = theta(:,2); |
56 theta = theta(:,1); | 55 theta = theta(:,1); |
57 else | 56 else |
58 error ("pol2cart: matrix input must have 2 or 3 columns [THETA, R (, Z)]"); | 57 error ("pol2cart: matrix input must have 2 or 3 columns [THETA, R (, Z)]"); |
73 | 72 |
74 x = r .* cos (theta); | 73 x = r .* cos (theta); |
75 y = r .* sin (theta); | 74 y = r .* sin (theta); |
76 | 75 |
77 if (nargout <= 1) | 76 if (nargout <= 1) |
78 x = [x, y, z]; | 77 x = [x(:), y(:), z(:)]; |
79 endif | 78 endif |
80 | 79 |
81 endfunction | 80 endfunction |
82 | 81 |
83 | 82 |
89 %! assert (y, [0, 1, 0], sqrt (eps)); | 88 %! assert (y, [0, 1, 0], sqrt (eps)); |
90 | 89 |
91 %!test | 90 %!test |
92 %! t = [0, 1, 1] * pi/4; | 91 %! t = [0, 1, 1] * pi/4; |
93 %! r = sqrt (2) * [0, 1, 2]; | 92 %! r = sqrt (2) * [0, 1, 2]; |
94 %! [x, y] = pol2cart (t, r); | 93 %! cart = pol2cart (t, r); |
95 %! assert (x, [0, 1, 2], sqrt (eps)); | 94 %! assert (cart(:,1), [0; 1; 2], sqrt (eps)); |
96 %! assert (y, [0, 1, 2], sqrt (eps)); | 95 %! assert (cart(:,2), [0; 1; 2], sqrt (eps)); |
97 | 96 |
98 %!test | 97 %!test |
99 %! t = [0, 1, 1] * pi/4; | 98 %! t = [0, 1, 1] * pi/4; |
100 %! r = sqrt (2) * [0, 1, 2]; | 99 %! r = sqrt (2) * [0, 1, 2]; |
101 %! z = [0, 1, 2]; | 100 %! z = [0, 1, 2]; |