Mercurial > hg > octave-lyh
annotate src/DLD-FUNCTIONS/__lin_interpn__.cc @ 8377:25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 29 Oct 2008 16:52:10 +0100 |
parents | 82be108cc558 |
children | eb63fbe60fab |
rev | line source |
---|---|
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" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
28 #include "oct-locbuf.h" |
6702 | 29 |
30 #include "defun-dld.h" | |
31 #include "error.h" | |
32 #include "oct-obj.h" | |
33 | |
34 // equivalent to isvector.m | |
35 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
36 template <class T> |
6702 | 37 bool |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
38 isvector (const T& array) |
6702 | 39 { |
40 const dim_vector dv = array.dims (); | |
41 return dv.length () == 2 && (dv(0) == 1 || dv(1) == 1); | |
42 } | |
43 | |
44 // lookup a value in a sorted table (lookup.m) | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
45 template <class T> |
6702 | 46 octave_idx_type |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
47 lookup (const T *x, octave_idx_type n, T y) |
6702 | 48 { |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
49 octave_idx_type j; |
6702 | 50 |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
51 if (x[0] < x[n-1]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
52 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
53 // increasing x |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
54 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
55 if (y > x[n-1] || y < x[0]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
56 return -1; |
6702 | 57 |
58 #ifdef EXHAUSTIF | |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
59 for (j = 0; j < n - 1; j++) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
60 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
61 if (x[j] <= y && y <= x[j+1]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
62 return j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
63 } |
6702 | 64 #else |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
65 octave_idx_type j0 = 0; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
66 octave_idx_type j1 = n - 1; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
67 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
68 while (true) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
69 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
70 j = (j0+j1)/2; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
71 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
72 if (y <= x[j+1]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
73 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
74 if (x[j] <= y) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
75 return j; |
6702 | 76 |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
77 j1 = j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
78 } |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
79 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
80 if (x[j] <= y) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
81 j0 = j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
82 } |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
83 #endif |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
84 } |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
85 else |
6702 | 86 { |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
87 // decreasing x |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
88 // previous code with x -> -x and y -> -y |
6702 | 89 |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
90 if (y > x[0] || y < x[n-1]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
91 return -1; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
92 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
93 #ifdef EXHAUSTIF |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
94 for (j = 0; j < n - 1; j++) |
6702 | 95 { |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
96 if (x[j+1] <= y && y <= x[j]) |
6702 | 97 return j; |
98 } | |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
99 #else |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
100 octave_idx_type j0 = 0; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
101 octave_idx_type j1 = n - 1; |
6702 | 102 |
7561
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
103 while (true) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
104 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
105 j = (j0+j1)/2; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
106 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
107 if (y >= x[j+1]) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
108 { |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
109 if (x[j] >= y) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
110 return j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
111 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
112 j1 = j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
113 } |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
114 |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
115 if (x[j] >= y) |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
116 j0 = j; |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
117 } |
a938cd7869b2
__lin_interpn__.cc: handle decreasing coordinate values
Alexander Barth
parents:
7016
diff
changeset
|
118 #endif |
6702 | 119 } |
120 } | |
121 | |
122 // n-dimensional linear interpolation | |
123 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
124 template <class T> |
6702 | 125 void |
126 lin_interpn (int n, const octave_idx_type *size, const octave_idx_type *scale, | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
127 octave_idx_type Ni, T extrapval, const T **x, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
128 const T *v, const T **y, T *vi) |
6702 | 129 { |
130 bool out = false; | |
131 int bit; | |
132 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
133 OCTAVE_LOCAL_BUFFER (T, coef, 2*n); |
6702 | 134 OCTAVE_LOCAL_BUFFER (octave_idx_type, index, n); |
135 | |
136 // loop over all points | |
137 for (octave_idx_type m = 0; m < Ni; m++) | |
138 { | |
139 // loop over all dimensions | |
140 for (int i = 0; i < n; i++) | |
141 { | |
142 index[i] = lookup (x[i], size[i], y[i][m]); | |
143 out = index[i] == -1; | |
144 | |
145 if (out) | |
146 break; | |
147 else | |
148 { | |
149 octave_idx_type j = index[i]; | |
150 coef[2*i+1] = (y[i][m] - x[i][j])/(x[i][j+1] - x[i][j]); | |
151 coef[2*i] = 1 - coef[2*i+1]; | |
152 } | |
153 } | |
154 | |
155 | |
156 if (out) | |
157 vi[m] = extrapval; | |
158 else | |
159 { | |
160 vi[m] = 0; | |
161 | |
162 // loop over all corners of hypercube (1<<n = 2^n) | |
163 for (int i = 0; i < (1 << n); i++) | |
164 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
165 T c = 1; |
6702 | 166 octave_idx_type l = 0; |
167 | |
168 // loop over all dimensions | |
169 for (int j = 0; j < n; j++) | |
170 { | |
171 // test if the jth bit in i is set | |
172 bit = i >> j & 1; | |
173 l += scale[j] * (index[j] + bit); | |
174 c *= coef[2*j+bit]; | |
175 } | |
176 | |
177 vi[m] += c * v[l]; | |
178 } | |
179 } | |
180 } | |
181 } | |
182 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
183 template <class T, class M> |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
184 octave_value |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
185 lin_interpn (int n, M *X, const M V, M *Y) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
186 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
187 octave_value retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
188 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
189 M Vi = M (Y[0].dims ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
190 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
191 OCTAVE_LOCAL_BUFFER (const T *, y, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
192 OCTAVE_LOCAL_BUFFER (octave_idx_type, size, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
193 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
194 for (int i = 0; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
195 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
196 y[i] = Y[i].data (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
197 size[i] = V.dims()(i); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
198 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
199 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
200 OCTAVE_LOCAL_BUFFER (const T *, x, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
201 OCTAVE_LOCAL_BUFFER (octave_idx_type, scale, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
202 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
203 const T *v = V.data (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
204 T *vi = Vi.fortran_vec (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
205 octave_idx_type Ni = Vi.numel (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
206 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
207 T extrapval = octave_NA; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
208 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
209 // offset in memory of each dimension |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
210 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
211 scale[0] = 1; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
212 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
213 for (int i = 1; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
214 scale[i] = scale[i-1] * size[i-1]; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
215 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
216 // tests if X[0] is a vector, if yes, assume that all elements of X are |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
217 // in the ndgrid format. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
218 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
219 if (! isvector (X[0])) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
220 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
221 for (int i = 0; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
222 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
223 if (X[i].dims () != V.dims ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
224 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
225 error ("interpn: incompatible size of argument number %d", i+1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
226 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
227 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
228 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
229 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
230 M tmp = M (dim_vector (size[i], 1)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
231 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
232 for (octave_idx_type j = 0; j < size[i]; j++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
233 tmp(j) = X[i](scale[i]*j); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
234 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
235 X[i] = tmp; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
236 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
237 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
238 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
239 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
240 for (int i = 0; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
241 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
242 if (! isvector (X[i]) && X[i].numel () != size[i]) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
243 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
244 error ("interpn: incompatible size of argument number %d", i+1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
245 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
246 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
247 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
248 x[i] = X[i].data (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
249 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
250 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
251 lin_interpn (n, size, scale, Ni, extrapval, x, v, y, vi); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
252 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
253 retval = Vi; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
254 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
255 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
256 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
257 |
6945 | 258 // Perform @var{n}-dimensional interpolation. Each element of then |
259 // @var{n}-dimensional array @var{v} represents a value at a location | |
260 // given by the parameters @var{x1}, @var{x2},...,@var{xn}. The parameters | |
261 // @var{x1}, @var{x2}, @dots{}, @var{xn} are either @var{n}-dimensional | |
262 // arrays of the same size as the array @var{v} in the \"ndgrid\" format | |
263 // or vectors. The parameters @var{y1}, @var{y2}, @dots{}, @var{yn} are | |
264 // all @var{n}-dimensional arrays of the same size and represent the | |
265 // points at which the array @var{vi} is interpolated. | |
266 // | |
267 //This function only performs linear interpolation. | |
268 | |
6702 | 269 DEFUN_DLD (__lin_interpn__, args, , |
270 "-*- texinfo -*-\n\ | |
271 @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 | 272 Undocumented internal function.\n\ |
6702 | 273 @end deftypefn") |
274 { | |
275 octave_value retval; | |
276 | |
277 int nargin = args.length (); | |
278 | |
279 if (nargin < 2 || nargin % 2 == 0) | |
280 { | |
281 print_usage (); | |
282 return retval; | |
283 } | |
284 | |
285 // dimension of the problem | |
286 int n = (nargin-1)/2; | |
287 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
288 if (args(n).is_single_type()) |
6702 | 289 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
290 OCTAVE_LOCAL_BUFFER (FloatNDArray, X, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
291 OCTAVE_LOCAL_BUFFER (FloatNDArray, Y, n); |
6702 | 292 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
293 const FloatNDArray V = args(n).float_array_value (); |
6702 | 294 |
295 if (error_state) | |
296 { | |
297 print_usage (); | |
298 return retval; | |
299 } | |
300 | |
301 for (int i = 0; i < n; i++) | |
302 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
303 X[i] = args(i).float_array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
304 Y[i] = args(n+i+1).float_array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
305 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
306 if (error_state) |
6702 | 307 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
308 print_usage (); |
6702 | 309 return retval; |
310 } | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
311 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
312 if (Y[0].dims () != Y[i].dims ()) |
6702 | 313 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
314 error ("interpn: incompatible size of argument number %d", n+i+2); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
315 return retval; |
6702 | 316 } |
317 } | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
318 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
319 retval = lin_interpn<float, FloatNDArray> (n, X, V, Y); |
6702 | 320 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
321 else |
6702 | 322 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
323 OCTAVE_LOCAL_BUFFER (NDArray, X, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
324 OCTAVE_LOCAL_BUFFER (NDArray, Y, n); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
325 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
326 const NDArray V = args(n).array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
327 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
328 if (error_state) |
6702 | 329 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
330 print_usage (); |
6702 | 331 return retval; |
332 } | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
333 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
334 for (int i = 0; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
335 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
336 X[i] = args(i).array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
337 Y[i] = args(n+i+1).array_value (); |
6702 | 338 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
339 if (error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
340 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
341 print_usage (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
342 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
343 } |
6702 | 344 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
345 if (Y[0].dims () != Y[i].dims ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
346 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
347 error ("interpn: incompatible size of argument number %d", n+i+2); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
348 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
349 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
350 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
351 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
352 retval = lin_interpn<double, NDArray> (n, X, V, Y); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7561
diff
changeset
|
353 } |
6702 | 354 |
355 return retval; | |
356 } |