46 #define LM_MACHEP DBL_EPSILON
47 #define LM_DWARF DBL_MIN
48 #define LM_SQRT_DWARF sqrt(DBL_MIN)
49 #define LM_SQRT_GIANT sqrt(DBL_MAX)
50 #define LM_USERTOL 30*LM_MACHEP
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)"
104 double epsilon,
double stepbound,
int maxcall,
int scale_diag,
125 double(*f)(
double,
double* ) )
134 QString statmsg = longmsg
145 const void * ,
const double *fvec,
146 int printflags,
int iflag,
int iter,
int nfev)
160 if( printflags & 1 ){
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);
173 if( printflags & 2 ){
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));
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] );
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),
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) )
206 double *fvec, *diag, *fjac, *qtf, *wa1, *wa2, *wa3, *wa4;
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 ) {
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();
238 fjac = v_fjac.data();
243 ipvt = v_ipvt.data();
246 for( j=0; j<n_par; ++j )
258 &(status->
nfev), fjac, ipvt, qtf, wa1, wa2, wa3, wa4,
259 evaluate, printout, control->
printflags, data );
262 (*printout)( n, par, m, data, fvec,
264 status->
fnorm = (*lm_use_norm)(m, fvec);
265 if ( status->
info < 0 )
284 #define MIN(a,b) (((a)<=(b)) ? (a) : (b))
285 #define MAX(a,b) (((a)>=(b)) ? (a) : (b))
286 #define SQR(x) (x)*(x)
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 )
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;
468 if ((n <= 0) || (m < n) || (ftol < 0.)
469 || (xtol < 0.) || (gtol < 0.) || (maxfev <= 0) || (factor <= 0.)) {
474 for (j = 0; j < n; j++) {
475 if (diag[j] <= 0.0) {
481 #ifdef LMFIT_DEBUG_MESSAGES
488 (*evaluate) (x, m, data, fvec, info);
491 (*printout) (n, x, m, data, fvec, printflags, 0, 0, *nfev);
494 fnorm = (*lm_use_norm)(m, fvec);
503 #ifdef LMFIT_DEBUG_MESSAGES
504 printf(
"lmdif/ outer loop iter=%d nfev=%d fnorm=%.10e\n",
510 for (j = 0; j < n; j++) {
512 step =
MAX(eps*eps, eps * fabs(temp));
515 (*evaluate) (x, m, data, wa4, info);
518 (*printout) (n, x, m, data, wa4, printflags, 1, iter, *nfev);
521 for (i = 0; i < m; i++)
522 fjac[j*m+i] = (wa4[i] - fvec[i]) / step;
525 #ifdef LMFIT_DEBUG_MATRIX
527 for (i = 0; i < m; i++) {
528 for (j = 0; j < n; j++)
529 printf(
"%.5e ", fjac[j*m+i]);
536 lm_qrfac(m, n, fjac, 1, ipvt, wa1, wa2, wa3);
543 for (j = 0; j < n; j++) {
550 for (j = 0; j < n; j++)
551 wa3[j] = diag[j] * x[j];
552 xnorm = (*lm_use_norm)(n, wa3);
554 delta = factor * xnorm;
559 for (j = 0; j < n; j++)
560 diag[j] =
MAX( diag[j], wa2[j] );
566 for (i = 0; i < m; i++)
569 for (j = 0; j < n; j++) {
573 for (i = j; i < m; i++)
574 sum += fjac[j*m+i] * wa4[i];
576 for (i = j; i < m; i++)
577 wa4[i] += fjac[j*m+i] * temp;
579 fjac[j*m+j] = wa1[j];
586 for (j = 0; j < n; j++) {
587 if (wa2[ipvt[j]] == 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 ) );
602 #ifdef LMFIT_DEBUG_MESSAGES
603 printf(
"lmdif/ inner loop iter=%d nfev=%d\n", iter, *nfev);
608 lm_lmpar( n, fjac, m, ipvt, diag, qtf, delta, &par,
609 wa1, wa2, wa4, wa3 );
612 for (j = 0; j < n; j++)
613 wa2[j] = x[j] - wa1[j];
615 pnorm = (*lm_use_norm)(n, wa3);
620 delta =
MIN(delta, pnorm);
625 (*evaluate) (wa2, m, data, wa4, info);
628 (*printout) (n, wa2, m, data, wa4, printflags, 2, iter, *nfev);
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);
641 if (p1 * fnorm1 < fnorm)
642 actred = 1 -
SQR(fnorm1 / fnorm);
649 for (j = 0; j < n; j++) {
651 for (i = 0; i <= j; i++)
652 wa3[i] -= fjac[j*m+i] * wa1[ipvt[j]];
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));
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);
675 temp = 0.5 * dirder / (dirder + 0.5 * actred);
676 if (p1 * fnorm1 >= fnorm || temp < p1)
678 delta = temp *
MIN(delta, pnorm / p1);
680 }
else if (par == 0. || ratio >= 0.75) {
687 if (ratio >= p0001) {
689 for (j = 0; j < n; j++) {
691 wa2[j] = diag[j] * x[j];
693 for (i = 0; i < m; i++)
695 xnorm = (*lm_use_norm)(n, wa2);
699 #ifdef LMFIT_DEBUG_MESSAGES
701 printf(
"ATTN: iteration considered unsuccessful\n");
713 if (fabs(actred) <= ftol && prered <= ftol && 0.5 * ratio <= 1)
715 if (delta <= xtol * xnorm)
722 if (*nfev >= maxfev){
727 prered <=
LM_MACHEP && 0.5 * ratio <= 1){
742 }
while (ratio < p0001);
756 double *qtb,
double delta,
double *par,
double *x,
757 double *sdiag,
double *aux,
double *xdi)
831 int i, iter, j, nsing;
832 double dxnorm, fp, fp_old, gnorm, parc, parl, paru;
834 static double p1 = 0.1;
836 #ifdef LMFIT_DEBUG_MESSAGES
844 for (j = 0; j < n; j++) {
846 if (r[j * ldr + j] == 0 && nsing == n)
851 #ifdef LMFIT_DEBUG_MESSAGES
852 printf(
"nsing %d ", nsing);
854 for (j = nsing - 1; j >= 0; j--) {
855 aux[j] = aux[j] / r[j + ldr * j];
857 for (i = 0; i < j; i++)
858 aux[i] -= r[j * ldr + i] * temp;
861 for (j = 0; j < n; j++)
868 for (j = 0; j < n; j++)
869 xdi[j] = diag[j] * x[j];
870 dxnorm = (*lm_use_norm)(n, xdi);
872 if (fp <= p1 * delta) {
873 #ifdef LMFIT_DEBUG_MESSAGES
874 printf(
"lmpar/ terminate (fp<p1*delta)\n");
886 for (j = 0; j < n; j++)
887 aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
889 for (j = 0; j < n; j++) {
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];
895 temp = (*lm_use_norm)(n, aux);
896 parl = fp / delta / temp / temp;
901 for (j = 0; j < n; j++) {
903 for (i = 0; i <= j; i++)
904 sum += r[j * ldr + i] * qtb[i];
905 aux[j] = sum / diag[ipvt[j]];
907 gnorm = (*lm_use_norm)(n, aux);
908 paru = gnorm / delta;
915 *par =
MAX(*par, parl);
916 *par =
MIN(*par, paru);
918 *par = gnorm / dxnorm;
919 #ifdef LMFIT_DEBUG_MESSAGES
920 printf(
"lmpar/ parl %.4e par %.4e paru %.4e\n", parl, *par, paru);
932 for (j = 0; j < n; j++)
933 aux[j] = temp * diag[j];
935 lm_qrsolv( n, r, ldr, ipvt, aux, qtb, x, sdiag, xdi );
938 for (j = 0; j < n; j++)
939 xdi[j] = diag[j] * x[j];
940 dxnorm = (*lm_use_norm)(n, xdi);
948 if (fabs(fp) <= p1 * delta
949 || (parl == 0. && fp <= fp_old && fp_old < 0.)
955 for (j = 0; j < n; j++)
956 aux[j] = diag[ipvt[j]] * xdi[ipvt[j]] / dxnorm;
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];
963 temp = (*lm_use_norm)(n, aux);
964 parc = fp / delta / temp / temp;
969 parl =
MAX(parl, *par);
971 paru =
MIN(paru, *par);
976 *par =
MAX(parl, *par + parc);
988 double *rdiag,
double *acnorm,
double *wa)
1041 int i, j, k, kmax, minmn;
1042 double ajnorm, sum, temp;
1046 for (j = 0; j < n; j++) {
1047 acnorm[j] = (*lm_use_norm)(m, &a[j*m]);
1048 rdiag[j] = acnorm[j];
1053 #ifdef LMFIT_DEBUG_MESSAGES
1060 for (j = 0; j < minmn; j++) {
1067 for (k = j + 1; k < n; k++)
1068 if (rdiag[k] > rdiag[kmax])
1073 for (i = 0; i < m; i++) {
1075 a[j*m+i] = a[kmax*m+i];
1078 rdiag[kmax] = rdiag[j];
1081 ipvt[j] = ipvt[kmax];
1088 ajnorm = (*lm_use_norm)(m-j, &a[j*m+j]);
1096 for (i = j; i < m; i++)
1103 for (k = j + 1; k < n; k++) {
1106 for (i = j; i < m; i++)
1107 sum += a[j*m+i] * a[k*m+i];
1109 temp = sum / a[j + m * j];
1111 for (i = j; i < m; i++)
1112 a[k*m+i] -= temp * a[j*m+i];
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];
1120 rdiag[k] = (*lm_use_norm)(m-j-1, &a[m*k+j+1]);
1136 double *qtb,
double *x,
double *sdiag,
double *wa)
1199 int i, kk, j, k, nsing;
1200 double qtbpj, sum, temp;
1201 double _sin, _cos, _tan, _cot;
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];
1212 #ifdef LMFIT_DEBUG_MESSAGES
1218 for (j = 0; j < n; j++) {
1223 if (diag[ipvt[j]] == 0.)
1225 for (k = j; k < n; k++)
1227 sdiag[j] = diag[ipvt[j]];
1233 for (k = j; k < n; k++) {
1241 if (fabs(r[kk]) < fabs(sdiag[k])) {
1242 _cot = r[kk] / sdiag[k];
1243 _sin = 1 / sqrt(1 +
SQR(_cot));
1246 _tan = sdiag[k] / r[kk];
1247 _cos = 1 / sqrt(1 +
SQR(_tan));
1254 r[kk] = _cos * r[kk] + _sin * sdiag[k];
1255 temp = _cos * wa[k] + _sin * qtbpj;
1256 qtbpj = -_sin * wa[k] + _cos * qtbpj;
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;
1272 sdiag[j] = r[j * ldr + j];
1273 r[j * ldr + j] = x[j];
1280 for (j = 0; j < n; j++) {
1281 if (sdiag[j] == 0. && nsing == n)
1287 for (j = nsing - 1; j >= 0; j--) {
1289 for (i = j + 1; i < nsing; i++)
1290 sum += r[j * ldr + i] * wa[i];
1291 wa[j] = (wa[j] - sum) / sdiag[j];
1296 for (j = 0; j < n; j++)
1304 for (
int i = 0; i < n; i++ )
1306 rmsd += x[ i ] * x[ i ];
1308 return sqrt( rmsd );
1338 double agiant, s1, s2, s3, xabs, x1max, x3max, temp;
1349 for (i = 0; i < n; i++) {
1352 if ( xabs < agiant ) {
1354 }
else if ( xabs > x1max ) {
1355 temp = x1max / xabs;
1356 s1 = 1 + s1 *
SQR(temp);
1359 temp = xabs / x1max;
1362 }
else if ( xabs > x3max ) {
1363 temp = x3max / xabs;
1364 s3 = 1 + s3 *
SQR(temp);
1366 }
else if (xabs != 0.) {
1367 temp = xabs / x3max;
1375 return x1max * sqrt(s1 + (s2 / x1max) / x1max);
1378 return sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
1380 return sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
1382 return x3max * sqrt(s3);
1387 double *fvec,
int * )
1390 for ( i = 0; i < m_dat; i++ )
1398 const double *t,
const double *y,
1399 double (*f)(
double t,
double *par ),
1404 lmmin( n_par, par, m_dat, (
const void*) &data,
1409 const double *t,
const double *y,
1410 double (*f)(
double t,
double *par ),
1417 lmmin( n_par, par, m_dat, (
const void*) &data,
1425 1.e-7, 1.e-7, 1.e-7, 1.e-7, 100., 100, 0, 0 );