-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtools.h
337 lines (310 loc) · 8.1 KB
/
tools.h
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
331
332
333
334
335
336
337
/* tools.h - miscellaneous useful functions & classes
Adrian Groves, FMRIB Image Analysis Group
Copyright (C) 2007 University of Oxford */
/* CCOPYRIGHT */
#pragma once
#include "rundata.h"
#include "armawrap/newmat.h"
#include <assert.h>
#include <math.h>
namespace fabber
{
/**
* Read 'small' matrix from file.
*
* The matrix may be in ASCII or VEST format
*/
NEWMAT::Matrix read_matrix_file(std::string filename);
NEWMAT::ReturnMatrix MaskRows(NEWMAT::Matrix m, std::vector<int> masked_rows);
NEWMAT::ReturnMatrix MaskRows(NEWMAT::ColumnVector v, std::vector<int> masked_rows);
}
// Calculate log-gamma from a Taylor expansion; good to one part in 2e-10.
double gammaln(double x);
/**
* Base class for a generic 1-dimensional function which takes a double and returns a double
*
* Could add more functions.. e.g. initialGuess, domain of validity, etc.
*/
class GenericFunction1D : public Loggable
{
public:
/**
* Calculate the value of this function
*
* @param x Input value
* @return output of function
*/
virtual double Calculate(double x) const = 0;
/**
* Allow us to calculate the value of the function
* using the () operator
*
* e.g. MyFunction myfunc;
* double answer = myfunc(4);
*/
double operator()(double x) const
{
return Calculate(x);
}
/**
* This is useful if your function is very slow to calculate, but you have
* some cached partial calculations available. If you have a suitable
* cached value, store it into guess and return true. Otherwise return
* false (and leave guess unchanged).
*/
virtual bool PickFasterGuess(
double *guess, double lower, double upper, bool allowEndpoints = false) const
{
return false;
}
virtual ~GenericFunction1D()
{
}
private:
// Function's constant data should go here
};
#define REALMAX (1.7976931348623158e+308)
/**
* Base class for a method of guessing the next value to try if we are looking
* for the zero of a function
*
* @param lower Lower value of input
* @param upper Upper value of input. Must have lower<upper
* @param atLower Value of function at lower input
* @param atUpper Value of function at upper input
* @return Next value to try
*/
class Guesstimator : public Loggable
{
public:
virtual double GetGuess(double lower, double upper, double atLower, double atUpper) = 0;
virtual ~Guesstimator()
{
}
};
/**
* Guesstimator which just suggests you try half way in between the two values
*/
class BisectionGuesstimator : public Guesstimator
{
public:
virtual double GetGuess(double lower, double upper, double, double)
{
assert(lower < upper);
return (lower + upper) / 2;
}
};
/**
* Guesstimator which suggests the geometric mean of lower and upper
*/
class LogBisectionGuesstimator : public Guesstimator
{
public:
virtual double GetGuess(double lower, double upper, double, double)
{
assert(lower > 0 && upper > lower);
double guess = sqrt(lower * upper);
if (lower >= guess || guess >= upper)
{
std::cout << "Uh-oh... lower = " << lower << ", guess = " << guess << ", upper = " << upper
<< std::endl;
}
return guess;
}
};
/**
* equations below: from NRIC, section 9.2. Simpler than Brent, slightly less reliable.
*/
class RiddlersGuesstimator : public Guesstimator
{
public:
virtual double GetGuess(double lower, double upper, double atLower, double atUpper);
RiddlersGuesstimator()
: halfDone(false)
, x1(0)
, x2(0)
, fx1(0)
, fx2(0)
{
}
private:
bool halfDone; // waiting for f(x3) result?
double x1, x2, fx1, fx2; // save from phase 1 for phase 2; only valid when halfDone is true.
};
/**
* Performs a log transformation on a RiddlesGuesstimator
*/
class LogRiddlersGuesstimator : public RiddlersGuesstimator
{
public:
virtual double GetGuess(double lower, double upper, double atLower, double atUpper)
{
return exp(RiddlersGuesstimator::GetGuess(log(lower), log(upper), atLower, atUpper));
}
};
/**
* Performs linear interpolation to produce the next guess
*/
class InterpGuesstimator : public Guesstimator
{
public:
virtual double GetGuess(double lower, double upper, double atLower, double atUpper)
{
double guess = upper - atUpper * (upper - lower) / (atUpper - atLower);
if (lower >= guess || guess >= upper)
{
std::cout << "Uh-oh... lower = " << lower << ", guess = " << guess << ", upper = " << upper
<< ", atLower = " << atLower << ", atUpper = " << atUpper << std::endl;
}
return guess;
}
};
/**
* Finds the zero of a GenericFunction
*
* Note that you have to specify one or more tolerances to get any sensible
* results. Also note that the ratio tolerances current assume that the
* X or Y value always positive -- otherwise it'll stop too early!
*/
class ZeroFinder : public Loggable
{
public:
explicit ZeroFinder(const GenericFunction1D &f)
: fcn(f)
, searchMin(-REALMAX)
, searchMax(REALMAX)
, searchGuess(0)
, searchScale(REALMAX)
, searchScaleGrowth(2)
, maxEvaluations(1000000)
, tolX(REALMAX)
, tolY(REALMAX)
, ratioTolX(REALMAX)
, ratioTolY(REALMAX)
, guesstimator(new BisectionGuesstimator())
, verbosity(2)
{
m_log = f.GetLogger();
}
/**
* Return input value at which function is zero
*/
virtual double FindZero() const = 0;
/**
* Returns the input value at which the function is zero,
* using the () operator
*
* e.g. ZeroFinder finder(MyFunc);
* root = finder();
*/
operator double() const
{
return FindZero();
}
virtual ~ZeroFinder()
{
}
/**
* Set initial guess
*/
ZeroFinder &InitialGuess(double guess)
{
searchGuess = guess;
return *this;
}
/**
* Set the a minimum value we will not search below
*/
ZeroFinder &SearchMin(double min)
{
searchMin = min;
return *this;
}
/**
* Set the a maximum value we will not search above
*/
ZeroFinder &SearchMax(double max)
{
searchMax = max;
return *this;
}
ZeroFinder &InitialScale(double scale)
{
searchScale = scale;
return *this;
}
ZeroFinder &ScaleGrowth(double growth)
{
assert(growth > 1);
searchScaleGrowth = growth;
return *this;
}
/**
* Set the a maximum number of trials before we stop
*/
ZeroFinder &MaxEvaluations(int evals)
{
assert(evals > 1);
maxEvaluations = evals;
return *this;
}
ZeroFinder &TolX(double tx)
{
assert(tx > 0);
tolX = tx;
return *this;
}
ZeroFinder &TolY(double ty)
{
assert(ty > 0);
tolY = ty;
return *this;
}
// ZeroFinder& RatioTolY(double rty) // utterly pointless -- looking for a sign change!
// { assert(rty>1); ratioTolY = rty; return *this; }
ZeroFinder &RatioTolX(double rtx)
{
assert(rtx > 1);
ratioTolX = rtx;
return *this;
}
/**
* Set a Guesstimator to use to produce the next estimate
*/
ZeroFinder &SetGuesstimator(Guesstimator *g)
{
delete guesstimator;
guesstimator = g;
return *this;
}
ZeroFinder &Verbosity(int v)
{
verbosity = v;
return *this;
}
protected:
const GenericFunction1D &fcn;
// Optional parameters:
double searchMin;
double searchMax;
double searchGuess;
double searchScale;
double searchScaleGrowth;
int maxEvaluations;
double tolX;
double tolY;
double ratioTolX;
double ratioTolY;
Guesstimator *guesstimator;
int verbosity;
};
class DescendingZeroFinder : public ZeroFinder
{
public:
explicit DescendingZeroFinder(const GenericFunction1D &f)
: ZeroFinder(f)
{
return;
}
virtual double FindZero() const;
};