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