| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <esl_config.h> |
| 11 |
|
| 12 |
#include <math.h> |
| 13 |
|
| 14 |
#include "easel.h" |
| 15 |
#include "esl_rootfinder.h" |
| 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 |
ESL_ROOTFINDER * |
| 47 |
esl_rootfinder_Create(int (*func)(double, void*, double*), void *params) |
| 48 |
{ |
| 49 |
int status; |
| 50 |
ESL_ROOTFINDER *R = NULL; |
| 51 |
|
| 52 |
ESL_ALLOC(R, sizeof(ESL_ROOTFINDER)); |
| 53 |
R->func = func; |
| 54 |
R->fdf = NULL; |
| 55 |
R->params = params; |
| 56 |
R->xl = -eslINFINITY; |
| 57 |
R->fl = 0.; |
| 58 |
R->xr = eslINFINITY; |
| 59 |
R->fr = 0.; |
| 60 |
R->x0 = 0.; |
| 61 |
R->f0 = 0.; |
| 62 |
R->x = 0.; |
| 63 |
R->fx = 0.; |
| 64 |
R->dfx = 0.; |
| 65 |
R->iter = 0; |
| 66 |
R->abs_tolerance = 1e-12; |
| 67 |
R->rel_tolerance = 1e-12; |
| 68 |
R->residual_tol = 0.; |
| 69 |
R->max_iter = 100; |
| 70 |
return R; |
| 71 |
|
| 72 |
ERROR: |
| 73 |
esl_rootfinder_Destroy(R); |
| 74 |
return NULL; |
| 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 |
ESL_ROOTFINDER * |
| 101 |
esl_rootfinder_CreateFDF(int (*fdf)(double, void*, double*, double*), void *params) |
| 102 |
{ |
| 103 |
int status; |
| 104 |
ESL_ROOTFINDER *R = NULL; |
| 105 |
|
| 106 |
ESL_ALLOC(R, sizeof(ESL_ROOTFINDER)); |
| 107 |
R->func = NULL; |
| 108 |
R->fdf = fdf; |
| 109 |
R->params = params; |
| 110 |
R->xl = -eslINFINITY; |
| 111 |
R->fl = 0.; |
| 112 |
R->xr = eslINFINITY; |
| 113 |
R->fr = 0.; |
| 114 |
R->x0 = 0.; |
| 115 |
R->f0 = 0.; |
| 116 |
R->x = 0.; |
| 117 |
R->fx = 0.; |
| 118 |
R->dfx = 0.; |
| 119 |
R->iter = 0; |
| 120 |
R->abs_tolerance = 1e-15; |
| 121 |
R->rel_tolerance = 1e-15; |
| 122 |
R->residual_tol = 0.; |
| 123 |
R->max_iter = 100; |
| 124 |
return R; |
| 125 |
|
| 126 |
ERROR: |
| 127 |
esl_rootfinder_Destroy(R); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int |
| 153 |
esl_rootfinder_SetBrackets(ESL_ROOTFINDER *R, double xl, double xr) |
| 154 |
{ |
| 155 |
int status; |
| 156 |
double dfx; |
| 157 |
|
| 158 |
R->xl = xl; |
| 159 |
R->xr = xr; |
| 160 |
if (R->func != NULL) { |
| 161 |
if ((status = (*R->func)(R->xl, R->params, &(R->fl))) != eslOK) return status; |
| 162 |
if ((status = (*R->func)(R->xr, R->params, &(R->fr))) != eslOK) return status; |
| 163 |
} else { |
| 164 |
if ((status = (*R->fdf) (R->xl, R->params, &(R->fl), &dfx)) != eslOK) return status; |
| 165 |
if ((status = (*R->fdf) (R->xr, R->params, &(R->fr), &dfx)) != eslOK) return status; |
| 166 |
} |
| 167 |
if (R->fl * R->fr >= 0) ESL_EXCEPTION(eslEINVAL, "xl,xr do not bracket a root"); |
| 168 |
return eslOK; |
| 169 |
} |
| 170 |
|
| 171 |
int |
| 172 |
esl_rootfinder_SetAbsoluteTolerance(ESL_ROOTFINDER *R, double tol) |
| 173 |
{ |
| 174 |
R->abs_tolerance = tol; |
| 175 |
return eslOK; |
| 176 |
} |
| 177 |
|
| 178 |
int |
| 179 |
esl_rootfinder_SetRelativeTolerance(ESL_ROOTFINDER *R, double tol) |
| 180 |
{ |
| 181 |
R->rel_tolerance = tol; |
| 182 |
return eslOK; |
| 183 |
} |
| 184 |
|
| 185 |
int |
| 186 |
esl_rootfinder_SetResidualTolerance(ESL_ROOTFINDER *R, double tol) |
| 187 |
{ |
| 188 |
R->residual_tol = tol; |
| 189 |
return eslOK; |
| 190 |
} |
| 191 |
|
| 192 |
int |
| 193 |
esl_rootfinder_SetMaxIterations(ESL_ROOTFINDER *R, int maxiter) |
| 194 |
{ |
| 195 |
R->max_iter = maxiter; |
| 196 |
return eslOK; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
void |
| 201 |
esl_rootfinder_Destroy(ESL_ROOTFINDER *R) |
| 202 |
{ |
| 203 |
if (R == NULL) return; |
| 204 |
free(R); |
| 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 |
int |
| 244 |
esl_root_Bisection(ESL_ROOTFINDER *R, double xl, double xr, double *ret_x) |
| 245 |
{ |
| 246 |
int status; |
| 247 |
double xmag; |
| 248 |
|
| 249 |
if ((status = esl_rootfinder_SetBrackets(R, xl, xr)) != eslOK) goto ERROR; |
| 250 |
|
| 251 |
while (1) { |
| 252 |
R->iter++; |
| 253 |
if (R->iter > R->max_iter) ESL_XEXCEPTION(eslENOHALT, "failed to converge in Bisection"); |
| 254 |
|
| 255 |
|
| 256 |
R->x = (R->xl+R->xr)/2.; |
| 257 |
if (R->func != NULL) { |
| 258 |
if ((status = (*R->func)(R->x, R->params, &(R->fx))) != eslOK) ESL_XEXCEPTION(status, "user-provided function failed"); |
| 259 |
} else { |
| 260 |
if ((status = (*R->fdf) (R->x, R->params, &(R->fx), &(R->dfx))) != eslOK) ESL_XEXCEPTION(status, "user-provided function failed"); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
xmag = (R->xl < 0. && R->xr > 0.) ? 0. : R->x; |
| 265 |
if (R->fx == 0.) break; |
| 266 |
if (((R->xr-R->xl) < R->abs_tolerance + R->rel_tolerance*xmag) || fabs(R->fx) < R->residual_tol) break; |
| 267 |
|
| 268 |
|
| 269 |
if (R->fl > 0.) { |
| 270 |
if (R->fx > 0.) { R->xl = R->x; R->fl = R->fx; } |
| 271 |
else { R->xr = R->x; R->fr = R->fx; } |
| 272 |
} else { |
| 273 |
if (R->fx < 0.) { R->xl = R->x; R->fl = R->fx; } |
| 274 |
else { R->xr = R->x; R->fr = R->fx; } |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
*ret_x = R->x; |
| 279 |
return eslOK; |
| 280 |
|
| 281 |
ERROR: |
| 282 |
*ret_x = 0.0; |
| 283 |
return status; |
| 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 |
int |
| 315 |
esl_root_NewtonRaphson(ESL_ROOTFINDER *R, double guess, double *ret_x) |
| 316 |
{ |
| 317 |
int status; |
| 318 |
|
| 319 |
R->x = guess; |
| 320 |
if ((status = (*R->fdf)(R->x, R->params, &(R->fx), &(R->dfx))) != eslOK) return status; |
| 321 |
|
| 322 |
while (1) { |
| 323 |
R->iter++; |
| 324 |
if (R->iter > R->max_iter) ESL_EXCEPTION(eslENOHALT, "failed to converge in Newton"); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
R->x0 = R->x; |
| 330 |
R->f0 = R->fx; |
| 331 |
R->x = R->x - R->fx / R->dfx; |
| 332 |
(*R->fdf)(R->x, R->params, &(R->fx), &(R->dfx)); |
| 333 |
|
| 334 |
|
| 335 |
if (R->fx == 0) break; |
| 336 |
if ( (fabs(R->x - R->x0) < R->abs_tolerance + R->rel_tolerance*R->x) || fabs(R->fx) < R->residual_tol) break; |
| 337 |
} |
| 338 |
|
| 339 |
*ret_x = R->x; |
| 340 |
return eslOK; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#ifdef eslROOTFINDER_TESTDRIVE |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
struct polyparams { double a,b,c; }; |
| 356 |
|
| 357 |
static int quadratic_f(double x, void *params, double *ret_fx) |
| 358 |
{ |
| 359 |
struct polyparams *p = (struct polyparams *) params; |
| 360 |
*ret_fx = (p->a * x * x + p->b * x + p->c); |
| 361 |
return eslOK; |
| 362 |
} |
| 363 |
|
| 364 |
static int quadratic_fdf(double x, void *params, double *ret_fx, double *ret_dfx) |
| 365 |
{ |
| 366 |
struct polyparams *p = (struct polyparams *) params; |
| 367 |
|
| 368 |
*ret_fx = (p->a * x * x + p->b * x + p->c); |
| 369 |
*ret_dfx = (2 * p->a) * x + p->b; |
| 370 |
return eslOK; |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
utest_Bisection(void) |
| 375 |
{ |
| 376 |
char msg[] = "esl_rootfinder:: bisection unit test failed"; |
| 377 |
ESL_ROOTFINDER *R = NULL; |
| 378 |
struct polyparams p; |
| 379 |
double x; |
| 380 |
|
| 381 |
|
| 382 |
p.a = 5.; |
| 383 |
p.b = 9.; |
| 384 |
p.c = -2.; |
| 385 |
|
| 386 |
|
| 387 |
if (( R = esl_rootfinder_Create(quadratic_f, &p) ) == NULL) esl_fatal(msg); |
| 388 |
if ( esl_root_Bisection(R, 0., 100., &x) != eslOK) esl_fatal(msg); |
| 389 |
if ( fabs(x-0.2) > R->abs_tolerance) esl_fatal(msg); |
| 390 |
esl_rootfinder_Destroy(R); |
| 391 |
|
| 392 |
|
| 393 |
if (( R = esl_rootfinder_CreateFDF(quadratic_fdf, &p) ) == NULL) esl_fatal(msg); |
| 394 |
if ( esl_root_Bisection(R, -100., 0., &x) != eslOK) esl_fatal(msg); |
| 395 |
if ( fabs(x+2.) > R->abs_tolerance) esl_fatal(msg); |
| 396 |
esl_rootfinder_Destroy(R); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
static void |
| 401 |
utest_Newton(void) |
| 402 |
{ |
| 403 |
ESL_ROOTFINDER *R = NULL; |
| 404 |
struct polyparams p; |
| 405 |
double x; |
| 406 |
|
| 407 |
|
| 408 |
p.a = 5.; |
| 409 |
p.b = 9.; |
| 410 |
p.c = -2.; |
| 411 |
|
| 412 |
R = esl_rootfinder_CreateFDF(quadratic_fdf, &p); |
| 413 |
esl_root_NewtonRaphson(R, 1., &x); |
| 414 |
if (fabs(x-0.2) > R->abs_tolerance) esl_fatal("didn't find root 0.2"); |
| 415 |
esl_rootfinder_Destroy(R); |
| 416 |
|
| 417 |
R = esl_rootfinder_CreateFDF(quadratic_fdf, &p); |
| 418 |
esl_root_NewtonRaphson(R, -3., &x); |
| 419 |
if (fabs(x+2.) > R->abs_tolerance) esl_fatal("didn't find root -2"); |
| 420 |
esl_rootfinder_Destroy(R); |
| 421 |
} |
| 422 |
|
| 423 |
#endif |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
#ifdef eslROOTFINDER_TESTDRIVE |
| 434 |
|
| 435 |
int |
| 436 |
main(int argc, char **argv) |
| 437 |
{ |
| 438 |
utest_Bisection(); |
| 439 |
utest_Newton(); |
| 440 |
|
| 441 |
return eslOK; |
| 442 |
} |
| 443 |
|
| 444 |
#endif |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
#ifdef eslROOTFINDER_EXAMPLE |
| 454 |
|
| 455 |
#include "easel.h" |
| 456 |
#include "esl_rootfinder.h" |
| 457 |
|
| 458 |
struct polyparams { double a,b,c; }; |
| 459 |
|
| 460 |
int quadratic_f(double x, void *params, double *ret_fx) |
| 461 |
{ |
| 462 |
struct polyparams *p = (struct polyparams *) params; |
| 463 |
*ret_fx = (p->a * x * x + p->b * x + p->c); |
| 464 |
return eslOK; |
| 465 |
} |
| 466 |
|
| 467 |
int main(void) |
| 468 |
{ |
| 469 |
ESL_ROOTFINDER *R = NULL; |
| 470 |
struct polyparams p; |
| 471 |
double x, fx; |
| 472 |
|
| 473 |
p.a = 5.; |
| 474 |
p.b = 2.; |
| 475 |
p.c = -1.; |
| 476 |
|
| 477 |
R = esl_rootfinder_Create(quadratic_f, &p); |
| 478 |
esl_root_Bisection(R, 0., 100., &x); |
| 479 |
|
| 480 |
quadratic_f(x, &p, &fx); |
| 481 |
printf("Find an x such that f(x) = %.0fx^2 + %.0fx + %.0f = 0 ...\n", p.a, p.b, p.c); |
| 482 |
printf("x = %f (f(x) = %f)\n", x, fx); |
| 483 |
|
| 484 |
esl_rootfinder_Destroy(R); |
| 485 |
return 0; |
| 486 |
} |
| 487 |
|
| 488 |
#endif |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
#ifdef eslROOTFINDER_EXAMPLE2 |
| 495 |
|
| 496 |
#include "easel.h" |
| 497 |
#include "esl_rootfinder.h" |
| 498 |
|
| 499 |
struct polyparams { double a,b,c; }; |
| 500 |
|
| 501 |
int quadratic_fdf(double x, void *params, double *ret_fx, double *ret_dfx) |
| 502 |
{ |
| 503 |
struct polyparams *p = (struct polyparams *) params; |
| 504 |
|
| 505 |
*ret_fx = (p->a * x * x + p->b * x + p->c); |
| 506 |
*ret_dfx = (2 * p->a) * x + p->b; |
| 507 |
return eslOK; |
| 508 |
} |
| 509 |
|
| 510 |
int main(void) |
| 511 |
{ |
| 512 |
ESL_ROOTFINDER *R = NULL; |
| 513 |
struct polyparams p; |
| 514 |
double x; |
| 515 |
|
| 516 |
p.a = 5.; |
| 517 |
p.b = 2.; |
| 518 |
p.c = -1.; |
| 519 |
|
| 520 |
R = esl_rootfinder_CreateFDF(quadratic_fdf, &p); |
| 521 |
esl_root_NewtonRaphson(R, -1., &x); |
| 522 |
|
| 523 |
printf("Find an x such that f(x) = %.0fx^2 + %.0fx + %.0f = 0 ...\n", p.a, p.b, p.c); |
| 524 |
printf("x = %f\n", x); |
| 525 |
|
| 526 |
esl_rootfinder_Destroy(R); |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
#endif |
| 531 |
|