Mercurial > hg > octave-terminal
changeset 13151:e173fda06fca stable
Fix bug #30295.
* scripts/general/interp3.m: Improve input checks, and add tests.
* scripts/general/interpn.m: Ditto. Grid the implicit inputs.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 17 Sep 2011 21:43:01 -0400 |
parents | 621108cf81d1 |
children | 8d5f0b41e6b0 |
files | scripts/general/interp3.m scripts/general/interpn.m |
diffstat | 2 files changed, 47 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/interp3.m +++ b/scripts/general/interp3.m @@ -69,14 +69,14 @@ extrapval = NA; nargs = nargin; - if (nargin < 1) + if (nargin < 1 || ! isnumeric (varargin{1})) print_usage (); endif if (ischar (varargin{end})) method = varargin{end}; nargs = nargs - 1; - elseif (ischar (varargin{end-1})) + elseif (nargs > 1 && ischar (varargin{end - 1})) if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) error ("interp3: extrapal is expected to be a numeric scalar"); endif @@ -146,3 +146,21 @@ %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); %! vi2 = interpn(x, y, z, v, xxi, yyi, zzi); %! assert (vi, vi2); + +%!shared z, zout, tol +%! z = zeros (3, 3, 3); +%! zout = zeros (5, 5, 5); +%! z(:,:,1) = [1 3 5; 3 5 7; 5 7 9]; +%! z(:,:,2) = z(:,:,1) + 2; +%! z(:,:,3) = z(:,:,2) + 2; +%! for n = 1:5 +%! zout(:,:,n) = [1 2 3 4 5; +%! 2 3 4 5 6; +%! 3 4 5 6 7; +%! 4 5 6 7 8; +%! 5 6 7 8 9] + (n-1); +%! end +%! tol = 10 * eps; +%!assert (interp3 (z), zout, tol) +%!assert (interp3 (z, "linear"), zout, tol) +%!assert (interp3 (z, "spline"), zout, tol)
--- a/scripts/general/interpn.m +++ b/scripts/general/interpn.m @@ -70,14 +70,14 @@ extrapval = NA; nargs = nargin; - if (nargin < 1) + if (nargin < 1 || ! isnumeric (varargin{1})) print_usage (); endif if (ischar (varargin{end})) method = varargin{end}; nargs = nargs - 1; - elseif (ischar (varargin{end - 1})) + elseif (nargs > 1 && ischar (varargin{end - 1})) if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) error ("interpn: extrapal is expected to be a numeric scalar"); endif @@ -90,9 +90,12 @@ v = varargin{1}; m = 1; if (nargs == 2) - m = varargin{2}; - if (! isnumeric (m) || ! isscalar (m) || floor (m) != m) - error ("interpn: M is expected to be a integer scalar"); + if (ischar (varargin{2})) + method = varargin{2}; + elseif (isnumeric (m) && isscalar (m) && round (m) == m) + m = varargin{2}; + else + print_usage (); endif endif sz = size (v); @@ -103,6 +106,8 @@ x{i} = 1 : sz(i); y{i} = 1 : (1 / (2 ^ m)) : sz(i); endfor + y{1} = y{1}.'; + [y{:}] = ndgrid (y{:}); elseif (! isvector (varargin{1}) && nargs == (ndims (varargin{1}) + 1)) v = varargin{1}; sz = size (v); @@ -290,3 +295,20 @@ %! X = meshgrid (1:4); %! assert (interpn (X, 2.5, 2.5, 'nearest'), 3); +%!shared z, zout, tol +%! z = zeros (3, 3, 3); +%! zout = zeros (5, 5, 5); +%! z(:,:,1) = [1 3 5; 3 5 7; 5 7 9]; +%! z(:,:,2) = z(:,:,1) + 2; +%! z(:,:,3) = z(:,:,2) + 2; +%! for n = 1:5 +%! zout(:,:,n) = [1 2 3 4 5; +%! 2 3 4 5 6; +%! 3 4 5 6 7; +%! 4 5 6 7 8; +%! 5 6 7 8 9] + (n-1); +%! end +%! tol = 10 * eps; +%!assert (interpn (z), zout, tol) +%!assert (interpn (z, "linear"), zout, tol) +%!assert (interpn (z, "spline"), zout, tol)