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))
20 (let ((p1 0.1) (p001 0.001) (zero 0.0))
21 (declare (type (double-float) p1 p001 zero
))
22 (defun lmpar (n r ldr ipvt diag qtb delta par x sdiag wa1 wa2
)
23 (declare (type (double-float) par delta
)
24 (type (array f2cl-lib
:integer4
(*)) ipvt
)
25 (type (array double-float
(*)) wa2 wa1 sdiag x qtb diag r
)
26 (type (f2cl-lib:integer4
) ldr n
))
27 (f2cl-lib:with-multi-array-data
28 ((r double-float r-%data% r-%offset%
)
29 (diag double-float diag-%data% diag-%offset%
)
30 (qtb double-float qtb-%data% qtb-%offset%
)
31 (x double-float x-%data% x-%offset%
)
32 (sdiag double-float sdiag-%data% sdiag-%offset%
)
33 (wa1 double-float wa1-%data% wa1-%offset%
)
34 (wa2 double-float wa2-%data% wa2-%offset%
)
35 (ipvt f2cl-lib
:integer4 ipvt-%data% ipvt-%offset%
))
36 (prog ((dxnorm 0.0) (dwarf 0.0) (fp 0.0) (gnorm 0.0) (parc 0.0)
37 (parl 0.0) (paru 0.0) (sum 0.0) (temp 0.0) (i 0) (iter 0) (j 0)
38 (jm1 0) (jp1 0) (k 0) (l 0) (nsing 0))
39 (declare (type (f2cl-lib:integer4
) nsing l k jp1 jm1 j iter i
)
40 (type (double-float) temp sum paru parl parc gnorm fp dwarf
46 '" given an m by n matrix a, an n by n nonsingular diagonal"
47 '" matrix d, an m-vector b, and a positive number delta,"
48 '" the problem is to determine a value for the parameter"
49 '" par such that if x solves the system"
51 '" a*x = b , sqrt(par)*d*x = 0 ,"
53 '" in the least squares sense, and dxnorm is the euclidean"
54 '" norm of d*x, then either par is zero and"
56 '" (dxnorm-delta) .le. 0.1*delta ,"
58 '" or par is positive and"
60 '" abs(dxnorm-delta) .le. 0.1*delta ."
62 '" this subroutine completes the solution of the problem"
63 '" if it is provided with the necessary information from the"
64 '" qr factorization, with column pivoting, of a. that is, if"
65 '" a*p = q*r, where p is a permutation matrix, q has orthogonal"
66 '" columns, and r is an upper triangular matrix with diagonal"
67 '" elements of nonincreasing magnitude, then lmpar expects"
68 '" the full upper triangle of r, the permutation matrix p,"
69 '" and the first n components of (q transpose)*b. on output"
70 '" lmpar also provides an upper triangular matrix s such that"
73 '" p *(a *a + par*d*d)*p = s *s ."
75 '" s is employed within lmpar and may be of separate interest."
77 '" only a few iterations are generally needed for convergence"
78 '" of the algorithm. if, however, the limit of 10 iterations"
79 '" is reached, then the output par will contain the best"
80 '" value obtained so far."
82 '" the subroutine statement is"
84 '" subroutine lmpar(n,r,ldr,ipvt,diag,qtb,delta,par,x,sdiag,"
89 '" n is a positive integer input variable set to the order of r."
91 '" r is an n by n array. on input the full upper triangle"
92 '" must contain the full upper triangle of the matrix r."
93 '" on output the full upper triangle is unaltered, and the"
94 '" strict lower triangle contains the strict upper triangle"
95 '" (transposed) of the upper triangular matrix s."
97 '" ldr is a positive integer input variable not less than n"
98 '" which specifies the leading dimension of the array r."
100 '" ipvt is an integer input array of length n which defines the"
101 '" permutation matrix p such that a*p = q*r. column j of p"
102 '" is column ipvt(j) of the identity matrix."
104 '" diag is an input array of length n which must contain the"
105 '" diagonal elements of the matrix d."
107 '" qtb is an input array of length n which must contain the first"
108 '" n elements of the vector (q transpose)*b."
110 '" delta is a positive input variable which specifies an upper"
111 '" bound on the euclidean norm of d*x."
113 '" par is a nonnegative variable. on input par contains an"
114 '" initial estimate of the levenberg-marquardt parameter."
115 '" on output par contains the final estimate."
117 '" x is an output array of length n which contains the least"
118 '" squares solution of the system a*x = b, sqrt(par)*d*x = 0,"
119 '" for the output par."
121 '" sdiag is an output array of length n which contains the"
122 '" diagonal elements of the upper triangular matrix s."
124 '" wa1 and wa2 are work arrays of length n."
126 '" subprograms called"
128 '" minpack-supplied ... dpmpar,enorm,qrsolv"
130 '" fortran-supplied ... dabs,dmax1,dmin1,dsqrt"
132 '" argonne national laboratory. minpack project. march 1980."
133 '" burton s. garbow, kenneth e. hillstrom, jorge j. more"
137 '" dwarf is the smallest positive magnitude."
139 (setf dwarf
(dpmpar 2))
141 '" compute and store in x the gauss-newton direction. if the"
142 '" jacobian is rank-deficient, obtain a least squares solution."
145 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
148 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
149 (f2cl-lib:fref qtb-%data%
(j) ((1 n
)) qtb-%offset%
))
152 (= (f2cl-lib:fref r-%data%
(j j
) ((1 ldr
) (1 n
)) r-%offset%
)
155 (setf nsing
(f2cl-lib:int-sub j
1)))
157 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
160 (if (< nsing
1) (go label50
))
161 (f2cl-lib:fdo
(k 1 (f2cl-lib:int-add k
1))
164 (setf j
(f2cl-lib:int-add
(f2cl-lib:int-sub nsing k
) 1))
165 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
166 (/ (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
167 (f2cl-lib:fref r-%data%
171 (setf temp
(f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
))
172 (setf jm1
(f2cl-lib:int-sub j
1))
173 (if (< jm1
1) (go label30
))
174 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
177 (setf (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
178 (- (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
180 (f2cl-lib:fref r-%data%
189 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
192 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
193 (setf (f2cl-lib:fref x-%data%
(l) ((1 n
)) x-%offset%
)
194 (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
))
197 '" initialize the iteration counter."
198 '" evaluate the function at the origin, and test"
199 '" for acceptance of the gauss-newton direction."
202 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
205 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)
206 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
207 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)))
209 (setf dxnorm
(enorm n wa2
))
210 (setf fp
(- dxnorm delta
))
211 (if (<= fp
(* p1 delta
)) (go label220
))
213 '" if the jacobian is not rank deficient, the newton"
214 '" step provides a lower bound, parl, for the zero of"
215 '" the function. otherwise set this bound to zero."
218 (if (< nsing n
) (go label120
))
219 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
222 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
223 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
224 (* (f2cl-lib:fref diag-%data%
(l) ((1 n
)) diag-%offset%
)
225 (/ (f2cl-lib:fref wa2-%data%
(l) ((1 n
)) wa2-%offset%
)
228 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
232 (setf jm1
(f2cl-lib:int-sub j
1))
233 (if (< jm1
1) (go label100
))
234 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
240 (f2cl-lib:fref r-%data%
244 (f2cl-lib:fref wa1-%data%
250 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
252 (- (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
254 (f2cl-lib:fref r-%data%
259 (setf temp
(enorm n wa1
))
260 (setf parl
(/ (/ (/ fp delta
) temp
) temp
))
263 '" calculate an upper bound, paru, for the zero of the function."
265 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
269 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
275 (f2cl-lib:fref r-%data%
279 (f2cl-lib:fref qtb-%data%
284 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
285 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
287 (f2cl-lib:fref diag-%data%
(l) ((1 n
)) diag-%offset%
)))
289 (setf gnorm
(enorm n wa1
))
290 (setf paru
(/ gnorm delta
))
291 (if (= paru zero
) (setf paru
(/ dwarf
(f2cl-lib:dmin1 delta p1
))))
293 '" if the input par lies outside of the interval (parl,paru),"
294 '" set par to the closer endpoint."
296 (setf par
(f2cl-lib:dmax1 par parl
))
297 (setf par
(f2cl-lib:dmin1 par paru
))
298 (if (= par zero
) (setf par
(/ gnorm dxnorm
)))
300 '" beginning of an iteration."
303 (setf iter
(f2cl-lib:int-add iter
1))
305 '" evaluate the function at the current value of par."
307 (if (= par zero
) (setf par
(f2cl-lib:dmax1 dwarf
(* p001 paru
))))
308 (setf temp
(f2cl-lib:dsqrt par
))
309 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
312 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
314 (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)))
316 (qrsolv n r ldr ipvt wa1 qtb x sdiag wa2
)
317 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
320 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)
321 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
322 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)))
324 (setf dxnorm
(enorm n wa2
))
326 (setf fp
(- dxnorm delta
))
328 '" if the function is small enough, accept the current value"
329 '" of par. also test for the exceptional cases where parl"
330 '" is zero or the number of iterations has reached 10."
333 (or (<= (f2cl-lib:dabs fp
) (* p1 delta
))
334 (and (= parl zero
) (<= fp temp
) (< temp zero
))
338 '" compute the newton correction."
340 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
343 (setf l
(f2cl-lib:fref ipvt-%data%
(j) ((1 n
)) ipvt-%offset%
))
344 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
345 (* (f2cl-lib:fref diag-%data%
(l) ((1 n
)) diag-%offset%
)
346 (/ (f2cl-lib:fref wa2-%data%
(l) ((1 n
)) wa2-%offset%
)
349 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
352 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
353 (/ (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
354 (f2cl-lib:fref sdiag-%data%
358 (setf temp
(f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
))
359 (setf jp1
(f2cl-lib:int-add j
1))
360 (if (< n jp1
) (go label200
))
361 (f2cl-lib:fdo
(i jp1
(f2cl-lib:int-add i
1))
364 (setf (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
365 (- (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
367 (f2cl-lib:fref r-%data%
375 (setf temp
(enorm n wa1
))
376 (setf parc
(/ (/ (/ fp delta
) temp
) temp
))
378 '" depending on the sign of the function, update parl or paru."
380 (if (> fp zero
) (setf parl
(f2cl-lib:dmax1 parl par
)))
381 (if (< fp zero
) (setf paru
(f2cl-lib:dmin1 paru par
)))
383 '" compute an improved estimate for par."
385 (setf par
(f2cl-lib:dmax1 parl
(+ par parc
)))
387 '" end of an iteration."
394 (if (= iter
0) (setf par zero
))
397 '" last card of subroutine lmpar."
400 (return (values nil nil nil nil nil nil nil par nil nil nil nil
))))))
402 (in-package #:cl-user
)
403 #+#.
(cl:if
(cl:find-package
'#:f2cl
) '(and) '(or))
404 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
405 (setf (gethash 'fortran-to-lisp
::lmpar fortran-to-lisp
::*f2cl-function-info
*)
406 (fortran-to-lisp::make-f2cl-finfo
407 :arg-types
'((fortran-to-lisp::integer4
) (array double-float
(*))
408 (fortran-to-lisp::integer4
)
409 (array fortran-to-lisp
::integer4
(*))
410 (array double-float
(*)) (array double-float
(*))
411 (double-float) (double-float) (array double-float
(*))
412 (array double-float
(*)) (array double-float
(*))
413 (array double-float
(*)))
414 :return-values
'(nil nil nil nil nil nil nil fortran-to-lisp
::par
416 :calls
'(fortran-to-lisp::qrsolv fortran-to-lisp
::enorm
417 fortran-to-lisp
::dpmpar
))))