Mercurial > jwe > octave
annotate scripts/general/interpn.m @ 31170:06c76eca5f95 default tip @
maint: merge stable to default
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 29 Jul 2022 12:02:42 -0400 |
parents | 731b6a5c7c4c |
children |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 ## |
30569
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30335
diff
changeset
|
3 ## Copyright (C) 2007-2022 The Octave Project Developers |
27923
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27858
diff
changeset
|
4 ## |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
6702 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
6702 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
6702 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
6702 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 ######################################################################## |
6702 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
27 ## @deftypefn {} {@var{vi} =} interpn (@var{x1}, @var{x2}, @dots{}, @var{v}, @var{y1}, @var{y2}, @dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
28 ## @deftypefnx {} {@var{vi} =} interpn (@var{v}, @var{y1}, @var{y2}, @dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
29 ## @deftypefnx {} {@var{vi} =} interpn (@var{v}, @var{m}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
30 ## @deftypefnx {} {@var{vi} =} interpn (@var{v}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
31 ## @deftypefnx {} {@var{vi} =} interpn (@dots{}, @var{method}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
32 ## @deftypefnx {} {@var{vi} =} interpn (@dots{}, @var{method}, @var{extrapval}) |
6702 | 33 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
34 ## Perform @var{n}-dimensional interpolation, where @var{n} is at least two. |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
36 ## Each element of the @var{n}-dimensional array @var{v} represents a value |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 ## at a location given by the parameters @var{x1}, @var{x2}, @dots{}, @var{xn}. |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
38 ## The parameters @var{x1}, @var{x2}, @dots{}, @var{xn} are either |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
39 ## @var{n}-dimensional arrays of the same size as the array @var{v} in |
29289
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
40 ## the @qcode{"ndgrid"} format or vectors. |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
41 ## |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
42 ## The parameters @var{y1}, @var{y2}, @dots{}, @var{yn} represent the points at |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
43 ## which the array @var{vi} is interpolated. They can be vectors of the same |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
44 ## length and orientation in which case they are interpreted as coordinates of |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
45 ## scattered points. If they are vectors of differing orientation or length, |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
46 ## they are used to form a grid in @qcode{"ndgrid"} format. They can also be |
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
47 ## @var{n}-dimensional arrays of equal size. |
6702 | 48 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
49 ## If @var{x1}, @dots{}, @var{xn} are omitted, they are assumed to be |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
50 ## @code{x1 = 1 : size (@var{v}, 1)}, etc. If @var{m} is specified, then |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 ## the interpolation adds a point half way between each of the interpolation |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
52 ## points. This process is performed @var{m} times. If only @var{v} is |
6702 | 53 ## specified, then @var{m} is assumed to be @code{1}. |
54 ## | |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
55 ## The interpolation @var{method} is one of: |
6702 | 56 ## |
57 ## @table @asis | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16891
diff
changeset
|
58 ## @item @qcode{"nearest"} |
9070
e9dc2ed2ec0f
Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
59 ## Return the nearest neighbor. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10819
diff
changeset
|
60 ## |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
61 ## @item @qcode{"linear"} (default) |
9070
e9dc2ed2ec0f
Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
62 ## Linear interpolation from nearest neighbors. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10819
diff
changeset
|
63 ## |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
64 ## @item @qcode{"pchip"} |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
65 ## Piecewise cubic Hermite interpolating polynomial---shape-preserving |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
66 ## interpolation with smooth first derivative (not implemented yet). |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
67 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16891
diff
changeset
|
68 ## @item @qcode{"cubic"} |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
69 ## Cubic interpolation (same as @qcode{"pchip"} [not implemented yet]). |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10819
diff
changeset
|
70 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16891
diff
changeset
|
71 ## @item @qcode{"spline"} |
12175
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 ## Cubic spline interpolation---smooth first and second derivatives |
6702 | 73 ## throughout the curve. |
74 ## @end table | |
75 ## | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16891
diff
changeset
|
76 ## The default method is @qcode{"linear"}. |
6702 | 77 ## |
19795
ca7599ae464d
doc: Grammarcheck documentation ahead of 4.0 release.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
78 ## @var{extrapval} is a scalar number. It replaces values beyond the endpoints |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
79 ## with @var{extrapval}. Note that if @var{extrapval} is used, @var{method} |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
80 ## must be specified as well. If @var{extrapval} is omitted and the |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
81 ## @var{method} is @qcode{"spline"}, then the extrapolated values of the |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
82 ## @qcode{"spline"} are used. Otherwise the default @var{extrapval} value for |
29293 | 83 ## any other @var{method} is @code{NA}. |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
84 ## @seealso{interp1, interp2, interp3, spline, ndgrid} |
6702 | 85 ## @end deftypefn |
86 | |
87 function vi = interpn (varargin) | |
88 | |
29293 | 89 if (nargin < 1 || ! isnumeric (varargin{1})) |
90 print_usage (); | |
91 endif | |
92 | |
6702 | 93 method = "linear"; |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
94 extrapval = []; |
6702 | 95 nargs = nargin; |
96 | |
29293 | 97 ## Find and validate EXTRAPVAL and/or METHOD inputs |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
98 if (nargs > 1 && ischar (varargin{end-1})) |
7208 | 99 if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
100 error ("interpn: EXTRAPVAL must be a numeric scalar"); |
6702 | 101 endif |
8116 | 102 extrapval = varargin{end}; |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
103 method = varargin{end-1}; |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
104 nargs -= 2; |
19658
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
105 elseif (ischar (varargin{end})) |
5a59c0e1203d
Modified the "extrap" option for interp2, interp3, and interpn (bug #44002).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18587
diff
changeset
|
106 method = varargin{end}; |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
107 nargs -= 1; |
6702 | 108 endif |
109 | |
18587
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 if (method(1) == "*") |
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 warning ("interpn: ignoring unsupported '*' flag to METHOD"); |
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 method(1) = []; |
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 endif |
29293 | 114 method = validatestring (method, |
115 {"nearest", "linear", "pchip", "cubic", "spline"}, | |
116 "interpn"); | |
18587
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 |
29293 | 118 if (nargs <= 2) |
119 ## Calling form interpn (V, ...) | |
7208 | 120 v = varargin{1}; |
6702 | 121 m = 1; |
122 if (nargs == 2) | |
29293 | 123 m = varargin{2}; |
124 if (! (isnumeric (m) && isscalar (m) && m == fix (m))) | |
13151 | 125 print_usage (); |
6702 | 126 endif |
127 endif | |
128 sz = size (v); | |
129 nd = ndims (v); | |
130 x = cell (1, nd); | |
131 y = cell (1, nd); | |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
132 for i = 1 : nd |
6702 | 133 x{i} = 1 : sz(i); |
134 y{i} = 1 : (1 / (2 ^ m)) : sz(i); | |
135 endfor | |
13151 | 136 y{1} = y{1}.'; |
137 [y{:}] = ndgrid (y{:}); | |
7208 | 138 elseif (! isvector (varargin{1}) && nargs == (ndims (varargin{1}) + 1)) |
29293 | 139 ## Calling form interpn (V, Y1, Y2, ...) |
7208 | 140 v = varargin{1}; |
6702 | 141 sz = size (v); |
142 nd = ndims (v); | |
143 x = cell (1, nd); | |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
144 y = varargin(2 : nargs); |
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
145 for i = 1 : nd |
6702 | 146 x{i} = 1 : sz(i); |
147 endfor | |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
148 elseif (rem (nargs, 2) == 1 |
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
149 && nargs == (2 * ndims (varargin{ceil (nargs / 2)})) + 1) |
29293 | 150 ## Calling form interpn (X1, X2, ..., V, Y1, Y2, ...) |
6702 | 151 nv = ceil (nargs / 2); |
7208 | 152 v = varargin{nv}; |
6702 | 153 sz = size (v); |
154 nd = ndims (v); | |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
155 x = varargin(1 : (nv - 1)); |
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
156 y = varargin((nv + 1) : nargs); |
6702 | 157 else |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
158 error ("interpn: wrong number or incorrectly formatted input arguments"); |
6702 | 159 endif |
160 | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12175
diff
changeset
|
161 if (any (! cellfun ("isvector", x))) |
29293 | 162 for i = 1 : nd |
163 if (! size_equal (x{i}, v)) | |
164 error ("interpn: incorrect dimensions for input X%d", i); | |
6702 | 165 endif |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
166 idx(1 : nd) = {1}; |
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
167 idx(i) = ":"; |
6721 | 168 x{i} = x{i}(idx{:})(:); |
6702 | 169 endfor |
170 endif | |
171 | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12175
diff
changeset
|
172 all_vectors = all (cellfun ("isvector", y)); |
29291
4cb0fc7ecd4b
interpn.m: Use size_equal for 10X speedup in cset 067b663529bb (bug #59856).
Rik <rik@octave.org>
parents:
29289
diff
changeset
|
173 same_size = size_equal (y{:}); |
29289
067b663529bb
interpn.m: Fix check for scattered point coordinates (bug #59856).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27928
diff
changeset
|
174 if (all_vectors && ! same_size) |
29293 | 175 [y{:}] = ndgrid (y{:}); |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
176 endif |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
177 |
30689
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
178 if (! strcmp (method, "spline") && isempty (extrapval)) |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
179 if (iscomplex (v)) |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
180 extrapval = NA + 1i*NA; |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
181 else |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
182 extrapval = NA; |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
183 endif |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
184 endif |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
185 |
6702 | 186 if (strcmp (method, "linear")) |
187 vi = __lin_interpn__ (x{:}, v, y{:}); | |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
188 vi(isna (vi)) = extrapval; |
6702 | 189 elseif (strcmp (method, "nearest")) |
29293 | 190 ## FIXME: This seems overly complicated. Is there a way to simplify |
191 ## all the code after the call to lookup (which should be fast)? | |
192 ## Could Qhull be used for quick nearest neighbor calculation? | |
6702 | 193 yshape = size (y{1}); |
194 yidx = cell (1, nd); | |
29293 | 195 ## Find rough nearest index using lookup function [O(log2 (N)]. |
6702 | 196 for i = 1 : nd |
197 y{i} = y{i}(:); | |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7561
diff
changeset
|
198 yidx{i} = lookup (x{i}, y{i}, "lr"); |
6702 | 199 endfor |
29293 | 200 ## Single comparison to next largest index to see which is closer. |
6702 | 201 idx = cell (1,nd); |
202 for i = 1 : nd | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19795
diff
changeset
|
203 idx{i} = yidx{i} ... |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19795
diff
changeset
|
204 + (y{i} - x{i}(yidx{i})(:) >= x{i}(yidx{i} + 1)(:) - y{i}); |
6702 | 205 endfor |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
206 vi = v(sub2ind (sz, idx{:})); |
29293 | 207 ## Apply EXTRAPVAL to points outside original volume. |
208 idx = false (prod (yshape), 1); | |
6702 | 209 for i = 1 : nd |
210 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); | |
211 endfor | |
212 vi(idx) = extrapval; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
213 vi = reshape (vi, yshape); |
6721 | 214 elseif (strcmp (method, "spline")) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12175
diff
changeset
|
215 if (any (! cellfun ("isvector", y))) |
29293 | 216 ysz = size (y{1}); |
217 for i = 1 : nd | |
218 if (any (size (y{i}) != ysz)) | |
219 error ("interpn: incorrect dimensions for input Y%d", i); | |
10549 | 220 endif |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
221 idx(1 : nd) = {1}; |
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
222 idx(i) = ":"; |
10549 | 223 y{i} = y{i}(idx{:}); |
7423 | 224 endfor |
225 endif | |
7421 | 226 |
6702 | 227 vi = __splinen__ (x, v, y, extrapval, "interpn"); |
7421 | 228 |
7423 | 229 if (size_equal (y{:})) |
230 ly = length (y{1}); | |
231 idx = cell (1, ly); | |
232 q = cell (1, nd); | |
233 for i = 1 : ly | |
10549 | 234 q(:) = i; |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
235 idx{i} = q; |
7423 | 236 endfor |
16891
486c3e2731ff
interpn.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
237 vi = vi(cellfun (@(x) sub2ind (size (vi), x{:}), idx)); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
238 vi = reshape (vi, size (y{1})); |
7423 | 239 endif |
29293 | 240 elseif (strcmp (method, "pchip")) |
241 error ("interpn: pchip interpolation not yet implemented"); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
242 elseif (strcmp (method, "cubic")) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
243 error ("interpn: cubic interpolation not yet implemented"); |
6702 | 244 endif |
245 | |
246 endfunction | |
247 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
248 |
6702 | 249 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
250 %! clf; |
14247
c4fa5e0b6193
test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
251 %! colormap ("default"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
252 %! A = [13,-1,12;5,4,3;1,6,2]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
253 %! x = [0,1,4]; y = [10,11,12]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
254 %! xi = linspace (min (x), max (x), 17); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
255 %! yi = linspace (min (y), max (y), 26)'; |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
256 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "linear").'); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
257 %! [x,y] = meshgrid (x,y); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
258 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off; |
6702 | 259 |
260 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
261 %! clf; |
14247
c4fa5e0b6193
test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
262 %! colormap ("default"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
263 %! A = [13,-1,12;5,4,3;1,6,2]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
264 %! x = [0,1,4]; y = [10,11,12]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
265 %! xi = linspace (min (x), max (x), 17); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
266 %! yi = linspace (min (y), max (y), 26)'; |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
267 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "nearest").'); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
268 %! [x,y] = meshgrid (x,y); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
269 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off; |
6702 | 270 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
271 %!#demo # FIXME: Uncomment when support for "cubic" has been added |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
272 %! clf; |
14247
c4fa5e0b6193
test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
273 %! colormap ("default"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
274 %! A = [13,-1,12;5,4,3;1,6,2]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
275 %! x = [0,1,2]; y = [10,11,12]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
276 %! xi = linspace (min (x), max (x), 17); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
277 %! yi = linspace (min (y), max (y), 26)'; |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
278 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "cubic").'); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
279 %! [x,y] = meshgrid (x,y); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
280 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off; |
6702 | 281 |
282 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
283 %! clf; |
14247
c4fa5e0b6193
test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
284 %! colormap ("default"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
285 %! A = [13,-1,12;5,4,3;1,6,2]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
286 %! x = [0,1,2]; y = [10,11,12]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
287 %! xi = linspace (min (x), max (x), 17); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
288 %! yi = linspace (min (y), max (y), 26)'; |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
289 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "spline").'); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
290 %! [x,y] = meshgrid (x,y); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
291 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off; |
6721 | 292 |
293 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
294 %! clf; |
14247
c4fa5e0b6193
test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
295 %! colormap ("default"); |
6721 | 296 %! x = y = z = -1:1; |
297 %! f = @(x,y,z) x.^2 - y - z.^2; | |
298 %! [xx, yy, zz] = meshgrid (x, y, z); | |
299 %! v = f (xx,yy,zz); | |
300 %! xi = yi = zi = -1:0.1:1; | |
301 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
302 %! vi = interpn (x, y, z, v, xxi, yyi, zzi, "spline"); |
6721 | 303 %! mesh (yi, zi, squeeze (vi(1,:,:))); |
304 | |
7421 | 305 %!test |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
306 %! [x,y,z] = ndgrid (0:2); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
307 %! f = x + y + z; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
308 %! assert (interpn (x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5]), [1.5, 4.5]); |
30335
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29364
diff
changeset
|
309 %! assert (interpn (x,y,z,f,[.51 1.51],[.51 1.51],[.51 1.51],"nearest"), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29364
diff
changeset
|
310 %! [3, 6]); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
311 %! assert (interpn (x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5],"spline"), [1.5, 4.5]); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 %! assert (interpn (x,y,z,f,x,y,z), f); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
313 %! assert (interpn (x,y,z,f,x,y,z,"nearest"), f); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
314 %! assert (interpn (x,y,z,f,x,y,z,"spline"), f); |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7424
diff
changeset
|
315 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7424
diff
changeset
|
316 %!test |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
317 %! [x, y, z] = ndgrid (0:2, 1:4, 2:6); |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
318 %! f = x + y + z; |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
319 %! xi = [0.5 1.0 1.5]; yi = [1.5 2.0 2.5 3.5]; zi = [2.5 3.5 4.0 5.0 5.5]; |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
320 %! fi = interpn (x, y, z, f, xi, yi, zi); |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
321 %! [xi, yi, zi] = ndgrid (xi, yi, zi); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
322 %! assert (fi, xi + yi + zi); |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
323 |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
324 %!test |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
325 %! xi = 0:2; yi = 1:4; zi = 2:6; |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
326 %! [x, y, z] = ndgrid (xi, yi, zi); |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
327 %! f = x + y + z; |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
328 %! fi = interpn (x, y, z, f, xi, yi, zi, "nearest"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
329 %! assert (fi, x + y + z); |
10819
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
330 |
f3c984d45dcb
interpn.m: Convert interpolation vectors of non-equal length to nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
331 %!test |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
332 %! [x,y,z] = ndgrid (0:2); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
333 %! f = x.^2 + y.^2 + z.^2; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
334 %! assert (interpn (x,y,-z,f,1.5,1.5,-1.5), 7.5); |
10427
62bb59f927b1
scripts/general/interp2.m, scripts/general/interpn.m: For nearest neighbour interpolation ceil (instead of floor) at the center of the data intervals to be compatible with Matlab. Add test.
Soren Hauberg <hauberg@gmail.com>
parents:
9245
diff
changeset
|
335 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
336 %!test # for Matlab-compatible rounding for "nearest" |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
337 %! x = meshgrid (1:4); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
338 %! assert (interpn (x, 2.5, 2.5, "nearest"), 3); |
10427
62bb59f927b1
scripts/general/interp2.m, scripts/general/interpn.m: For nearest neighbour interpolation ceil (instead of floor) at the center of the data intervals to be compatible with Matlab. Add test.
Soren Hauberg <hauberg@gmail.com>
parents:
9245
diff
changeset
|
339 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
340 %!test |
13151 | 341 %! z = zeros (3, 3, 3); |
342 %! zout = zeros (5, 5, 5); | |
343 %! z(:,:,1) = [1 3 5; 3 5 7; 5 7 9]; | |
344 %! z(:,:,2) = z(:,:,1) + 2; | |
345 %! z(:,:,3) = z(:,:,2) + 2; | |
346 %! for n = 1:5 | |
347 %! zout(:,:,n) = [1 2 3 4 5; | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
348 %! 2 3 4 5 6; |
13151 | 349 %! 3 4 5 6 7; |
350 %! 4 5 6 7 8; | |
351 %! 5 6 7 8 9] + (n-1); | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
352 %! endfor |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
353 %! tol = 10*eps; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
354 %! assert (interpn (z), zout, tol); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
355 %! assert (interpn (z, "linear"), zout, tol); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
356 %! assert (interpn (z, "spline"), zout, tol); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
357 |
30689
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
358 ## Test that interpolating a complex matrix is equivalent to interpolating its |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
359 ## real and imaginary parts separately. |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
360 %!test <*61907> |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
361 %! yi = [0.5, 1.5]'; |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
362 %! xi = [2.5, 3.5]; |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
363 %! zi = [2.25, 4.75]; |
31166
8f3885b4d7ae
test: Set random state and increase tolerance for interpn.m test (bug #62817)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30689
diff
changeset
|
364 %! rand ("state", 1340640850); |
30689
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
365 %! v = rand (4, 3, 5) + 1i * rand (4, 3, 5); |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
366 %! for method = {"nearest", "linear", "spline"} |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
367 %! vi_complex = interpn (v, yi, xi, zi, method{1}); |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
368 %! vi_real = interpn (real (v), yi, xi, zi, method{1}); |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
369 %! vi_imag = interpn (imag (v), yi, xi, zi, method{1}); |
31166
8f3885b4d7ae
test: Set random state and increase tolerance for interpn.m test (bug #62817)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30689
diff
changeset
|
370 %! assert (real (vi_complex), vi_real, 2*eps) |
8f3885b4d7ae
test: Set random state and increase tolerance for interpn.m test (bug #62817)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30689
diff
changeset
|
371 %! assert (imag (vi_complex), vi_imag, 2*eps) |
30689
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
372 %! endfor |
92cda11fb171
interpn: Fix issues with complex input (bug #61907).
Christof Kaufmann <christofkaufmann.dev@gmail.com>
parents:
30569
diff
changeset
|
373 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19795
diff
changeset
|
374 ## Test input validation |
29293 | 375 %!error <Invalid call> interpn () |
376 %!error <Invalid call> interpn ("foobar") | |
377 %!error <EXTRAPVAL must be a numeric scalar> interpn (1, "linear", {1}) | |
378 %!error <EXTRAPVAL must be a numeric scalar> interpn (1, "linear", [1, 2]) | |
18587
5cf9a02732b6
Issue warning when '*' flag used with interp2,3,n.m (bug #41459).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
379 %!warning <ignoring unsupported '\*' flag> interpn (rand (3,3), 1, "*linear"); |
29293 | 380 %!error <'foobar' does not match any of> interpn (1, "foobar") |
381 %!error <wrong number or incorrectly formatted input arguments> | |
382 %! interpn (1, 2, 3, 4); | |
383 %!error <incorrect dimensions for input X1> | |
384 %! interpn ([1,2], ones (2,2), magic (3), [1,2], [1,2]) | |
385 %!error <incorrect dimensions for input X2> | |
386 %! interpn (ones (3,3), ones (2,2), magic (3), [1,2], [1,2]) | |
387 %!error <incorrect dimensions for input Y2> | |
388 %! interpn ([1,2], [1,2], magic (3), [1,2], ones (2,2), "spline") | |
389 %!error <pchip interpolation not yet implemented> interpn ([1,2], "pchip") | |
390 %!error <cubic interpolation not yet implemented> interpn ([1,2], "cubic") | |
391 |