28 #define MIN(a,b) (((a)<=(b)) ? (a) : (b))
35 #define MAX(a,b) (((a)>=(b)) ? (a) : (b))
42 #define SQR(x) (x)*(x)
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 );
101 #define LM_MACHEP DBL_EPSILON
102 #define LM_DWARF DBL_MIN
103 #define LM_SQRT_DWARF sqrt(DBL_MIN)
104 #define LM_SQRT_GIANT sqrt(DBL_MAX)
105 #define LM_USERTOL 30*LM_MACHEP
126 1.e-7, 1.e-7, 1.e-7, 1.e-7, 100., 100, 1,
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)"
173 for (i = 0; i < nout; ++i)
174 fprintf( fout,
" %16.9g", par[i] );
175 fprintf( fout,
" => %18.11g\n", fnorm );
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),
188 double *fvec, *diag, *fjac, *qtf, *wa1, *wa2, *wa3, *wf;
191 double actred, dirder, fnorm, fnorm1, gnorm, pnorm,
192 prered, ratio, step, sum, temp, temp1, temp2, temp3;
193 static double p0001 = 1.0e-4;
217 fprintf( stderr,
"lmmin: invalid number of parameters %i\n", n );
222 fprintf( stderr,
"lmmin: number of data points (%i) "
223 "smaller than number of parameters (%i)\n", m, n );
229 "lmmin: negative tolerance (at least one of %g %g %g)\n",
235 fprintf( stderr,
"lmmin: nonpositive function evaluations limit %i\n",
241 fprintf( stderr,
"lmmin: nonpositive stepbound %g\n", C->
stepbound );
246 fprintf( stderr,
"lmmin: logical variable scale_diag=%i, "
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 ) {
268 for (j = 0; j < n; j++)
274 (*evaluate)( x, m, data, fvec, &(S->
userbreak) );
280 fprintf( msgfile,
"lmmin start " );
290 for ( outer = 0; ; ++outer ) {
294 for (j = 0; j < n; j++) {
296 step =
MAX(eps * eps, eps * fabs(temp));
298 (*evaluate)( x, m, data, wf, &(S->
userbreak) );
302 for (i = 0; i < m; i++)
303 fjac[j * m + i] = (wf[i] - fvec[i]) / step;
308 printf(
"\nlmmin Jacobian\n");
309 for (i = 0; i < m; i++) {
311 for (j = 0; j < n; j++)
312 printf(
"%.5e ", fjac[j * m + i]);
339 lm_qrfac(m, n, fjac, ipvt, wa1, wa2, wa3);
344 for (i = 0; i < m; i++)
347 for (j = 0; j < n; j++) {
348 temp3 = fjac[j * m + j];
351 for (i = j; i < m; i++)
352 sum += fjac[j * m + i] * wf[i];
354 for (i = j; i < m; i++)
355 wf[i] += fjac[j * m + i] * temp;
357 fjac[j * m + j] = wa1[j];
364 for (j = 0; j < n; j++) {
365 if (wa2[ipvt[j]] == 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 ) );
373 if (gnorm <= C->gtol) {
384 for (j = 0; j < n; j++)
385 diag[j] = wa2[j] ? wa2[j] : 1;
387 for (j = 0; j < n; j++)
388 wa3[j] = diag[j] * x[j];
391 fprintf( msgfile,
"lmmin diag " );
396 fprintf( msgfile,
" o i lmpar prered"
397 " ratio dirder delta"
399 for (i = 0; i < nout; ++i)
400 fprintf( msgfile,
" p%i", i );
401 fprintf( msgfile,
"\n" );
413 for (j = 0; j < n; j++)
414 diag[j] =
MAX( diag[j], wa2[j] );
424 lm_lmpar( n, fjac, m, ipvt, diag, qtf, delta, &lmpar,
430 temp2 = lmpar *
SQR( pnorm / fnorm );
431 for (j = 0; j < n; j++) {
433 for (i = 0; i <= j; i++)
434 wa3[i] -= fjac[j * m + i] * wa1[ipvt[j]];
437 prered = temp1 + 2 * temp2;
438 dirder = -temp1 + temp2;
441 if ( !outer && pnorm < delta )
446 for (j = 0; j < n; j++)
447 wa2[j] = x[j] - wa1[j];
449 (*evaluate)( wa2, m, data, wf, &(S->
userbreak) );
458 actred = 1 -
SQR(fnorm1 / fnorm);
461 ratio = prered ? actred / prered : 0;
464 fprintf( msgfile,
"lmmin (%i:%i) ", outer, inner );
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" );
477 if ( ratio <= 0.25 ) {
480 else if ( actred > -99 )
481 temp =
MAX( dirder / (2 * dirder + actred), 0.1 );
484 delta = temp *
MIN(delta, pnorm / 0.1);
486 }
else if ( ratio >= 0.75 ) {
489 }
else if ( !lmpar ) {
495 inner_success = ratio >= p0001;
496 if ( inner_success ) {
500 for (j = 0; j < n; j++) {
502 wa2[j] = diag[j] * x[j];
505 for (j = 0; j < n; j++)
508 for (i = 0; i < m; i++)
519 if (fabs(actred) <= C->
ftol && prered <= C->ftol && ratio <= 2)
521 if (delta <= C->xtol * xnorm)
529 if ( S->
nfev >= maxfev ) {
550 }
while ( !inner_success );
559 printf(
"lmmin outcome (%i) xnorm %g ftol %g xtol %g\n",
562 fprintf( msgfile,
"lmmin final " );
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)
662 int i, iter, j, nsing;
663 double dxnorm, fp, fp_old, gnorm, parc, parl, paru;
665 static double p1 = 0.1;
671 for (j = 0; j < n; j++) {
673 if (r[j * ldr + j] == 0 && nsing == n)
678 for (j = nsing - 1; j >= 0; j--) {
679 aux[j] = aux[j] / r[j + ldr * j];
681 for (i = 0; i < j; i++)
682 aux[i] -= r[j * ldr + i] * temp;
685 for (j = 0; j < n; j++)
691 for (j = 0; j < n; j++)
692 xdi[j] = diag[j] * x[j];
695 if (fp <= p1 * delta) {
696 #ifdef LMFIT_DEBUG_MESSAGES
697 printf(
"debug lmpar nsing %d n %d, terminate (fp<p1*delta)\n",
710 for (j = 0; j < n; j++)
711 aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
713 for (j = 0; j < n; j++) {
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];
720 parl = fp / delta / temp / temp;
725 for (j = 0; j < n; j++) {
727 for (i = 0; i <= j; i++)
728 sum += r[j * ldr + i] * qtb[i];
729 aux[j] = sum / diag[ipvt[j]];
732 paru = gnorm / delta;
739 *par =
MAX(*par, parl);
740 *par =
MIN(*par, paru);
742 *par = gnorm / dxnorm;
746 for (iter = 0; ; iter++) {
753 for (j = 0; j < n; j++)
754 aux[j] = temp * diag[j];
756 lm_qrsolv( n, r, ldr, ipvt, aux, qtb, x, sdiag, xdi );
759 for (j = 0; j < n; j++)
760 xdi[j] = diag[j] * x[j];
769 if (fabs(fp) <= p1 * delta
770 || (parl == 0. && fp <= fp_old && fp_old < 0.)
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);
782 for (j = 0; j < n; j++)
783 aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
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];
791 parc = fp / delta / temp / temp;
796 parl =
MAX(parl, *par);
798 paru =
MIN(paru, *par);
803 *par =
MAX(parl, *par + parc);
814 double *rdiag,
double *acnorm,
double *wa)
861 int i, j, k, kmax, minmn;
862 double ajnorm, sum, temp;
866 for (j = 0; j < n; j++) {
868 rdiag[j] = acnorm[j];
872 #ifdef LMFIT_DEBUG_MESSAGES
873 printf(
"debug qrfac\n");
879 for (j = 0; j < minmn; j++) {
884 for (k = j + 1; k < n; k++)
885 if (rdiag[k] > rdiag[kmax])
890 for (i = 0; i < m; i++) {
892 a[j * m + i] = a[kmax * m + i];
893 a[kmax * m + i] = temp;
895 rdiag[kmax] = rdiag[j];
898 ipvt[j] = ipvt[kmax];
905 ajnorm =
lm_enorm(m - j, &a[j * m + j]);
911 if (a[j * m + j] < 0.)
913 for (i = j; i < m; i++)
914 a[j * m + i] /= ajnorm;
920 for (k = j + 1; k < n; k++) {
923 for (i = j; i < m; i++)
924 sum += a[j * m + i] * a[k * m + i];
926 temp = sum / a[j + m * j];
928 for (i = j; i < m; i++)
929 a[k * m + i] -= temp * a[j * m + i];
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];
937 rdiag[k] =
lm_enorm(m - j - 1, &a[m * k + j + 1]);
952 void lm_qrsolv(
int n,
double *r,
int ldr,
int *ipvt,
double *diag,
953 double *qtb,
double *x,
double *sdiag,
double *wa)
1016 int i, kk, j, k, nsing;
1017 double qtbpj, sum, temp;
1018 double _sin, _cos, _tan, _cot;
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];
1031 for (j = 0; j < n; j++) {
1036 if (diag[ipvt[j]] == 0.)
1038 for (k = j; k < n; k++)
1040 sdiag[j] = diag[ipvt[j]];
1046 for (k = j; k < n; k++) {
1054 if (fabs(r[kk]) < fabs(sdiag[k])) {
1055 _cot = r[kk] / sdiag[k];
1056 _sin = 1 / sqrt(1 +
SQR(_cot));
1059 _tan = sdiag[k] / r[kk];
1060 _cos = 1 / sqrt(1 +
SQR(_tan));
1067 r[kk] = _cos * r[kk] + _sin * sdiag[k];
1068 temp = _cos * wa[k] + _sin * qtbpj;
1069 qtbpj = -_sin * wa[k] + _cos * qtbpj;
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;
1085 sdiag[j] = r[j * ldr + j];
1086 r[j * ldr + j] = x[j];
1093 for (j = 0; j < n; j++) {
1094 if (sdiag[j] == 0. && nsing == n)
1100 for (j = nsing - 1; j >= 0; j--) {
1102 for (i = j + 1; i < nsing; i++)
1103 sum += r[j * ldr + i] * wa[i];
1104 wa[j] = (wa[j] - sum) / sdiag[j];
1109 for (j = 0; j < n; j++)
1142 double agiant, s1, s2, s3, xabs, x1max, x3max, temp;
1153 for (i = 0; i < n; i++) {
1156 if ( xabs < agiant ) {
1158 }
else if ( xabs > x1max ) {
1159 temp = x1max / xabs;
1160 s1 = 1 + s1 *
SQR(temp);
1163 temp = xabs / x1max;
1166 }
else if ( xabs > x3max ) {
1167 temp = x3max / xabs;
1168 s3 = 1 + s3 *
SQR(temp);
1170 }
else if (xabs != 0.) {
1171 temp = xabs / x3max;
1179 return x1max * sqrt(s1 + (s2 / x1max) / x1max);
1182 return sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
1184 return sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
1186 return x3max * sqrt(s3);
double epsilon
Step used to calculate the Jacobian.
Declarations for Levenberg-Marquardt minimization.
const lm_control_struct lm_control_float
Preset (and recommended) control parameter settings.
Collection of input parameters for fit control.
void lm_qrfac(int m, int n, double *a, int *ipvt, double *rdiag, double *acnorm, double *wa)
[brief description]
int n_maxpri
Maximum parameters to print.
double lm_enorm(int n, const double *x)
Refined calculation of Eucledian norm.
int patience
Used to set the maximum number of function evaluations.
const char * lm_infmsg[]
Preset message texts.
int userbreak
Set when function evaluation requests termination.
const lm_control_struct lm_control_double
Preset (and recommended) control parameter settings.
Collection of output parameters for status info.
double stepbound
Used in determining the initial step bound.
int verbosity
Controls the ouput verbosity.
#define SQR(x)
[brief description]
double xtol
Relative error between last two approximations.
const char * lm_shortmsg[]
Preset message texts.
FILE * msgfile
Progress messages will be written to this file.
void lm_print_pars(int nout, const double *par, double fnorm, FILE *fout)
int scale_diag
If 1, the variables will be rescaled internally.
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]
double gtol
Orthogonality desired between fvec and its derivs.
void lm_qrsolv(int n, double *r, int ldr, int *ipvt, double *diag, double *qtb, double *x, double *sdiag, double *wa)
[brief description]
#define MAX(a, b)
[brief description]
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.
#define MIN(a, b)
[brief description]
int outcome
Status indicator.
int nfev
actual number of iterations.
double fnorm
norm of the residue vector fvec.
double ftol
Relative error desired in the sum of squares.