ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
lmmin.cxx
Go to the documentation of this file.
1 /*
2  * Project: LevenbergMarquardtLeastSquaresFitting
3  *
4  * File: lmmin.c
5  *
6  * Contents: Levenberg-Marquardt core implementation,
7  * and simplified user interface.
8  *
9  * Author: Joachim Wuttke <j.wuttke@fz-juelich.de>
10  *
11  * Homepage: joachimwuttke.de/lmfit
12  */
13 
14 /*
15  * lmfit is released under the LMFIT-BEER-WARE licence:
16  *
17  * In writing this software, I borrowed heavily from the public domain,
18  * especially from work by Burton S. Garbow, Kenneth E. Hillstrom,
19  * Jorge J. MorĂ©, Steve Moshier, and the authors of lapack. To avoid
20  * unneccessary complications, I put my additions and amendments also
21  * into the public domain. Please retain this notice. Otherwise feel
22  * free to do whatever you want with this stuff. If we meet some day,
23  * and you think this work is worth it, you can buy me a beer in return.
24  */
25 
26 #include "lmmin.h"
27 
28 #include <cfloat>
29 #include <cmath>
30 #include <cstdio>
31 #include <cstdlib>
32 
33 /*****************************************************************************/
34 /* set numeric constants */
35 /*****************************************************************************/
36 
37 /* machine-dependent constants from float.h */
38 #define LM_MACHEP DBL_EPSILON /* resolution of arithmetic */
39 #define LM_DWARF DBL_MIN /* smallest nonzero number */
40 #define LM_SQRT_DWARF sqrt(DBL_MIN) /* square should not underflow */
41 #define LM_SQRT_GIANT sqrt(DBL_MAX) /* square should not overflow */
42 #define LM_USERTOL 30 * LM_MACHEP /* users are recommended to require this */
43 
44 /* If the above values do not work, the following seem good for an x86:
45  LM_MACHEP .555e-16
46  LM_DWARF 9.9e-324
47  LM_SQRT_DWARF 1.e-160
48  LM_SQRT_GIANT 1.e150
49  LM_USER_TOL 1.e-14
50  The following values should work on any machine:
51  LM_MACHEP 1.2e-16
52  LM_DWARF 1.0e-38
53  LM_SQRT_DWARF 3.834e-20
54  LM_SQRT_GIANT 1.304e19
55  LM_USER_TOL 1.e-14
56 */
57 
59 const lm_control_struct lm_control_float = {1.e-7, 1.e-7, 1.e-7, 1.e-7, 100., 100, 0, 0};
60 
61 /*****************************************************************************/
62 /* set message texts (indexed by status.info) */
63 /*****************************************************************************/
64 
65 const char *lm_infmsg[] = {"success (sum of squares below underflow limit)",
66  "success (the relative error in the sum of squares is at most tol)",
67  "success (the relative error between x and the solution is at most tol)",
68  "success (both errors are at most tol)",
69  "trapped by degeneracy (fvec is orthogonal to the columns of the jacobian)",
70  "timeout (number of calls to fcn has reached maxcall*(n+1))",
71  "failure (ftol<tol: cannot reduce sum of squares any further)",
72  "failure (xtol<tol: cannot improve approximate solution any further)",
73  "failure (gtol<tol: cannot improve approximate solution any further)",
74  "exception (not enough memory)",
75  "fatal coding error (improper input parameters)",
76  "exception (break requested within function evaluation)"};
77 
78 const char *lm_shortmsg[] = {"success (0)", "success (f)", "success (p)", "success (f,p)",
79  "degenerate", "call limit", "failed (f)", "failed (p)",
80  "failed (o)", "no memory", "invalid input", "user break"};
81 
82 /*****************************************************************************/
83 /* lm_printout_std (default monitoring routine) */
84 /*****************************************************************************/
85 
86 void lm_printout_std(int n_par, const double *par, int m_dat, const void *data, const double *fvec, int printflags,
87  int iflag, int iter, int nfev)
88 /*
89  * data : for soft control of printout behaviour, add control
90  * variables to the data struct
91  * iflag : 0 (init) 1 (outer loop) 2(inner loop) -1(terminated)
92  * iter : outer loop counter
93  * nfev : number of calls to *evaluate
94  */
95 {
96  int i = 0;
97 
98  if (!printflags)
99  return;
100 
101  if (printflags & 1) {
102  /* location of printout call within lmdif */
103  if (iflag == 2) {
104  printf("trying step in gradient direction ");
105  } else if (iflag == 1) {
106  printf("determining gradient (iteration %2d)", iter);
107  } else if (iflag == 0) {
108  printf("starting minimization ");
109  } else if (iflag == -1) {
110  printf("terminated after %3d evaluations ", nfev);
111  }
112  }
113 
114  if (printflags & 2) {
115  printf(" par: ");
116  for (i = 0; i < n_par; ++i)
117  printf(" %18.11g", par[i]);
118  printf(" => norm: %18.11g", lm_enorm(m_dat, fvec));
119  }
120 
121  if (printflags & 3)
122  printf("\n");
123 
124  if ((printflags & 8) || ((printflags & 4) && iflag == -1)) {
125  printf(" residuals:\n");
126  for (i = 0; i < m_dat; ++i)
127  printf(" fvec[%2d]=%12g\n", i, fvec[i]);
128  }
129 }
130 
131 /*****************************************************************************/
132 /* lm_minimize (intermediate-level interface) */
133 /*****************************************************************************/
134 
135 void lmmin(int n_par, double *par, int m_dat, const void *data,
136  void (*evaluate)(const double *par, int m_dat, const void *data, double *fvec, int *info),
137  const lm_control_struct *control, lm_status_struct *status,
138  void (*printout)(int n_par, const double *par, int m_dat, const void *data, const double *fvec,
139  int printflags, int iflag, int iter, int nfev))
140 {
141 
142  /*** allocate work space. ***/
143 
144  double *fvec, *diag, *fjac, *qtf, *wa1, *wa2, *wa3, *wa4; // NOLINT
145  int *ipvt = nullptr, j = 0;
146 
147  int n = n_par;
148  int m = m_dat;
149 
150  if ((fvec = (double *)malloc(m * sizeof(double))) == nullptr ||
151  (diag = (double *)malloc(n * sizeof(double))) == nullptr ||
152  (qtf = (double *)malloc(n * sizeof(double))) == nullptr ||
153  (fjac = (double *)malloc(n * m * sizeof(double))) == nullptr ||
154  (wa1 = (double *)malloc(n * sizeof(double))) == nullptr ||
155  (wa2 = (double *)malloc(n * sizeof(double))) == nullptr ||
156  (wa3 = (double *)malloc(n * sizeof(double))) == nullptr ||
157  (wa4 = (double *)malloc(m * sizeof(double))) == nullptr || (ipvt = (int *)malloc(n * sizeof(int))) == nullptr) {
158  status->info = 9; // NOLINT
159  return;
160  }
161 
162  if (!control->scale_diag)
163  for (j = 0; j < n_par; ++j)
164  diag[j] = 1;
165 
166  /*** perform fit. ***/
167 
168  status->info = 0;
169 
170  /* this goes through the modified legacy interface: */
171  lm_lmdif(m, n, par, fvec, control->ftol, control->xtol, control->gtol, control->maxcall * (n + 1), control->epsilon,
172  diag, (control->scale_diag ? 1 : 2), control->stepbound, &(status->info), &(status->nfev), fjac, ipvt, qtf,
173  wa1, wa2, wa3, wa4, evaluate, printout, control->printflags, data);
174 
175  if (printout)
176  (*printout)(n, par, m, data, fvec, control->printflags, -1, 0, status->nfev);
177  status->fnorm = lm_enorm(m, fvec);
178  if (status->info < 0)
179  status->info = 11;
180 
181  /*** clean up. ***/
182 
183  free(fvec);
184  free(diag);
185  free(qtf);
186  free(fjac);
187  free(wa1);
188  free(wa2);
189  free(wa3);
190  free(wa4);
191  free(ipvt);
192 } /*** lmmin. ***/
193 
194 /*****************************************************************************/
195 /* lm_lmdif (low-level, modified legacy interface for full control) */
196 /*****************************************************************************/
197 
198 void lm_lmpar(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double delta, double *par, double *x,
199  double *sdiag, double *aux, double *xdi);
200 void lm_qrfac(int m, int n, double *a, int pivot, int *ipvt, double *rdiag, double *acnorm, double *wa);
201 void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double *x, double *sdiag, double *wa);
202 
203 #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
204 #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
205 #define SQR(x) (x) * (x)
206 
207 void lm_lmdif(int m, int n, double *x, double *fvec, double ftol, double xtol, double gtol, int maxfev, double epsfcn,
208  double *diag, int mode, double factor, int *info, int *nfev, double *fjac, int *ipvt, double *qtf,
209  double *wa1, double *wa2, double *wa3, double *wa4,
210  void (*evaluate)(const double *par, int m_dat, const void *data, double *fvec, int *info),
211  void (*printout)(int n_par, const double *par, int m_dat, const void *data, const double *fvec,
212  int printflags, int iflag, int iter, int nfev),
213  int printflags, const void *data)
214 {
215  /*
216  * The purpose of lmdif is to minimize the sum of the squares of
217  * m nonlinear functions in n variables by a modification of
218  * the levenberg-marquardt algorithm. The user must provide a
219  * subroutine evaluate which calculates the functions. The jacobian
220  * is then calculated by a forward-difference approximation.
221  *
222  * The multi-parameter interface lm_lmdif is for users who want
223  * full control and flexibility. Most users will be better off using
224  * the simpler interface lmmin provided above.
225  *
226  * Parameters:
227  *
228  * m is a positive integer input variable set to the number
229  * of functions.
230  *
231  * n is a positive integer input variable set to the number
232  * of variables; n must not exceed m.
233  *
234  * x is an array of length n. On input x must contain an initial
235  * estimate of the solution vector. On OUTPUT x contains the
236  * final estimate of the solution vector.
237  *
238  * fvec is an OUTPUT array of length m which contains
239  * the functions evaluated at the output x.
240  *
241  * ftol is a nonnegative input variable. Termination occurs when
242  * both the actual and predicted relative reductions in the sum
243  * of squares are at most ftol. Therefore, ftol measures the
244  * relative error desired in the sum of squares.
245  *
246  * xtol is a nonnegative input variable. Termination occurs when
247  * the relative error between two consecutive iterates is at
248  * most xtol. Therefore, xtol measures the relative error desired
249  * in the approximate solution.
250  *
251  * gtol is a nonnegative input variable. Termination occurs when
252  * the cosine of the angle between fvec and any column of the
253  * jacobian is at most gtol in absolute value. Therefore, gtol
254  * measures the orthogonality desired between the function vector
255  * and the columns of the jacobian.
256  *
257  * maxfev is a positive integer input variable. Termination
258  * occurs when the number of calls to lm_fcn is at least
259  * maxfev by the end of an iteration.
260  *
261  * epsfcn is an input variable used in choosing a step length for
262  * the forward-difference approximation. The relative errors in
263  * the functions are assumed to be of the order of epsfcn.
264  *
265  * diag is an array of length n. If mode = 1 (see below), diag is
266  * internally set. If mode = 2, diag must contain positive entries
267  * that serve as multiplicative scale factors for the variables.
268  *
269  * mode is an integer input variable. If mode = 1, the
270  * variables will be scaled internally. If mode = 2,
271  * the scaling is specified by the input diag.
272  *
273  * factor is a positive input variable used in determining the
274  * initial step bound. This bound is set to the product of
275  * factor and the euclidean norm of diag*x if nonzero, or else
276  * to factor itself. In most cases factor should lie in the
277  * interval (0.1,100.0). Generally, the value 100.0 is recommended.
278  *
279  * info is an integer OUTPUT variable that indicates the termination
280  * status of lm_lmdif as follows:
281  *
282  * info < 0 termination requested by user-supplied routine *evaluate;
283  *
284  * info = 0 fnorm almost vanishing;
285  *
286  * info = 1 both actual and predicted relative reductions
287  * in the sum of squares are at most ftol;
288  *
289  * info = 2 relative error between two consecutive iterates
290  * is at most xtol;
291  *
292  * info = 3 conditions for info = 1 and info = 2 both hold;
293  *
294  * info = 4 the cosine of the angle between fvec and any
295  * column of the jacobian is at most gtol in
296  * absolute value;
297  *
298  * info = 5 number of calls to lm_fcn has reached or
299  * exceeded maxfev;
300  *
301  * info = 6 ftol is too small: no further reduction in
302  * the sum of squares is possible;
303  *
304  * info = 7 xtol is too small: no further improvement in
305  * the approximate solution x is possible;
306  *
307  * info = 8 gtol is too small: fvec is orthogonal to the
308  * columns of the jacobian to machine precision;
309  *
310  * info =10 improper input parameters;
311  *
312  * nfev is an OUTPUT variable set to the number of calls to the
313  * user-supplied routine *evaluate.
314  *
315  * fjac is an OUTPUT m by n array. The upper n by n submatrix
316  * of fjac contains an upper triangular matrix r with
317  * diagonal elements of nonincreasing magnitude such that
318  *
319  * pT*(jacT*jac)*p = rT*r
320  *
321  * (NOTE: T stands for matrix transposition),
322  *
323  * where p is a permutation matrix and jac is the final
324  * calculated jacobian. Column j of p is column ipvt(j)
325  * (see below) of the identity matrix. The lower trapezoidal
326  * part of fjac contains information generated during
327  * the computation of r.
328  *
329  * ipvt is an integer OUTPUT array of length n. It defines a
330  * permutation matrix p such that jac*p = q*r, where jac is
331  * the final calculated jacobian, q is orthogonal (not stored),
332  * and r is upper triangular with diagonal elements of
333  * nonincreasing magnitude. Column j of p is column ipvt(j)
334  * of the identity matrix.
335  *
336  * qtf is an OUTPUT array of length n which contains
337  * the first n elements of the vector (q transpose)*fvec.
338  *
339  * wa1, wa2, and wa3 are work arrays of length n.
340  *
341  * wa4 is a work array of length m, used among others to hold
342  * residuals from evaluate.
343  *
344  * evaluate points to the subroutine which calculates the
345  * m nonlinear functions. Implementations should be written as follows:
346  *
347  * void evaluate( double* par, int m_dat, void *data,
348  * double* fvec, int *info )
349  * {
350  * // for ( i=0; i<m_dat; ++i )
351  * // calculate fvec[i] for given parameters par;
352  * // to stop the minimization,
353  * // set *info to a negative integer.
354  * }
355  *
356  * printout points to the subroutine which informs about fit progress.
357  * Call with printout=0 if no printout is desired.
358  * Call with printout=lm_printout_std to use the default implementation.
359  *
360  * printflags is passed to printout.
361  *
362  * data is an input pointer to an arbitrary structure that is passed to
363  * evaluate. Typically, it contains experimental data to be fitted.
364  *
365  */
366  int i = 0, iter = 0, j = 0;
367  double actred = NAN, delta = NAN, dirder = NAN, eps = NAN, fnorm = NAN, fnorm1 = NAN, gnorm = NAN, par = NAN,
368  pnorm = NAN, prered = NAN, ratio = NAN, step = NAN, sum = NAN, temp = NAN, temp1 = NAN, temp2 = NAN,
369  temp3 = NAN, xnorm = NAN;
370  static double p1 = 0.1;
371  static double p0001 = 1.0e-4;
372 
373  *nfev = 0; /* function evaluation counter */
374  iter = 0; /* outer loop counter */
375  par = 0; /* levenberg-marquardt parameter */
376  delta = 0; /* to prevent a warning (initialization within if-clause) */
377  xnorm = 0; /* ditto */
378  temp = MAX(epsfcn, LM_MACHEP);
379  eps = sqrt(temp); /* for calculating the Jacobian by forward differences */
380 
381  /*** lmdif: check input parameters for errors. ***/
382 
383  if ((n <= 0) || (m < n) || (ftol < 0.) || (xtol < 0.) || (gtol < 0.) || (maxfev <= 0) || (factor <= 0.)) {
384  *info = 10; // invalid parameter
385  return;
386  }
387  if (mode == 2) { /* scaling by diag[] */
388  for (j = 0; j < n; j++) { /* check for nonpositive elements */
389  if (diag[j] <= 0.0) {
390  *info = 10; // invalid parameter
391  return;
392  }
393  }
394  }
395 #ifdef LMFIT_DEBUG_MESSAGES
396  printf("lmdif\n");
397 #endif
398 
399  /*** lmdif: evaluate function at starting point and calculate norm. ***/
400 
401  *info = 0;
402  (*evaluate)(x, m, data, fvec, info);
403  ++(*nfev);
404  if (printout)
405  (*printout)(n, x, m, data, fvec, printflags, 0, 0, *nfev);
406  if (*info < 0)
407  return;
408  fnorm = lm_enorm(m, fvec);
409  if (fnorm <= LM_DWARF) {
410  *info = 0;
411  return;
412  }
413 
414  /*** lmdif: the outer loop. ***/
415 
416  do {
417 #ifdef LMFIT_DEBUG_MESSAGES
418  printf("lmdif/ outer loop iter=%d nfev=%d fnorm=%.10e\n", iter, *nfev, fnorm);
419 #endif
420 
421  /*** outer: calculate the Jacobian. ***/
422 
423  for (j = 0; j < n; j++) {
424  temp = x[j];
425  step = MAX(eps * eps, eps * fabs(temp));
426  x[j] = temp + step; /* replace temporarily */
427  *info = 0;
428  (*evaluate)(x, m, data, wa4, info);
429  ++(*nfev);
430  if (printout)
431  (*printout)(n, x, m, data, wa4, printflags, 1, iter, *nfev);
432  if (*info < 0)
433  return; /* user requested break */
434  for (i = 0; i < m; i++)
435  fjac[j * m + i] = (wa4[i] - fvec[i]) / step;
436  x[j] = temp; /* restore */
437  }
438 #ifdef LMFIT_DEBUG_MAtRIX
439  /* print the entire matrix */
440  for (i = 0; i < m; i++) {
441  for (j = 0; j < n; j++)
442  printf("%.5e ", fjac[j * m + i]);
443  printf("\n");
444  }
445 #endif
446 
447  /*** outer: compute the qr factorization of the Jacobian. ***/
448 
449  lm_qrfac(m, n, fjac, 1, ipvt, wa1, wa2, wa3);
450  /* return values are ipvt, wa1=rdiag, wa2=acnorm */
451 
452  if (!iter) {
453  /* first iteration only */
454  if (mode != 2) {
455  /* diag := norms of the columns of the initial Jacobian */
456  for (j = 0; j < n; j++) {
457  diag[j] = wa2[j];
458  if (wa2[j] == 0.)
459  diag[j] = 1.;
460  }
461  }
462  /* use diag to scale x, then calculate the norm */
463  for (j = 0; j < n; j++)
464  wa3[j] = diag[j] * x[j];
465  xnorm = lm_enorm(n, wa3);
466  /* initialize the step bound delta. */
467  delta = factor * xnorm;
468  if (delta == 0.)
469  delta = factor;
470  } else {
471  if (mode != 2) {
472  for (j = 0; j < n; j++)
473  diag[j] = MAX(diag[j], wa2[j]);
474  }
475  }
476 
477  /*** outer: form (q transpose)*fvec and store first n components in qtf. ***/
478 
479  for (i = 0; i < m; i++)
480  wa4[i] = fvec[i];
481 
482  for (j = 0; j < n; j++) {
483  temp3 = fjac[j * m + j];
484  if (temp3 != 0.) {
485  sum = 0;
486  for (i = j; i < m; i++)
487  sum += fjac[j * m + i] * wa4[i];
488  temp = -sum / temp3;
489  for (i = j; i < m; i++)
490  wa4[i] += fjac[j * m + i] * temp;
491  }
492  fjac[j * m + j] = wa1[j];
493  qtf[j] = wa4[j];
494  }
495 
496  /*** outer: compute norm of scaled gradient and test for convergence. ***/
497 
498  gnorm = 0;
499  for (j = 0; j < n; j++) {
500  if (wa2[ipvt[j]] == 0)
501  continue;
502  sum = 0.;
503  for (i = 0; i <= j; i++)
504  sum += fjac[j * m + i] * qtf[i];
505  gnorm = MAX(gnorm, fabs(sum / wa2[ipvt[j]] / fnorm));
506  }
507 
508  if (gnorm <= gtol) {
509  *info = 4;
510  return;
511  }
512 
513  /*** the inner loop. ***/
514  do {
515 #ifdef LMFIT_DEBUG_MESSAGES
516  printf("lmdif/ inner loop iter=%d nfev=%d\n", iter, *nfev);
517 #endif
518 
519  /*** inner: determine the levenberg-marquardt parameter. ***/
520 
521  lm_lmpar(n, fjac, m, ipvt, diag, qtf, delta, &par, wa1, wa2, wa4, wa3);
522  /* used return values are fjac (partly), par, wa1=x, wa3=diag*x */
523 
524  for (j = 0; j < n; j++)
525  wa2[j] = x[j] - wa1[j]; /* new parameter vector ? */
526 
527  pnorm = lm_enorm(n, wa3);
528 
529  /* at first call, adjust the initial step bound. */
530 
531  if (*nfev <= 1 + n)
532  delta = MIN(delta, pnorm);
533 
534  /*** inner: evaluate the function at x + p and calculate its norm. ***/
535 
536  *info = 0;
537  (*evaluate)(wa2, m, data, wa4, info);
538  ++(*nfev);
539  if (printout)
540  (*printout)(n, wa2, m, data, wa4, printflags, 2, iter, *nfev);
541  if (*info < 0)
542  return; /* user requested break. */
543 
544  fnorm1 = lm_enorm(m, wa4);
545 #ifdef LMFIT_DEBUG_MESSAGES
546  printf("lmdif/ pnorm %.10e fnorm1 %.10e fnorm %.10e"
547  " delta=%.10e par=%.10e\n",
548  pnorm, fnorm1, fnorm, delta, par);
549 #endif
550 
551  /*** inner: compute the scaled actual reduction. ***/
552 
553  if (p1 * fnorm1 < fnorm)
554  actred = 1 - SQR(fnorm1 / fnorm);
555  else
556  actred = -1;
557 
558  /*** inner: compute the scaled predicted reduction and
559  the scaled directional derivative. ***/
560 
561  for (j = 0; j < n; j++) {
562  wa3[j] = 0;
563  for (i = 0; i <= j; i++)
564  wa3[i] -= fjac[j * m + i] * wa1[ipvt[j]];
565  }
566  temp1 = lm_enorm(n, wa3) / fnorm;
567  temp2 = sqrt(par) * pnorm / fnorm;
568  prered = SQR(temp1) + 2 * SQR(temp2);
569  dirder = -(SQR(temp1) + SQR(temp2));
570 
571  /*** inner: compute the ratio of the actual to the predicted reduction. ***/
572 
573  ratio = prered != 0 ? actred / prered : 0;
574 #ifdef LMFIT_DEBUG_MESSAGES
575  printf("lmdif/ actred=%.10e prered=%.10e ratio=%.10e"
576  " sq(1)=%.10e sq(2)=%.10e dd=%.10e\n",
577  actred, prered, prered != 0 ? ratio : 0., SQR(temp1), SQR(temp2), dirder);
578 #endif
579 
580  /*** inner: update the step bound. ***/
581 
582  if (ratio <= 0.25) {
583  if (actred >= 0.)
584  temp = 0.5;
585  else
586  temp = 0.5 * dirder / (dirder + 0.5 * actred);
587  if (p1 * fnorm1 >= fnorm || temp < p1)
588  temp = p1;
589  delta = temp * MIN(delta, pnorm / p1);
590  par /= temp;
591  } else if (par == 0. || ratio >= 0.75) {
592  delta = pnorm / 0.5;
593  par *= 0.5;
594  }
595 
596  /*** inner: test for successful iteration. ***/
597 
598  if (ratio >= p0001) {
599  /* yes, success: update x, fvec, and their norms. */
600  for (j = 0; j < n; j++) {
601  x[j] = wa2[j];
602  wa2[j] = diag[j] * x[j];
603  }
604  for (i = 0; i < m; i++)
605  fvec[i] = wa4[i];
606  xnorm = lm_enorm(n, wa2);
607  fnorm = fnorm1;
608  iter++;
609  }
610 #ifdef LMFIT_DEBUG_MESSAGES
611  else {
612  printf("AtTN: iteration considered unsuccessful\n");
613  }
614 #endif
615 
616  /*** inner: test for convergence. ***/
617 
618  if (fnorm <= LM_DWARF) {
619  *info = 0;
620  return;
621  }
622 
623  *info = 0;
624  if (fabs(actred) <= ftol && prered <= ftol && 0.5 * ratio <= 1)
625  *info = 1;
626  if (delta <= xtol * xnorm)
627  *info += 2;
628  if (*info != 0)
629  return;
630 
631  /*** inner: tests for termination and stringent tolerances. ***/
632 
633  if (*nfev >= maxfev) {
634  *info = 5;
635  return;
636  }
637  if (fabs(actred) <= LM_MACHEP && prered <= LM_MACHEP && 0.5 * ratio <= 1) {
638  *info = 6;
639  return;
640  }
641  if (delta <= LM_MACHEP * xnorm) {
642  *info = 7;
643  return;
644  }
645  if (gnorm <= LM_MACHEP) {
646  *info = 8;
647  return;
648  }
649 
650  /*** inner: end of the loop. repeat if iteration unsuccessful. ***/
651 
652  } while (ratio < p0001);
653 
654  /*** outer: end of the loop. ***/
655 
656  } while (true);
657 
658 } /*** lm_lmdif. ***/
659 
660 /*****************************************************************************/
661 /* lm_lmpar (determine Levenberg-Marquardt parameter) */
662 /*****************************************************************************/
663 
664 void lm_lmpar(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double delta, double *par, double *x,
665  double *sdiag, double *aux, double *xdi)
666 {
667  /* Given an m by n matrix a, an n by n nonsingular diagonal
668  * matrix d, an m-vector b, and a positive number delta,
669  * the problem is to determine a value for the parameter
670  * par such that if x solves the system
671  *
672  * a*x = b and sqrt(par)*d*x = 0
673  *
674  * in the least squares sense, and dxnorm is the euclidean
675  * norm of d*x, then either par=0 and (dxnorm-delta) < 0.1*delta,
676  * or par>0 and abs(dxnorm-delta) < 0.1*delta.
677  *
678  * Using lm_qrsolv, this subroutine completes the solution of the problem
679  * if it is provided with the necessary information from the
680  * qr factorization, with column pivoting, of a. That is, if
681  * a*p = q*r, where p is a permutation matrix, q has orthogonal
682  * columns, and r is an upper triangular matrix with diagonal
683  * elements of nonincreasing magnitude, then lmpar expects
684  * the full upper triangle of r, the permutation matrix p,
685  * and the first n components of qT*b. On output
686  * lmpar also provides an upper triangular matrix s such that
687  *
688  * pT*(aT*a + par*d*d)*p = sT*s.
689  *
690  * s is employed within lmpar and may be of separate interest.
691  *
692  * Only a few iterations are generally needed for convergence
693  * of the algorithm. If, however, the limit of 10 iterations
694  * is reached, then the output par will contain the best
695  * value obtained so far.
696  *
697  * parameters:
698  *
699  * n is a positive integer input variable set to the order of r.
700  *
701  * r is an n by n array. on input the full upper triangle
702  * must contain the full upper triangle of the matrix r.
703  * on OUTPUT the full upper triangle is unaltered, and the
704  * strict lower triangle contains the strict upper triangle
705  * (transposed) of the upper triangular matrix s.
706  *
707  * ldr is a positive integer input variable not less than n
708  * which specifies the leading dimension of the array r.
709  *
710  * ipvt is an integer input array of length n which defines the
711  * permutation matrix p such that a*p = q*r. column j of p
712  * is column ipvt(j) of the identity matrix.
713  *
714  * diag is an input array of length n which must contain the
715  * diagonal elements of the matrix d.
716  *
717  * qtb is an input array of length n which must contain the first
718  * n elements of the vector (q transpose)*b.
719  *
720  * delta is a positive input variable which specifies an upper
721  * bound on the euclidean norm of d*x.
722  *
723  * par is a nonnegative variable. on input par contains an
724  * initial estimate of the levenberg-marquardt parameter.
725  * on OUTPUT par contains the final estimate.
726  *
727  * x is an OUTPUT array of length n which contains the least
728  * squares solution of the system a*x = b, sqrt(par)*d*x = 0,
729  * for the output par.
730  *
731  * sdiag is an array of length n which contains the
732  * diagonal elements of the upper triangular matrix s.
733  *
734  * aux is a multi-purpose work array of length n.
735  *
736  * xdi is a work array of length n. On OUTPUT: diag[j] * x[j].
737  *
738  */
739  int i = 0, iter = 0, j = 0, nsing = 0;
740  double dxnorm = NAN, fp = NAN, fp_old = NAN, gnorm = NAN, parc = NAN, parl = NAN, paru = NAN;
741  double sum = NAN, temp = NAN;
742  static double p1 = 0.1;
743 
744 #ifdef LMFIT_DEBUG_MESSAGES
745  printf("lmpar\n");
746 #endif
747 
748  /*** lmpar: compute and store in x the gauss-newton direction. if the
749  jacobian is rank-deficient, obtain a least squares solution. ***/
750 
751  nsing = n;
752  for (j = 0; j < n; j++) {
753  aux[j] = qtb[j];
754  if (r[j * ldr + j] == 0 && nsing == n)
755  nsing = j;
756  if (nsing < n)
757  aux[j] = 0;
758  }
759 #ifdef LMFIT_DEBUG_MESSAGES
760  printf("nsing %d ", nsing);
761 #endif
762  for (j = nsing - 1; j >= 0; j--) {
763  aux[j] = aux[j] / r[j + ldr * j];
764  temp = aux[j];
765  for (i = 0; i < j; i++)
766  aux[i] -= r[j * ldr + i] * temp;
767  }
768 
769  for (j = 0; j < n; j++)
770  x[ipvt[j]] = aux[j];
771 
772  /*** lmpar: initialize the iteration counter, evaluate the function at the
773  origin, and test for acceptance of the gauss-newton direction. ***/
774 
775  iter = 0;
776  for (j = 0; j < n; j++)
777  xdi[j] = diag[j] * x[j];
778  dxnorm = lm_enorm(n, xdi);
779  fp = dxnorm - delta;
780  if (fp <= p1 * delta) {
781 #ifdef LMFIT_DEBUG_MESSAGES
782  printf("lmpar/ terminate (fp<p1*delta)\n");
783 #endif
784  *par = 0;
785  return;
786  }
787 
788  /*** lmpar: if the jacobian is not rank deficient, the newton
789  step provides a lower bound, parl, for the 0. of
790  the function. otherwise set this bound to 0.. ***/
791 
792  parl = 0;
793  if (nsing >= n) {
794  for (j = 0; j < n; j++)
795  aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
796 
797  for (j = 0; j < n; j++) {
798  sum = 0.;
799  for (i = 0; i < j; i++)
800  sum += r[j * ldr + i] * aux[i];
801  aux[j] = (aux[j] - sum) / r[j + ldr * j];
802  }
803  temp = lm_enorm(n, aux);
804  parl = fp / delta / temp / temp;
805  }
806 
807  /*** lmpar: calculate an upper bound, paru, for the 0. of the function. ***/
808 
809  for (j = 0; j < n; j++) {
810  sum = 0;
811  for (i = 0; i <= j; i++)
812  sum += r[j * ldr + i] * qtb[i];
813  aux[j] = sum / diag[ipvt[j]];
814  }
815  gnorm = lm_enorm(n, aux);
816  paru = gnorm / delta;
817  if (paru == 0.)
818  paru = LM_DWARF / MIN(delta, p1);
819 
820  /*** lmpar: if the input par lies outside of the interval (parl,paru),
821  set par to the closer endpoint. ***/
822 
823  *par = MAX(*par, parl);
824  *par = MIN(*par, paru);
825  if (*par == 0.)
826  *par = gnorm / dxnorm;
827 #ifdef LMFIT_DEBUG_MESSAGES
828  printf("lmpar/ parl %.4e par %.4e paru %.4e\n", parl, *par, paru);
829 #endif
830 
831  /*** lmpar: iterate. ***/
832 
833  for (;; iter++) {
834 
837  if (*par == 0.)
838  *par = MAX(LM_DWARF, 0.001 * paru);
839  temp = sqrt(*par);
840  for (j = 0; j < n; j++)
841  aux[j] = temp * diag[j];
842 
843  lm_qrsolv(n, r, ldr, ipvt, aux, qtb, x, sdiag, xdi);
844  /* return values are r, x, sdiag */
845 
846  for (j = 0; j < n; j++)
847  xdi[j] = diag[j] * x[j]; /* used as output */
848  dxnorm = lm_enorm(n, xdi);
849  fp_old = fp;
850  fp = dxnorm - delta;
851 
856  if (fabs(fp) <= p1 * delta || (parl == 0. && fp <= fp_old && fp_old < 0.) || iter == 10)
857  break; /* the only exit from the iteration. */
858 
861  for (j = 0; j < n; j++)
862  aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
863 
864  for (j = 0; j < n; j++) {
865  aux[j] = aux[j] / sdiag[j];
866  for (i = j + 1; i < n; i++)
867  aux[i] -= r[j * ldr + i] * aux[j];
868  }
869  temp = lm_enorm(n, aux);
870  parc = fp / delta / temp / temp;
871 
874  if (fp > 0)
875  parl = MAX(parl, *par);
876  else if (fp < 0)
877  paru = MIN(paru, *par);
878  /* the case fp==0 is precluded by the break condition */
879 
882  *par = MAX(parl, *par + parc);
883  }
884 
885 } /*** lm_lmpar. ***/
886 
887 /*****************************************************************************/
888 /* lm_qrfac (QR factorisation, from lapack) */
889 /*****************************************************************************/
890 
891 void lm_qrfac(int m, int n, double *a, int pivot, int *ipvt, double *rdiag, double *acnorm, double *wa)
892 {
893  /*
894  * This subroutine uses householder transformations with column
895  * pivoting (optional) to compute a qr factorization of the
896  * m by n matrix a. That is, qrfac determines an orthogonal
897  * matrix q, a permutation matrix p, and an upper trapezoidal
898  * matrix r with diagonal elements of nonincreasing magnitude,
899  * such that a*p = q*r. The householder transformation for
900  * column k, k = 1,2,...,min(m,n), is of the form
901  *
902  * i - (1/u(k))*u*uT
903  *
904  * where u has zeroes in the first k-1 positions. The form of
905  * this transformation and the method of pivoting first
906  * appeared in the corresponding linpack subroutine.
907  *
908  * Parameters:
909  *
910  * m is a positive integer input variable set to the number
911  * of rows of a.
912  *
913  * n is a positive integer input variable set to the number
914  * of columns of a.
915  *
916  * a is an m by n array. On input a contains the matrix for
917  * which the qr factorization is to be computed. On OUTPUT
918  * the strict upper trapezoidal part of a contains the strict
919  * upper trapezoidal part of r, and the lower trapezoidal
920  * part of a contains a factored form of q (the non-trivial
921  * elements of the u vectors described above).
922  *
923  * pivot is a logical input variable. If pivot is set true,
924  * then column pivoting is enforced. If pivot is set false,
925  * then no column pivoting is done.
926  *
927  * ipvt is an integer OUTPUT array of length lipvt. This array
928  * defines the permutation matrix p such that a*p = q*r.
929  * Column j of p is column ipvt(j) of the identity matrix.
930  * If pivot is false, ipvt is not referenced.
931  *
932  * rdiag is an OUTPUT array of length n which contains the
933  * diagonal elements of r.
934  *
935  * acnorm is an OUTPUT array of length n which contains the
936  * norms of the corresponding columns of the input matrix a.
937  * If this information is not needed, then acnorm can coincide
938  * with rdiag.
939  *
940  * wa is a work array of length n. If pivot is false, then wa
941  * can coincide with rdiag.
942  *
943  */
944  int i = 0, j = 0, k = 0, kmax = 0, minmn = 0;
945  double ajnorm = NAN, sum = NAN, temp = NAN;
946 
947  /*** qrfac: compute initial column norms and initialize several arrays. ***/
948 
949  for (j = 0; j < n; j++) {
950  acnorm[j] = lm_enorm(m, &a[j * m]);
951  rdiag[j] = acnorm[j];
952  wa[j] = rdiag[j];
953  if (pivot)
954  ipvt[j] = j;
955  }
956 #ifdef LMFIT_DEBUG_MESSAGES
957  printf("qrfac\n");
958 #endif
959 
960  /*** qrfac: reduce a to r with householder transformations. ***/
961 
962  minmn = MIN(m, n);
963  for (j = 0; j < minmn; j++) {
964  if (!pivot)
965  goto pivot_ok;
966 
969  kmax = j;
970  for (k = j + 1; k < n; k++)
971  if (rdiag[k] > rdiag[kmax])
972  kmax = k;
973  if (kmax == j)
974  goto pivot_ok;
975 
976  for (i = 0; i < m; i++) {
977  temp = a[j * m + i];
978  a[j * m + i] = a[kmax * m + i];
979  a[kmax * m + i] = temp;
980  }
981  rdiag[kmax] = rdiag[j];
982  wa[kmax] = wa[j];
983  k = ipvt[j];
984  ipvt[j] = ipvt[kmax];
985  ipvt[kmax] = k;
986 
987  pivot_ok:
991  ajnorm = lm_enorm(m - j, &a[j * m + j]);
992  if (ajnorm == 0.) {
993  rdiag[j] = 0;
994  continue;
995  }
996 
997  if (a[j * m + j] < 0.)
998  ajnorm = -ajnorm;
999  for (i = j; i < m; i++)
1000  a[j * m + i] /= ajnorm;
1001  a[j * m + j] += 1;
1002 
1006  for (k = j + 1; k < n; k++) {
1007  sum = 0;
1008 
1009  for (i = j; i < m; i++)
1010  sum += a[j * m + i] * a[k * m + i];
1011 
1012  temp = sum / a[j + m * j];
1013 
1014  for (i = j; i < m; i++)
1015  a[k * m + i] -= temp * a[j * m + i];
1016 
1017  if (pivot && rdiag[k] != 0.) {
1018  temp = a[m * k + j] / rdiag[k];
1019  temp = MAX(0., 1 - temp * temp);
1020  rdiag[k] *= sqrt(temp);
1021  temp = rdiag[k] / wa[k];
1022  if (0.05 * SQR(temp) <= LM_MACHEP) {
1023  rdiag[k] = lm_enorm(m - j - 1, &a[m * k + j + 1]);
1024  wa[k] = rdiag[k];
1025  }
1026  }
1027  }
1028 
1029  rdiag[j] = -ajnorm;
1030  }
1031 }
1032 
1033 /*****************************************************************************/
1034 /* lm_qrsolv (linear least-squares) */
1035 /*****************************************************************************/
1036 
1037 void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double *x, double *sdiag, double *wa)
1038 {
1039  /*
1040  * Given an m by n matrix a, an n by n diagonal matrix d,
1041  * and an m-vector b, the problem is to determine an x which
1042  * solves the system
1043  *
1044  * a*x = b and d*x = 0
1045  *
1046  * in the least squares sense.
1047  *
1048  * This subroutine completes the solution of the problem
1049  * if it is provided with the necessary information from the
1050  * qr factorization, with column pivoting, of a. That is, if
1051  * a*p = q*r, where p is a permutation matrix, q has orthogonal
1052  * columns, and r is an upper triangular matrix with diagonal
1053  * elements of nonincreasing magnitude, then qrsolv expects
1054  * the full upper triangle of r, the permutation matrix p,
1055  * and the first n components of (q transpose)*b. The system
1056  * a*x = b, d*x = 0, is then equivalent to
1057  *
1058  * r*z = qT*b, pT*d*p*z = 0,
1059  *
1060  * where x = p*z. If this system does not have full rank,
1061  * then a least squares solution is obtained. On output qrsolv
1062  * also provides an upper triangular matrix s such that
1063  *
1064  * pT *(aT *a + d*d)*p = sT *s.
1065  *
1066  * s is computed within qrsolv and may be of separate interest.
1067  *
1068  * Parameters
1069  *
1070  * n is a positive integer input variable set to the order of r.
1071  *
1072  * r is an n by n array. On input the full upper triangle
1073  * must contain the full upper triangle of the matrix r.
1074  * On OUTPUT the full upper triangle is unaltered, and the
1075  * strict lower triangle contains the strict upper triangle
1076  * (transposed) of the upper triangular matrix s.
1077  *
1078  * ldr is a positive integer input variable not less than n
1079  * which specifies the leading dimension of the array r.
1080  *
1081  * ipvt is an integer input array of length n which defines the
1082  * permutation matrix p such that a*p = q*r. Column j of p
1083  * is column ipvt(j) of the identity matrix.
1084  *
1085  * diag is an input array of length n which must contain the
1086  * diagonal elements of the matrix d.
1087  *
1088  * qtb is an input array of length n which must contain the first
1089  * n elements of the vector (q transpose)*b.
1090  *
1091  * x is an OUTPUT array of length n which contains the least
1092  * squares solution of the system a*x = b, d*x = 0.
1093  *
1094  * sdiag is an OUTPUT array of length n which contains the
1095  * diagonal elements of the upper triangular matrix s.
1096  *
1097  * wa is a work array of length n.
1098  *
1099  */
1100  int i = 0, kk = 0, j = 0, k = 0, nsing = 0;
1101  double qtbpj = NAN, sum = NAN, temp = NAN;
1102  double _sin = NAN, _cos = NAN, _tan = NAN, _cot = NAN; /* local variables, not functions */
1103 
1104  /*** qrsolv: copy r and (q transpose)*b to preserve input and initialize s.
1105  in particular, save the diagonal elements of r in x. ***/
1106 
1107  for (j = 0; j < n; j++) {
1108  for (i = j; i < n; i++)
1109  r[j * ldr + i] = r[i * ldr + j];
1110  x[j] = r[j * ldr + j];
1111  wa[j] = qtb[j];
1112  }
1113 #ifdef LMFIT_DEBUG_MESSAGES
1114  printf("qrsolv\n");
1115 #endif
1116 
1117  /*** qrsolv: eliminate the diagonal matrix d using a Givens rotation. ***/
1118 
1119  for (j = 0; j < n; j++) {
1120 
1121  /*** qrsolv: prepare the row of d to be eliminated, locating the
1122  diagonal element using p from the qr factorization. ***/
1123 
1124  if (diag[ipvt[j]] == 0.)
1125  goto L90;
1126  for (k = j; k < n; k++)
1127  sdiag[k] = 0.;
1128  sdiag[j] = diag[ipvt[j]];
1129 
1130  /*** qrsolv: the transformations to eliminate the row of d modify only
1131  a single element of qT*b beyond the first n, which is initially 0. ***/
1132 
1133  qtbpj = 0.;
1134  for (k = j; k < n; k++) {
1135 
1139  if (sdiag[k] == 0.)
1140  continue;
1141  kk = k + ldr * k;
1142  if (fabs(r[kk]) < fabs(sdiag[k])) {
1143  _cot = r[kk] / sdiag[k];
1144  _sin = 1 / sqrt(1 + SQR(_cot));
1145  _cos = _sin * _cot;
1146  } else {
1147  _tan = sdiag[k] / r[kk];
1148  _cos = 1 / sqrt(1 + SQR(_tan));
1149  _sin = _cos * _tan;
1150  }
1151 
1155  r[kk] = _cos * r[kk] + _sin * sdiag[k];
1156  temp = _cos * wa[k] + _sin * qtbpj;
1157  qtbpj = -_sin * wa[k] + _cos * qtbpj;
1158  wa[k] = temp;
1159 
1162  for (i = k + 1; i < n; i++) {
1163  temp = _cos * r[k * ldr + i] + _sin * sdiag[i];
1164  sdiag[i] = -_sin * r[k * ldr + i] + _cos * sdiag[i];
1165  r[k * ldr + i] = temp;
1166  }
1167  }
1168 
1169  L90:
1173  sdiag[j] = r[j * ldr + j];
1174  r[j * ldr + j] = x[j];
1175  }
1176 
1177  /*** qrsolv: solve the triangular system for z. if the system is
1178  singular, then obtain a least squares solution. ***/
1179 
1180  nsing = n;
1181  for (j = 0; j < n; j++) {
1182  if (sdiag[j] == 0. && nsing == n)
1183  nsing = j;
1184  if (nsing < n)
1185  wa[j] = 0;
1186  }
1187 
1188  for (j = nsing - 1; j >= 0; j--) {
1189  sum = 0;
1190  for (i = j + 1; i < nsing; i++)
1191  sum += r[j * ldr + i] * wa[i];
1192  wa[j] = (wa[j] - sum) / sdiag[j];
1193  }
1194 
1195  /*** qrsolv: permute the components of z back to components of x. ***/
1196 
1197  for (j = 0; j < n; j++)
1198  x[ipvt[j]] = wa[j];
1199 
1200 } /*** lm_qrsolv. ***/
1201 
1202 /*****************************************************************************/
1203 /* lm_enorm (Euclidean norm) */
1204 /*****************************************************************************/
1205 
1206 double lm_enorm(int n, const double *x)
1207 {
1208  /* Given an n-vector x, this function calculates the
1209  * euclidean norm of x.
1210  *
1211  * The euclidean norm is computed by accumulating the sum of
1212  * squares in three different sums. The sums of squares for the
1213  * small and large components are scaled so that no overflows
1214  * occur. Non-destructive underflows are permitted. Underflows
1215  * and overflows do not occur in the computation of the unscaled
1216  * sum of squares for the intermediate components.
1217  * The definitions of small, intermediate and large components
1218  * depend on two constants, LM_SQRT_DWARF and LM_SQRT_GIANT. The main
1219  * restrictions on these constants are that LM_SQRT_DWARF**2 not
1220  * underflow and LM_SQRT_GIANT**2 not overflow.
1221  *
1222  * Parameters
1223  *
1224  * n is a positive integer input variable.
1225  *
1226  * x is an input array of length n.
1227  */
1228  int i = 0;
1229  double agiant = NAN, s1 = NAN, s2 = NAN, s3 = NAN, xabs = NAN, x1max = NAN, x3max = NAN, temp = NAN;
1230 
1231  s1 = 0;
1232  s2 = 0;
1233  s3 = 0;
1234  x1max = 0;
1235  x3max = 0;
1236  agiant = LM_SQRT_GIANT / n;
1237 
1240  for (i = 0; i < n; i++) {
1241  xabs = fabs(x[i]);
1242  if (xabs > LM_SQRT_DWARF) {
1243  if (xabs < agiant) {
1244  s2 += xabs * xabs;
1245  } else if (xabs > x1max) {
1246  temp = x1max / xabs;
1247  s1 = 1 + s1 * SQR(temp);
1248  x1max = xabs;
1249  } else {
1250  temp = xabs / x1max;
1251  s1 += SQR(temp);
1252  }
1253  } else if (xabs > x3max) {
1254  temp = x3max / xabs;
1255  s3 = 1 + s3 * SQR(temp);
1256  x3max = xabs;
1257  } else if (xabs != 0.) {
1258  temp = xabs / x3max;
1259  s3 += SQR(temp);
1260  }
1261  }
1262 
1265  if (s1 != 0)
1266  return x1max * sqrt(s1 + (s2 / x1max) / x1max);
1267  else if (s2 != 0)
1268  if (s2 >= x3max)
1269  return sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
1270  else
1271  return sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
1272  else
1273  return x3max * sqrt(s3);
1274 
1275 } /*** lm_enorm. ***/
lmmin.h
LM_SQRT_DWARF
#define LM_SQRT_DWARF
Definition: lmmin.cxx:40
lm_control_struct::ftol
double ftol
Definition: lmmin.h:24
lm_infmsg
const char * lm_infmsg[]
Definition: lmmin.cxx:65
lm_qrsolv
void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double *x, double *sdiag, double *wa)
Definition: lmmin.cxx:1037
lm_printout_std
void lm_printout_std(int n_par, const double *par, int m_dat, const void *data, const double *fvec, int printflags, int iflag, int iter, int nfev)
Definition: lmmin.cxx:86
lm_status_struct::info
int info
Definition: lmmin.h:38
LM_MACHEP
#define LM_MACHEP
Definition: lmmin.cxx:38
lm_control_struct::xtol
double xtol
Definition: lmmin.h:25
lm_control_struct
Definition: lmmin.h:23
LM_DWARF
#define LM_DWARF
Definition: lmmin.cxx:39
lm_lmpar
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)
Definition: lmmin.cxx:664
lm_qrfac
void lm_qrfac(int m, int n, double *a, int pivot, int *ipvt, double *rdiag, double *acnorm, double *wa)
Definition: lmmin.cxx:891
LM_USERTOL
#define LM_USERTOL
Definition: lmmin.cxx:42
lm_status_struct
Definition: lmmin.h:35
lm_control_struct::printflags
int printflags
Definition: lmmin.h:31
lmmin
void lmmin(int n_par, double *par, int m_dat, const void *data, void(*evaluate)(const double *par, int m_dat, const void *data, double *fvec, int *info), const lm_control_struct *control, lm_status_struct *status, void(*printout)(int n_par, const double *par, int m_dat, const void *data, const double *fvec, int printflags, int iflag, int iter, int nfev))
Definition: lmmin.cxx:135
LM_SQRT_GIANT
#define LM_SQRT_GIANT
Definition: lmmin.cxx:41
lm_control_struct::scale_diag
int scale_diag
Definition: lmmin.h:30
lm_control_struct::epsilon
double epsilon
Definition: lmmin.h:27
lm_control_struct::stepbound
double stepbound
Definition: lmmin.h:28
lm_control_struct::gtol
double gtol
Definition: lmmin.h:26
lm_lmdif
void lm_lmdif(int m, int n, double *x, double *fvec, double ftol, double xtol, double gtol, int maxfev, double epsfcn, double *diag, int mode, double factor, int *info, int *nfev, double *fjac, int *ipvt, double *qtf, double *wa1, double *wa2, double *wa3, double *wa4, void(*evaluate)(const double *par, int m_dat, const void *data, double *fvec, int *info), void(*printout)(int n_par, const double *par, int m_dat, const void *data, const double *fvec, int printflags, int iflag, int iter, int nfev), int printflags, const void *data)
Definition: lmmin.cxx:207
lm_control_struct::maxcall
int maxcall
Definition: lmmin.h:29
lm_control_float
const lm_control_struct lm_control_float
Definition: lmmin.cxx:59
lm_control_double
const lm_control_struct lm_control_double
Definition: lmmin.cxx:58
lm_shortmsg
const char * lm_shortmsg[]
Definition: lmmin.cxx:78
lm_enorm
double lm_enorm(int n, const double *x)
Definition: lmmin.cxx:1206
MAX
#define MAX(a, b)
Definition: lmmin.cxx:204
MIN
#define MIN(a, b)
Definition: lmmin.cxx:203
SQR
#define SQR(x)
Definition: lmmin.cxx:205
lm_status_struct::nfev
int nfev
Definition: lmmin.h:37
lm_status_struct::fnorm
double fnorm
Definition: lmmin.h:36