-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinterp.m
257 lines (242 loc) · 6.38 KB
/
spinterp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
function [ip, ipder] = spinterp(z, varargin)
% SPINTERP Evaluation of sparse grid interpolant
% IP = SPINTERP(Z, Y1, ..., YD) Computes the interpolated
% values at (Y1, ..., YD) over the sparse grid. The Y may be
% double arrays of equal size for vectorized processing. The
% sparse grid data must be given as a structure Z containing the
% hierarchical surpluses (computed with SPVALS).
%
% [IP, IPDER] = SPINTERP(Z, Y1, ..., YD) Computes interpolated
% values and derivatives at the specified points. The
% derivatives are returned as D x 1 gradient vectors inside of a
% cell array that has equal size as the double array IP.
%
% [...] = SPINTERP(Z, Y) Alternative syntax for calling spinterp
% with a single matrix Y containing the points to evaluate. The
% matrix must have the size 1xD, Dx1, or NxD, where N are the
% number of points to evaluate.
%
% Two additional options are available with SPINTERP that are
% set by adding a field to the structure Z:
% selectOutput [ integer {1} ] Set the output variable number
% if an interpolant with multiple output variables was
% constructed with SPVALS.
% continuousDerivatives [ 'on' | {'off'} ] Enable augmented
% continuous derivatives for the Clenshaw-Curtis grid.
%
% Examples:
% f = inline('x.^2 + y.^2 - 2.*z');
% z = spvals(f,3);
% f_interp = spinterp(z, 0.5, 0.2, 0.2)
% f_exact = f(0.5, 0.2, 0.2)
% error = abs(f_exact - f_interp)
%
% [f_interp, f_ipgrad] = spinterp(z, [0.5, 0.2, 0.2])
% z.continuousDerivatives = 'on';
% [f_interp, f_ipgrad] = spinterp(z, [0.5, 0.2, 0.2])
%
% See also SPGRID, SPVALS, SPSET, SPDIM.
gridtype = z.gridType;
d = z.d;
range = z.range;
deriv = 0;
continuous = 0;
if nargout > 1
deriv = 1;
% Default is computing derivatives with jumps
if strcmpi(gridtype, 'clenshaw-curtis')
if isfield(z, 'continuousDerivatives')
if strcmpi(z.continuousDerivatives, 'on')
continuous = 1;
end
end
end
end
if isfield(z, 'indices');
sparseIndices = 'on';
indices = z.indices;
else
sparseIndices = 'off';
n = size(z.vals,2) - 1;
nfrom = 0;
end
if isfield(z, 'selectOutput')
output = z.selectOutput;
else
output = 1;
end
vals = z.vals;
switch lower(gridtype)
case 'clenshaw-curtis'
if strcmpi(sparseIndices, 'off')
if deriv
if continuous
ipmethod = @spcontdercc;
else
ipmethod = @spderivcc;
end
else
ipmethod = @spinterpcc;
end
else
if deriv
if continuous
ipmethod = @spcontderccsp;
else
ipmethod = @spderivccsp;
end
else
ipmethod = @spinterpccsp;
end
end
case 'maximum'
if deriv
error('MATLAB:spinterp:badopt',['Computing derivatives not ' ...
'supported for grid type ''' gridtype '''.']);
else
ipmethod = @spinterpm;
end
case 'noboundary'
if deriv
error('MATLAB:spinterp:badopt',['Computing derivatives not ' ...
'supported for grid type ''' gridtype '''.']);
else
ipmethod = @spinterpnb;
end
case 'chebyshev'
if strcmpi(sparseIndices, 'off')
if deriv
ipmethod = @spderivcb;
else
ipmethod = @spinterpcb;
end
else
if deriv
ipmethod = @spderivcbsp;
else
ipmethod = @spinterpcbsp;
end
end
case 'gauss-patterson'
if strcmpi(sparseIndices, 'off')
ipmethod = @spinterpgp;
else
ipmethod = @spinterpgpsp;
end
otherwise
error('MATLAB:spinterp:badopt',['Unknown grid type ''' gridtype '''.']);
end
if length(varargin) == 1 && d > 1
y = varargin{1};
if ndims(y) == 2
if size(y,1) == 1 || size(y,2) == 1
% allow call both as row and as column vector
y = y(:)';
orgsize = [1, 1];
else
orgsize = [size(y,1), 1];
end
else
error('MATLAB:spinterp:badopt','Invalid array dimension');
end
% Call in the form of a single vector or matrix instead of a
% comma-separated list
if size(y,2) == d
if ~isempty(range)
y = (y - ones(size(y,1),1)*range(:,1)') ./ ...
(ones(size(y,1),1)*(range(:,2)'-range(:,1)'));
end
else
error('MATLAB:spinterp:badopt', ...
['Number of inputs does not match sparse grid' ...
' dimension.']);
end
elseif length(varargin) ~= d
error('MATLAB:spinterp:badopt', ...
['Number of inputs does not match sparse grid' ...
' dimension.']);
else
% store the original array shape
orgsize = size(varargin{1});
% Transform multiple parameter inputs to a matrix for fast
% processing; also perform normalization to unit cube.
for k = 1:length(varargin)
if ~isempty(range)
varargin{k} = (varargin{k}(:)-range(k,1))./(range(k,2)-range(k,1));
else
varargin{k} = varargin{k}(:);
end
end
y = [varargin{:}];
end
ninterp = size(y,1);
ip = zeros(ninterp,1);
if deriv, ipder = zeros(ninterp,d); end
if continuous, ipder2 = zeros(ninterp,d); end
% do multiple levels at once
if strcmpi(sparseIndices, 'off')
for k = nfrom:n
% get the sequence of levels
levelseq = spgetseq(k,d);
if isfield(z, 'purgeData')
pd = z.purgeData{k+1}(:,output);
else
pd = [];
end
if deriv
if continuous
[iptemp, ipdertemp, ipdertemp2] = ...
feval(ipmethod,d,vals{output,k+1},y,levelseq,pd,...
z.maxLevel);
ipder2 = ipder2 + ipdertemp2;
else
[iptemp, ipdertemp] = feval(ipmethod,d,vals{output,k+1},y,levelseq,pd);
end
ip = ip + iptemp;
ipder = ipder + ipdertemp;
else
ip = ip + feval(ipmethod,d,vals{output,k+1},y,levelseq,pd);
end
end
else
if isfield(z, 'purgeData')
pd = z.purgeData(:,output);
else
pd = [];
end
if deriv
if continuous
[ip, ipder, ipder2] = feval(ipmethod,d,vals{output},y,indices,pd,...
z.maxLevel);
else
[ip, ipder] = feval(ipmethod,d,vals{output},y,indices,pd);
end
else
ip = feval(ipmethod,d,vals{output},y,indices,pd);
end
end
if continuous
% Post-process if continuous derivatives are requested
ipder = ppderiv(ipder, ipder2, z.maxLevel, y);
end
if deriv && ~isempty(range)
for k = 1:d
ipder(:,k) = ipder(:,k) / (range(k,2)-range(k,1));
end
end
% restore original array shape
ip = reshape(ip,orgsize);
% if requested by the user, return derivatives as cell array matching
% the original array shape; each cell containts derivatives as a [dx1]
% vector.
if deriv
if ninterp == 1
% Treat special case of only a single point
ipder = {ipder(:)};
else
nd = length(orgsize);
ipder = permute(num2cell(...
permute(reshape(ipder,[orgsize double(d)]),...
[nd+1,1:nd]),1),[2:nd+1,1]);
end
end