-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpolateHypercube.cc
More file actions
289 lines (266 loc) · 10.1 KB
/
interpolateHypercube.cc
File metadata and controls
289 lines (266 loc) · 10.1 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
@copyright Russell Standish 2020
@author Russell Standish
This file is part of Civita.
Civita is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Civita is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Civita. If not, see <http://www.gnu.org/licenses/>.
*/
#include "interpolateHypercube.h"
#ifdef CLASSDESC
#include <classdesc_epilogue.h>
#endif
namespace civita
{
namespace
{
// used in an assertion.
bool sorted(civita::XVector::const_iterator begin, civita::XVector::const_iterator end)
{
civita::AnyLess less;
if (begin==end-1) return true;
for (; begin!=end-1; ++begin)
if (less(*(begin+1),*begin))
return false;
return true;
}
}
vector<size_t> InterpolateHC::splitAndRotate(size_t hcIndex) const
{
vector<size_t> r(rank());
const auto& h=hypercube();
for (size_t dim=0; dim<rank(); ++dim)
r[rotation[dim]] = (hcIndex/strides[dim]) % h.xvectors[dim].size();
return r;
}
void InterpolateHC::setArgument(const TensorPtr& a, const Args&)
{
arg=a;
if (rank()!=arg->rank())
throw runtime_error("Rank of interpolated tensor doesn't match its argument");
// reorder hypercube for type and name
interimHC.xvectors.clear();
size_t stride=1;
const auto& targetHC=hypercube().xvectors;
rotation.clear();
rotation.resize(rank(), rank());
// ensure rotation vector will contain unique indices
std::map<size_t,size_t> tmpRotation;
// construct interim hypercube and its rotation permutation
for (size_t i=0; i<rank(); ++i)
{
const auto& src=arg->hypercube().xvectors[i];
sortAndAdd(src);
if (src.name.empty())
{
const auto& dst=targetHC[i];
if (!dst.name.empty() || src.dimension.type!=dst.dimension.type ||
src.dimension.units!=dst.dimension.units) //TODO handle conversion between different units
throw runtime_error("mismatch between unnamed dimensions");
interimHC.xvectors.push_back(dst);
tmpRotation.emplace(make_pair(i,i));
}
else // find matching names dimension
{
auto dst=find_if(targetHC.begin(), targetHC.end(),
[&](const XVector& i){return i.name==src.name;});
if (dst==targetHC.end())
{
// possible alternative when targetHC has no xvectors or undefined ones. for feature 147
interimHC.xvectors.push_back(src);
tmpRotation.emplace(make_pair(i,i));
}
else
{
interimHC.xvectors.push_back(*dst);
tmpRotation.emplace(make_pair(dst-targetHC.begin(),i));
}
}
strides.push_back(stride);
stride*=targetHC[i].size();
}
if (tmpRotation.size()!=rank()) throw runtime_error("rotation of indices is not a permutation");
for (auto& i: tmpRotation) rotation[i.first]=i.second;
#ifndef NDEBUG
for (auto& i: rotation) assert(i<rank()); // check that no indices have been doubly assigned.
// Now we're sure rotation is a permutation
#endif
if (index().empty())
for (size_t i=0; i<size(); checkCancel(), ++i)
weightedIndices.push_back(bodyCentredNeighbourhood(i));
else
for (auto i: index())
checkCancel(), weightedIndices.push_back(bodyCentredNeighbourhood(i));
argInterpolatedHCsize=interpolateHCSize=1;
auto argDims=arg->hypercube().dims();
for (size_t dim=0; dim<min(maxInterpolateDimension, rank()); ++dim)
{
interpolateHCSize*=targetHC[dim].size();
argInterpolatedHCsize*=argDims[dim];
}
}
void InterpolateHC::sortAndAdd(const XVector& xv)
{
sortedArgHC.emplace_back();
auto& s=sortedArgHC.back();
for (size_t i=0; i<xv.size(); ++i)
s.second.push_back(i);
AnyLess less;
sort(s.second.begin(), s.second.end(),
[&](size_t i, size_t j){return less(xv[i],xv[j]);});
for (auto i: s.second)
s.first.push_back(xv[i]);
assert(sorted(s.first.begin(), s.first.end()));
}
double InterpolateHC::operator[](size_t idx) const
{
auto div=lldiv(idx,interpolateHCSize);
if (size_t(div.rem)<weightedIndices.size())
{
if (weightedIndices[div.rem].empty()) return nan("");
double r=0;
for (const auto& i: weightedIndices[div.rem])
{
assert(i.index<arg->hypercube().numElements());
r+=i.weight * arg->atHCIndex(i.index+argInterpolatedHCsize*div.quot);
}
return r;
}
return nan("");
}
InterpolateHC::WeightedIndexVector InterpolateHC::bodyCentredNeighbourhood(size_t destIdx) const
{
// note this agorithm is limited in rank (typically 32 dims on 32bit machine, or 64 dims on 64bit)
if (rank()>sizeof(size_t)*8)
throw runtime_error("Ranks > "+to_string(sizeof(size_t)*8)+" not supported");
auto dimsToInterpolate=min(maxInterpolateDimension,rank());
size_t numNeighbours=size_t(1)<<dimsToInterpolate;
vector<size_t> iIdx=splitAndRotate(destIdx);
const auto& argHC=arg->hypercube();
// loop over the nearest neighbours in argument hypercube space of
// this point in interimHypercube space
double sumWeight=0;
WeightedIndexVector r;
// multivariate interpolation - eg see Abramowitz & Stegun 25.2.66
for (size_t nbr=0; nbr<numNeighbours; ++nbr)
{
double weight=1;
size_t idx=0;
for (size_t dim=0, stride=1; dim<dimsToInterpolate; stride*=argHC.xvectors[dim].size(), ++dim)
{
const auto& x=sortedArgHC[dim].first;
assert(!x.empty());
assert(sorted(x.begin(),x.end()));
auto v=interimHC.xvectors[dim][iIdx[dim]];
auto lesserIt=std::upper_bound(x.begin(), x.end(), v, AnyLess());
if (lesserIt!=x.begin()) --lesserIt; // find greatest value <= v,
any lesser=*lesserIt, greater;
if (diff(lesser, v)>=0)
greater=lesser; // one sided interpolation for beginning or exact match
else if (lesserIt+1==x.end())
greater=x.back(); // one sided interpolation for end
else
greater=*(lesserIt+1);
bool greaterSide = nbr&(size_t(1)<<dim); // on higher side of hypercube
double d=diff(greater,lesser);
if (d==0 && greaterSide) goto nextNeighbour; // already taken lesserVal at weight 1.
idx += sortedArgHC[dim].second[(lesserIt-x.begin()+greaterSide)]*stride;
assert(idx<argHC.numElements());
if (d>0)
weight *= greaterSide? diff(v,lesser): d-diff(v,lesser);
}
if (index().empty())
{
r.emplace_back(WeightedIndex{idx,weight});
sumWeight+=weight;
}
else
{
auto indexV=index();
if (binary_search(indexV.begin(), indexV.end(), idx))
{
r.emplace_back(WeightedIndex{idx,weight});
sumWeight+=weight;
}
}
nextNeighbour:;
}
// normalise weights
for (auto& i: r) i.weight/=sumWeight;
return r;
}
void PivotedInterpolateHC::setArgument(const TensorPtr& a, const ITensor::Args&)
{
if (!a) return;
vector<string> initPivotOrder, finalPivotOrder, stringDims;
map<string, const XVector*> targetXVectors, argXVectors;
for (auto& i: a->hypercube().xvectors)
argXVectors[i.name]=&i;
TensorPtr chain=a;
// note hypercube is currently set with the target Hypercube to interpolate argument
for (auto& xv: hypercube().xvectors)
{
finalPivotOrder.push_back(xv.name);
targetXVectors[xv.name]=&xv;
auto& argXv=*argXVectors[xv.name];
if (xv==argXv)
stringDims.push_back(xv.name);
else if (xv.dimension.type==Dimension::string)
{
stringDims.push_back(xv.name);
// need to permute arguments a's arguments to match that of xv
vector<size_t> permutation;
map<any, size_t> argSlices;
for (size_t i=0; i<argXv.size(); ++i)
argSlices.emplace(argXv[i], i);
for (auto& i: xv)
{
auto slice=argSlices.find(i);
if (slice!=argSlices.end())
permutation.push_back(slice->second);
}
if (permutation.empty())
{
m_index=set<unsigned>{0}; // axes do not match, no data
return;
}
auto permute=make_shared<PermuteAxis>();
permute->setArgument(chain,{argXv.name,0});
permute->setPermutation(permutation);
chain=std::move(permute);
}
else
initPivotOrder.push_back(xv.name);
}
size_t interpolateSize=initPivotOrder.size();
initPivotOrder.insert(initPivotOrder.end(),stringDims.begin(),stringDims.end());
if (initPivotOrder!=finalPivotOrder)
{
auto initPivot=make_shared<Pivot>();
initPivot->setArgument(chain, {});
initPivot->setOrientation(initPivotOrder);
chain=std::move(initPivot);
}
if (interpolateSize>0)
{
auto interpolate=make_shared<InterpolateHC>(interpolateSize);
Hypercube pivotedTargetHC;
// moved, because hypercube() recreated below
for (auto& i: initPivotOrder) pivotedTargetHC.xvectors.emplace_back(std::move(*targetXVectors[i]));
interpolate->hypercube(pivotedTargetHC);
interpolate->setArgument(chain,{});
Pivot::setArgument(interpolate,{}); // resets hypercube
}
else
Pivot::setArgument(chain, {});
if (initPivotOrder!=finalPivotOrder)
setOrientation(finalPivotOrder);
}
}