changeset 29917:09b36aa7088a stable

interp1.m: Don't interpret later numeric input as "xi" (bug #60967). * scripts/general/interp1.m: Fix input check to potentially interpret only the first optional argument as "xi". Add tests that have numeric "extrap" value.
author Markus Mützel <markus.muetzel@gmx.de>
date Sun, 25 Jul 2021 20:17:08 +0200
parents 0f41e117789f
children 8c7685d70cf3 a3ac00db296b
files scripts/general/interp1.m
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/interp1.m	Thu Jul 22 13:52:49 2021 -0400
+++ b/scripts/general/interp1.m	Sun Jul 25 20:17:08 2021 +0200
@@ -126,12 +126,12 @@
   extrap = NA;
   xi = [];
   ispp = false;
-  firstnumeric = true;
+  have_xi = false;
   rightcontinuous = NaN;
 
   if (nargin > 2)
-    for i = 1:length (varargin)
-      arg = varargin{i};
+    for i_arg = 1:length (varargin)
+      arg = varargin{i_arg};
       if (ischar (arg))
         arg = tolower (arg);
         switch (arg)
@@ -147,9 +147,9 @@
             method = arg;
         endswitch
       else
-        if (firstnumeric)
+        if (i_arg == 1)
           xi = arg;
-          firstnumeric = false;
+          have_xi = true;
         else
           extrap = arg;
         endif
@@ -157,7 +157,7 @@
     endfor
   endif
 
-  if (isempty (xi) && firstnumeric && ! ispp)
+  if (! have_xi && ! ispp)
     xi = y;
     y = x;
     if (isvector (y))
@@ -524,6 +524,8 @@
 %!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 
@@ -561,6 +563,8 @@
 #%!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 
@@ -596,6 +600,8 @@
 #%!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 
@@ -631,6 +637,8 @@
 %!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!assert (interp1 ([1 2 2 3], [1 2 3 4], 2), 3)
 %!assert (interp1 ([3 2 2 1], [4 3 2 1], 2), 2)
 %!error interp1 (1,1,1, style)
@@ -668,6 +676,8 @@
 %!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),100*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 
@@ -703,6 +713,8 @@
 %!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 
@@ -738,6 +750,8 @@
 %!        interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
 %!        interp1 (xp,yp,xi,style,"extrap"),10*eps)
+%!assert (interp1 (yp, xi, style, 0), ...
+%!        interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
 %!error interp1 (1,1,1, style)
 ## ENDBLOCK
 ## ENDBLOCKTEST