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