laserIMUCalibration
lmmin.c
Go to the documentation of this file.
1 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <math.h>
19 #include <float.h>
20 #include "lmmin.h"
21 
28 #define MIN(a,b) (((a)<=(b)) ? (a) : (b))
29 
35 #define MAX(a,b) (((a)>=(b)) ? (a) : (b))
36 
42 #define SQR(x) (x)*(x)
43 
61 void lm_lmpar( int n, double *r, int ldr, int *ipvt, double *diag,
62  double *qtb, double delta, double *par, double *x,
63  double *sdiag, double *aux, double *xdi );
76 void lm_qrfac( int m, int n, double *a, int *ipvt,
77  double *rdiag, double *acnorm, double *wa );
92 void lm_qrsolv( int n, double *r, int ldr, int *ipvt, double *diag,
93  double *qtb, double *x, double *sdiag, double *wa );
94 
95 
96 /*****************************************************************************/
97 /* Numeric constants */
98 /*****************************************************************************/
99 
100 /* machine-dependent constants from float.h */
101 #define LM_MACHEP DBL_EPSILON /* resolution of arithmetic */
102 #define LM_DWARF DBL_MIN /* smallest nonzero number */
103 #define LM_SQRT_DWARF sqrt(DBL_MIN) /* square should not underflow */
104 #define LM_SQRT_GIANT sqrt(DBL_MAX) /* square should not overflow */
105 #define LM_USERTOL 30*LM_MACHEP /* users are recommended to require this */
106 
107 /* If the above values do not work, the following seem good for an x86:
108  LM_MACHEP .555e-16
109  LM_DWARF 9.9e-324
110  LM_SQRT_DWARF 1.e-160
111  LM_SQRT_GIANT 1.e150
112  LM_USER_TOL 1.e-14
113  The following values should work on any machine:
114  LM_MACHEP 1.2e-16
115  LM_DWARF 1.0e-38
116  LM_SQRT_DWARF 3.834e-20
117  LM_SQRT_GIANT 1.304e19
118  LM_USER_TOL 1.e-14
119 */
120 
122  LM_USERTOL, LM_USERTOL, LM_USERTOL, LM_USERTOL, 100., 100, 1,
123  NULL, 0, -1, -1
124 };
126  1.e-7, 1.e-7, 1.e-7, 1.e-7, 100., 100, 1,
127  NULL, 0, -1, -1
128 };
129 
130 
131 /*****************************************************************************/
132 /* Message texts (indexed by status.info) */
133 /*****************************************************************************/
134 
135 const char *lm_infmsg[] = {
136  "found zero (sum of squares below underflow limit)",
137  "converged (the relative error in the sum of squares is at most tol)",
138  "converged (the relative error of the parameter vector is at most tol)",
139  "converged (both errors are at most tol)",
140  "trapped (by degeneracy; increasing epsilon might help)",
141  "exhausted (number of function calls exceeding preset patience)",
142  "failed (ftol<tol: cannot reduce sum of squares any further)",
143  "failed (xtol<tol: cannot improve approximate solution any further)",
144  "failed (gtol<tol: cannot improve approximate solution any further)",
145  "crashed (not enough memory)",
146  "exploded (fatal coding error: improper input parameters)",
147  "stopped (break requested within function evaluation)"
148 };
149 
150 const char *lm_shortmsg[] = {
151  "found zero",
152  "converged (f)",
153  "converged (p)",
154  "converged (2)",
155  "degenerate",
156  "call limit",
157  "failed (f)",
158  "failed (p)",
159  "failed (o)",
160  "no memory",
161  "invalid input",
162  "user break"
163 };
164 
165 
166 /*****************************************************************************/
167 /* Monitoring auxiliaries. */
168 /*****************************************************************************/
169 
170 void lm_print_pars( int nout, const double *par, double fnorm, FILE* fout )
171 {
172  int i;
173  for (i = 0; i < nout; ++i)
174  fprintf( fout, " %16.9g", par[i] );
175  fprintf( fout, " => %18.11g\n", fnorm );
176 }
177 
178 
179 /*****************************************************************************/
180 /* lmmin (main minimization routine) */
181 /*****************************************************************************/
182 
183 void lmmin( int n, double *x, int m, const void *data,
184  void (*evaluate) (const double *par, int m_dat, const void *data,
185  double *fvec, int *userbreak),
186  const lm_control_struct *C, lm_status_struct *S )
187 {
188  double *fvec, *diag, *fjac, *qtf, *wa1, *wa2, *wa3, *wf;
189  int *ipvt;
190  int j, i;
191  double actred, dirder, fnorm, fnorm1, gnorm, pnorm,
192  prered, ratio, step, sum, temp, temp1, temp2, temp3;
193  static double p0001 = 1.0e-4;
194 
195  int maxfev = C->patience * (n + 1);
196 
197  int outer, inner; /* loop counters, for monitoring */
198  int inner_success; /* flag for loop control */
199  double lmpar = 0; /* Levenberg-Marquardt parameter */
200  double delta = 0;
201  double xnorm = 0;
202  double eps = sqrt(MAX(C->epsilon, LM_MACHEP)); /* for forward differences */
203 
204  int nout = C->n_maxpri == -1 ? n : MIN( C->n_maxpri, n );
205 
206  /* The workaround msgfile=NULL is needed for default initialization */
207  FILE* msgfile = C->msgfile ? C->msgfile : stdout;
208 
209  /* Default status info; must be set ahead of first return statements */
210  S->outcome = 0; /* status code */
211  S->userbreak = 0;
212  S->nfev = 0; /* function evaluation counter */
213 
214  /*** Check input parameters for errors. ***/
215 
216  if ( n <= 0 ) {
217  fprintf( stderr, "lmmin: invalid number of parameters %i\n", n );
218  S->outcome = 10; /* invalid parameter */
219  return;
220  }
221  if (m < n) {
222  fprintf( stderr, "lmmin: number of data points (%i) "
223  "smaller than number of parameters (%i)\n", m, n );
224  S->outcome = 10;
225  return;
226  }
227  if (C->ftol < 0. || C->xtol < 0. || C->gtol < 0.) {
228  fprintf( stderr,
229  "lmmin: negative tolerance (at least one of %g %g %g)\n",
230  C->ftol, C->xtol, C->gtol );
231  S->outcome = 10;
232  return;
233  }
234  if (maxfev <= 0) {
235  fprintf( stderr, "lmmin: nonpositive function evaluations limit %i\n",
236  maxfev );
237  S->outcome = 10;
238  return;
239  }
240  if (C->stepbound <= 0.) {
241  fprintf( stderr, "lmmin: nonpositive stepbound %g\n", C->stepbound );
242  S->outcome = 10;
243  return;
244  }
245  if (C->scale_diag != 0 && C->scale_diag != 1) {
246  fprintf( stderr, "lmmin: logical variable scale_diag=%i, "
247  "should be 0 or 1\n", C->scale_diag );
248  S->outcome = 10;
249  return;
250  }
251 
252  /*** Allocate work space. ***/
253 
254  if ( (fvec = (double *) malloc(m * sizeof(double))) == NULL ||
255  (diag = (double *) malloc(n * sizeof(double))) == NULL ||
256  (qtf = (double *) malloc(n * sizeof(double))) == NULL ||
257  (fjac = (double *) malloc(n * m * sizeof(double))) == NULL ||
258  (wa1 = (double *) malloc(n * sizeof(double))) == NULL ||
259  (wa2 = (double *) malloc(n * sizeof(double))) == NULL ||
260  (wa3 = (double *) malloc(n * sizeof(double))) == NULL ||
261  (wf = (double *) malloc(m * sizeof(double))) == NULL ||
262  (ipvt = (int *) malloc(n * sizeof(int) )) == NULL ) {
263  S->outcome = 9;
264  return;
265  }
266 
267  if (!C->scale_diag) {
268  for (j = 0; j < n; j++)
269  diag[j] = 1.;
270  }
271 
272  /*** Evaluate function at starting point and calculate norm. ***/
273 
274  (*evaluate)( x, m, data, fvec, &(S->userbreak) );
275  S->nfev = 1;
276  if ( S->userbreak )
277  goto terminate;
278  fnorm = lm_enorm(m, fvec);
279  if ( C->verbosity ) {
280  fprintf( msgfile, "lmmin start " );
281  lm_print_pars( nout, x, fnorm, msgfile );
282  }
283  if ( fnorm <= LM_DWARF ) {
284  S->outcome = 0; /* sum of squares almost zero, nothing to do */
285  goto terminate;
286  }
287 
288  /*** The outer loop: compute gradient, then descend. ***/
289 
290  for ( outer = 0; ; ++outer ) {
291 
292  /*** [outer] Calculate the Jacobian. ***/
293 
294  for (j = 0; j < n; j++) {
295  temp = x[j];
296  step = MAX(eps * eps, eps * fabs(temp));
297  x[j] += step; /* replace temporarily */
298  (*evaluate)( x, m, data, wf, &(S->userbreak) );
299  ++(S->nfev);
300  if ( S->userbreak )
301  goto terminate;
302  for (i = 0; i < m; i++)
303  fjac[j * m + i] = (wf[i] - fvec[i]) / step;
304  x[j] = temp; /* restore */
305  }
306  if ( C->verbosity >= 10 ) {
307  /* print the entire matrix */
308  printf("\nlmmin Jacobian\n");
309  for (i = 0; i < m; i++) {
310  printf(" ");
311  for (j = 0; j < n; j++)
312  printf("%.5e ", fjac[j * m + i]);
313  printf("\n");
314  }
315  }
316 
317  /*** [outer] Compute the QR factorization of the Jacobian. ***/
318 
319  /* fjac is an m by n array. The upper n by n submatrix of fjac
320  * is made to contain an upper triangular matrix r with diagonal
321  * elements of nonincreasing magnitude such that
322  *
323  * p^T*(jac^T*jac)*p = r^T*r
324  *
325  * (NOTE: ^T stands for matrix transposition),
326  *
327  * where p is a permutation matrix and jac is the final calculated
328  * Jacobian. Column j of p is column ipvt(j) of the identity matrix.
329  * The lower trapezoidal part of fjac contains information generated
330  * during the computation of r.
331  *
332  * ipvt is an integer array of length n. It defines a permutation
333  * matrix p such that jac*p = q*r, where jac is the final calculated
334  * Jacobian, q is orthogonal (not stored), and r is upper triangular
335  * with diagonal elements of nonincreasing magnitude. Column j of p
336  * is column ipvt(j) of the identity matrix.
337  */
338 
339  lm_qrfac(m, n, fjac, ipvt, wa1, wa2, wa3);
340  /* return values are ipvt, wa1=rdiag, wa2=acnorm */
341 
342  /*** [outer] Form q^T * fvec and store first n components in qtf. ***/
343 
344  for (i = 0; i < m; i++)
345  wf[i] = fvec[i];
346 
347  for (j = 0; j < n; j++) {
348  temp3 = fjac[j * m + j];
349  if (temp3 != 0.) {
350  sum = 0;
351  for (i = j; i < m; i++)
352  sum += fjac[j * m + i] * wf[i];
353  temp = -sum / temp3;
354  for (i = j; i < m; i++)
355  wf[i] += fjac[j * m + i] * temp;
356  }
357  fjac[j * m + j] = wa1[j];
358  qtf[j] = wf[j];
359  }
360 
361  /*** [outer] Compute norm of scaled gradient and detect degeneracy. ***/
362 
363  gnorm = 0;
364  for (j = 0; j < n; j++) {
365  if (wa2[ipvt[j]] == 0)
366  continue;
367  sum = 0.;
368  for (i = 0; i <= j; i++)
369  sum += fjac[j * m + i] * qtf[i];
370  gnorm = MAX( gnorm, fabs( sum / wa2[ipvt[j]] / fnorm ) );
371  }
372 
373  if (gnorm <= C->gtol) {
374  S->outcome = 4;
375  goto terminate;
376  }
377 
378  /*** [outer] Initialize / update diag and delta. ***/
379 
380  if ( !outer ) {
381  /* first iteration only */
382  if (C->scale_diag) {
383  /* diag := norms of the columns of the initial Jacobian */
384  for (j = 0; j < n; j++)
385  diag[j] = wa2[j] ? wa2[j] : 1;
386  /* xnorm := || D x || */
387  for (j = 0; j < n; j++)
388  wa3[j] = diag[j] * x[j];
389  xnorm = lm_enorm(n, wa3);
390  if ( C->verbosity >= 2 ) {
391  fprintf( msgfile, "lmmin diag " );
392  lm_print_pars( nout, x, xnorm, msgfile );
393  }
394  /* only now print the header for the loop table */
395  if ( C->verbosity >= 3 ) {
396  fprintf( msgfile, " o i lmpar prered"
397  " ratio dirder delta"
398  " pnorm fnorm" );
399  for (i = 0; i < nout; ++i)
400  fprintf( msgfile, " p%i", i );
401  fprintf( msgfile, "\n" );
402  }
403  } else {
404  xnorm = lm_enorm(n, x);
405  }
406  /* initialize the step bound delta. */
407  if ( xnorm )
408  delta = C->stepbound * xnorm;
409  else
410  delta = C->stepbound;
411  } else {
412  if (C->scale_diag) {
413  for (j = 0; j < n; j++)
414  diag[j] = MAX( diag[j], wa2[j] );
415  }
416  }
417 
418  /*** The inner loop. ***/
419  inner = 0;
420  do {
421 
422  /*** [inner] Determine the Levenberg-Marquardt parameter. ***/
423 
424  lm_lmpar( n, fjac, m, ipvt, diag, qtf, delta, &lmpar,
425  wa1, wa2, wf, wa3 );
426  /* used return values are fjac (partly), lmpar, wa1=x, wa3=diag*x */
427 
428  /* predict scaled reduction */
429  pnorm = lm_enorm(n, wa3);
430  temp2 = lmpar * SQR( pnorm / fnorm );
431  for (j = 0; j < n; j++) {
432  wa3[j] = 0;
433  for (i = 0; i <= j; i++)
434  wa3[i] -= fjac[j * m + i] * wa1[ipvt[j]];
435  }
436  temp1 = SQR( lm_enorm(n, wa3) / fnorm );
437  prered = temp1 + 2 * temp2;
438  dirder = -temp1 + temp2; /* scaled directional derivative */
439 
440  /* at first call, adjust the initial step bound. */
441  if ( !outer && pnorm < delta )
442  delta = pnorm;
443 
444  /*** [inner] Evaluate the function at x + p. ***/
445 
446  for (j = 0; j < n; j++)
447  wa2[j] = x[j] - wa1[j];
448 
449  (*evaluate)( wa2, m, data, wf, &(S->userbreak) );
450  ++(S->nfev);
451  if ( S->userbreak )
452  goto terminate;
453  fnorm1 = lm_enorm(m, wf);
454 
455  /*** [inner] Evaluate the scaled reduction. ***/
456 
457  /* actual scaled reduction */
458  actred = 1 - SQR(fnorm1 / fnorm);
459 
460  /* ratio of actual to predicted reduction */
461  ratio = prered ? actred / prered : 0;
462 
463  if ( C->verbosity == 2 ) {
464  fprintf( msgfile, "lmmin (%i:%i) ", outer, inner );
465  lm_print_pars( nout, wa2, fnorm1, msgfile );
466  } else if ( C->verbosity >= 3 ) {
467  printf( "%3i %2i %9.2g %9.2g %14.6g"
468  " %9.2g %10.3e %10.3e %21.15e",
469  outer, inner, lmpar, prered, ratio,
470  dirder, delta, pnorm, fnorm1 );
471  for (i = 0; i < nout; ++i)
472  fprintf( msgfile, " %16.9g", wa2[i] );
473  fprintf( msgfile, "\n" );
474  }
475 
476  /* update the step bound */
477  if ( ratio <= 0.25 ) {
478  if ( actred >= 0 )
479  temp = 0.5;
480  else if ( actred > -99 ) /* -99 = 1-1/0.1^2 */
481  temp = MAX( dirder / (2 * dirder + actred), 0.1 );
482  else
483  temp = 0.1;
484  delta = temp * MIN(delta, pnorm / 0.1);
485  lmpar /= temp;
486  } else if ( ratio >= 0.75 ) {
487  delta = 2 * pnorm;
488  lmpar *= 0.5;
489  } else if ( !lmpar ) {
490  delta = 2 * pnorm;
491  }
492 
493  /*** [inner] On success, update solution, and test for convergence. ***/
494 
495  inner_success = ratio >= p0001;
496  if ( inner_success ) {
497 
498  /* update x, fvec, and their norms */
499  if (C->scale_diag) {
500  for (j = 0; j < n; j++) {
501  x[j] = wa2[j];
502  wa2[j] = diag[j] * x[j];
503  }
504  } else {
505  for (j = 0; j < n; j++)
506  x[j] = wa2[j];
507  }
508  for (i = 0; i < m; i++)
509  fvec[i] = wf[i];
510  xnorm = lm_enorm(n, wa2);
511  fnorm = fnorm1;
512  }
513 
514  /* convergence tests */
515  S->outcome = 0;
516  if ( fnorm <= LM_DWARF )
517  goto terminate; /* success: sum of squares almost zero */
518  /* test two criteria (both may be fulfilled) */
519  if (fabs(actred) <= C->ftol && prered <= C->ftol && ratio <= 2)
520  S->outcome = 1; /* success: x almost stable */
521  if (delta <= C->xtol * xnorm)
522  S->outcome += 2; /* success: sum of squares almost stable */
523  if (S->outcome != 0) {
524  goto terminate;
525  }
526 
527  /*** [inner] Tests for termination and stringent tolerances. ***/
528 
529  if ( S->nfev >= maxfev ) {
530  S->outcome = 5;
531  goto terminate;
532  }
533  if ( fabs(actred) <= LM_MACHEP &&
534  prered <= LM_MACHEP && ratio <= 2 ) {
535  S->outcome = 6;
536  goto terminate;
537  }
538  if ( delta <= LM_MACHEP * xnorm ) {
539  S->outcome = 7;
540  goto terminate;
541  }
542  if ( gnorm <= LM_MACHEP ) {
543  S->outcome = 8;
544  goto terminate;
545  }
546 
547  /*** [inner] End of the loop. Repeat if iteration unsuccessful. ***/
548 
549  ++inner;
550  } while ( !inner_success );
551 
552  /*** [outer] End of the loop. ***/
553 
554  };
555 
556 terminate:
557  S->fnorm = lm_enorm(m, fvec);
558  if ( C->verbosity >= 2 )
559  printf("lmmin outcome (%i) xnorm %g ftol %g xtol %g\n",
560  S->outcome, xnorm, C->ftol, C->xtol );
561  if ( C->verbosity & 1 ) {
562  fprintf( msgfile, "lmmin final " );
563  lm_print_pars( nout, x, S->fnorm, msgfile );
564  }
565  if ( S->userbreak ) /* user-requested break */
566  S->outcome = 11;
567 
568  /*** Deallocate the workspace. ***/
569  free(fvec);
570  free(diag);
571  free(qtf);
572  free(fjac);
573  free(wa1);
574  free(wa2);
575  free(wa3);
576  free(wf);
577  free(ipvt);
578 
579 } /*** lmmin. ***/
580 
581 
582 /*****************************************************************************/
583 /* lm_lmpar (determine Levenberg-Marquardt parameter) */
584 /*****************************************************************************/
585 
586 void lm_lmpar(int n, double *r, int ldr, int *ipvt, double *diag,
587  double *qtb, double delta, double *par, double *x,
588  double *sdiag, double *aux, double *xdi)
589 {
590  /* Given an m by n matrix a, an n by n nonsingular diagonal
591  * matrix d, an m-vector b, and a positive number delta,
592  * the problem is to determine a value for the parameter
593  * par such that if x solves the system
594  *
595  * a*x = b and sqrt(par)*d*x = 0
596  *
597  * in the least squares sense, and dxnorm is the euclidean
598  * norm of d*x, then either par=0 and (dxnorm-delta) < 0.1*delta,
599  * or par>0 and abs(dxnorm-delta) < 0.1*delta.
600  *
601  * Using lm_qrsolv, this subroutine completes the solution of the problem
602  * if it is provided with the necessary information from the
603  * qr factorization, with column pivoting, of a. That is, if
604  * a*p = q*r, where p is a permutation matrix, q has orthogonal
605  * columns, and r is an upper triangular matrix with diagonal
606  * elements of nonincreasing magnitude, then lmpar expects
607  * the full upper triangle of r, the permutation matrix p,
608  * and the first n components of qT*b. On output
609  * lmpar also provides an upper triangular matrix s such that
610  *
611  * p^T*(a^T*a + par*d*d)*p = s^T*s.
612  *
613  * s is employed within lmpar and may be of separate interest.
614  *
615  * Only a few iterations are generally needed for convergence
616  * of the algorithm. If, however, the limit of 10 iterations
617  * is reached, then the output par will contain the best
618  * value obtained so far.
619  *
620  * parameters:
621  *
622  * n is a positive integer input variable set to the order of r.
623  *
624  * r is an n by n array. on input the full upper triangle
625  * must contain the full upper triangle of the matrix r.
626  * on OUTPUT the full upper triangle is unaltered, and the
627  * strict lower triangle contains the strict upper triangle
628  * (transposed) of the upper triangular matrix s.
629  *
630  * ldr is a positive integer input variable not less than n
631  * which specifies the leading dimension of the array r.
632  *
633  * ipvt is an integer input array of length n which defines the
634  * permutation matrix p such that a*p = q*r. column j of p
635  * is column ipvt(j) of the identity matrix.
636  *
637  * diag is an input array of length n which must contain the
638  * diagonal elements of the matrix d.
639  *
640  * qtb is an input array of length n which must contain the first
641  * n elements of the vector (q transpose)*b.
642  *
643  * delta is a positive input variable which specifies an upper
644  * bound on the euclidean norm of d*x.
645  *
646  * par is a nonnegative variable. on input par contains an
647  * initial estimate of the levenberg-marquardt parameter.
648  * on OUTPUT par contains the final estimate.
649  *
650  * x is an OUTPUT array of length n which contains the least
651  * squares solution of the system a*x = b, sqrt(par)*d*x = 0,
652  * for the output par.
653  *
654  * sdiag is an array of length n needed as workspace; on OUTPUT
655  * it contains the diagonal elements of the upper triangular matrix s.
656  *
657  * aux is a multi-purpose work array of length n.
658  *
659  * xdi is a work array of length n. On OUTPUT: diag[j] * x[j].
660  *
661  */
662  int i, iter, j, nsing;
663  double dxnorm, fp, fp_old, gnorm, parc, parl, paru;
664  double sum, temp;
665  static double p1 = 0.1;
666 
667  /*** lmpar: compute and store in x the gauss-newton direction. if the
668  jacobian is rank-deficient, obtain a least squares solution. ***/
669 
670  nsing = n;
671  for (j = 0; j < n; j++) {
672  aux[j] = qtb[j];
673  if (r[j * ldr + j] == 0 && nsing == n)
674  nsing = j;
675  if (nsing < n)
676  aux[j] = 0;
677  }
678  for (j = nsing - 1; j >= 0; j--) {
679  aux[j] = aux[j] / r[j + ldr * j];
680  temp = aux[j];
681  for (i = 0; i < j; i++)
682  aux[i] -= r[j * ldr + i] * temp;
683  }
684 
685  for (j = 0; j < n; j++)
686  x[ipvt[j]] = aux[j];
687 
688  /*** lmpar: initialize the iteration counter, evaluate the function at the
689  origin, and test for acceptance of the gauss-newton direction. ***/
690 
691  for (j = 0; j < n; j++)
692  xdi[j] = diag[j] * x[j];
693  dxnorm = lm_enorm(n, xdi);
694  fp = dxnorm - delta;
695  if (fp <= p1 * delta) {
696 #ifdef LMFIT_DEBUG_MESSAGES
697  printf("debug lmpar nsing %d n %d, terminate (fp<p1*delta)\n",
698  nsing, n);
699 #endif
700  *par = 0;
701  return;
702  }
703 
704  /*** lmpar: if the jacobian is not rank deficient, the newton
705  step provides a lower bound, parl, for the 0. of
706  the function. otherwise set this bound to 0.. ***/
707 
708  parl = 0;
709  if (nsing >= n) {
710  for (j = 0; j < n; j++)
711  aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
712 
713  for (j = 0; j < n; j++) {
714  sum = 0.;
715  for (i = 0; i < j; i++)
716  sum += r[j * ldr + i] * aux[i];
717  aux[j] = (aux[j] - sum) / r[j + ldr * j];
718  }
719  temp = lm_enorm(n, aux);
720  parl = fp / delta / temp / temp;
721  }
722 
723  /*** lmpar: calculate an upper bound, paru, for the 0. of the function. ***/
724 
725  for (j = 0; j < n; j++) {
726  sum = 0;
727  for (i = 0; i <= j; i++)
728  sum += r[j * ldr + i] * qtb[i];
729  aux[j] = sum / diag[ipvt[j]];
730  }
731  gnorm = lm_enorm(n, aux);
732  paru = gnorm / delta;
733  if (paru == 0.)
734  paru = LM_DWARF / MIN(delta, p1);
735 
736  /*** lmpar: if the input par lies outside of the interval (parl,paru),
737  set par to the closer endpoint. ***/
738 
739  *par = MAX(*par, parl);
740  *par = MIN(*par, paru);
741  if (*par == 0.)
742  *par = gnorm / dxnorm;
743 
744  /*** lmpar: iterate. ***/
745 
746  for (iter = 0; ; iter++) {
747 
750  if (*par == 0.)
751  *par = MAX(LM_DWARF, 0.001 * paru);
752  temp = sqrt(*par);
753  for (j = 0; j < n; j++)
754  aux[j] = temp * diag[j];
755 
756  lm_qrsolv( n, r, ldr, ipvt, aux, qtb, x, sdiag, xdi );
757  /* return values are r, x, sdiag */
758 
759  for (j = 0; j < n; j++)
760  xdi[j] = diag[j] * x[j]; /* used as output */
761  dxnorm = lm_enorm(n, xdi);
762  fp_old = fp;
763  fp = dxnorm - delta;
764 
769  if (fabs(fp) <= p1 * delta
770  || (parl == 0. && fp <= fp_old && fp_old < 0.)
771  || iter == 10) {
772 #ifdef LMFIT_DEBUG_MESSAGES
773  printf("debug lmpar nsing %d iter %d "
774  "par %.4e [%.4e %.4e] delta %.4e fp %.4e\n",
775  nsing, iter, *par, parl, paru, delta, fp);
776 #endif
777  break; /* the only exit from the iteration. */
778  }
779 
782  for (j = 0; j < n; j++)
783  aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
784 
785  for (j = 0; j < n; j++) {
786  aux[j] = aux[j] / sdiag[j];
787  for (i = j + 1; i < n; i++)
788  aux[i] -= r[j * ldr + i] * aux[j];
789  }
790  temp = lm_enorm(n, aux);
791  parc = fp / delta / temp / temp;
792 
795  if (fp > 0)
796  parl = MAX(parl, *par);
797  else if (fp < 0)
798  paru = MIN(paru, *par);
799  /* the case fp==0 is precluded by the break condition */
800 
803  *par = MAX(parl, *par + parc);
804 
805  }
806 
807 } /*** lm_lmpar. ***/
808 
809 /*****************************************************************************/
810 /* lm_qrfac (QR factorization, from lapack) */
811 /*****************************************************************************/
812 
813 void lm_qrfac(int m, int n, double *a, int *ipvt,
814  double *rdiag, double *acnorm, double *wa)
815 {
816  /*
817  * This subroutine uses Householder transformations with column
818  * pivoting (optional) to compute a qr factorization of the
819  * m by n matrix a. That is, qrfac determines an orthogonal
820  * matrix q, a permutation matrix p, and an upper trapezoidal
821  * matrix r with diagonal elements of nonincreasing magnitude,
822  * such that a*p = q*r. The Householder transformation for
823  * column k, k = 1,2,...,min(m,n), is of the form
824  *
825  * i - (1/u(k))*u*uT
826  *
827  * where u has zeroes in the first k-1 positions. The form of
828  * this transformation and the method of pivoting first
829  * appeared in the corresponding linpack subroutine.
830  *
831  * Parameters:
832  *
833  * m is a positive integer input variable set to the number
834  * of rows of a.
835  *
836  * n is a positive integer input variable set to the number
837  * of columns of a.
838  *
839  * a is an m by n array. On input a contains the matrix for
840  * which the qr factorization is to be computed. On OUTPUT
841  * the strict upper trapezoidal part of a contains the strict
842  * upper trapezoidal part of r, and the lower trapezoidal
843  * part of a contains a factored form of q (the non-trivial
844  * elements of the u vectors described above).
845  *
846  * ipvt is an integer OUTPUT array of length lipvt. This array
847  * defines the permutation matrix p such that a*p = q*r.
848  * Column j of p is column ipvt(j) of the identity matrix.
849  *
850  * rdiag is an OUTPUT array of length n which contains the
851  * diagonal elements of r.
852  *
853  * acnorm is an OUTPUT array of length n which contains the
854  * norms of the corresponding columns of the input matrix a.
855  * If this information is not needed, then acnorm can coincide
856  * with rdiag.
857  *
858  * wa is a work array of length n.
859  *
860  */
861  int i, j, k, kmax, minmn;
862  double ajnorm, sum, temp;
863 
864  /*** qrfac: compute initial column norms and initialize several arrays. ***/
865 
866  for (j = 0; j < n; j++) {
867  acnorm[j] = lm_enorm(m, &a[j * m]);
868  rdiag[j] = acnorm[j];
869  wa[j] = rdiag[j];
870  ipvt[j] = j;
871  }
872 #ifdef LMFIT_DEBUG_MESSAGES
873  printf("debug qrfac\n");
874 #endif
875 
876  /*** qrfac: reduce a to r with Householder transformations. ***/
877 
878  minmn = MIN(m, n);
879  for (j = 0; j < minmn; j++) {
880 
883  kmax = j;
884  for (k = j + 1; k < n; k++)
885  if (rdiag[k] > rdiag[kmax])
886  kmax = k;
887  if (kmax == j)
888  goto pivot_ok;
889 
890  for (i = 0; i < m; i++) {
891  temp = a[j * m + i];
892  a[j * m + i] = a[kmax * m + i];
893  a[kmax * m + i] = temp;
894  }
895  rdiag[kmax] = rdiag[j];
896  wa[kmax] = wa[j];
897  k = ipvt[j];
898  ipvt[j] = ipvt[kmax];
899  ipvt[kmax] = k;
900 
901 pivot_ok:
905  ajnorm = lm_enorm(m - j, &a[j * m + j]);
906  if (ajnorm == 0.) {
907  rdiag[j] = 0;
908  continue;
909  }
910 
911  if (a[j * m + j] < 0.)
912  ajnorm = -ajnorm;
913  for (i = j; i < m; i++)
914  a[j * m + i] /= ajnorm;
915  a[j * m + j] += 1;
916 
920  for (k = j + 1; k < n; k++) {
921  sum = 0;
922 
923  for (i = j; i < m; i++)
924  sum += a[j * m + i] * a[k * m + i];
925 
926  temp = sum / a[j + m * j];
927 
928  for (i = j; i < m; i++)
929  a[k * m + i] -= temp * a[j * m + i];
930 
931  if (rdiag[k] != 0.) {
932  temp = a[m * k + j] / rdiag[k];
933  temp = MAX(0., 1 - temp * temp);
934  rdiag[k] *= sqrt(temp);
935  temp = rdiag[k] / wa[k];
936  if ( 0.05 * SQR(temp) <= LM_MACHEP ) {
937  rdiag[k] = lm_enorm(m - j - 1, &a[m * k + j + 1]);
938  wa[k] = rdiag[k];
939  }
940  }
941  }
942 
943  rdiag[j] = -ajnorm;
944  }
945 } /*** lm_qrfac. ***/
946 
947 
948 /*****************************************************************************/
949 /* lm_qrsolv (linear least-squares) */
950 /*****************************************************************************/
951 
952 void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag,
953  double *qtb, double *x, double *sdiag, double *wa)
954 {
955  /*
956  * Given an m by n matrix a, an n by n diagonal matrix d,
957  * and an m-vector b, the problem is to determine an x which
958  * solves the system
959  *
960  * a*x = b and d*x = 0
961  *
962  * in the least squares sense.
963  *
964  * This subroutine completes the solution of the problem
965  * if it is provided with the necessary information from the
966  * qr factorization, with column pivoting, of a. That is, if
967  * a*p = q*r, where p is a permutation matrix, q has orthogonal
968  * columns, and r is an upper triangular matrix with diagonal
969  * elements of nonincreasing magnitude, then qrsolv expects
970  * the full upper triangle of r, the permutation matrix p,
971  * and the first n components of (q transpose)*b. The system
972  * a*x = b, d*x = 0, is then equivalent to
973  *
974  * r*z = q^T*b, p^T*d*p*z = 0,
975  *
976  * where x = p*z. If this system does not have full rank,
977  * then a least squares solution is obtained. On output qrsolv
978  * also provides an upper triangular matrix s such that
979  *
980  * p^T *(a^T *a + d*d)*p = s^T *s.
981  *
982  * s is computed within qrsolv and may be of separate interest.
983  *
984  * Parameters
985  *
986  * n is a positive integer input variable set to the order of r.
987  *
988  * r is an n by n array. On input the full upper triangle
989  * must contain the full upper triangle of the matrix r.
990  * On OUTPUT the full upper triangle is unaltered, and the
991  * strict lower triangle contains the strict upper triangle
992  * (transposed) of the upper triangular matrix s.
993  *
994  * ldr is a positive integer input variable not less than n
995  * which specifies the leading dimension of the array r.
996  *
997  * ipvt is an integer input array of length n which defines the
998  * permutation matrix p such that a*p = q*r. Column j of p
999  * is column ipvt(j) of the identity matrix.
1000  *
1001  * diag is an input array of length n which must contain the
1002  * diagonal elements of the matrix d.
1003  *
1004  * qtb is an input array of length n which must contain the first
1005  * n elements of the vector (q transpose)*b.
1006  *
1007  * x is an OUTPUT array of length n which contains the least
1008  * squares solution of the system a*x = b, d*x = 0.
1009  *
1010  * sdiag is an OUTPUT array of length n which contains the
1011  * diagonal elements of the upper triangular matrix s.
1012  *
1013  * wa is a work array of length n.
1014  *
1015  */
1016  int i, kk, j, k, nsing;
1017  double qtbpj, sum, temp;
1018  double _sin, _cos, _tan, _cot; /* local variables, not functions */
1019 
1020  /*** qrsolv: copy r and q^T*b to preserve input and initialize s.
1021  in particular, save the diagonal elements of r in x. ***/
1022 
1023  for (j = 0; j < n; j++) {
1024  for (i = j; i < n; i++)
1025  r[j * ldr + i] = r[i * ldr + j];
1026  x[j] = r[j * ldr + j];
1027  wa[j] = qtb[j];
1028  }
1029  /*** qrsolv: eliminate the diagonal matrix d using a Givens rotation. ***/
1030 
1031  for (j = 0; j < n; j++) {
1032 
1033  /*** qrsolv: prepare the row of d to be eliminated, locating the
1034  diagonal element using p from the qr factorization. ***/
1035 
1036  if (diag[ipvt[j]] == 0.)
1037  goto L90;
1038  for (k = j; k < n; k++)
1039  sdiag[k] = 0.;
1040  sdiag[j] = diag[ipvt[j]];
1041 
1042  /*** qrsolv: the transformations to eliminate the row of d modify only
1043  a single element of qT*b beyond the first n, which is initially 0. ***/
1044 
1045  qtbpj = 0.;
1046  for (k = j; k < n; k++) {
1047 
1051  if (sdiag[k] == 0.)
1052  continue;
1053  kk = k + ldr * k;
1054  if (fabs(r[kk]) < fabs(sdiag[k])) {
1055  _cot = r[kk] / sdiag[k];
1056  _sin = 1 / sqrt(1 + SQR(_cot));
1057  _cos = _sin * _cot;
1058  } else {
1059  _tan = sdiag[k] / r[kk];
1060  _cos = 1 / sqrt(1 + SQR(_tan));
1061  _sin = _cos * _tan;
1062  }
1063 
1067  r[kk] = _cos * r[kk] + _sin * sdiag[k];
1068  temp = _cos * wa[k] + _sin * qtbpj;
1069  qtbpj = -_sin * wa[k] + _cos * qtbpj;
1070  wa[k] = temp;
1071 
1074  for (i = k + 1; i < n; i++) {
1075  temp = _cos * r[k * ldr + i] + _sin * sdiag[i];
1076  sdiag[i] = -_sin * r[k * ldr + i] + _cos * sdiag[i];
1077  r[k * ldr + i] = temp;
1078  }
1079  }
1080 
1081 L90:
1085  sdiag[j] = r[j * ldr + j];
1086  r[j * ldr + j] = x[j];
1087  }
1088 
1089  /*** qrsolv: solve the triangular system for z. if the system is
1090  singular, then obtain a least squares solution. ***/
1091 
1092  nsing = n;
1093  for (j = 0; j < n; j++) {
1094  if (sdiag[j] == 0. && nsing == n)
1095  nsing = j;
1096  if (nsing < n)
1097  wa[j] = 0;
1098  }
1099 
1100  for (j = nsing - 1; j >= 0; j--) {
1101  sum = 0;
1102  for (i = j + 1; i < nsing; i++)
1103  sum += r[j * ldr + i] * wa[i];
1104  wa[j] = (wa[j] - sum) / sdiag[j];
1105  }
1106 
1107  /*** qrsolv: permute the components of z back to components of x. ***/
1108 
1109  for (j = 0; j < n; j++)
1110  x[ipvt[j]] = wa[j];
1111 
1112 } /*** lm_qrsolv. ***/
1113 
1114 
1115 /*****************************************************************************/
1116 /* lm_enorm (Euclidean norm) */
1117 /*****************************************************************************/
1118 
1119 double lm_enorm(int n, const double *x)
1120 {
1121  /* Given an n-vector x, this function calculates the
1122  * euclidean norm of x.
1123  *
1124  * The euclidean norm is computed by accumulating the sum of
1125  * squares in three different sums. The sums of squares for the
1126  * small and large components are scaled so that no overflows
1127  * occur. Non-destructive underflows are permitted. Underflows
1128  * and overflows do not occur in the computation of the unscaled
1129  * sum of squares for the intermediate components.
1130  * The definitions of small, intermediate and large components
1131  * depend on two constants, LM_SQRT_DWARF and LM_SQRT_GIANT. The main
1132  * restrictions on these constants are that LM_SQRT_DWARF**2 not
1133  * underflow and LM_SQRT_GIANT**2 not overflow.
1134  *
1135  * Parameters
1136  *
1137  * n is a positive integer input variable.
1138  *
1139  * x is an input array of length n.
1140  */
1141  int i;
1142  double agiant, s1, s2, s3, xabs, x1max, x3max, temp;
1143 
1144  s1 = 0;
1145  s2 = 0;
1146  s3 = 0;
1147  x1max = 0;
1148  x3max = 0;
1149  agiant = LM_SQRT_GIANT / n;
1150 
1153  for (i = 0; i < n; i++) {
1154  xabs = fabs(x[i]);
1155  if (xabs > LM_SQRT_DWARF) {
1156  if ( xabs < agiant ) {
1157  s2 += xabs * xabs;
1158  } else if ( xabs > x1max ) {
1159  temp = x1max / xabs;
1160  s1 = 1 + s1 * SQR(temp);
1161  x1max = xabs;
1162  } else {
1163  temp = xabs / x1max;
1164  s1 += SQR(temp);
1165  }
1166  } else if ( xabs > x3max ) {
1167  temp = x3max / xabs;
1168  s3 = 1 + s3 * SQR(temp);
1169  x3max = xabs;
1170  } else if (xabs != 0.) {
1171  temp = xabs / x3max;
1172  s3 += SQR(temp);
1173  }
1174  }
1175 
1178  if (s1 != 0)
1179  return x1max * sqrt(s1 + (s2 / x1max) / x1max);
1180  else if (s2 != 0)
1181  if (s2 >= x3max)
1182  return sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
1183  else
1184  return sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
1185  else
1186  return x3max * sqrt(s3);
1187 
1188 } /*** lm_enorm. ***/
double epsilon
Step used to calculate the Jacobian.
Definition: lmstruct.h:59
Declarations for Levenberg-Marquardt minimization.
const lm_control_struct lm_control_float
Preset (and recommended) control parameter settings.
Definition: lmmin.c:125
Collection of input parameters for fit control.
Definition: lmstruct.h:33
void lm_qrfac(int m, int n, double *a, int *ipvt, double *rdiag, double *acnorm, double *wa)
[brief description]
Definition: lmmin.c:813
int n_maxpri
Maximum parameters to print.
Definition: lmstruct.h:95
double lm_enorm(int n, const double *x)
Refined calculation of Eucledian norm.
Definition: lmmin.c:1119
#define LM_SQRT_DWARF
Definition: lmmin.c:103
#define LM_DWARF
Definition: lmmin.c:102
int patience
Used to set the maximum number of function evaluations.
Definition: lmstruct.h:73
const char * lm_infmsg[]
Preset message texts.
Definition: lmmin.c:135
#define LM_SQRT_GIANT
Definition: lmmin.c:104
int userbreak
Set when function evaluation requests termination.
Definition: lmstruct.h:124
const lm_control_struct lm_control_double
Preset (and recommended) control parameter settings.
Definition: lmmin.c:121
Collection of output parameters for status info.
Definition: lmstruct.h:106
double stepbound
Used in determining the initial step bound.
Definition: lmstruct.h:68
int verbosity
Controls the ouput verbosity.
Definition: lmstruct.h:90
#define SQR(x)
[brief description]
Definition: lmmin.c:42
double xtol
Relative error between last two approximations.
Definition: lmstruct.h:46
const char * lm_shortmsg[]
Preset message texts.
Definition: lmmin.c:150
FILE * msgfile
Progress messages will be written to this file.
Definition: lmstruct.h:82
void lm_print_pars(int nout, const double *par, double fnorm, FILE *fout)
Definition: lmmin.c:170
int scale_diag
If 1, the variables will be rescaled internally.
Definition: lmstruct.h:78
void lm_lmpar(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double delta, double *par, double *x, double *sdiag, double *aux, double *xdi)
[brief description]
Definition: lmmin.c:586
double gtol
Orthogonality desired between fvec and its derivs.
Definition: lmstruct.h:53
void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double *x, double *sdiag, double *wa)
[brief description]
Definition: lmmin.c:952
#define MAX(a, b)
[brief description]
Definition: lmmin.c:35
#define LM_MACHEP
Definition: lmmin.c:101
void lmmin(int n, double *x, int m, const void *data, void(*evaluate)(const double *par, int m_dat, const void *data, double *fvec, int *userbreak), const lm_control_struct *C, lm_status_struct *S)
Levenberg-Marquardt minimization.
Definition: lmmin.c:183
#define MIN(a, b)
[brief description]
Definition: lmmin.c:28
int outcome
Status indicator.
Definition: lmstruct.h:120
int nfev
actual number of iterations.
Definition: lmstruct.h:114
double fnorm
norm of the residue vector fvec.
Definition: lmstruct.h:110
double ftol
Relative error desired in the sum of squares.
Definition: lmstruct.h:40
#define LM_USERTOL
Definition: lmmin.c:105