comparison scripts/general/interp1.m @ 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 0a5b15007766
children 8c7685d70cf3
comparison
equal deleted inserted replaced
29910:0f41e117789f 29917:09b36aa7088a
124 124
125 method = "linear"; 125 method = "linear";
126 extrap = NA; 126 extrap = NA;
127 xi = []; 127 xi = [];
128 ispp = false; 128 ispp = false;
129 firstnumeric = true; 129 have_xi = false;
130 rightcontinuous = NaN; 130 rightcontinuous = NaN;
131 131
132 if (nargin > 2) 132 if (nargin > 2)
133 for i = 1:length (varargin) 133 for i_arg = 1:length (varargin)
134 arg = varargin{i}; 134 arg = varargin{i_arg};
135 if (ischar (arg)) 135 if (ischar (arg))
136 arg = tolower (arg); 136 arg = tolower (arg);
137 switch (arg) 137 switch (arg)
138 case "extrap" 138 case "extrap"
139 extrap = "extrap"; 139 extrap = "extrap";
145 rightcontinuous = false; 145 rightcontinuous = false;
146 otherwise 146 otherwise
147 method = arg; 147 method = arg;
148 endswitch 148 endswitch
149 else 149 else
150 if (firstnumeric) 150 if (i_arg == 1)
151 xi = arg; 151 xi = arg;
152 firstnumeric = false; 152 have_xi = true;
153 else 153 else
154 extrap = arg; 154 extrap = arg;
155 endif 155 endif
156 endif 156 endif
157 endfor 157 endfor
158 endif 158 endif
159 159
160 if (isempty (xi) && firstnumeric && ! ispp) 160 if (! have_xi && ! ispp)
161 xi = y; 161 xi = y;
162 y = x; 162 y = x;
163 if (isvector (y)) 163 if (isvector (y))
164 x = 1:numel (y); 164 x = 1:numel (y);
165 else 165 else
522 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 522 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
523 %!assert (interp1 (xp,yp,xi,style),... 523 %!assert (interp1 (xp,yp,xi,style),...
524 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 524 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
525 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 525 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
526 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 526 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
527 %!assert (interp1 (yp, xi, style, 0), ...
528 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
527 %!error interp1 (1,1,1, style) 529 %!error interp1 (1,1,1, style)
528 ## ENDBLOCK 530 ## ENDBLOCK
529 531
530 %!test style = "previous"; 532 %!test style = "previous";
531 ## BLOCK 533 ## BLOCK
559 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 561 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
560 #%!assert (interp1 (xp,yp,xi,style),... 562 #%!assert (interp1 (xp,yp,xi,style),...
561 #%! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 563 #%! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
562 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 564 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
563 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 565 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
566 %!assert (interp1 (yp, xi, style, 0), ...
567 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
564 %!error interp1 (1,1,1, style) 568 %!error interp1 (1,1,1, style)
565 ## ENDBLOCK 569 ## ENDBLOCK
566 570
567 %!test style = "next"; 571 %!test style = "next";
568 ## BLOCK 572 ## BLOCK
594 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 598 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
595 #%!assert (interp1 (xp,yp,xi,style),... 599 #%!assert (interp1 (xp,yp,xi,style),...
596 #%! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 600 #%! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
597 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 601 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
598 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 602 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
603 %!assert (interp1 (yp, xi, style, 0), ...
604 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
599 %!error interp1 (1,1,1, style) 605 %!error interp1 (1,1,1, style)
600 ## ENDBLOCK 606 ## ENDBLOCK
601 607
602 %!test style = "linear"; 608 %!test style = "linear";
603 ## BLOCK 609 ## BLOCK
629 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 635 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
630 %!assert (interp1 (xp,yp,xi,style),... 636 %!assert (interp1 (xp,yp,xi,style),...
631 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 637 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
632 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 638 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
633 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 639 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
640 %!assert (interp1 (yp, xi, style, 0), ...
641 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
634 %!assert (interp1 ([1 2 2 3], [1 2 3 4], 2), 3) 642 %!assert (interp1 ([1 2 2 3], [1 2 3 4], 2), 3)
635 %!assert (interp1 ([3 2 2 1], [4 3 2 1], 2), 2) 643 %!assert (interp1 ([3 2 2 1], [4 3 2 1], 2), 2)
636 %!error interp1 (1,1,1, style) 644 %!error interp1 (1,1,1, style)
637 ## ENDBLOCK 645 ## ENDBLOCK
638 646
666 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 674 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
667 %!assert (interp1 (xp,yp,xi,style),... 675 %!assert (interp1 (xp,yp,xi,style),...
668 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 676 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
669 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 677 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
670 %! interp1 (xp,yp,xi,style,"extrap"),100*eps) 678 %! interp1 (xp,yp,xi,style,"extrap"),100*eps)
679 %!assert (interp1 (yp, xi, style, 0), ...
680 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
671 %!error interp1 (1,1,1, style) 681 %!error interp1 (1,1,1, style)
672 ## ENDBLOCK 682 ## ENDBLOCK
673 683
674 %!test style = "pchip"; 684 %!test style = "pchip";
675 ## BLOCK 685 ## BLOCK
701 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 711 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
702 %!assert (interp1 (xp,yp,xi,style),... 712 %!assert (interp1 (xp,yp,xi,style),...
703 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 713 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
704 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 714 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
705 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 715 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
716 %!assert (interp1 (yp, xi, style, 0), ...
717 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
706 %!error interp1 (1,1,1, style) 718 %!error interp1 (1,1,1, style)
707 ## ENDBLOCK 719 ## ENDBLOCK
708 720
709 %!test style = "spline"; 721 %!test style = "spline";
710 ## BLOCK 722 ## BLOCK
736 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]) 748 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)])
737 %!assert (interp1 (xp,yp,xi,style),... 749 %!assert (interp1 (xp,yp,xi,style),...
738 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps) 750 %! interp1 (fliplr (xp),fliplr (yp),xi,style),100*eps)
739 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi), 751 %!assert (ppval (interp1 (xp,yp,style,"pp"),xi),
740 %! interp1 (xp,yp,xi,style,"extrap"),10*eps) 752 %! interp1 (xp,yp,xi,style,"extrap"),10*eps)
753 %!assert (interp1 (yp, xi, style, 0), ...
754 %! interp1 (1:numel (yp), yp, xi, style, 0), 10*eps)
741 %!error interp1 (1,1,1, style) 755 %!error interp1 (1,1,1, style)
742 ## ENDBLOCK 756 ## ENDBLOCK
743 ## ENDBLOCKTEST 757 ## ENDBLOCKTEST
744 758
745 ## test extrapolation 759 ## test extrapolation