44 #include <visp3/vision/vpLevenbergMarquartd.h>
45 #include <visp3/core/vpMath.h>
47 #define SIGN(x) ((x) < 0 ? -1 : 1)
48 #define SWAP(a,b,c) {(c) = (a); (a) = (b); (b) = (c);}
49 #define MIJ(m,i,j,s) ((m) + ((long) (i) * (long) (s)) + (long) (j))
80 double enorm (
const double *x,
int n)
82 const double rdwarf = 3.834e-20;
83 const double rgiant = 1.304e19;
86 double agiant, floatn;
87 double norm_eucl = 0.0;
88 double s1 = 0.0, s2 = 0.0, s3 = 0.0;
89 double x1max = 0.0 , x3max = 0.0;
92 agiant = rgiant / floatn;
94 for (i = 0; i < n; i++)
96 double xabs = std::fabs(x[i]);
97 if ((xabs > rdwarf) && (xabs < agiant))
105 else if (xabs <= rdwarf)
113 if (xabs > std::numeric_limits<double>::epsilon())
114 s3 += (xabs / x3max) * (xabs / x3max);
118 s3 = 1.0 + s3 * (x3max / xabs) * (x3max / xabs);
130 s1 += (xabs / x1max) * (xabs / x1max);
134 s1 = 1.0 + s1 * (x1max / xabs) * (x1max / xabs);
144 if (std::fabs(s1) <= std::numeric_limits<double>::epsilon())
147 if (std::fabs(s2) <= std::numeric_limits<double>::epsilon())
148 norm_eucl = x3max * sqrt(s3);
149 else if (s2 >= x3max)
150 norm_eucl = sqrt (s2 * (1.0 + ( x3max / s2) * (x3max * s3)));
152 norm_eucl = sqrt (x3max * ((s2 / x3max) + (x3max * s3)));
155 norm_eucl = x1max * sqrt (s1 + (s2 / x1max) / x1max);
231 int lmpar(
int n,
double *r,
int ldr,
int *ipvt,
double *diag,
double *qtb,
232 double *delta,
double *par,
double *x,
double *sdiag,
double *wa1,
double *wa2)
234 const double tol1 = 0.1;
241 double dwarf = DBL_MIN;
250 for (
int i = 0; i < n; i++)
253 double *pt = MIJ(r, i, i, ldr);
255 if (std::fabs(*pt) <= std::numeric_limits<double>::epsilon() && nsing == n)
264 for (
int k = 0; k < nsing; k++)
266 int i = nsing - 1 - k;
267 wa1[i] /= *MIJ(r, i, i, ldr);
273 for (
unsigned int j = 0; j <= (
unsigned int)jm1; j++)
274 wa1[j] -= *MIJ(r, i, j, ldr) * temp;
279 for (
int j = 0; j < n; j++)
292 for (
int i = 0; i < n; i++)
293 wa2[i] = diag[i] * x[i];
295 dxnorm = enorm(wa2, n);
297 fp = dxnorm - *delta;
299 if (fp > tol1 * (*delta))
310 for (
int i = 0; i < n; i++)
313 wa1[i] = diag[l] * (wa2[l] / dxnorm);
316 for (
int i = 0; i < n; i++)
324 for (
unsigned int j = 0; j <= (
unsigned int)im1; j++)
325 sum += (*MIJ(r, i, j, ldr) * wa1[j]);
327 wa1[i] = (wa1[i] - sum) / *MIJ(r, i, i, ldr);
330 temp = enorm(wa1, n);
331 parl = ((fp / *delta) / temp) / temp;
338 for (
int i = 0; i < n; i++)
342 for (
int j = 0; j <= i; j++)
343 sum += *MIJ(r, i, j, ldr) * qtb[j];
346 wa1[i] = sum / diag[l];
349 double gnorm = enorm(wa1, n);
350 double paru = gnorm / *delta;
353 if (std::fabs(paru) <= std::numeric_limits<double>::epsilon())
365 if (std::fabs(*par) <= std::numeric_limits<double>::epsilon() )
366 *par = gnorm / dxnorm;
380 if (std::fabs(*par) <= std::numeric_limits<double>::epsilon() ) {
381 const double tol001 = 0.001;
387 for (
int i = 0; i < n; i++)
388 wa1[i] = temp * diag[i];
390 qrsolv(n, r, ldr, ipvt, wa1, qtb, x, sdiag, wa2);
392 for (
int i = 0; i < n; i++)
393 wa2[i] = diag[i] * x[i];
395 dxnorm = enorm(wa2, n);
397 fp = dxnorm - *delta;
407 if ((std::fabs(fp) <= tol1 * (*delta)) || ((std::fabs(parl) <= std::numeric_limits<double>::epsilon()) && (fp <= temp)
408 && (temp < 0.0)) || (iter == 10))
423 for (
int i = 0; i < n; i++)
426 wa1[i] = diag[l] * (wa2[l] / dxnorm);
429 for (
unsigned int i = 0; i < (
unsigned int)n; i++)
431 wa1[i] = wa1[i] / sdiag[i];
433 unsigned int jp1 = i + 1;
434 if ( (
unsigned int) n >= jp1)
436 for (
unsigned int j = jp1; j < (
unsigned int)n; j++)
437 wa1[j] -= (*MIJ(r, i, j, ldr) * temp);
441 temp = enorm(wa1, n);
442 double parc = ((fp / *delta) / temp) / temp;
484 double pythag (
double a,
double b)
486 double pyth, p, r, t;
491 if (std::fabs(p) <= std::numeric_limits<double>::epsilon() )
497 r = (std::min(std::fabs(a), std::fabs(b)) / p) * (std::min(std::fabs(a), std::fabs(b)) / p);
501 while (std::fabs(t - 4.0) < std::fabs(
vpMath::maximum(t,4.0)) * std::numeric_limits<double>::epsilon() )
504 double u = 1.0 + 2.0 * s;
506 r *= (s / u) * (s / u);
561 int qrfac(
int m,
int n,
double *a,
int lda,
int *pivot,
int *ipvt,
562 int ,
double *rdiag,
double *acnorm,
double *wa)
564 const double tolerance = 0.05;
566 int i, j, ip1, k, kmax, minmn;
568 double sum, temp, tmp;
573 epsmch = std::numeric_limits<double>::epsilon();
579 for (i = 0; i < m; i++)
581 acnorm[i] = enorm(MIJ(a, i, 0, lda), n);
582 rdiag[i] = acnorm[i];
592 for (i = 0; i < minmn; i++)
601 for (k = i; k < m; k++)
603 if (rdiag[k] > rdiag[kmax])
609 for (j = 0; j < n; j++)
610 SWAP(*MIJ(a, i, j, lda),
611 *MIJ(a, kmax, j, lda), tmp);
613 rdiag[kmax] = rdiag[i];
616 SWAP( ipvt[i], ipvt[kmax], k);
624 double ajnorm = enorm(MIJ(a, i, i, lda), n - i);
627 if (std::fabs(ajnorm) > std::numeric_limits<double>::epsilon() )
629 if (*MIJ(a, i, i, lda) < 0.0)
632 for (j = i; j < n; j++)
633 *MIJ(a, i, j, lda) /= ajnorm;
634 *MIJ(a, i, i, lda) += 1.0;
644 for (k = ip1; k < m; k++)
647 for (j = i; j < n; j++)
648 sum += *MIJ(a, i, j, lda) * *MIJ(a, k, j, lda);
650 temp = sum / *MIJ(a, i, i, lda);
652 for (j = i; j < n; j++)
653 *MIJ(a, k, j, lda) -= temp * *MIJ(a, i, j, lda);
656 if (pivot && (std::fabs(rdiag[k]) > std::numeric_limits<double>::epsilon()) )
658 temp = *MIJ (a, k, i, lda) / rdiag[k];
661 if (tolerance * (rdiag[k] / wa[k]) * (rdiag[k] / wa[k]) <= epsmch)
663 rdiag[k] = enorm(MIJ(a, k, ip1, lda), (n -1 - (
int) i));
737 int qrsolv (
int n,
double *r,
int ldr,
int *ipvt,
double *diag,
double *qtb,
738 double *x,
double *sdiag,
double *wa)
742 double cosi, cotg, qtbpj, sinu, tg, temp;
749 for (i = 0; i < n; i++)
751 for (j = i; j < n; j++)
752 *MIJ(r, i, j, ldr) = *MIJ(r, j, i, ldr);
754 x[i] = *MIJ(r, i, i, ldr);
763 for (i = 0; i < n; i++)
773 if (std::fabs(diag[l]) > std::numeric_limits<double>::epsilon())
775 for (k = i; k < n; k++)
789 for (k = i; k < n; k++)
798 if (std::fabs(sdiag[k]) > std::numeric_limits<double>::epsilon())
800 if (std::fabs(*MIJ(r, k, k, ldr)) >= std::fabs(sdiag[k]))
802 tg = sdiag[k] / *MIJ(r, k, k, ldr);
803 cosi = 0.5 / sqrt(0.25 + 0.25 * (tg * tg));
808 cotg = *MIJ(r, k, k, ldr) / sdiag[k];
809 sinu = 0.5 / sqrt(0.25 + 0.25 * (cotg * cotg));
818 *MIJ(r, k, k, ldr) = cosi * *MIJ(r, k, k, ldr) + sinu * sdiag[k];
819 temp = cosi * wa[k] + sinu * qtbpj;
820 qtbpj = -sinu * wa[k] + cosi * qtbpj;
832 for (j = kp1; j < n; j++)
834 temp = cosi * *MIJ(r, k, j, ldr) +
836 sdiag[j] = - sinu * *MIJ(r, k, j, ldr) +
838 *MIJ(r, k, j, ldr) = temp;
849 sdiag[i] = *MIJ(r, i, i, ldr);
850 *MIJ(r, i, i, ldr) = x[i];
859 for (i = 0; i < n; i++)
862 if ( (std::fabs(sdiag[i]) <= std::numeric_limits<double>::epsilon()) && nsing == n)
871 for (k = 0; k < nsing; k++)
879 for (j = jp1; j < nsing; j++)
880 sum += *MIJ(r, i, j, ldr) * wa[j];
882 wa[i] = (wa[i] - sum) / sdiag[i];
889 for (j = 0; j < n; j++)
1009 int lmder (
void (*ptr_fcn)(
int m,
int n,
double *xc,
double *fvecc,
1010 double *jac,
int ldfjac,
int iflag),
int m,
int n,
double *x,
1011 double *fvec,
double *fjac,
int ldfjac,
double ftol,
double xtol,
1012 double gtol,
unsigned int maxfev,
double *diag,
int mode,
1013 const double factor,
int nprint,
int *info,
unsigned int *nfev,
1014 int *njev,
int *ipvt,
double *qtf,
double *wa1,
double *wa2,
1015 double *wa3,
double *wa4)
1017 const double tol1 = 0.1, tol5 = 0.5, tol25 = 0.25, tol75 = 0.75, tol0001 = 0.0001;
1022 double actred, delta, dirder, epsmch, fnorm, fnorm1;
1023 double ratio = std::numeric_limits<double>::epsilon();
1024 double par, pnorm, prered;
1025 double sum, temp, temp1, temp2, xnorm = 0.0;
1028 epsmch = std::numeric_limits<double>::epsilon();
1053 if ((n <= 0) || (m < n) || (ldfjac < m) || (ftol < 0.0) || (xtol < 0.0)
1054 || (gtol < 0.0) || (maxfev == 0) || (factor <= 0.0))
1065 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1072 for (j = 0; j < n; j++)
1085 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1098 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1114 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1119 fnorm = enorm(fvec, m);
1132 while (count < (
int)maxfev)
1141 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1156 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1167 if ((iter-1) % nprint == 0)
1168 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1181 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1190 qrfac(n, m, fjac, ldfjac, &oncol, ipvt, n, wa1, wa2, wa3);
1201 for (j = 0; j < n; j++)
1205 if (std::fabs(wa2[j]) <= std::numeric_limits<double>::epsilon())
1216 for (j = 0; j < n; j++)
1217 wa3[j] = diag[j] * x[j];
1219 xnorm = enorm (wa3, n);
1220 delta = factor * xnorm;
1223 if (std::fabs(delta) <= std::numeric_limits<double>::epsilon())
1231 for (i = 0; i < m; i++)
1234 for (i = 0; i < n; i++)
1236 double *pt = MIJ(fjac, i, i, ldfjac);
1238 if (std::fabs(*pt) > std::numeric_limits<double>::epsilon() )
1242 for (j = i; j < m; j++)
1243 sum += *MIJ(fjac, i, j, ldfjac) * wa4[j];
1245 temp = - sum / *MIJ(fjac, i, i, ldfjac);
1247 for (j = i; j < m; j++)
1248 wa4[j] += *MIJ(fjac, i, j, ldfjac) * temp;
1252 *MIJ(fjac, i, i, ldfjac) = wa1[i];
1263 if (std::fabs(fnorm) > std::numeric_limits<double>::epsilon())
1265 for (i = 0; i < n; i++)
1269 if (std::fabs(wa2[l]) > std::numeric_limits<double>::epsilon())
1272 for (j = 0; j <= i; j++)
1273 sum += *MIJ(fjac, i, j, ldfjac) * (qtf[j] / fnorm);
1298 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1309 for (j = 0; j < n; j++)
1317 while (ratio < tol0001)
1323 lmpar(n, fjac, ldfjac, ipvt, diag, qtf, &delta, &par, wa1,
1330 for (j = 0; j < n; j++)
1333 wa2[j] = x[j] + wa1[j];
1334 wa3[j] = diag[j] * wa1[j];
1337 pnorm = enorm(wa3, n);
1352 (*ptr_fcn)(m, n, wa2, wa4, fjac, ldfjac, iflag);
1365 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1370 fnorm1 = enorm(wa4, m);
1378 if ((tol1 * fnorm1) < fnorm)
1379 actred = 1.0 - ((fnorm1 / fnorm) * (fnorm1 / fnorm));
1386 for (i = 0; i < n; i++)
1391 for (j = 0; j <= i; j++)
1392 wa3[j] += *MIJ(fjac, i, j, ldfjac) * temp;
1395 temp1 = enorm(wa3, n) / fnorm;
1396 temp2 = (sqrt(par) * pnorm) / fnorm;
1397 prered = (temp1 * temp1) + (temp2 * temp2) / tol5;
1398 dirder = - ((temp1 * temp1) + (temp2 * temp2));
1407 if (std::fabs(prered) > std::numeric_limits<double>::epsilon())
1408 ratio = actred / prered;
1417 if ((std::fabs(par) <= std::numeric_limits<double>::epsilon()) || (ratio <= tol75))
1419 delta = pnorm / tol5;
1429 temp = tol5 * dirder / (dirder + tol5 * actred);
1431 if ((tol1 * fnorm1 >= fnorm) || (temp < tol1))
1441 if (ratio >= tol0001)
1448 for (j = 0; j < n; j++)
1451 wa2[j] = diag[j] * x[j];
1454 for (i = 0; i < m; i++)
1457 xnorm = enorm(wa2, n);
1466 if ((std::fabs(actred) <= ftol) && (prered <= ftol) && (tol5 * ratio <= 1.0))
1469 if (delta <= xtol * xnorm)
1472 if ((std::fabs(actred) <= ftol) && (prered <= ftol) && (tol5 * ratio <= 1.0)
1487 (*ptr_fcn)(m,n,x,fvec,fjac,ldfjac, iflag);
1496 if (*nfev >= maxfev)
1499 if ((std::fabs(actred) <= epsmch) && (prered <= epsmch)
1500 && (tol5 * ratio <= 1.0))
1503 if (delta <= epsmch * xnorm)
1506 if (gnorm <= epsmch)
1520 (*ptr_fcn)(m, n, x, fvec, fjac, ldfjac, iflag);
1611 int lmder1 (
void (*ptr_fcn)(
int m,
int n,
double *xc,
double *fvecc,
1612 double *jac,
int ldfjac,
int iflag),
1613 int m,
int n,
double *x,
double *fvec,
double *fjac,
1614 int ldfjac,
double tol,
int *info,
int *ipvt,
int lwa,
double *wa)
1616 const double factor = 100.0;
1617 unsigned int maxfev, nfev;
1618 int mode, njev, nprint;
1619 double ftol, gtol, xtol;
1626 if ( (m < n) || (ldfjac < m) || (tol < 0.0) ||
1627 (lwa < (5 * n + m)) )
1629 printf(
"%d %d %d %d \n", (m < n), (ldfjac < m), (tol < 0.0) , (lwa < (5 * n + m))) ;
1635 maxfev = (
unsigned int)(100 * (n + 1));
1642 lmder (ptr_fcn , m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, wa,
1643 mode, factor, nprint, info, &nfev, &njev, ipvt, &wa[n], &wa[2 * n],
1644 &wa[3 * n], &wa[4 * n], &wa[5 * n]);
static Type maximum(const Type &a, const Type &b)
static Type minimum(const Type &a, const Type &b)