1 ;;; Compiled by f2cl version:
2 ;;; ("f2cl1.l,v 1.215 2009/04/07 22:05:21 rtoy Exp $"
3 ;;; "f2cl2.l,v 1.37 2008/02/22 22:19:33 rtoy Exp $"
4 ;;; "f2cl3.l,v 1.6 2008/02/22 22:19:33 rtoy Exp $"
5 ;;; "f2cl4.l,v 1.7 2008/02/22 22:19:34 rtoy Exp $"
6 ;;; "f2cl5.l,v 1.200 2009/01/19 02:38:17 rtoy Exp $"
7 ;;; "f2cl6.l,v 1.48 2008/08/24 00:56:27 rtoy Exp $"
8 ;;; "macros.l,v 1.112 2009/01/08 12:57:19 rtoy Exp $")
10 ;;; Using Lisp CMU Common Lisp 19f (19F)
12 ;;; Options: ((:prune-labels nil) (:auto-save t) (:relaxed-array-decls nil)
13 ;;; (:coerce-assigns :as-needed) (:array-type ':array)
14 ;;; (:array-slicing t) (:declare-common nil)
15 ;;; (:float-format double-float))
27 (declare (type (double-float) one p1 p5 p25 p75 p0001 zero
))
29 (fcn m n x fvec ftol xtol gtol maxfev epsfcn diag mode factor nprint
30 info nfev fjac ldfjac ipvt qtf wa1 wa2 wa3 wa4
)
31 (declare (type (array f2cl-lib
:integer4
(*)) ipvt
)
32 (type (double-float) factor epsfcn gtol xtol ftol
)
33 (type (array double-float
(*)) wa4 wa3 wa2 wa1 qtf fjac diag fvec
35 (type (f2cl-lib:integer4
) ldfjac nfev info nprint mode maxfev n
37 (f2cl-lib:with-multi-array-data
38 ((x double-float x-%data% x-%offset%
)
39 (fvec double-float fvec-%data% fvec-%offset%
)
40 (diag double-float diag-%data% diag-%offset%
)
41 (fjac double-float fjac-%data% fjac-%offset%
)
42 (qtf double-float qtf-%data% qtf-%offset%
)
43 (wa1 double-float wa1-%data% wa1-%offset%
)
44 (wa2 double-float wa2-%data% wa2-%offset%
)
45 (wa3 double-float wa3-%data% wa3-%offset%
)
46 (wa4 double-float wa4-%data% wa4-%offset%
)
47 (ipvt f2cl-lib
:integer4 ipvt-%data% ipvt-%offset%
))
48 (prog ((actred 0.0) (delta 0.0) (dirder 0.0) (epsmch 0.0) (fnorm 0.0)
49 (fnorm1 0.0) (gnorm 0.0) (par 0.0) (pnorm 0.0) (prered 0.0)
50 (ratio 0.0) (sum 0.0) (temp 0.0) (temp1 0.0) (temp2 0.0)
51 (xnorm 0.0) (i 0) (iflag 0) (iter 0) (j 0) (l 0))
52 (declare (type (f2cl-lib:integer4
) l j iter iflag i
)
53 (type (double-float) xnorm temp2 temp1 temp sum ratio prered
54 pnorm par gnorm fnorm1 fnorm epsmch
60 '" the purpose of lmdif is to minimize the sum of the squares of"
61 '" m nonlinear functions in n variables by a modification of"
62 '" the levenberg-marquardt algorithm. the user must provide a"
63 '" subroutine which calculates the functions. the jacobian is"
64 '" then calculated by a forward-difference approximation."
66 '" the subroutine statement is"
68 '" subroutine lmdif(fcn,m,n,x,fvec,ftol,xtol,gtol,maxfev,epsfcn,"
69 '" diag,mode,factor,nprint,info,nfev,fjac,"
70 '" ldfjac,ipvt,qtf,wa1,wa2,wa3,wa4)"
74 '" fcn is the name of the user-supplied subroutine which"
75 '" calculates the functions. fcn must be declared"
76 '" in an external statement in the user calling"
77 '" program, and should be written as follows."
79 '" subroutine fcn(m,n,x,fvec,iflag)"
81 '" double precision x(n),fvec(m)"
83 '" calculate the functions at x and"
84 '" return this vector in fvec."
89 '" the value of iflag should not be changed by fcn unless"
90 '" the user wants to terminate execution of lmdif."
91 '" in this case set iflag to a negative integer."
93 '" m is a positive integer input variable set to the number"
96 '" n is a positive integer input variable set to the number"
97 '" of variables. n must not exceed m."
99 '" x is an array of length n. on input x must contain"
100 '" an initial estimate of the solution vector. on output x"
101 '" contains the final estimate of the solution vector."
103 '" fvec is an output array of length m which contains"
104 '" the functions evaluated at the output x."
106 '" ftol is a nonnegative input variable. termination"
107 '" occurs when both the actual and predicted relative"
108 '" reductions in the sum of squares are at most ftol."
109 '" therefore, ftol measures the relative error desired"
110 '" in the sum of squares."
112 '" xtol is a nonnegative input variable. termination"
113 '" occurs when the relative error between two consecutive"
114 '" iterates is at most xtol. therefore, xtol measures the"
115 '" relative error desired in the approximate solution."
117 '" gtol is a nonnegative input variable. termination"
118 '" occurs when the cosine of the angle between fvec and"
119 '" any column of the jacobian is at most gtol in absolute"
120 '" value. therefore, gtol measures the orthogonality"
121 '" desired between the function vector and the columns"
124 '" maxfev is a positive integer input variable. termination"
125 '" occurs when the number of calls to fcn is at least"
126 '" maxfev by the end of an iteration."
128 '" epsfcn is an input variable used in determining a suitable"
129 '" step length for the forward-difference approximation. this"
130 '" approximation assumes that the relative errors in the"
131 '" functions are of the order of epsfcn. if epsfcn is less"
132 '" than the machine precision, it is assumed that the relative"
133 '" errors in the functions are of the order of the machine"
136 '" diag is an array of length n. if mode = 1 (see"
137 '" below), diag is internally set. if mode = 2, diag"
138 '" must contain positive entries that serve as"
139 '" multiplicative scale factors for the variables."
141 '" mode is an integer input variable. if mode = 1, the"
142 '" variables will be scaled internally. if mode = 2,"
143 '" the scaling is specified by the input diag. other"
144 '" values of mode are equivalent to mode = 1."
146 '" factor is a positive input variable used in determining the"
147 '" initial step bound. this bound is set to the product of"
148 '" factor and the euclidean norm of diag*x if nonzero, or else"
149 '" to factor itself. in most cases factor should lie in the"
150 '" interval (.1,100.). 100. is a generally recommended value."
152 '" nprint is an integer input variable that enables controlled"
153 '" printing of iterates if it is positive. in this case,"
154 '" fcn is called with iflag = 0 at the beginning of the first"
155 '" iteration and every nprint iterations thereafter and"
156 '" immediately prior to return, with x and fvec available"
157 '" for printing. if nprint is not positive, no special calls"
158 '" of fcn with iflag = 0 are made."
160 '" info is an integer output variable. if the user has"
161 '" terminated execution, info is set to the (negative)"
162 '" value of iflag. see description of fcn. otherwise,"
163 '" info is set as follows."
165 '" info = 0 improper input parameters."
167 '" info = 1 both actual and predicted relative reductions"
168 '" in the sum of squares are at most ftol."
170 '" info = 2 relative error between two consecutive iterates"
173 '" info = 3 conditions for info = 1 and info = 2 both hold."
175 '" info = 4 the cosine of the angle between fvec and any"
176 '" column of the jacobian is at most gtol in"
179 '" info = 5 number of calls to fcn has reached or"
182 '" info = 6 ftol is too small. no further reduction in"
183 '" the sum of squares is possible."
185 '" info = 7 xtol is too small. no further improvement in"
186 '" the approximate solution x is possible."
188 '" info = 8 gtol is too small. fvec is orthogonal to the"
189 '" columns of the jacobian to machine precision."
191 '" nfev is an integer output variable set to the number of"
194 '" fjac is an output m by n array. the upper n by n submatrix"
195 '" of fjac contains an upper triangular matrix r with"
196 '" diagonal elements of nonincreasing magnitude such that"
199 '" p *(jac *jac)*p = r *r,"
201 '" where p is a permutation matrix and jac is the final"
202 '" calculated jacobian. column j of p is column ipvt(j)"
203 '" (see below) of the identity matrix. the lower trapezoidal"
204 '" part of fjac contains information generated during"
205 '" the computation of r."
207 '" ldfjac is a positive integer input variable not less than m"
208 '" which specifies the leading dimension of the array fjac."
210 '" ipvt is an integer output array of length n. ipvt"
211 '" defines a permutation matrix p such that jac*p = q*r,"
212 '" where jac is the final calculated jacobian, q is"
213 '" orthogonal (not stored), and r is upper triangular"
214 '" with diagonal elements of nonincreasing magnitude."
215 '" column j of p is column ipvt(j) of the identity matrix."
217 '" qtf is an output array of length n which contains"
218 '" the first n elements of the vector (q transpose)*fvec."
220 '" wa1, wa2, and wa3 are work arrays of length n."
222 '" wa4 is a work array of length m."
224 '" subprograms called"
226 '" user-supplied ...... fcn"
228 '" minpack-supplied ... dpmpar,enorm,fdjac2,lmpar,qrfac"
230 '" fortran-supplied ... dabs,dmax1,dmin1,dsqrt,mod"
232 '" argonne national laboratory. minpack project. march 1980."
233 '" burton s. garbow, kenneth e. hillstrom, jorge j. more"
237 '" epsmch is the machine precision."
239 (setf epsmch
(dpmpar 1))
245 '" check the input parameters for errors."
257 (if (/= mode
2) (go label20
))
258 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
261 (if (<= (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
) zero
)
266 '" evaluate the function at the starting point"
267 '" and calculate its norm."
270 (multiple-value-bind (var-0 var-1 var-2 var-3 var-4
)
271 (funcall fcn m n x fvec iflag
)
272 (declare (ignore var-2 var-3
))
280 (if (< iflag
0) (go label300
))
281 (setf fnorm
(enorm m fvec
))
283 '" initialize levenberg-marquardt parameter and iteration counter."
288 '" beginning of the outer loop."
292 '" calculate the jacobian matrix."
296 (var-0 var-1 var-2 var-3 var-4 var-5 var-6 var-7 var-8 var-9
)
297 (fdjac2 fcn m n x fvec fjac ldfjac iflag epsfcn wa4
)
298 (declare (ignore var-0 var-3 var-4 var-5 var-6 var-8 var-9
))
302 (setf nfev
(f2cl-lib:int-add nfev n
))
303 (if (< iflag
0) (go label300
))
305 '" if requested, call fcn to enable printing of iterates."
307 (if (<= nprint
0) (go label40
))
309 (if (= (mod (f2cl-lib:int-sub iter
1) nprint
) 0)
310 (multiple-value-bind (var-0 var-1 var-2 var-3 var-4
)
311 (funcall fcn m n x fvec iflag
)
312 (declare (ignore var-2 var-3
))
318 (setf iflag var-4
))))
319 (if (< iflag
0) (go label300
))
322 '" compute the qr factorization of the jacobian."
324 (qrfac m n fjac ldfjac f2cl-lib
:%true% ipvt n wa1 wa2 wa3
)
326 '" on the first iteration and if mode is 1, scale according"
327 '" to the norms of the columns of the initial jacobian."
329 (if (/= iter
1) (go label80
))
330 (if (= mode
2) (go label60
))
331 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
334 (setf (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
335 (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
))
336 (if (= (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
) zero
)
337 (setf (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
342 '" on the first iteration, calculate the norm of the scaled x"
343 '" and initialize the step bound delta."
345 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
348 (setf (f2cl-lib:fref wa3-%data%
(j) ((1 n
)) wa3-%offset%
)
349 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
350 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)))
352 (setf xnorm
(enorm n wa3
))
353 (setf delta
(* factor xnorm
))
354 (if (= delta zero
) (setf delta factor
))
357 '" form (q transpose)*fvec and store the first n components in"
360 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
363 (setf (f2cl-lib:fref wa4-%data%
(i) ((1 m
)) wa4-%offset%
)
364 (f2cl-lib:fref fvec-%data%
(i) ((1 m
)) fvec-%offset%
))
366 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
371 (f2cl-lib:fref fjac-%data%
378 (f2cl-lib:fdo
(i j
(f2cl-lib:int-add i
1))
384 (f2cl-lib:fref fjac-%data%
388 (f2cl-lib:fref wa4-%data%
395 (f2cl-lib:fref fjac-%data%
399 (f2cl-lib:fdo
(i j
(f2cl-lib:int-add i
1))
402 (setf (f2cl-lib:fref wa4-%data%
(i) ((1 m
)) wa4-%offset%
)
403 (+ (f2cl-lib:fref wa4-%data%
(i) ((1 m
)) wa4-%offset%
)
405 (f2cl-lib:fref fjac-%data%
412 (setf (f2cl-lib:fref fjac-%data%
416 (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
))
417 (setf (f2cl-lib:fref qtf-%data%
(j) ((1 n
)) qtf-%offset%
)
418 (f2cl-lib:fref wa4-%data%
(j) ((1 m
)) wa4-%offset%
))
421 '" compute the norm of the scaled gradient."
424 (if (= fnorm zero
) (go label170
))
425 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
428 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
429 (if (= (f2cl-lib:fref wa2-%data%
(l) ((1 n
)) wa2-%offset%
) zero
)
432 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
438 (f2cl-lib:fref fjac-%data%
443 (f2cl-lib:fref qtf-%data%
450 (f2cl-lib:dmax1 gnorm
453 (f2cl-lib:fref wa2-%data%
461 '" test for convergence of the gradient norm."
463 (if (<= gnorm gtol
) (setf info
4))
464 (if (/= info
0) (go label300
))
466 '" rescale if necessary."
468 (if (= mode
2) (go label190
))
469 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
472 (setf (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
474 (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
475 (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)))
479 '" beginning of the inner loop."
483 '" determine the levenberg-marquardt parameter."
486 (var-0 var-1 var-2 var-3 var-4 var-5 var-6 var-7 var-8 var-9
488 (lmpar n fjac ldfjac ipvt diag qtf delta par wa1 wa2 wa3 wa4
)
489 (declare (ignore var-0 var-1 var-2 var-3 var-4 var-5 var-6 var-8
490 var-9 var-10 var-11
))
493 '" store the direction p and x + p. calculate the norm of p."
495 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
498 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
499 (- (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)))
500 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)
501 (+ (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)
502 (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)))
503 (setf (f2cl-lib:fref wa3-%data%
(j) ((1 n
)) wa3-%offset%
)
504 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
505 (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)))
507 (setf pnorm
(enorm n wa3
))
509 '" on the first iteration, adjust the initial step bound."
511 (if (= iter
1) (setf delta
(f2cl-lib:dmin1 delta pnorm
)))
513 '" evaluate the function at x + p and calculate its norm."
516 (multiple-value-bind (var-0 var-1 var-2 var-3 var-4
)
517 (funcall fcn m n wa2 wa4 iflag
)
518 (declare (ignore var-2 var-3
))
525 (setf nfev
(f2cl-lib:int-add nfev
1))
526 (if (< iflag
0) (go label300
))
527 (setf fnorm1
(enorm m wa4
))
529 '" compute the scaled actual reduction."
531 (setf actred
(- one
))
532 (if (< (* p1 fnorm1
) fnorm
)
533 (setf actred
(- one
(expt (/ fnorm1 fnorm
) 2))))
535 '" compute the scaled predicted reduction and"
536 '" the scaled directional derivative."
538 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
541 (setf (f2cl-lib:fref wa3-%data%
(j) ((1 n
)) wa3-%offset%
) zero
)
542 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
543 (setf temp
(f2cl-lib:fref wa1-%data%
(l) ((1 n
)) wa1-%offset%
))
544 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
547 (setf (f2cl-lib:fref wa3-%data%
(i) ((1 n
)) wa3-%offset%
)
548 (+ (f2cl-lib:fref wa3-%data%
(i) ((1 n
)) wa3-%offset%
)
550 (f2cl-lib:fref fjac-%data%
557 (setf temp1
(/ (enorm n wa3
) fnorm
))
558 (setf temp2
(/ (* (f2cl-lib:dsqrt par
) pnorm
) fnorm
))
559 (setf prered
(+ (expt temp1
2) (/ (expt temp2
2) p5
)))
560 (setf dirder
(- (+ (expt temp1
2) (expt temp2
2))))
562 '" compute the ratio of the actual to the predicted"
566 (if (/= prered zero
) (setf ratio
(/ actred prered
)))
568 '" update the step bound."
570 (if (> ratio p25
) (go label240
))
571 (if (>= actred zero
) (setf temp p5
))
573 (setf temp
(/ (* p5 dirder
) (+ dirder
(* p5 actred
)))))
574 (if (or (>= (* p1 fnorm1
) fnorm
) (< temp p1
)) (setf temp p1
))
575 (setf delta
(* temp
(f2cl-lib:dmin1 delta
(/ pnorm p1
))))
576 (setf par
(/ par temp
))
579 (if (and (/= par zero
) (< ratio p75
)) (go label250
))
580 (setf delta
(/ pnorm p5
))
581 (setf par
(* p5 par
))
585 '" test for successful iteration."
587 (if (< ratio p0001
) (go label290
))
589 '" successful iteration. update x, fvec, and their norms."
591 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
594 (setf (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)
595 (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
))
596 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)
597 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
598 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)))
600 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
603 (setf (f2cl-lib:fref fvec-%data%
(i) ((1 m
)) fvec-%offset%
)
604 (f2cl-lib:fref wa4-%data%
(i) ((1 m
)) wa4-%offset%
))
606 (setf xnorm
(enorm n wa2
))
608 (setf iter
(f2cl-lib:int-add iter
1))
611 '" tests for convergence."
614 (and (<= (f2cl-lib:dabs actred
) ftol
)
616 (<= (* p5 ratio
) one
))
618 (if (<= delta
(* xtol xnorm
)) (setf info
2))
620 (and (<= (f2cl-lib:dabs actred
) ftol
)
622 (<= (* p5 ratio
) one
)
625 (if (/= info
0) (go label300
))
627 '" tests for termination and stringent tolerances."
629 (if (>= nfev maxfev
) (setf info
5))
631 (and (<= (f2cl-lib:dabs actred
) epsmch
)
633 (<= (* p5 ratio
) one
))
635 (if (<= delta
(* epsmch xnorm
)) (setf info
7))
636 (if (<= gnorm epsmch
) (setf info
8))
637 (if (/= info
0) (go label300
))
639 '" end of the inner loop. repeat if iteration unsuccessful."
641 (if (< ratio p0001
) (go label200
))
643 '" end of the outer loop."
648 '" termination, either normal or user imposed."
650 (if (< iflag
0) (setf info iflag
))
653 (multiple-value-bind (var-0 var-1 var-2 var-3 var-4
)
654 (funcall fcn m n x fvec iflag
)
655 (declare (ignore var-2 var-3
))
661 (setf iflag var-4
))))
664 '" last card of subroutine lmdif."
693 (in-package #:cl-user
)
694 #+#.
(cl:if
(cl:find-package
'#:f2cl
) '(and) '(or))
695 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
696 (setf (gethash 'fortran-to-lisp
::lmdif fortran-to-lisp
::*f2cl-function-info
*)
697 (fortran-to-lisp::make-f2cl-finfo
698 :arg-types
'(t (fortran-to-lisp::integer4
)
699 (fortran-to-lisp::integer4
) (array double-float
(*))
700 (array double-float
(*)) (double-float) (double-float)
701 (double-float) (fortran-to-lisp::integer4
)
702 (double-float) (array double-float
(*))
703 (fortran-to-lisp::integer4
) (double-float)
704 (fortran-to-lisp::integer4
) (fortran-to-lisp::integer4
)
705 (fortran-to-lisp::integer4
) (array double-float
(*))
706 (fortran-to-lisp::integer4
)
707 (array fortran-to-lisp
::integer4
(*))
708 (array double-float
(*)) (array double-float
(*))
709 (array double-float
(*)) (array double-float
(*))
710 (array double-float
(*)))
711 :return-values
'(nil fortran-to-lisp
::m fortran-to-lisp
::n nil nil
712 nil nil nil nil nil nil nil nil nil
713 fortran-to-lisp
::info fortran-to-lisp
::nfev nil nil
714 nil nil nil nil nil nil
)
715 :calls
'(fortran-to-lisp::lmpar fortran-to-lisp
::qrfac
716 fortran-to-lisp
::fdjac2 fortran-to-lisp
::enorm
717 fortran-to-lisp
::dpmpar
))))