-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumerics.h
216 lines (201 loc) · 7.01 KB
/
numerics.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* numerics.h
*
* Created on: Oct 25, 2011
* Author: gerlach
*/
// update for SDW DQMC (2015-02-06 - )
#ifndef NUMERICS_H_
#define NUMERICS_H_
#include <iostream>
#include <cmath>
#include <limits>
#include "tools.h"
#include "exceptions.h"
//find brentMinimize<f> finds the minimum of a function (callable
//object) f. It searches intervalStart <= x <= intervalEnd.
//Afterwards: @minLocation = x_min, @minValue = f(x_min).
//eps -- accuracy of evaluation
//The Brent algorithm is used to keep the number of evaluations of f low.
template <typename Function>
void brentMinimize(double& minLocation, double& minValue, Function& f,
double intervalStart, double intervalEnd,
double tolerance = 3.0e-8) {
using namespace std;
const double eps = std::numeric_limits<double>::epsilon() * 1.0e-3;
const double goldenRatio = 0.3819660; //(3 - sqrt(5))/2
//minimum between a, b:
double a = intervalStart;
double b = intervalEnd;
double x; //current minimum
double v; //second best point
double w; //previous value of w
v = w = x = a + goldenRatio * (b - a);
double u; //most recent point of evaluation
//function evaluated at x,v,w
double fx, fv, fw, fu;
fv = fw = fx = f(x);
double tol1, tol2; //minimal relative movement in x
double m; //mid point of a and b
double d = 0; //distance moved in last step
double e = 0; //distance moved in before last step
do {
m = 0.5 * (a + b);
// tol = eps * abs(x) + eps / 4; //Brent: parameter t instead of eps/4
tol1 = tolerance * abs(x) + eps;
tol2 = 2 * tol1;
//Check stopping criterion max(x-a, b-x) <= 2*tol
if (abs(x - m) <= tol2 - (b - a) / 2) break;
if (abs(e) > tol1) {
//fit parabola in p, q, r
double r = (x - w) * (fx - fv);
double q = (x - v) * (fx - fw);
double p = (x - v) * q - (x - w) * r;
q = 2 * (q - r);
if (q > 0) {
p = -p;
} else {
q = -q;
}
double eLast = e; //assigned to r in Brent
e = d;
//changed relation signs in following line:
if (abs(p) >= abs(0.5 * q * eLast) or p <= q * (a - x) or p >= q * (b - x)) { //TODO:check signs
//can't do parabolic fit, do a golden section step instead:
e = (x < m) ? (b - x) : (a - x);
d = goldenRatio * e;
} else {
//parabolic interpolation step
d = p / q;
u = x + d;
//don't evaluate f to close to a or b
if (u - a < tol2 or b - u < tol2) {
d = (x < m) ? tol1 : -tol1;
}
}
} else {
//golden section
e = (x < m) ? (b - x) : (a - x);
d = goldenRatio * e;
}
//update current position, don't evaluate f too close to x
u = (abs(d) > tol1) ? (x + d) : ((d > 0) ? (x + tol1) : (x - tol1));
fu = f(u);
//update a,b,v,w,x
if (fu <= fx) {
//accept new point!
if (u < x) {
b = x;
} else {
a = x;
}
v = w;
fv = fw;
w = x;
fw = fx;
x = u;
fx = fu;
} else {
//new point is worse, but must be better than one of a or b
if (u < x) {
a = u;
} else {
b = u;
}
if (fu <= fw or w == x) {
v = w;
fv = fw;
w = u;
fw = fu;
} else if (fu <= fv or v ==x or v == w) {
v = u;
fv = fu;
}
}
} while (true); //TODO: enforce max number of iterations?
//finished:
minLocation = x;
minValue = fx;
}
//use a combination of bisection and Newton-Raphson to find a root x0 of
//func(x), uses the derivative dfunc as well
//pass an interval [xMin, xMax] within which x0 is to be found
//convergence condition: abs(func(x0)) < acc (this is different
//from normal root finding -- adapted to the CEI problem)
//this is similar to Numerical Recipes 3rd editon, Ch 9.4, rtsafe()
template<typename Function, typename Derivative>
double findRoot(Function& func, Derivative& dfunc,
double xMin, double xMax, double acc,
int maxIterations = 100, bool verbose = true) {
double xh, xl; //"high" and "low" limits of bracket
double fl = func(xMin);
if (fl == .0) return xMin;
double fh = func(xMax);
if (fh == .0) return xMax;
if ((fl > .0 and fh > .0) or (fl < .0 and fh < .0)) {
throw GeneralError("Root is not bracketed between "
+ numToString(xMin) + " and " + numToString(xMax));
}
//find direction of search:
if (fl <.0) {
xl = xMin;
xh = xMax;
} else {
xh = xMin;
xl = xMax;
}
double root = .5 * (xMin + xMax); //initial guess
double dxOld = std::fabs(xMax - xMin);
double dx = dxOld;
double f = func(root);
double df = dfunc(root);
for (int j = 0; j < maxIterations; ++j) {
bool finished = false;
if ( (((root-xh)*df - f) * ((root-xl)*df - f) > .0)
or (std::fabs(2.0*f) > std::fabs(dxOld*df)) ) {
//Newton is out of range or not decreasing fast enough
// -> bisection step
dxOld = dx;
dx = .5 * (xh - xl);
root = xl + dx;
// if (xl == root) finished = true; //negligible change -> finished
} else {
//Newton step can be taken
dxOld = dx;
dx = f / df;
double temp = root;
root -= dx;
// if (temp == root) finished = true; //negligible change -> finished
}
//new function evaluation:
f = func(root);
df = dfunc(root);
if ((j > 0) and (std::fabs(f) < acc)) finished = true; //converged
if (verbose) {
std::cout << " [" << xl << ", " << xh << "]: "
"f(" << root << ")=" << f << std::endl;
}
if (finished) {
return root;
}
//update bracket:
if (f < .0) {
xl = root;
} else {
xh = root;
}
}
throw GeneralError(numToString(maxIterations) + " iterations did"
"not suffice to find root with relative accuracy " +
numToString(acc) + ", last estimate: " + numToString(root)
+ ", accuracy " + numToString(std::fabs(dx / root)));
}
#endif /* NUMERICS_H_ */