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 ((one 1.0) (zero 0.0))
21 (declare (type (double-float) one zero
))
22 (defun dogleg (n r lr diag qtb delta x wa1 wa2
)
23 (declare (type (double-float) delta
)
24 (type (array double-float
(*)) wa2 wa1 x qtb diag r
)
25 (type (f2cl-lib:integer4
) lr n
))
26 (f2cl-lib:with-multi-array-data
27 ((r double-float r-%data% r-%offset%
)
28 (diag double-float diag-%data% diag-%offset%
)
29 (qtb double-float qtb-%data% qtb-%offset%
)
30 (x double-float x-%data% x-%offset%
)
31 (wa1 double-float wa1-%data% wa1-%offset%
)
32 (wa2 double-float wa2-%data% wa2-%offset%
))
33 (prog ((alpha 0.0) (bnorm 0.0) (epsmch 0.0) (gnorm 0.0) (qnorm 0.0)
34 (sgnorm 0.0) (sum 0.0) (temp 0.0) (i 0) (j 0) (jj 0) (jp1 0) (k 0)
36 (declare (type (f2cl-lib:integer4
) l k jp1 jj j i
)
37 (type (double-float) temp sum sgnorm qnorm gnorm epsmch bnorm
43 '" given an m by n matrix a, an n by n nonsingular diagonal"
44 '" matrix d, an m-vector b, and a positive number delta, the"
45 '" problem is to determine the convex combination x of the"
46 '" gauss-newton and scaled gradient directions that minimizes"
47 '" (a*x - b) in the least squares sense, subject to the"
48 '" restriction that the euclidean norm of d*x be at most delta."
50 '" this subroutine completes the solution of the problem"
51 '" if it is provided with the necessary information from the"
52 '" qr factorization of a. that is, if a = q*r, where q has"
53 '" orthogonal columns and r is an upper triangular matrix,"
54 '" then dogleg expects the full upper triangle of r and"
55 '" the first n components of (q transpose)*b."
57 '" the subroutine statement is"
59 '" subroutine dogleg(n,r,lr,diag,qtb,delta,x,wa1,wa2)"
63 '" n is a positive integer input variable set to the order of r."
65 '" r is an input array of length lr which must contain the upper"
66 '" triangular matrix r stored by rows."
68 '" lr is a positive integer input variable not less than"
71 '" diag is an input array of length n which must contain the"
72 '" diagonal elements of the matrix d."
74 '" qtb is an input array of length n which must contain the first"
75 '" n elements of the vector (q transpose)*b."
77 '" delta is a positive input variable which specifies an upper"
78 '" bound on the euclidean norm of d*x."
80 '" x is an output array of length n which contains the desired"
81 '" convex combination of the gauss-newton direction and the"
82 '" scaled gradient direction."
84 '" wa1 and wa2 are work arrays of length n."
86 '" subprograms called"
88 '" minpack-supplied ... dpmpar,enorm"
90 '" fortran-supplied ... dabs,dmax1,dmin1,dsqrt"
92 '" argonne national laboratory. minpack project. march 1980."
93 '" burton s. garbow, kenneth e. hillstrom, jorge j. more"
97 '" epsmch is the machine precision."
99 (setf epsmch
(dpmpar 1))
101 '" first, calculate the gauss-newton direction."
103 (setf jj
(+ (the f2cl-lib
:integer4
(truncate (* n
(+ n
1)) 2)) 1))
104 (f2cl-lib:fdo
(k 1 (f2cl-lib:int-add k
1))
107 (setf j
(f2cl-lib:int-add
(f2cl-lib:int-sub n k
) 1))
108 (setf jp1
(f2cl-lib:int-add j
1))
109 (setf jj
(f2cl-lib:int-sub jj k
))
110 (setf l
(f2cl-lib:int-add jj
1))
112 (if (< n jp1
) (go label20
))
113 (f2cl-lib:fdo
(i jp1
(f2cl-lib:int-add i
1))
118 (* (f2cl-lib:fref r-%data%
(l) ((1 lr
)) r-%offset%
)
119 (f2cl-lib:fref x-%data%
123 (setf l
(f2cl-lib:int-add l
1))
126 (setf temp
(f2cl-lib:fref r-%data%
(jj) ((1 lr
)) r-%offset%
))
127 (if (/= temp zero
) (go label40
))
129 (f2cl-lib:fdo
(i 1 (f2cl-lib:int-add i
1))
135 (f2cl-lib:fref r-%data%
139 (setf l
(f2cl-lib:int-sub
(f2cl-lib:int-add l n
) i
))
141 (setf temp
(* epsmch temp
))
142 (if (= temp zero
) (setf temp epsmch
))
144 (setf (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)
146 (- (f2cl-lib:fref qtb-%data%
(j) ((1 n
)) qtb-%offset%
)
151 '" test whether the gauss-newton direction is acceptable."
153 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
156 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
) zero
)
157 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
)
158 (* (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)
159 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)))
161 (setf qnorm
(enorm n wa2
))
162 (if (<= qnorm delta
) (go label140
))
164 '" the gauss-newton direction is not acceptable."
165 '" next, calculate the scaled gradient direction."
168 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
171 (setf temp
(f2cl-lib:fref qtb-%data%
(j) ((1 n
)) qtb-%offset%
))
172 (f2cl-lib:fdo
(i j
(f2cl-lib:int-add i
1))
175 (setf (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
176 (+ (f2cl-lib:fref wa1-%data%
(i) ((1 n
)) wa1-%offset%
)
177 (* (f2cl-lib:fref r-%data%
(l) ((1 lr
)) r-%offset%
)
179 (setf l
(f2cl-lib:int-add l
1))
181 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
182 (/ (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
183 (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)))
186 '" calculate the norm of the scaled gradient and test for"
187 '" the special case in which the scaled gradient is zero."
189 (setf gnorm
(enorm n wa1
))
191 (setf alpha
(/ delta qnorm
))
192 (if (= gnorm zero
) (go label120
))
194 '" calculate the point along the scaled gradient"
195 '" at which the quadratic is minimized."
197 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
200 (setf (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
202 (/ (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
)
204 (f2cl-lib:fref diag-%data%
(j) ((1 n
)) diag-%offset%
)))
207 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
211 (f2cl-lib:fdo
(i j
(f2cl-lib:int-add i
1))
216 (* (f2cl-lib:fref r-%data%
(l) ((1 lr
)) r-%offset%
)
217 (f2cl-lib:fref wa1-%data%
221 (setf l
(f2cl-lib:int-add l
1))
223 (setf (f2cl-lib:fref wa2-%data%
(j) ((1 n
)) wa2-%offset%
) sum
)
225 (setf temp
(enorm n wa2
))
226 (setf sgnorm
(/ (/ gnorm temp
) temp
))
228 '" test whether the scaled gradient direction is acceptable."
231 (if (>= sgnorm delta
) (go label120
))
233 '" the scaled gradient direction is not acceptable."
234 '" finally, calculate the point along the dogleg"
235 '" at which the quadratic is minimized."
237 (setf bnorm
(enorm n qtb
))
238 (setf temp
(* (/ bnorm gnorm
) (/ bnorm qnorm
) (/ sgnorm delta
)))
240 (+ (- temp
(* (/ delta qnorm
) (expt (/ sgnorm delta
) 2)))
242 (+ (expt (- temp
(/ delta qnorm
)) 2)
243 (* (- one
(expt (/ delta qnorm
) 2))
244 (- one
(expt (/ sgnorm delta
) 2)))))))
246 (/ (* (/ delta qnorm
) (- one
(expt (/ sgnorm delta
) 2))) temp
))
249 '" form appropriate convex combination of the gauss-newton"
250 '" direction and the scaled gradient direction."
252 (setf temp
(* (- one alpha
) (f2cl-lib:dmin1 sgnorm delta
)))
253 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
256 (setf (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
)
259 (f2cl-lib:fref wa1-%data%
(j) ((1 n
)) wa1-%offset%
))
261 (f2cl-lib:fref x-%data%
(j) ((1 n
)) x-%offset%
))))
266 '" last card of subroutine dogleg."
269 (return (values nil nil nil nil nil nil nil nil nil
))))))
271 (in-package #:cl-user
)
272 #+#.
(cl:if
(cl:find-package
'#:f2cl
) '(and) '(or))
273 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
274 (setf (gethash 'fortran-to-lisp
::dogleg
275 fortran-to-lisp
::*f2cl-function-info
*)
276 (fortran-to-lisp::make-f2cl-finfo
277 :arg-types
'((fortran-to-lisp::integer4
) (array double-float
(*))
278 (fortran-to-lisp::integer4
) (array double-float
(*))
279 (array double-float
(*)) (double-float)
280 (array double-float
(*)) (array double-float
(*))
281 (array double-float
(*)))
282 :return-values
'(nil nil nil nil nil nil nil nil nil
)
283 :calls
'(fortran-to-lisp::enorm fortran-to-lisp
::dpmpar
))))