6702
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 Alexander Barth |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
6702
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
6702
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "dNDArray.h" |
|
28 |
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "oct-obj.h" |
|
32 |
|
33 // equivalent to isvector.m |
|
34 |
|
35 bool |
|
36 isvector (const NDArray& array) |
|
37 { |
|
38 const dim_vector dv = array.dims (); |
|
39 return dv.length () == 2 && (dv(0) == 1 || dv(1) == 1); |
|
40 } |
|
41 |
|
42 // lookup a value in a sorted table (lookup.m) |
|
43 octave_idx_type |
|
44 lookup (const double *x, octave_idx_type n, double y) |
|
45 { |
|
46 octave_idx_type j, j0, j1; |
|
47 |
|
48 if (y > x[n-1] || y < x[0]) |
|
49 return -1; |
|
50 |
|
51 #ifdef EXHAUSTIF |
|
52 for (j = 0; j < n - 1; j++) |
|
53 { |
|
54 if (x[j] <= y && y <= x[j+1]) |
|
55 return j; |
|
56 } |
|
57 #else |
|
58 j0 = 0; |
|
59 j1 = n - 1; |
|
60 |
|
61 while (true) |
|
62 { |
|
63 j = (j0+j1)/2; |
|
64 |
|
65 if (y <= x[j+1]) |
|
66 { |
|
67 if (x[j] <= y) |
|
68 return j; |
|
69 |
|
70 j1 = j; |
|
71 } |
|
72 |
|
73 if (x[j] <= y) |
|
74 j0 = j; |
|
75 } |
|
76 |
|
77 #endif |
|
78 } |
|
79 |
|
80 // n-dimensional linear interpolation |
|
81 |
|
82 void |
|
83 lin_interpn (int n, const octave_idx_type *size, const octave_idx_type *scale, |
|
84 octave_idx_type Ni, double extrapval, const double **x, |
|
85 const double *v, const double **y, double *vi) |
|
86 { |
|
87 bool out = false; |
|
88 int bit; |
|
89 |
|
90 OCTAVE_LOCAL_BUFFER (double, coef, 2*n); |
|
91 OCTAVE_LOCAL_BUFFER (octave_idx_type, index, n); |
|
92 |
|
93 // loop over all points |
|
94 for (octave_idx_type m = 0; m < Ni; m++) |
|
95 { |
|
96 // loop over all dimensions |
|
97 for (int i = 0; i < n; i++) |
|
98 { |
|
99 index[i] = lookup (x[i], size[i], y[i][m]); |
|
100 out = index[i] == -1; |
|
101 |
|
102 if (out) |
|
103 break; |
|
104 else |
|
105 { |
|
106 octave_idx_type j = index[i]; |
|
107 coef[2*i+1] = (y[i][m] - x[i][j])/(x[i][j+1] - x[i][j]); |
|
108 coef[2*i] = 1 - coef[2*i+1]; |
|
109 } |
|
110 } |
|
111 |
|
112 |
|
113 if (out) |
|
114 vi[m] = extrapval; |
|
115 else |
|
116 { |
|
117 vi[m] = 0; |
|
118 |
|
119 // loop over all corners of hypercube (1<<n = 2^n) |
|
120 for (int i = 0; i < (1 << n); i++) |
|
121 { |
|
122 double c = 1; |
|
123 octave_idx_type l = 0; |
|
124 |
|
125 // loop over all dimensions |
|
126 for (int j = 0; j < n; j++) |
|
127 { |
|
128 // test if the jth bit in i is set |
|
129 bit = i >> j & 1; |
|
130 l += scale[j] * (index[j] + bit); |
|
131 c *= coef[2*j+bit]; |
|
132 } |
|
133 |
|
134 vi[m] += c * v[l]; |
|
135 } |
|
136 } |
|
137 } |
|
138 } |
|
139 |
6945
|
140 // Perform @var{n}-dimensional interpolation. Each element of then |
|
141 // @var{n}-dimensional array @var{v} represents a value at a location |
|
142 // given by the parameters @var{x1}, @var{x2},...,@var{xn}. The parameters |
|
143 // @var{x1}, @var{x2}, @dots{}, @var{xn} are either @var{n}-dimensional |
|
144 // arrays of the same size as the array @var{v} in the \"ndgrid\" format |
|
145 // or vectors. The parameters @var{y1}, @var{y2}, @dots{}, @var{yn} are |
|
146 // all @var{n}-dimensional arrays of the same size and represent the |
|
147 // points at which the array @var{vi} is interpolated. |
|
148 // |
|
149 //This function only performs linear interpolation. |
|
150 |
6702
|
151 DEFUN_DLD (__lin_interpn__, args, , |
|
152 "-*- texinfo -*-\n\ |
|
153 @deftypefn {Loadable Function} {@var{vi} =} __lin_interpn__ (@var{x1}, @var{x2}, @dots{}, @var{xn}, @var{v}, @var{y1}, @var{y2}, @dots{}, @var{yn})\n\ |
6945
|
154 Undocumented internal function.\n\ |
6702
|
155 @end deftypefn") |
|
156 { |
|
157 octave_value retval; |
|
158 |
|
159 int nargin = args.length (); |
|
160 |
|
161 if (nargin < 2 || nargin % 2 == 0) |
|
162 { |
|
163 print_usage (); |
|
164 return retval; |
|
165 } |
|
166 |
|
167 // dimension of the problem |
|
168 int n = (nargin-1)/2; |
|
169 |
|
170 OCTAVE_LOCAL_BUFFER (NDArray, X, n); |
|
171 OCTAVE_LOCAL_BUFFER (NDArray, Y, n); |
|
172 |
|
173 OCTAVE_LOCAL_BUFFER (const double *, x, n); |
|
174 OCTAVE_LOCAL_BUFFER (const double *, y, n); |
|
175 OCTAVE_LOCAL_BUFFER (octave_idx_type, scale, n); |
|
176 OCTAVE_LOCAL_BUFFER (octave_idx_type, size, n); |
|
177 |
|
178 const NDArray V = args(n).array_value (); |
|
179 NDArray Vi = NDArray (args(n+1).dims ()); |
|
180 |
|
181 if (error_state) |
|
182 { |
|
183 print_usage (); |
|
184 return retval; |
|
185 } |
|
186 |
|
187 const double *v = V.data (); |
|
188 double *vi = Vi.fortran_vec (); |
|
189 octave_idx_type Ni = Vi.numel (); |
|
190 |
6742
|
191 double extrapval = octave_NA; |
6702
|
192 |
|
193 for (int i = 0; i < n; i++) |
|
194 { |
|
195 X[i] = args(i).array_value (); |
|
196 Y[i] = args(n+i+1).array_value (); |
|
197 |
|
198 if (error_state) |
|
199 { |
|
200 print_usage (); |
|
201 return retval; |
|
202 } |
|
203 |
|
204 y[i] = Y[i].data (); |
|
205 size[i] = V.dims()(i); |
|
206 |
|
207 if (Y[0].dims () != Y[i].dims ()) |
|
208 { |
|
209 error ("interpn: incompatible size of argument number %d", n+i+2); |
|
210 return retval; |
|
211 } |
|
212 } |
|
213 |
|
214 // offset in memory of each dimension |
|
215 |
|
216 scale[0] = 1; |
|
217 |
|
218 for (int i = 1; i < n; i++) |
|
219 scale[i] = scale[i-1] * size[i-1]; |
|
220 |
|
221 // tests if X[0] is a vector, if yes, assume that all elements of X are |
|
222 // in the ndgrid format. |
|
223 |
|
224 if (! isvector (X[0])) |
|
225 { |
|
226 for (int i = 0; i < n; i++) |
|
227 { |
|
228 if (X[i].dims () != V.dims ()) |
|
229 { |
|
230 error ("interpn: incompatible size of argument number %d", i+1); |
|
231 return retval; |
|
232 } |
|
233 else |
|
234 { |
|
235 NDArray tmp = NDArray (dim_vector (size[i], 1)); |
|
236 |
|
237 for (octave_idx_type j = 0; j < size[i]; j++) |
|
238 tmp(j) = X[i](scale[i]*j); |
|
239 |
|
240 X[i] = tmp; |
|
241 } |
|
242 } |
|
243 } |
|
244 |
|
245 for (int i = 0; i < n; i++) |
|
246 { |
|
247 if (! isvector (X[i]) && X[i].numel () != size[i]) |
|
248 { |
|
249 error ("interpn: incompatible size of argument number %d", i+1); |
|
250 return retval; |
|
251 } |
|
252 else |
|
253 x[i] = X[i].data (); |
|
254 } |
|
255 |
|
256 lin_interpn (n, size, scale, Ni, extrapval, x, v, y, vi); |
|
257 |
|
258 retval = Vi; |
|
259 |
|
260 return retval; |
|
261 } |