gsl_multiroots__hybridj.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* multiroots/hybridj.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <float.h>
  25. #include "gsl_math.h"
  26. #include "gsl_errno.h"
  27. #include "gsl_multiroots.h"
  28. #include "gsl_linalg.h"
  29. #include "gsl_multiroots__dogleg.c"
  30. typedef struct
  31. {
  32. size_t iter;
  33. size_t ncfail;
  34. size_t ncsuc;
  35. size_t nslow1;
  36. size_t nslow2;
  37. double fnorm;
  38. double delta;
  39. gsl_matrix *q;
  40. gsl_matrix *r;
  41. gsl_vector *tau;
  42. gsl_vector *diag;
  43. gsl_vector *qtf;
  44. gsl_vector *newton;
  45. gsl_vector *gradient;
  46. gsl_vector *x_trial;
  47. gsl_vector *f_trial;
  48. gsl_vector *df;
  49. gsl_vector *qtdf;
  50. gsl_vector *rdx;
  51. gsl_vector *w;
  52. gsl_vector *v;
  53. }
  54. hybridj_state_t;
  55. static int hybridj_alloc (void *vstate, size_t n);
  56. static int hybridj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
  57. static int hybridsj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
  58. static int set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
  59. static int hybridj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
  60. static void hybridj_free (void *vstate);
  61. static int iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
  62. static int
  63. hybridj_alloc (void *vstate, size_t n)
  64. {
  65. hybridj_state_t *state = (hybridj_state_t *) vstate;
  66. gsl_matrix *q, *r;
  67. gsl_vector *tau, *diag, *qtf, *newton, *gradient, *x_trial, *f_trial,
  68. *df, *qtdf, *rdx, *w, *v;
  69. q = gsl_matrix_calloc (n, n);
  70. if (q == 0)
  71. {
  72. GSL_ERROR ("failed to allocate space for q", GSL_ENOMEM);
  73. }
  74. state->q = q;
  75. r = gsl_matrix_calloc (n, n);
  76. if (r == 0)
  77. {
  78. gsl_matrix_free (q);
  79. GSL_ERROR ("failed to allocate space for r", GSL_ENOMEM);
  80. }
  81. state->r = r;
  82. tau = gsl_vector_calloc (n);
  83. if (tau == 0)
  84. {
  85. gsl_matrix_free (q);
  86. gsl_matrix_free (r);
  87. GSL_ERROR ("failed to allocate space for tau", GSL_ENOMEM);
  88. }
  89. state->tau = tau;
  90. diag = gsl_vector_calloc (n);
  91. if (diag == 0)
  92. {
  93. gsl_matrix_free (q);
  94. gsl_matrix_free (r);
  95. gsl_vector_free (tau);
  96. GSL_ERROR ("failed to allocate space for diag", GSL_ENOMEM);
  97. }
  98. state->diag = diag;
  99. qtf = gsl_vector_calloc (n);
  100. if (qtf == 0)
  101. {
  102. gsl_matrix_free (q);
  103. gsl_matrix_free (r);
  104. gsl_vector_free (tau);
  105. gsl_vector_free (diag);
  106. GSL_ERROR ("failed to allocate space for qtf", GSL_ENOMEM);
  107. }
  108. state->qtf = qtf;
  109. newton = gsl_vector_calloc (n);
  110. if (newton == 0)
  111. {
  112. gsl_matrix_free (q);
  113. gsl_matrix_free (r);
  114. gsl_vector_free (tau);
  115. gsl_vector_free (diag);
  116. gsl_vector_free (qtf);
  117. GSL_ERROR ("failed to allocate space for newton", GSL_ENOMEM);
  118. }
  119. state->newton = newton;
  120. gradient = gsl_vector_calloc (n);
  121. if (gradient == 0)
  122. {
  123. gsl_matrix_free (q);
  124. gsl_matrix_free (r);
  125. gsl_vector_free (tau);
  126. gsl_vector_free (diag);
  127. gsl_vector_free (qtf);
  128. gsl_vector_free (newton);
  129. GSL_ERROR ("failed to allocate space for gradient", GSL_ENOMEM);
  130. }
  131. state->gradient = gradient;
  132. x_trial = gsl_vector_calloc (n);
  133. if (x_trial == 0)
  134. {
  135. gsl_matrix_free (q);
  136. gsl_matrix_free (r);
  137. gsl_vector_free (tau);
  138. gsl_vector_free (diag);
  139. gsl_vector_free (qtf);
  140. gsl_vector_free (newton);
  141. gsl_vector_free (gradient);
  142. GSL_ERROR ("failed to allocate space for x_trial", GSL_ENOMEM);
  143. }
  144. state->x_trial = x_trial;
  145. f_trial = gsl_vector_calloc (n);
  146. if (f_trial == 0)
  147. {
  148. gsl_matrix_free (q);
  149. gsl_matrix_free (r);
  150. gsl_vector_free (tau);
  151. gsl_vector_free (diag);
  152. gsl_vector_free (qtf);
  153. gsl_vector_free (newton);
  154. gsl_vector_free (gradient);
  155. gsl_vector_free (x_trial);
  156. GSL_ERROR ("failed to allocate space for f_trial", GSL_ENOMEM);
  157. }
  158. state->f_trial = f_trial;
  159. df = gsl_vector_calloc (n);
  160. if (df == 0)
  161. {
  162. gsl_matrix_free (q);
  163. gsl_matrix_free (r);
  164. gsl_vector_free (tau);
  165. gsl_vector_free (diag);
  166. gsl_vector_free (qtf);
  167. gsl_vector_free (newton);
  168. gsl_vector_free (gradient);
  169. gsl_vector_free (x_trial);
  170. gsl_vector_free (f_trial);
  171. GSL_ERROR ("failed to allocate space for df", GSL_ENOMEM);
  172. }
  173. state->df = df;
  174. qtdf = gsl_vector_calloc (n);
  175. if (qtdf == 0)
  176. {
  177. gsl_matrix_free (q);
  178. gsl_matrix_free (r);
  179. gsl_vector_free (tau);
  180. gsl_vector_free (diag);
  181. gsl_vector_free (qtf);
  182. gsl_vector_free (newton);
  183. gsl_vector_free (gradient);
  184. gsl_vector_free (x_trial);
  185. gsl_vector_free (f_trial);
  186. gsl_vector_free (df);
  187. GSL_ERROR ("failed to allocate space for qtdf", GSL_ENOMEM);
  188. }
  189. state->qtdf = qtdf;
  190. rdx = gsl_vector_calloc (n);
  191. if (rdx == 0)
  192. {
  193. gsl_matrix_free (q);
  194. gsl_matrix_free (r);
  195. gsl_vector_free (tau);
  196. gsl_vector_free (diag);
  197. gsl_vector_free (qtf);
  198. gsl_vector_free (newton);
  199. gsl_vector_free (gradient);
  200. gsl_vector_free (x_trial);
  201. gsl_vector_free (f_trial);
  202. gsl_vector_free (df);
  203. gsl_vector_free (qtdf);
  204. GSL_ERROR ("failed to allocate space for rdx", GSL_ENOMEM);
  205. }
  206. state->rdx = rdx;
  207. w = gsl_vector_calloc (n);
  208. if (w == 0)
  209. {
  210. gsl_matrix_free (q);
  211. gsl_matrix_free (r);
  212. gsl_vector_free (tau);
  213. gsl_vector_free (diag);
  214. gsl_vector_free (qtf);
  215. gsl_vector_free (newton);
  216. gsl_vector_free (gradient);
  217. gsl_vector_free (x_trial);
  218. gsl_vector_free (f_trial);
  219. gsl_vector_free (df);
  220. gsl_vector_free (qtdf);
  221. gsl_vector_free (rdx);
  222. GSL_ERROR ("failed to allocate space for w", GSL_ENOMEM);
  223. }
  224. state->w = w;
  225. v = gsl_vector_calloc (n);
  226. if (v == 0)
  227. {
  228. gsl_matrix_free (q);
  229. gsl_matrix_free (r);
  230. gsl_vector_free (tau);
  231. gsl_vector_free (diag);
  232. gsl_vector_free (qtf);
  233. gsl_vector_free (newton);
  234. gsl_vector_free (gradient);
  235. gsl_vector_free (x_trial);
  236. gsl_vector_free (f_trial);
  237. gsl_vector_free (df);
  238. gsl_vector_free (qtdf);
  239. gsl_vector_free (rdx);
  240. gsl_vector_free (w);
  241. GSL_ERROR ("failed to allocate space for v", GSL_ENOMEM);
  242. }
  243. state->v = v;
  244. return GSL_SUCCESS;
  245. }
  246. static int
  247. hybridj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  248. {
  249. int status = set (vstate, fdf, x, f, J, dx, 0);
  250. return status ;
  251. }
  252. static int
  253. hybridsj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  254. {
  255. int status = set (vstate, fdf, x, f, J, dx, 1);
  256. return status ;
  257. }
  258. static int
  259. set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale)
  260. {
  261. hybridj_state_t *state = (hybridj_state_t *) vstate;
  262. gsl_matrix *q = state->q;
  263. gsl_matrix *r = state->r;
  264. gsl_vector *tau = state->tau;
  265. gsl_vector *diag = state->diag;
  266. GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
  267. state->iter = 1;
  268. state->fnorm = enorm (f);
  269. state->ncfail = 0;
  270. state->ncsuc = 0;
  271. state->nslow1 = 0;
  272. state->nslow2 = 0;
  273. gsl_vector_set_all (dx, 0.0);
  274. /* Store column norms in diag */
  275. if (scale)
  276. compute_diag (J, diag);
  277. else
  278. gsl_vector_set_all (diag, 1.0);
  279. /* Set delta to factor |D x| or to factor if |D x| is zero */
  280. state->delta = compute_delta (diag, x);
  281. /* Factorize J into QR decomposition */
  282. gsl_linalg_QR_decomp (J, tau);
  283. gsl_linalg_QR_unpack (J, tau, q, r);
  284. return GSL_SUCCESS;
  285. }
  286. static int
  287. hybridj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  288. {
  289. int status = iterate (vstate, fdf, x, f, J, dx, 0);
  290. return status;
  291. }
  292. static int
  293. hybridsj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
  294. {
  295. int status = iterate (vstate, fdf, x, f, J, dx, 1);
  296. return status;
  297. }
  298. static int
  299. iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale)
  300. {
  301. hybridj_state_t *state = (hybridj_state_t *) vstate;
  302. const double fnorm = state->fnorm;
  303. gsl_matrix *q = state->q;
  304. gsl_matrix *r = state->r;
  305. gsl_vector *tau = state->tau;
  306. gsl_vector *diag = state->diag;
  307. gsl_vector *qtf = state->qtf;
  308. gsl_vector *x_trial = state->x_trial;
  309. gsl_vector *f_trial = state->f_trial;
  310. gsl_vector *df = state->df;
  311. gsl_vector *qtdf = state->qtdf;
  312. gsl_vector *rdx = state->rdx;
  313. gsl_vector *w = state->w;
  314. gsl_vector *v = state->v;
  315. double prered, actred;
  316. double pnorm, fnorm1, fnorm1p;
  317. double ratio;
  318. double p1 = 0.1, p5 = 0.5, p001 = 0.001, p0001 = 0.0001;
  319. /* Compute qtf = Q^T f */
  320. compute_qtf (q, f, qtf);
  321. /* Compute dogleg step */
  322. dogleg (r, qtf, diag, state->delta, state->newton, state->gradient, dx);
  323. /* Take a trial step */
  324. compute_trial_step (x, dx, state->x_trial);
  325. pnorm = scaled_enorm (diag, dx);
  326. if (state->iter == 1)
  327. {
  328. if (pnorm < state->delta)
  329. {
  330. state->delta = pnorm;
  331. }
  332. }
  333. /* Evaluate function at x + p */
  334. {
  335. int status = GSL_MULTIROOT_FN_EVAL_F (fdf, x_trial, f_trial);
  336. if (status != GSL_SUCCESS)
  337. {
  338. return GSL_EBADFUNC;
  339. }
  340. }
  341. /* Set df = f_trial - f */
  342. compute_df (f_trial, f, df);
  343. /* Compute the scaled actual reduction */
  344. fnorm1 = enorm (f_trial);
  345. actred = compute_actual_reduction (fnorm, fnorm1);
  346. /* Compute rdx = R dx */
  347. compute_rdx (r, dx, rdx);
  348. /* Compute the scaled predicted reduction phi1p = |Q^T f + R dx| */
  349. fnorm1p = enorm_sum (qtf, rdx);
  350. prered = compute_predicted_reduction (fnorm, fnorm1p);
  351. /* Compute the ratio of the actual to predicted reduction */
  352. if (prered > 0)
  353. {
  354. ratio = actred / prered;
  355. }
  356. else
  357. {
  358. ratio = 0;
  359. }
  360. /* Update the step bound */
  361. if (ratio < p1)
  362. {
  363. state->ncsuc = 0;
  364. state->ncfail++;
  365. state->delta *= p5;
  366. }
  367. else
  368. {
  369. state->ncfail = 0;
  370. state->ncsuc++;
  371. if (ratio >= p5 || state->ncsuc > 1)
  372. state->delta = GSL_MAX (state->delta, pnorm / p5);
  373. if (fabs (ratio - 1) <= p1)
  374. state->delta = pnorm / p5;
  375. }
  376. /* Test for successful iteration */
  377. if (ratio >= p0001)
  378. {
  379. gsl_vector_memcpy (x, x_trial);
  380. gsl_vector_memcpy (f, f_trial);
  381. state->fnorm = fnorm1;
  382. state->iter++;
  383. }
  384. /* Determine the progress of the iteration */
  385. state->nslow1++;
  386. if (actred >= p001)
  387. state->nslow1 = 0;
  388. if (actred >= p1)
  389. state->nslow2 = 0;
  390. if (state->ncfail == 2)
  391. {
  392. {
  393. int status = GSL_MULTIROOT_FN_EVAL_DF (fdf, x, J);
  394. if (status != GSL_SUCCESS)
  395. {
  396. return GSL_EBADFUNC;
  397. }
  398. }
  399. state->nslow2++;
  400. if (state->iter == 1)
  401. {
  402. if (scale)
  403. compute_diag (J, diag);
  404. state->delta = compute_delta (diag, x);
  405. }
  406. else
  407. {
  408. if (scale)
  409. update_diag (J, diag);
  410. }
  411. /* Factorize J into QR decomposition */
  412. gsl_linalg_QR_decomp (J, tau);
  413. gsl_linalg_QR_unpack (J, tau, q, r);
  414. return GSL_SUCCESS;
  415. }
  416. /* Compute qtdf = Q^T df, w = (Q^T df - R dx)/|dx|, v = D^2 dx/|dx| */
  417. compute_qtf (q, df, qtdf);
  418. compute_wv (qtdf, rdx, dx, diag, pnorm, w, v);
  419. /* Rank-1 update of the jacobian Q'R' = Q(R + w v^T) */
  420. gsl_linalg_QR_update (q, r, w, v);
  421. /* No progress as measured by jacobian evaluations */
  422. if (state->nslow2 == 5)
  423. {
  424. return GSL_ENOPROGJ;
  425. }
  426. /* No progress as measured by function evaluations */
  427. if (state->nslow1 == 10)
  428. {
  429. return GSL_ENOPROG;
  430. }
  431. return GSL_SUCCESS;
  432. }
  433. static void
  434. hybridj_free (void *vstate)
  435. {
  436. hybridj_state_t *state = (hybridj_state_t *) vstate;
  437. gsl_vector_free (state->v);
  438. gsl_vector_free (state->w);
  439. gsl_vector_free (state->rdx);
  440. gsl_vector_free (state->qtdf);
  441. gsl_vector_free (state->df);
  442. gsl_vector_free (state->f_trial);
  443. gsl_vector_free (state->x_trial);
  444. gsl_vector_free (state->gradient);
  445. gsl_vector_free (state->newton);
  446. gsl_vector_free (state->qtf);
  447. gsl_vector_free (state->diag);
  448. gsl_vector_free (state->tau);
  449. gsl_matrix_free (state->r);
  450. gsl_matrix_free (state->q);
  451. }
  452. static const gsl_multiroot_fdfsolver_type hybridj_type =
  453. {
  454. "hybridj", /* name */
  455. sizeof (hybridj_state_t),
  456. &hybridj_alloc,
  457. &hybridj_set,
  458. &hybridj_iterate,
  459. &hybridj_free
  460. };
  461. static const gsl_multiroot_fdfsolver_type hybridsj_type =
  462. {
  463. "hybridsj", /* name */
  464. sizeof (hybridj_state_t),
  465. &hybridj_alloc,
  466. &hybridsj_set,
  467. &hybridsj_iterate,
  468. &hybridj_free
  469. };
  470. const gsl_multiroot_fdfsolver_type *gsl_multiroot_fdfsolver_hybridj = &hybridj_type;
  471. const gsl_multiroot_fdfsolver_type *gsl_multiroot_fdfsolver_hybridsj = &hybridsj_type;