Repository navigation
Expand file tree
/
Copy pathTable.cpp
More file actions
330 lines (299 loc) · 11.7 KB
/
Copy pathTable.cpp
File metadata and controls
330 lines (299 loc) · 11.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* -*- c++ -*-
* Copyright (c) 2012-2014 by the GalSim developers team on GitHub
* https://github.com/GalSim-developers
*
* This file is part of GalSim: The modular galaxy image simulation toolkit.
* https://github.com/GalSim-developers/GalSim
*
* GalSim is free software: redistribution and use in source and binary forms,
* with or without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions, and the disclaimer given in the accompanying LICENSE
* file.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, and the disclaimer given in the documentation
* and/or other materials provided with the distribution.
*/
// icpc pretends to be GNUC, since it thinks it's compliant, but it's not.
// It doesn't understand "pragma GCC"
#ifndef __INTEL_COMPILER
// For 32-bit machines, g++ -O2 optimization in the TMV stuff below uses an optimization
// that is technically isn't known to not overflow 32 bit integers. In fact, it is totally
// fine to use, but we need to remove a warning about it in this file for gcc >= 4.5
#if defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ >= 5 || __GNUC_MINOR__ >= 5)
#pragma GCC diagnostic ignored "-Wstrict-overflow"
#endif
#endif
#include "TMV.h"
#include "TMV_SymBand.h"
#include "Table.h"
#include <cmath>
#include <vector>
#include <iostream>
namespace galsim {
// Look up an index. Use STL binary search; maybe faster to use
template<class V, class A>
int Table<V,A>::upperIndex(const A a) const
{
if (a<_argMin() || a>_argMax()) throw TableOutOfRange(a,_argMin(),_argMax());
// Go directly to index if arguments are regularly spaced.
if (equalSpaced) {
int index = int( std::ceil( (a-_argMin()) / dx) );
if (index >= int(v.size())) --index; // in case of rounding error
if (index == 0) ++index;
// check if we need to move ahead or back one step due to rounding errors
while (a > v[index].arg) ++index;
while (a < v[index-1].arg) --index;
return index;
} else {
xassert(lastIndex >= 1);
xassert(lastIndex < int(v.size()));
if ( a < v[lastIndex-1].arg ) {
xassert(lastIndex-2 >= 0);
// Check to see if the previous one is it.
if (a >= v[lastIndex-2].arg) return --lastIndex;
else {
// Look for the entry from 0..lastIndex-1:
Entry e(a,0);
iter p = std::upper_bound(v.begin(), v.begin()+lastIndex-1, e);
xassert(p != v.begin());
xassert(p != v.begin()+lastIndex-1);
lastIndex = p-v.begin();
return lastIndex;
}
} else if (a > v[lastIndex].arg) {
xassert(lastIndex+1 < int(v.size()));
// Check to see if the next one is it.
if (a <= v[lastIndex+1].arg) return ++lastIndex;
else {
// Look for the entry from lastIndex..end
Entry e(a,0);
iter p = std::lower_bound(v.begin()+lastIndex+1, v.end(), e);
xassert(p != v.begin()+lastIndex+1);
xassert(p != v.end());
lastIndex = p-v.begin();
return lastIndex;
}
} else {
// Then lastIndex is correct.
return lastIndex;
}
}
}
//new element for table.
template<class V, class A>
void Table<V,A>::addEntry(const A _arg, const V _val)
{
Entry e(_arg,_val);
v.push_back(e);
isReady = false; //re-sort array next time used
}
template<class V, class A>
Table<V,A>::Table(const A* argvec, const V* valvec, int N, interpolant in) :
iType(in), isReady(false)
{
v.reserve(N);
const A* aptr;
const V* vptr;
int i;
for (i=0, aptr=argvec, vptr=valvec; i<N; i++, aptr++, vptr++) {
Entry e(*aptr,*vptr);
v.push_back(e);
}
}
template<class V, class A>
Table<V,A>::Table(const std::vector<A>& aa, const std::vector<V>& vv, interpolant in) :
iType(in), isReady(false)
{
v.reserve(aa.size());
if (vv.size() != aa.size())
throw TableError("input vector lengths don't match");
if (iType == spline && vv.size() < 3)
throw TableError("input vectors are too short to spline interpolate");
if (vv.size() < 2 && (iType == linear || iType == ceil || iType == floor))
throw TableError("input vectors are too short for interpolation");
typename std::vector<A>::const_iterator aptr=aa.begin();
typename std::vector<V>::const_iterator vptr=vv.begin();
for (size_t i=0; i<aa.size(); i++, ++aptr, ++vptr) {
Entry e(*aptr,*vptr);
v.push_back(e);
}
}
//lookup & interp. function value. - this one returns 0 out of bounds.
template<class V, class A>
V Table<V,A>::operator() (const A a) const
{
setup(); //do any necessary prep
if (a<_argMin() || a>_argMax()) return V(0);
else {
int i = upperIndex(a);
return interpolate(a,i,v,y2);
}
}
//lookup & interp. function value.
template<class V, class A>
V Table<V,A>::lookup(const A a) const
{
setup();
int i = upperIndex(a);
return interpolate(a,i,v,y2);
}
template<class V, class A>
V Table<V,A>::linearInterpolate(
A a, int i, const std::vector<Entry>& v, const std::vector<V>& )
{
A h = v[i].arg - v[i-1].arg;
A aa = (v[i].arg - a) / h;
A bb = 1. - aa;
return aa*v[i-1].val + bb*v[i].val;
}
template<class V, class A>
V Table<V,A>::splineInterpolate(
A a, int i, const std::vector<Entry>& v, const std::vector<V>& y2)
{
#if 0
// Direct calculation saved for comparison:
A h = v[i].arg - v[i-1].arg;
A aa = (v[i].arg - a)/h;
A bb = 1. - aa;
return aa*v[i-1].val +bb*v[i].val +
((aa*aa*aa-aa)*y2[i-1]+(bb*bb*bb-bb)*y2[i]) *
(h*h)/6.0;
#else
// Factor out h factors, so only need 1 division by h.
// Also, use the fact that bb = h-aa to simplify the calculation.
A h = v[i].arg - v[i-1].arg;
A aa = (v[i].arg - a);
A bb = h-aa;
return ( aa*v[i-1].val + bb*v[i].val -
(1./6.) * aa * bb * ( (aa+h)*y2[i-1] +
(bb+h)*y2[i]) ) / h;
#endif
}
template<class V, class A>
V Table<V,A>::floorInterpolate(
A a, int i, const std::vector<Entry>& v, const std::vector<V>& )
{
// On entry, it is only guaranteed that v[i-1].arg <= a <= v[i].arg.
// Normally those ='s are ok, but for floor and ceil we make the extra
// check to see if we should choose the opposite bound.
if (v[i].arg == a) return v[i].val;
else return v[i-1].val;
}
template<class V, class A>
V Table<V,A>::ceilInterpolate(
A a, int i, const std::vector<Entry>& v, const std::vector<V>& )
{
if (v[i-1].arg == a) return v[i-1].val;
return v[i].val;
}
template<class V, class A>
void Table<V,A>::read(std::istream& is)
{
std::string line;
const std::string comments="#;!"; //starts comment
V vv;
A aa;
while (is) {
getline(is,line);
// skip leading white space:
size_t i;
for (i=0; isspace(line[i]) && i<line.length(); i++) ;
// skip line if blank or just comment
if (i==line.length()) continue;
if (comments.find(line[i])!=std::string::npos) continue;
// try reading arg & val from line:
std::istringstream iss(line);
iss >> aa >> vv;
if (iss.fail()) throw TableReadError(line) ;
addEntry(aa,vv);
}
}
// Do any necessary setup of the table before using
template<class V, class A>
void Table<V,A>::setup() const
{
if (isReady) return;
if (v.size() <= 1)
throw TableError("Trying to use a null Table (need at least 2 entries)");
sortIt();
lastIndex = 1; // Start back at the beginning for the next search.
// See if arguments are equally spaced
// ...within this fractional error:
const double tolerance = 0.01;
dx = (v.back().arg - v.front().arg) / (v.size()-1);
if (dx == 0.)
throw TableError("First and last Table entry are equal.");
equalSpaced = true;
for (int i=1; i<int(v.size()); i++) {
if ( std::abs( ((v[i].arg-v[0].arg)/dx - i)) > tolerance) equalSpaced = false;
if (v[i].arg == v[i-1].arg)
throw TableError("Table has repeated arguments.");
}
switch (iType) {
case linear:
interpolate = &Table<V,A>::linearInterpolate;
break;
case spline :
setupSpline();
interpolate = &Table<V,A>::splineInterpolate;
break;
case floor:
interpolate = &Table<V,A>::floorInterpolate;
break;
case ceil:
interpolate = &Table<V,A>::ceilInterpolate;
break;
default:
throw TableError("interpolation method not yet implemented");
}
isReady = true;
}
template <class V, class A>
void Table<V,A>::setupSpline() const
{
/**
* Calculate the 2nd derivatives of the natural cubic spline.
*
* Here we follow the broad procedure outlined in this technical note by Jim
* Armstrong, freely available online:
* http://www.algorithmist.net/spline.html
*
* The system we solve is equation [7]. In our adopted notation u_i are the diagonals
* of the matrix M, and h_i the off-diagonals. y'' is z_i and the rhs = v_i.
*
* For table sizes larger than the fully trivial (2 or 3 elements), we use the
* symmetric tridiagonal matrix solution capabilities of MJ's TMV library.
*/
// Set up the 2nd-derivative table for splines
int n = v.size();
y2.resize(n);
// End points 2nd-derivatives zero for natural cubic spline
y2[0] = V(0);
y2[n-1] = V(0);
// For 3 points second derivative at i=1 is simple
if (n == 3){
y2[1] = 3.*((v[2].val - v[1].val) / (v[2].arg - v[1].arg) -
(v[1].val - v[0].val) / (v[1].arg - v[0].arg)) / (v[2].arg - v[0].arg);
} else { // For 4 or more points we use the TMV symmetric tridiagonal matrix solver
tmv::SymBandMatrix<V> M(n-2, 1);
for (int i=1; i<=n-3; i++){
M(i, i-1) = v[i+1].arg - v[i].arg;
}
tmv::Vector<V> rhs(n-2);
for (int i=1; i<=n-2; i++){
M(i-1, i-1) = 2. * (v[i+1].arg - v[i-1].arg);
rhs(i-1) = 6. * ( (v[i+1].val - v[i].val) / (v[i+1].arg - v[i].arg) -
(v[i].val - v[i-1].val) / (v[i].arg - v[i-1].arg) );
}
tmv::Vector<V> solution(n-2);
solution = rhs / M; // solve the tridiagonal system of equations
for (int i=1; i<=n-2; i++){
y2[i] = solution[i-1];
}
}
}
template class Table<double,double>;
}