-
Notifications
You must be signed in to change notification settings - Fork 1
/
brentq.c
139 lines (114 loc) · 3.71 KB
/
brentq.c
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
/* Written by Charles Harris [email protected] */
/* Modified to this project by Kasimir Tanner [email protected] */
#include <math.h>
#include "zeros.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/*
At the top of the loop the situation is the following:
1. the root is bracketed between xa and xb
2. xa is the most recent estimate
3. xp is the previous estimate
4. |fp| < |fb|
The order of xa and xp doesn't matter, but assume xp < xb. Then xa lies to
the right of xp and the assumption is that xa is increasing towards the root.
In this situation we will attempt quadratic extrapolation as long as the
condition
* |fa| < |fp| < |fb|
is satisfied. That is, the function value is decreasing as we go along.
Note the 4 above implies that the right inequlity already holds.
The first check is that xa is still to the left of the root. If not, xb is
replaced by xp and the interval reverses, with xb < xa. In this situation
we will try linear interpolation. That this has happened is signaled by the
equality xb == xp;
The second check is that |fa| < |fb|. If this is not the case, we swap
xa and xb and resort to bisection.
*/
// Compilation command used: gcc -shared -o brentq.so brentq.c
double optim(double z, double y, double V, double w_prime)
{
double a = y*z;
if (a < 0) {
return (y*V) / (1.0 + exp(a)) + w_prime - z;
}
else {
return (y*V*exp(-a)) / (1.0 + exp(-a)) + w_prime - z;
}
}
double brentq(double xa, double xb, double xtol, double rtol,
int iter, double y, double V, double w_prime)
{
double xpre = xa, xcur = xb;
double xblk = 0., fpre, fcur, fblk = 0., spre = 0., scur = 0., sbis;
/* the tolerance is 2*delta */
double delta;
double stry, dpre, dblk;
int i;
fpre = optim(xpre, y, V, w_prime);
fcur = optim(xcur, y, V, w_prime);
if (fpre == 0) {
return xpre;
}
if (fcur == 0) {
return xcur;
}
if (signbit(fpre)==signbit(fcur)) {
return 0.;
}
for (i = 0; i < iter; i++) {
if (fpre != 0 && fcur != 0 &&
(signbit(fpre) != signbit(fcur))) {
xblk = xpre;
fblk = fpre;
spre = scur = xcur - xpre;
}
if (fabs(fblk) < fabs(fcur)) {
xpre = xcur;
xcur = xblk;
xblk = xpre;
fpre = fcur;
fcur = fblk;
fblk = fpre;
}
delta = (xtol + rtol*fabs(xcur))/2;
sbis = (xblk - xcur)/2;
if (fcur == 0 || fabs(sbis) < delta) {
return xcur;
}
if (fabs(spre) > delta && fabs(fcur) < fabs(fpre)) {
if (xpre == xblk) {
/* interpolate */
stry = -fcur*(xcur - xpre)/(fcur - fpre);
}
else {
/* extrapolate */
dpre = (fpre - fcur)/(xpre - xcur);
dblk = (fblk - fcur)/(xblk - xcur);
stry = -fcur*(fblk*dblk - fpre*dpre)
/(dblk*dpre*(fblk - fpre));
}
if (2*fabs(stry) < MIN(fabs(spre), 3*fabs(sbis) - delta)) {
/* good short step */
spre = scur;
scur = stry;
} else {
/* bisect */
spre = sbis;
scur = sbis;
}
}
else {
/* bisect */
spre = sbis;
scur = sbis;
}
xpre = xcur; fpre = fcur;
if (fabs(scur) > delta) {
xcur += scur;
}
else {
xcur += (sbis > 0 ? delta : -delta);
}
fcur = optim(xcur, y, V, w_prime);
}
return xcur;
}