annotate pyPRADA_1.2/tools/samtools-0.1.16/bcftools/kmin.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 /* The MIT License
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 Copyright (c) 2008, 2010 by Attractive Chaos <attractor@live.co.uk>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 a copy of this software and associated documentation files (the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 the following conditions:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 SOFTWARE.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 /* Hooke-Jeeves algorithm for nonlinear minimization
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 Based on the pseudocodes by Bell and Pike (CACM 9(9):684-685), and
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 the revision by Tomlin and Smith (CACM 12(11):637-638). Both of the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 papers are comments on Kaupe's Algorithm 178 "Direct Search" (ACM
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 6(6):313-314). The original algorithm was designed by Hooke and
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 Jeeves (ACM 8:212-229). This program is further revised according to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 Johnson's implementation at Netlib (opt/hooke.c).
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 Hooke-Jeeves algorithm is very simple and it works quite well on a
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 few examples. However, it might fail to converge due to its heuristic
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 nature. A possible improvement, as is suggested by Johnson, may be to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 choose a small r at the beginning to quickly approach to the minimum
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 and a large r at later step to hit the minimum.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 #include <math.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 #include "kmin.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 static double __kmin_hj_aux(kmin_f func, int n, double *x1, void *data, double fx1, double *dx, int *n_calls)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 int k, j = *n_calls;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 double ftmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 for (k = 0; k != n; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 x1[k] += dx[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 ftmp = func(n, x1, data); ++j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 if (ftmp < fx1) fx1 = ftmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 else { /* search the opposite direction */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 dx[k] = 0.0 - dx[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 x1[k] += dx[k] + dx[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 ftmp = func(n, x1, data); ++j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 if (ftmp < fx1) fx1 = ftmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 else x1[k] -= dx[k]; /* back to the original x[k] */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 *n_calls = j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 return fx1; /* here: fx1=f(n,x1) */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 double kmin_hj(kmin_f func, int n, double *x, void *data, double r, double eps, int max_calls)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 double fx, fx1, *x1, *dx, radius;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 int k, n_calls = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 x1 = (double*)calloc(n, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 dx = (double*)calloc(n, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 for (k = 0; k != n; ++k) { /* initial directions, based on MGJ */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 dx[k] = fabs(x[k]) * r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 if (dx[k] == 0) dx[k] = r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 radius = r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 fx1 = fx = func(n, x, data); ++n_calls;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 for (;;) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 memcpy(x1, x, n * sizeof(double)); /* x1 = x */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 fx1 = __kmin_hj_aux(func, n, x1, data, fx, dx, &n_calls);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 while (fx1 < fx) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 for (k = 0; k != n; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 double t = x[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 dx[k] = x1[k] > x[k]? fabs(dx[k]) : 0.0 - fabs(dx[k]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 x[k] = x1[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 x1[k] = x1[k] + x1[k] - t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 fx = fx1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 if (n_calls >= max_calls) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 fx1 = func(n, x1, data); ++n_calls;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 fx1 = __kmin_hj_aux(func, n, x1, data, fx1, dx, &n_calls);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 if (fx1 >= fx) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 for (k = 0; k != n; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 if (fabs(x1[k] - x[k]) > .5 * fabs(dx[k])) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 if (k == n) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 if (radius >= eps) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 if (n_calls >= max_calls) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 radius *= r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 for (k = 0; k != n; ++k) dx[k] *= r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 } else break; /* converge */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 free(x1); free(dx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 return fx1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 // I copied this function somewhere several years ago with some of my modifications, but I forgot the source.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 double kmin_brent(kmin1_f func, double a, double b, void *data, double tol, double *xmin)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 double bound, u, r, q, fu, tmp, fa, fb, fc, c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 const double gold1 = 1.6180339887;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 const double gold2 = 0.3819660113;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 const double tiny = 1e-20;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 const int max_iter = 100;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 double e, d, w, v, mid, tol1, tol2, p, eold, fv, fw;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 int iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 fa = func(a, data); fb = func(b, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 if (fb > fa) { // swap, such that f(a) > f(b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 tmp = a; a = b; b = tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 tmp = fa; fa = fb; fb = tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 c = b + gold1 * (b - a), fc = func(c, data); // golden section extrapolation
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 while (fb > fc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 bound = b + 100.0 * (c - b); // the farthest point where we want to go
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 r = (b - a) * (fb - fc);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 q = (b - c) * (fb - fa);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 if (fabs(q - r) < tiny) { // avoid 0 denominator
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 tmp = q > r? tiny : 0.0 - tiny;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 } else tmp = q - r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 u = b - ((b - c) * q - (b - a) * r) / (2.0 * tmp); // u is the parabolic extrapolation point
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 if ((b > u && u > c) || (b < u && u < c)) { // u lies between b and c
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 if (fu < fc) { // (b,u,c) bracket the minimum
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 a = b; b = u; fa = fb; fb = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 } else if (fu > fb) { // (a,b,u) bracket the minimum
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 c = u; fc = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 u = c + gold1 * (c - b); fu = func(u, data); // golden section extrapolation
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 } else if ((c > u && u > bound) || (c < u && u < bound)) { // u lies between c and bound
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 if (fu < fc) { // fb > fc > fu
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 b = c; c = u; u = c + gold1 * (c - b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 fb = fc; fc = fu; fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 } else { // (b,c,u) bracket the minimum
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 a = b; b = c; c = u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 fa = fb; fb = fc; fc = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 } else if ((u > bound && bound > c) || (u < bound && bound < c)) { // u goes beyond the bound
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 u = bound; fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 } else { // u goes the other way around, use golden section extrapolation
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 u = c + gold1 * (c - b); fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 a = b; b = c; c = u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 fa = fb; fb = fc; fc = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 if (a > c) u = a, a = c, c = u; // swap
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 // now, a<b<c, fa>fb and fb<fc, move on to Brent's algorithm
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 e = d = 0.0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 w = v = b; fv = fw = fb;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 for (iter = 0; iter != max_iter; ++iter) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 mid = 0.5 * (a + c);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 tol2 = 2.0 * (tol1 = tol * fabs(b) + tiny);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 if (fabs(b - mid) <= (tol2 - 0.5 * (c - a))) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 *xmin = b; return fb; // found
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 if (fabs(e) > tol1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 // related to parabolic interpolation
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 r = (b - w) * (fb - fv);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 q = (b - v) * (fb - fw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 p = (b - v) * q - (b - w) * r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 q = 2.0 * (q - r);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 if (q > 0.0) p = 0.0 - p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 else q = 0.0 - q;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 eold = e; e = d;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 if (fabs(p) >= fabs(0.5 * q * eold) || p <= q * (a - b) || p >= q * (c - b)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 d = gold2 * (e = (b >= mid ? a - b : c - b));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 d = p / q; u = b + d; // actual parabolic interpolation happens here
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 if (u - a < tol2 || c - u < tol2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 d = (mid > b)? tol1 : 0.0 - tol1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 } else d = gold2 * (e = (b >= mid ? a - b : c - b)); // golden section interpolation
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 u = fabs(d) >= tol1 ? b + d : b + (d > 0.0? tol1 : -tol1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 fu = func(u, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 if (fu <= fb) { // u is the minimum point so far
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 if (u >= b) a = b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 else c = b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 v = w; w = b; b = u; fv = fw; fw = fb; fb = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 } else { // adjust (a,c) and (u,v,w)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 if (u < b) a = u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 else c = u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 if (fu <= fw || w == b) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 v = w; w = u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 fv = fw; fw = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 } else if (fu <= fv || v == b || v == w) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 v = u; fv = fu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 *xmin = b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 return fb;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 }