minpack.cpp

  1. #include "stdafx.h"
  2. #include "math.h"
  3. #include "f2c.h" 
  4. #include "minpack.h"
  5. #include "f:practiceyang2cal_main.h"
  6. static long c__1 = 1;
  7. static long c__2 = 1;
  8. static long c__3 = 2;
  9. static long c__4 = 1;
  10. int qrsolv_(long *n, double *r, long *ldr, long *ipvt, double *diag, double *qtb, 
  11.                     double *x, double *sdiag, double *wa)
  12. {
  13.     /* Initialized data */
  14.     static double p5 = .5;
  15.     static double p25 = .25;
  16.     static double zero = 0.;
  17.     /* System generated locals */
  18.     long r_dim1, r_offset, i__1, i__2, i__3;
  19.     double d__1, d__2;
  20.     /* Builtin functions */
  21.     /* Local variables */
  22.     static double temp;
  23.     static long i, j, k, l;
  24.     static double cotan;
  25.     static long nsing;
  26.     static double qtbpj;
  27.     static long jp1, kp1;
  28.     static double tan_, cos_, sin_, sum;
  29.     /* Parameter adjustments */
  30.     --wa;
  31.     --sdiag;
  32.     --x;
  33.     --qtb;
  34.     --diag;
  35.     --ipvt;
  36.     r_dim1 = *ldr;
  37.     r_offset = r_dim1 + 1;
  38.     r -= r_offset;
  39.     /* Function Body */
  40.     i__1 = *n;
  41.     for (j = 1; j <= i__1; ++j) {
  42. i__2 = *n;
  43. for (i = j; i <= i__2; ++i) {
  44.     r[i + j * r_dim1] = r[j + i * r_dim1];
  45. /* L10: */
  46. }
  47. x[j] = r[j + j * r_dim1];
  48. wa[j] = qtb[j];
  49. /* L20: */
  50.     }
  51. /*     eliminate the diagonal matrix d using a givens rotation. */
  52.     i__1 = *n;
  53.     for (j = 1; j <= i__1; ++j) {
  54. /*        prepare the row of d to be eliminated, locating the */
  55. /*        diagonal element using p from the qr factorization. */
  56. l = ipvt[j];
  57. if (diag[l] == zero) {
  58.     goto L90;
  59. }
  60. i__2 = *n;
  61. for (k = j; k <= i__2; ++k) {
  62.     sdiag[k] = zero;
  63. /* L30: */
  64. }
  65. sdiag[j] = diag[l];
  66. /*        the transformations to eliminate the row of d */
  67. /*        modify only a single element of (q transpose)*b */
  68. /*        beyond the first n, which is initially zero. */
  69. qtbpj = zero;
  70. i__2 = *n;
  71. for (k = j; k <= i__2; ++k) {
  72. /*           determine a givens rotation which eliminates the */
  73. /*           appropriate element in the current row of d. */
  74.     if (sdiag[k] == zero) {
  75. goto L70;
  76.     }
  77.     if ((d__1 = r[k + k * r_dim1], abs(d__1)) >= (d__2 = sdiag[k], 
  78.     abs(d__2))) {
  79. goto L40;
  80.     }
  81.     cotan = r[k + k * r_dim1] / sdiag[k];
  82. /* Computing 2nd power */
  83.     d__1 = cotan;
  84.     sin_ = p5 / sqrt(p25 + p25 * (d__1 * d__1));
  85.     cos_ = sin_ * cotan;
  86.     goto L50;
  87. L40:
  88.     tan_ = sdiag[k] / r[k + k * r_dim1];
  89. /* Computing 2nd power */
  90.     d__1 = tan_;
  91.     cos_ = p5 / sqrt(p25 + p25 * (d__1 * d__1));
  92.     sin_ = cos_ * tan_;
  93. L50:
  94. /*           compute the modified diagonal element of r and */
  95. /*           the modified element of ((q transpose)*b,0). */
  96.     r[k + k * r_dim1] = cos_ * r[k + k * r_dim1] + sin_ * sdiag[k];
  97.     temp = cos_ * wa[k] + sin_ * qtbpj;
  98.     qtbpj = -sin_ * wa[k] + cos_ * qtbpj;
  99.     wa[k] = temp;
  100. /*           accumulate the tranformation in the row of s. */
  101.     kp1 = k + 1;
  102.     if (*n < kp1) {
  103. goto L70;
  104.     }
  105.     i__3 = *n;
  106.     for (i = kp1; i <= i__3; ++i) {
  107. temp = cos_ * r[i + k * r_dim1] + sin_ * sdiag[i];
  108. sdiag[i] = -sin_ * r[i + k * r_dim1] + cos_ * sdiag[i];
  109. r[i + k * r_dim1] = temp;
  110. /* L60: */
  111.     }
  112. L70:
  113. /* L80: */
  114.     ;
  115. }
  116. L90:
  117. /*        store the diagonal element of s and restore */
  118. /*        the corresponding diagonal element of r. */
  119. sdiag[j] = r[j + j * r_dim1];
  120. r[j + j * r_dim1] = x[j];
  121. /* L100: */
  122.     }
  123. /*     solve the triangular system for z. if the system is */
  124. /*     singular, then obtain a least squares solution. */
  125.     nsing = *n;
  126.     i__1 = *n;
  127.     for (j = 1; j <= i__1; ++j) {
  128. if (sdiag[j] == zero && nsing == *n) {
  129.     nsing = j - 1;
  130. }
  131. if (nsing < *n) {
  132.     wa[j] = zero;
  133. }
  134. /* L110: */
  135.     }
  136.     if (nsing < 1) {
  137. goto L150;
  138.     }
  139.     i__1 = nsing;
  140.     for (k = 1; k <= i__1; ++k) {
  141. j = nsing - k + 1;
  142. sum = zero;
  143. jp1 = j + 1;
  144. if (nsing < jp1) {
  145.     goto L130;
  146. }
  147. i__2 = nsing;
  148. for (i = jp1; i <= i__2; ++i) {
  149.     sum += r[i + j * r_dim1] * wa[i];
  150. /* L120: */
  151. }
  152. L130:
  153. wa[j] = (wa[j] - sum) / sdiag[j];
  154. /* L140: */
  155.     }
  156. L150:
  157. /*     permute the components of z back to components of x. */
  158.     i__1 = *n;
  159.     for (j = 1; j <= i__1; ++j) {
  160. l = ipvt[j];
  161. x[l] = wa[j];
  162. /* L160: */
  163.     }
  164.     return 0;
  165. double dpmpar_(long *i)
  166. {
  167. static struct {
  168. long e_1[6];
  169. double fill_2[1];
  170. double e_3;
  171. } equiv_2 = { 1018167296, 0, 1048576, 0, 2146435071, -1, {0}, 0. };
  172.     /* System generated locals */
  173.     double ret_val;
  174.     /* Local variables */
  175. #define dmach ((double *)&equiv_2)
  176. #define minmag ((long *)&equiv_2 + 2)
  177. #define maxmag ((long *)&equiv_2 + 4)
  178. #define mcheps ((long *)&equiv_2)
  179.     ret_val = dmach[*i - 1];
  180.     return ret_val;
  181. #undef mcheps
  182. #undef maxmag
  183. #undef minmag
  184. #undef dmach
  185. double enorm_(long *n, double *x)
  186. {
  187.     /* Initialized data */
  188.     static double one = 1.;
  189.     static double zero = 0.;
  190.     static double rdwarf = 3.834e-20;
  191.     static double rgiant = 1.304e19;
  192.     /* System generated locals */
  193.     long i__1;
  194.     double ret_val, d__1;
  195.     /* Builtin functions */
  196.     /* Local variables */
  197.     static double xabs, x1max, x3max;
  198.     static long i;
  199.     static double s1, s2, s3, agiant, floatn;
  200.     /* Parameter adjustments */
  201.     --x;
  202.     /* Function Body */
  203.     s1 = zero;
  204.     s2 = zero;
  205.     s3 = zero;
  206.     x1max = zero;
  207.     x3max = zero;
  208.     floatn = (double) (*n);
  209.     agiant = rgiant / floatn;
  210.     i__1 = *n;
  211.     for (i = 1; i <= i__1; ++i) {
  212. xabs = (d__1 = x[i], abs(d__1));
  213. if (xabs > rdwarf && xabs < agiant) {
  214.     goto L70;
  215. }
  216. if (xabs <= rdwarf) {
  217.     goto L30;
  218. }
  219. /*              sum for large components. */
  220. if (xabs <= x1max) {
  221.     goto L10;
  222. }
  223. /* Computing 2nd power */
  224. d__1 = x1max / xabs;
  225. s1 = one + s1 * (d__1 * d__1);
  226. x1max = xabs;
  227. goto L20;
  228. L10:
  229. /* Computing 2nd power */
  230. d__1 = xabs / x1max;
  231. s1 += d__1 * d__1;
  232. L20:
  233. goto L60;
  234. L30:
  235. /*              sum for small components. */
  236. if (xabs <= x3max) {
  237.     goto L40;
  238. }
  239. /* Computing 2nd power */
  240. d__1 = x3max / xabs;
  241. s3 = one + s3 * (d__1 * d__1);
  242. x3max = xabs;
  243. goto L50;
  244. L40:
  245. if (xabs != zero) {
  246. /* Computing 2nd power */
  247.     d__1 = xabs / x3max;
  248.     s3 += d__1 * d__1;
  249. }
  250. L50:
  251. L60:
  252. goto L80;
  253. L70:
  254. /*           sum for intermediate components. */
  255. /* Computing 2nd power */
  256. d__1 = xabs;
  257. s2 += d__1 * d__1;
  258. L80:
  259. /* L90: */
  260. ;
  261.     }
  262. /*     calculation of norm. */
  263.     if (s1 == zero) {
  264. goto L100;
  265.     }
  266.     ret_val = x1max * sqrt(s1 + s2 / x1max / x1max);
  267.     goto L130;
  268. L100:
  269.     if (s2 == zero) {
  270. goto L110;
  271.     }
  272.     if (s2 >= x3max) {
  273. d__1 = x3max * s3;
  274. ret_val = sqrt(s2 * (one + x3max / s2 * d__1));
  275.     }
  276.     if (s2 < x3max) {
  277. ret_val = sqrt(x3max * (s2 / x3max + x3max * s3));
  278.     }
  279.     goto L120;
  280. L110:
  281.     ret_val = x3max * sqrt(s3);
  282. L120:
  283. L130:
  284.     return ret_val;
  285. int fdjac2_(void(*fcn)(long*,long*,double*,double*), long *m, long *n, double *x, double *fvec, double *fjac, long *ldfjac, 
  286. long *iflag, double *epsfcn, double *wa)
  287. {
  288.     static double zero = 0.;
  289.     long fjac_dim1, fjac_offset, i__1, i__2;
  290.     /* Builtin functions */
  291.     /* Local variables */
  292.     static double temp, h;
  293.     static long i, j;
  294.     static double epsmch;
  295.     static double eps;
  296.     /* Parameter adjustments */
  297.     --wa;
  298.     fjac_dim1 = *ldfjac;
  299.     fjac_offset = fjac_dim1 + 1;
  300.     fjac -= fjac_offset;
  301.     --fvec;
  302.     --x;
  303.     /* Function Body */
  304. /*     epsmch is the machine precision. */
  305.     epsmch = dpmpar_(&c__1);
  306.     eps = sqrt((max(*epsfcn,epsmch)));
  307.     i__1 = *n;
  308.     for (j = 1; j <= i__1; ++j) {
  309. temp = x[j];
  310. h = eps * abs(temp);
  311. if (h == zero) {
  312.     h = eps;
  313. }
  314. x[j] = temp + h;
  315. //(*fcn)(m, n, &x[1], &wa[1], iflag);
  316. (*fcn)(m, n, &x[1], &wa[1]);
  317. if (*iflag < 0) {
  318.     goto L30;
  319. }
  320. x[j] = temp;
  321. i__2 = *m;
  322. for (i = 1; i <= i__2; ++i) {
  323.     fjac[i + j * fjac_dim1] = (wa[i] - fvec[i]) / h;
  324. /* L10: */
  325. }
  326. /* L20: */
  327.     }
  328. L30:
  329.     return 0;
  330. int lmdif_(void(*fcn)(long*,long*,double*,double*), long *m, long *n, double *x, double *fvec, double *ftol, double *xtol, double *gtol,
  331.    long *maxfev,double *epsfcn, double *diag, long *mode, double *factor, long *nprint, long *info, 
  332.    long *nfev, double *fjac, long *ldfjac, long *ipvt,double *qtf, double *wa1, double *wa2,
  333.    double *wa3, double *wa4)
  334. {
  335.     /* Initialized data */
  336.     static double one = 1.;
  337.     static double p1 = .1;
  338.     static double p5 = .5;
  339.     static double p25 = .25;
  340.     static double p75 = .75;
  341.     static double p0001 = 1e-4;
  342.     static double zero = 0.;
  343.     /* System generated locals */
  344.     long fjac_dim1, fjac_offset, i__1, i__2;
  345.     double d__1, d__2, d__3;
  346.     /* Builtin functions */
  347.     /* Local variables */
  348.     static long iter;
  349.     static double temp, temp1, temp2;
  350.     static long i, j, l, iflag;
  351.     static double delta;
  352.     static double ratio;
  353.     static double fnorm, gnorm;
  354.     static double pnorm, xnorm, fnorm1, actred, dirder, epsmch, prered;
  355.     static double par, sum;
  356.     --wa4;
  357.     --wa3;
  358.     --wa2;
  359.     --wa1;
  360.     --qtf;
  361.     --ipvt;
  362.     fjac_dim1 = *ldfjac;
  363.     fjac_offset = fjac_dim1 + 1;
  364.     fjac -= fjac_offset;
  365.     --diag;
  366.     --fvec;
  367.     --x;
  368.     /* Function Body */
  369. /*     epsmch is the machine precision. */
  370.     epsmch = dpmpar_(&c__2);
  371.     *info = 0;
  372.     iflag = 0;
  373.     *nfev = 0;
  374. /*     check the input parameters for errors. */
  375.     if (*n <= 0 || *m < *n || *ldfjac < *m || *ftol < zero || *xtol < zero || 
  376.     *gtol < zero || *maxfev <= 0 || *factor <= zero) {
  377. goto L300;
  378.     }
  379.     if (*mode != 2) {
  380. goto L20;
  381.     }
  382.     i__1 = *n;
  383.     for (j = 1; j <= i__1; ++j) {
  384. if (diag[j] <= zero) {
  385.     goto L300;
  386. }
  387. /* L10: */
  388.     }
  389. L20:
  390. /*     evaluate the function at the starting point */
  391. /*     and calculate its norm. */
  392.     iflag = 1;
  393.     //(*fcn)(m, n, &x[1], &fvec[1], &iflag);
  394.     (*fcn)(m, n, &x[1], &fvec[1]);
  395.     *nfev = 1;
  396.     if (iflag < 0) {
  397. goto L300;
  398.     }
  399.     fnorm = enorm_(m, &fvec[1]);
  400. /*     initialize levenberg-marquardt parameter and iteration counter. */
  401.     par = zero;
  402.     iter = 1;
  403. /*     beginning of the outer loop. */
  404. L30:
  405. /*        calculate the jacobian matrix. */
  406.     iflag = 2;
  407.     fdjac2_(fcn, m, n, &x[1], &fvec[1], &fjac[fjac_offset], ldfjac, &iflag, 
  408.     epsfcn, &wa4[1]);
  409.     *nfev += *n;
  410.     if (iflag < 0) {
  411. goto L300;
  412.     }
  413. /*        if requested, call fcn to enable printing of iterates. */
  414.     if (*nprint <= 0) {
  415. goto L40;
  416.     }
  417.     iflag = 0;
  418.     if ((iter - 1) % *nprint == 0) {
  419. //(*fcn)(m, n, &x[1], &fvec[1], &iflag);
  420. (*fcn)(m, n, &x[1], &fvec[1]);
  421.     }
  422.     if (iflag < 0) {
  423. goto L300;
  424.     }
  425. L40:
  426. /*        compute the qr factorization of the jacobian. */
  427.     qrfac_(m, n, &fjac[fjac_offset], ldfjac, &c__2, &ipvt[1], n, &wa1[1], &
  428.     wa2[1], &wa3[1]);
  429. /*        on the first iteration and if mode is 1, scale according */
  430. /*        to the norms of the columns of the initial jacobian. */
  431.     if (iter != 1) {
  432. goto L80;
  433.     }
  434.     if (*mode == 2) {
  435. goto L60;
  436.     }
  437.     i__1 = *n;
  438.     for (j = 1; j <= i__1; ++j) {
  439. diag[j] = wa2[j];
  440. if (wa2[j] == zero) {
  441.     diag[j] = one;
  442. }
  443. /* L50: */
  444.     }
  445. L60:
  446. /*        on the first iteration, calculate the norm of the scaled x */
  447. /*        and initialize the step bound delta. */
  448.     i__1 = *n;
  449.     for (j = 1; j <= i__1; ++j) {
  450. wa3[j] = diag[j] * x[j];
  451. /* L70: */
  452.     }
  453.     xnorm = enorm_(n, &wa3[1]);
  454.     delta = *factor * xnorm;
  455.     if (delta == zero) {
  456. delta = *factor;
  457.     }
  458. L80:
  459. /*        form (q transpose)*fvec and store the first n components in */
  460. /*        qtf. */
  461.     i__1 = *m;
  462.     for (i = 1; i <= i__1; ++i) {
  463. wa4[i] = fvec[i];
  464. /* L90: */
  465.     }
  466.     i__1 = *n;
  467.     for (j = 1; j <= i__1; ++j) {
  468. if (fjac[j + j * fjac_dim1] == zero) {
  469.     goto L120;
  470. }
  471. sum = zero;
  472. i__2 = *m;
  473. for (i = j; i <= i__2; ++i) {
  474.     sum += fjac[i + j * fjac_dim1] * wa4[i];
  475. /* L100: */
  476. }
  477. temp = -sum / fjac[j + j * fjac_dim1];
  478. i__2 = *m;
  479. for (i = j; i <= i__2; ++i) {
  480.     wa4[i] += fjac[i + j * fjac_dim1] * temp;
  481. /* L110: */
  482. }
  483. L120:
  484. fjac[j + j * fjac_dim1] = wa1[j];
  485. qtf[j] = wa4[j];
  486. /* L130: */
  487.     }
  488. /*        compute the norm of the scaled gradient. */
  489.     gnorm = zero;
  490.     if (fnorm == zero) {
  491. goto L170;
  492.     }
  493.     i__1 = *n;
  494.     for (j = 1; j <= i__1; ++j) {
  495. l = ipvt[j];
  496. if (wa2[l] == zero) {
  497.     goto L150;
  498. }
  499. sum = zero;
  500. i__2 = j;
  501. for (i = 1; i <= i__2; ++i) {
  502.     sum += fjac[i + j * fjac_dim1] * (qtf[i] / fnorm);
  503. /* L140: */
  504. }
  505. /* Computing MAX */
  506. d__2 = gnorm, d__3 = (d__1 = sum / wa2[l], abs(d__1));
  507. gnorm = max(d__2,d__3);
  508. L150:
  509. /* L160: */
  510. ;
  511.     }
  512. L170:
  513. /*        test for convergence of the gradient norm. */
  514.     if (gnorm <= *gtol) {
  515. *info = 4;
  516.     }
  517.     if (*info != 0) {
  518. goto L300;
  519.     }
  520. /*        rescale if necessary. */
  521.     if (*mode == 2) {
  522. goto L190;
  523.     }
  524.     i__1 = *n;
  525.     for (j = 1; j <= i__1; ++j) {
  526. /* Computing MAX */
  527. d__1 = diag[j], d__2 = wa2[j];
  528. diag[j] = max(d__1,d__2);
  529. /* L180: */
  530.     }
  531. L190:
  532. /*        beginning of the inner loop. */
  533. L200:
  534. /*           determine the levenberg-marquardt parameter. */
  535.     lmpar_(n, &fjac[fjac_offset], ldfjac, &ipvt[1], &diag[1], &qtf[1], &delta,
  536.      &par, &wa1[1], &wa2[1], &wa3[1], &wa4[1]);
  537. /*           store the direction p and x + p. calculate the norm of p. */
  538.     i__1 = *n;
  539.     for (j = 1; j <= i__1; ++j) {
  540. wa1[j] = -wa1[j];
  541. wa2[j] = x[j] + wa1[j];
  542. wa3[j] = diag[j] * wa1[j];
  543. /* L210: */
  544.     }
  545.     pnorm = enorm_(n, &wa3[1]);
  546. /*           on the first iteration, adjust the initial step bound. */
  547.     if (iter == 1) {
  548. delta = min(delta,pnorm);
  549.     }
  550. /*           evaluate the function at x + p and calculate its norm. */
  551.     iflag = 1;
  552.     //(*fcn)(m, n, &wa2[1], &wa4[1], &iflag);
  553.     (*fcn)(m, n, &wa2[1], &wa4[1]);
  554.     ++(*nfev);
  555.     if (iflag < 0) {
  556. goto L300;
  557.     }
  558.     fnorm1 = enorm_(m, &wa4[1]);
  559. /*           compute the scaled actual reduction. */
  560.     actred = -one;
  561.     if (p1 * fnorm1 < fnorm) {
  562. /* Computing 2nd power */
  563. d__1 = fnorm1 / fnorm;
  564. actred = one - d__1 * d__1;
  565.     }
  566. /*           compute the scaled predicted reduction and */
  567. /*           the scaled directional derivative. */
  568.     i__1 = *n;
  569.     for (j = 1; j <= i__1; ++j) {
  570. wa3[j] = zero;
  571. l = ipvt[j];
  572. temp = wa1[l];
  573. i__2 = j;
  574. for (i = 1; i <= i__2; ++i) {
  575.     wa3[i] += fjac[i + j * fjac_dim1] * temp;
  576. /* L220: */
  577. }
  578. /* L230: */
  579.     }
  580.     temp1 = enorm_(n, &wa3[1]) / fnorm;
  581.     temp2 = sqrt(par) * pnorm / fnorm;
  582. /* Computing 2nd power */
  583.     d__1 = temp1;
  584. /* Computing 2nd power */
  585.     d__2 = temp2;
  586.     prered = d__1 * d__1 + d__2 * d__2 / p5;
  587. /* Computing 2nd power */
  588.     d__1 = temp1;
  589. /* Computing 2nd power */
  590.     d__2 = temp2;
  591.     dirder = -(d__1 * d__1 + d__2 * d__2);
  592. /*           compute the ratio of the actual to the predicted */
  593. /*           reduction. */
  594.     ratio = zero;
  595.     if (prered != zero) {
  596. ratio = actred / prered;
  597.     }
  598. /*           update the step bound. */
  599.     if (ratio > p25) {
  600. goto L240;
  601.     }
  602.     if (actred >= zero) {
  603. temp = p5;
  604.     }
  605.     if (actred < zero) {
  606. temp = p5 * dirder / (dirder + p5 * actred);
  607.     }
  608.     if (p1 * fnorm1 >= fnorm || temp < p1) {
  609. temp = p1;
  610.     }
  611. /* Computing MIN */
  612.     d__1 = delta, d__2 = pnorm / p1;
  613.     delta = temp * min(d__1,d__2);
  614.     par /= temp;
  615.     goto L260;
  616. L240:
  617.     if (par != zero && ratio < p75) {
  618. goto L250;
  619.     }
  620.     delta = pnorm / p5;
  621.     par = p5 * par;
  622. L250:
  623. L260:
  624. /*           test for successful iteration. */
  625.     if (ratio < p0001) {
  626. goto L290;
  627.     }
  628. /*           successful iteration. update x, fvec, and their norms. */
  629.     i__1 = *n;
  630.     for (j = 1; j <= i__1; ++j) {
  631. x[j] = wa2[j];
  632. wa2[j] = diag[j] * x[j];
  633. /* L270: */
  634.     }
  635.     i__1 = *m;
  636.     for (i = 1; i <= i__1; ++i) {
  637. fvec[i] = wa4[i];
  638. /* L280: */
  639.     }
  640.     xnorm = enorm_(n, &wa2[1]);
  641.     fnorm = fnorm1;
  642.     ++iter;
  643. L290:
  644. /*           tests for convergence. */
  645.     if (abs(actred) <= *ftol && prered <= *ftol && p5 * ratio <= one) {
  646. *info = 1;
  647.     }
  648.     if (delta <= *xtol * xnorm) {
  649. *info = 2;
  650.     }
  651.     if (abs(actred) <= *ftol && prered <= *ftol && p5 * ratio <= one && *info 
  652.     == 2) {
  653. *info = 3;
  654.     }
  655.     if (*info != 0) {
  656. goto L300;
  657.     }
  658. /*           tests for termination and stringent tolerances. */
  659.     if (*nfev >= *maxfev) {
  660. *info = 5;
  661.     }
  662.     if (abs(actred) <= epsmch && prered <= epsmch && p5 * ratio <= one) {
  663. *info = 6;
  664.     }
  665.     if (delta <= epsmch * xnorm) {
  666. *info = 7;
  667.     }
  668.     if (gnorm <= epsmch) {
  669. *info = 8;
  670.     }
  671.     if (*info != 0) {
  672. goto L300;
  673.     }
  674. /*           end of the inner loop. repeat if iteration unsuccessful. */
  675.     if (ratio < p0001) {
  676. goto L200;
  677.     }
  678. /*        end of the outer loop. */
  679.     goto L30;
  680. L300:
  681. /*     termination, either normal or user imposed. */
  682.     if (iflag < 0) {
  683. *info = iflag;
  684.     }
  685.     iflag = 0;
  686.     if (*nprint > 0) {
  687. //(*fcn)(m, n, &x[1], &fvec[1], &iflag);
  688. (*fcn)(m, n, &x[1], &fvec[1]);
  689.     }
  690.     return 0;
  691. int lmpar_(long *n,double *r,long *ldr,long *ipvt,double *diag,double *qtb,
  692.    double *delta,double * par,double *x,double *sdiag,double *wa1,
  693.    double *wa2)
  694. {
  695.     static double p1 = .1;
  696.     static double p001 = .001;
  697.     static double zero = 0.;
  698.     /* System generated locals */
  699.     long r_dim1, r_offset, i__1, i__2;
  700.     double d__1, d__2;
  701.     /* Builtin functions */
  702.     /* Local variables */
  703.     static double parc, parl;
  704.     static long iter;
  705.     static double temp, paru;
  706.     static long i, j, k, l;
  707.     static double dwarf;
  708.     static long nsing;
  709.     static double gnorm, fp;
  710.     static double dxnorm;
  711.     static long jm1, jp1;
  712.     static double sum;
  713.     /* Parameter adjustments */
  714.     --wa2;
  715.     --wa1;
  716.     --sdiag;
  717.     --x;
  718.     --qtb;
  719.     --diag;
  720.     --ipvt;
  721.     r_dim1 = *ldr;
  722.     r_offset = r_dim1 + 1;
  723.     r -= r_offset;
  724.     /* Function Body */
  725. /*     dwarf is the smallest positive magnitude. */
  726.     dwarf = dpmpar_(&c__3);
  727. /*     compute and store in x the gauss-newton direction. if the */
  728. /*     jacobian is rank-deficient, obtain a least squares solution. */
  729.     nsing = *n;
  730.     i__1 = *n;
  731.     for (j = 1; j <= i__1; ++j) {
  732. wa1[j] = qtb[j];
  733. if (r[j + j * r_dim1] == zero && nsing == *n) {
  734.     nsing = j - 1;
  735. }
  736. if (nsing < *n) {
  737.     wa1[j] = zero;
  738. }
  739. /* L10: */
  740.     }
  741.     if (nsing < 1) {
  742. goto L50;
  743.     }
  744.     i__1 = nsing;
  745.     for (k = 1; k <= i__1; ++k) {
  746. j = nsing - k + 1;
  747. wa1[j] /= r[j + j * r_dim1];
  748. temp = wa1[j];
  749. jm1 = j - 1;
  750. if (jm1 < 1) {
  751.     goto L30;
  752. }
  753. i__2 = jm1;
  754. for (i = 1; i <= i__2; ++i) {
  755.     wa1[i] -= r[i + j * r_dim1] * temp;
  756. /* L20: */
  757. }
  758. L30:
  759. /* L40: */
  760. ;
  761.     }
  762. L50:
  763.     i__1 = *n;
  764.     for (j = 1; j <= i__1; ++j) {
  765. l = ipvt[j];
  766. x[l] = wa1[j];
  767. /* L60: */
  768.     }
  769. /*     initialize the iteration counter. */
  770. /*     evaluate the function at the origin, and test */
  771. /*     for acceptance of the gauss-newton direction. */
  772.     iter = 0;
  773.     i__1 = *n;
  774.     for (j = 1; j <= i__1; ++j) {
  775. wa2[j] = diag[j] * x[j];
  776. /* L70: */
  777.     }
  778.     dxnorm = enorm_(n, &wa2[1]);
  779.     fp = dxnorm - *delta;
  780.     if (fp <= p1 * *delta) {
  781. goto L220;
  782.     }
  783. /*     if the jacobian is not rank deficient, the newton */
  784. /*     step provides a lower bound, parl, for the zero of */
  785. /*     the function. otherwise set this bound to zero. */
  786.     parl = zero;
  787.     if (nsing < *n) {
  788. goto L120;
  789.     }
  790.     i__1 = *n;
  791.     for (j = 1; j <= i__1; ++j) {
  792. l = ipvt[j];
  793. wa1[j] = diag[l] * (wa2[l] / dxnorm);
  794. /* L80: */
  795.     }
  796.     i__1 = *n;
  797.     for (j = 1; j <= i__1; ++j) {
  798. sum = zero;
  799. jm1 = j - 1;
  800. if (jm1 < 1) {
  801.     goto L100;
  802. }
  803. i__2 = jm1;
  804. for (i = 1; i <= i__2; ++i) {
  805.     sum += r[i + j * r_dim1] * wa1[i];
  806. /* L90: */
  807. }
  808. L100:
  809. wa1[j] = (wa1[j] - sum) / r[j + j * r_dim1];
  810. /* L110: */
  811.     }
  812.     temp = enorm_(n, &wa1[1]);
  813.     parl = fp / *delta / temp / temp;
  814. L120:
  815. /*     calculate an upper bound, paru, for the zero of the function. */
  816.     i__1 = *n;
  817.     for (j = 1; j <= i__1; ++j) {
  818. sum = zero;
  819. i__2 = j;
  820. for (i = 1; i <= i__2; ++i) {
  821.     sum += r[i + j * r_dim1] * qtb[i];
  822. /* L130: */
  823. }
  824. l = ipvt[j];
  825. wa1[j] = sum / diag[l];
  826. /* L140: */
  827.     }
  828.     gnorm = enorm_(n, &wa1[1]);
  829.     paru = gnorm / *delta;
  830.     if (paru == zero) {
  831. paru = dwarf / min(*delta,p1);
  832.     }
  833. /*     if the input par lies outside of the interval (parl,paru), */
  834. /*     set par to the closer endpoint. */
  835.     *par = max(*par,parl);
  836.     *par = min(*par,paru);
  837.     if (*par == zero) {
  838. *par = gnorm / dxnorm;
  839.     }
  840. /*     beginning of an iteration. */
  841. L150:
  842.     ++iter;
  843. /*        evaluate the function at the current value of par. */
  844.     if (*par == zero) {
  845. /* Computing MAX */
  846. d__1 = dwarf, d__2 = p001 * paru;
  847. *par = max(d__1,d__2);
  848.     }
  849.     temp = sqrt(*par);
  850.     i__1 = *n;
  851.     for (j = 1; j <= i__1; ++j) {
  852. wa1[j] = temp * diag[j];
  853. /* L160: */
  854.     }
  855.     qrsolv_(n, &r[r_offset], ldr, &ipvt[1], &wa1[1], &qtb[1], &x[1], &sdiag[1]
  856.     , &wa2[1]);
  857.     i__1 = *n;
  858.     for (j = 1; j <= i__1; ++j) {
  859. wa2[j] = diag[j] * x[j];
  860. /* L170: */
  861.     }
  862.     dxnorm = enorm_(n, &wa2[1]);
  863.     temp = fp;
  864.     fp = dxnorm - *delta;
  865. /*        if the function is small enough, accept the current value */
  866. /*        of par. also test for the exceptional cases where parl */
  867. /*        is zero or the number of iterations has reached 10. */
  868.     if (abs(fp) <= p1 * *delta || parl == zero && fp <= temp && temp < zero ||
  869.      iter == 10) {
  870. goto L220;
  871.     }
  872. /*        compute the newton correction. */
  873.     i__1 = *n;
  874.     for (j = 1; j <= i__1; ++j) {
  875. l = ipvt[j];
  876. wa1[j] = diag[l] * (wa2[l] / dxnorm);
  877. /* L180: */
  878.     }
  879.     i__1 = *n;
  880.     for (j = 1; j <= i__1; ++j) {
  881. wa1[j] /= sdiag[j];
  882. temp = wa1[j];
  883. jp1 = j + 1;
  884. if (*n < jp1) {
  885.     goto L200;
  886. }
  887. i__2 = *n;
  888. for (i = jp1; i <= i__2; ++i) {
  889.     wa1[i] -= r[i + j * r_dim1] * temp;
  890. /* L190: */
  891. }
  892. L200:
  893. /* L210: */
  894. ;
  895.     }
  896.     temp = enorm_(n, &wa1[1]);
  897.     parc = fp / *delta / temp / temp;
  898. /*        depending on the sign of the function, update parl or paru. */
  899.     if (fp > zero) {
  900. parl = max(parl,*par);
  901.     }
  902.     if (fp < zero) {
  903. paru = min(paru,*par);
  904.     }
  905. /*        compute an improved estimate for par. */
  906. /* Computing MAX */
  907.     d__1 = parl, d__2 = *par + parc;
  908.     *par = max(d__1,d__2);
  909. /*        end of an iteration. */
  910.     goto L150;
  911. L220:
  912. /*     termination. */
  913.     if (iter == 0) {
  914. *par = zero;
  915.     }
  916.     return 0;
  917. int qrfac_(long *m, long *n, double *a, long *lda, long *pivot, long *ipvt, long *lipvt, 
  918.    double *rdiag, double *acnorm, double *wa)
  919. {
  920.     static double one = 1.;
  921.     static double p05 = .05;
  922.     static double zero = 0.;
  923.     /* System generated locals */
  924.     long a_dim1, a_offset, i__1, i__2, i__3;
  925.     double d__1, d__2, d__3;
  926.     /* Builtin functions */
  927.     /* Local variables */
  928.     static long kmax;
  929.     static double temp;
  930.     static long i, j, k, minmn;
  931.     static double epsmch;
  932.     static double ajnorm;
  933.     static long jp1;
  934.     static double sum;
  935.     /* Parameter adjustments */
  936.     --wa;
  937.     --acnorm;
  938.     --rdiag;
  939.     --ipvt;
  940.     a_dim1 = *lda;
  941.     a_offset = a_dim1 + 1;
  942.     a -= a_offset;
  943.     /* Function Body */
  944. /*     epsmch is the machine precision. */
  945.     epsmch = dpmpar_(&c__4);
  946. /*     compute the initial column norms and initialize several arrays. */
  947.     i__1 = *n;
  948.     for (j = 1; j <= i__1; ++j) {
  949. acnorm[j] = enorm_(m, &a[j * a_dim1 + 1]);
  950. rdiag[j] = acnorm[j];
  951. wa[j] = rdiag[j];
  952. if (*pivot) {
  953.     ipvt[j] = j;
  954. }
  955. /* L10: */
  956.     }
  957. /*     reduce a to r with householder transformations. */
  958.     minmn = min(*m,*n);
  959.     i__1 = minmn;
  960.     for (j = 1; j <= i__1; ++j) {
  961. if (! (*pivot)) {
  962.     goto L40;
  963. }
  964. /*        bring the column of largest norm into the pivot position. */
  965. kmax = j;
  966. i__2 = *n;
  967. for (k = j; k <= i__2; ++k) {
  968.     if (rdiag[k] > rdiag[kmax]) {
  969. kmax = k;
  970.     }
  971. /* L20: */
  972. }
  973. if (kmax == j) {
  974.     goto L40;
  975. }
  976. i__2 = *m;
  977. for (i = 1; i <= i__2; ++i) {
  978.     temp = a[i + j * a_dim1];
  979.     a[i + j * a_dim1] = a[i + kmax * a_dim1];
  980.     a[i + kmax * a_dim1] = temp;
  981. /* L30: */
  982. }
  983. rdiag[kmax] = rdiag[j];
  984. wa[kmax] = wa[j];
  985. k = ipvt[j];
  986. ipvt[j] = ipvt[kmax];
  987. ipvt[kmax] = k;
  988. L40:
  989. /*        compute the householder transformation to reduce the */
  990. /*        j-th column of a to a multiple of the j-th unit vector. */
  991. i__2 = *m - j + 1;
  992. ajnorm = enorm_(&i__2, &a[j + j * a_dim1]);
  993. if (ajnorm == zero) {
  994.     goto L100;
  995. }
  996. if (a[j + j * a_dim1] < zero) {
  997.     ajnorm = -ajnorm;
  998. }
  999. i__2 = *m;
  1000. for (i = j; i <= i__2; ++i) {
  1001.     a[i + j * a_dim1] /= ajnorm;
  1002. /* L50: */
  1003. }
  1004. a[j + j * a_dim1] += one;
  1005. /*        apply the transformation to the remaining columns */
  1006. /*        and update the norms. */
  1007. jp1 = j + 1;
  1008. if (*n < jp1) {
  1009.     goto L100;
  1010. }
  1011. i__2 = *n;
  1012. for (k = jp1; k <= i__2; ++k) {
  1013.     sum = zero;
  1014.     i__3 = *m;
  1015.     for (i = j; i <= i__3; ++i) {
  1016. sum += a[i + j * a_dim1] * a[i + k * a_dim1];
  1017. /* L60: */
  1018.     }
  1019.     temp = sum / a[j + j * a_dim1];
  1020.     i__3 = *m;
  1021.     for (i = j; i <= i__3; ++i) {
  1022. a[i + k * a_dim1] -= temp * a[i + j * a_dim1];
  1023. /* L70: */
  1024.     }
  1025.     if (! (*pivot) || rdiag[k] == zero) {
  1026. goto L80;
  1027.     }
  1028.     temp = a[j + k * a_dim1] / rdiag[k];
  1029. /* Computing MAX */
  1030. /* Computing 2nd power */
  1031.     d__3 = temp;
  1032.     d__1 = zero, d__2 = one - d__3 * d__3;
  1033.     rdiag[k] *= sqrt((max(d__1,d__2)));
  1034. /* Computing 2nd power */
  1035.     d__1 = rdiag[k] / wa[k];
  1036.     if (p05 * (d__1 * d__1) > epsmch) {
  1037. goto L80;
  1038.     }
  1039.     i__3 = *m - j;
  1040.     rdiag[k] = enorm_(&i__3, &a[jp1 + k * a_dim1]);
  1041.     wa[k] = rdiag[k];
  1042. L80:
  1043. /* L90: */
  1044.     ;
  1045. }
  1046. L100:
  1047. rdiag[j] = -ajnorm;
  1048. /* L110: */
  1049.     }
  1050.     return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值