share/tensor/itensor.lisp: make X and D shared lexical variables for the functions...
[maxima.git] / share / minpack / lisp / dogleg.lisp
blob07a37617c44403b8cc55f05d38bbc1b60003673d
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)
11 ;;;
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))
17 (in-package :minpack)
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)
35 (l 0))
36 (declare (type (f2cl-lib:integer4) l k jp1 jj j i)
37 (type (double-float) temp sum sgnorm qnorm gnorm epsmch bnorm
38 alpha))
39 '" **********"
40 '""
41 '" subroutine dogleg"
42 '""
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."
49 '""
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."
56 '""
57 '" the subroutine statement is"
58 '""
59 '" subroutine dogleg(n,r,lr,diag,qtb,delta,x,wa1,wa2)"
60 '""
61 '" where"
62 '""
63 '" n is a positive integer input variable set to the order of r."
64 '""
65 '" r is an input array of length lr which must contain the upper"
66 '" triangular matrix r stored by rows."
67 '""
68 '" lr is a positive integer input variable not less than"
69 '" (n*(n+1))/2."
70 '""
71 '" diag is an input array of length n which must contain the"
72 '" diagonal elements of the matrix d."
73 '""
74 '" qtb is an input array of length n which must contain the first"
75 '" n elements of the vector (q transpose)*b."
76 '""
77 '" delta is a positive input variable which specifies an upper"
78 '" bound on the euclidean norm of d*x."
79 '""
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."
83 '""
84 '" wa1 and wa2 are work arrays of length n."
85 '""
86 '" subprograms called"
87 '""
88 '" minpack-supplied ... dpmpar,enorm"
89 '""
90 '" fortran-supplied ... dabs,dmax1,dmin1,dsqrt"
91 '""
92 '" argonne national laboratory. minpack project. march 1980."
93 '" burton s. garbow, kenneth e. hillstrom, jorge j. more"
94 '""
95 '" **********"
96 '""
97 '" epsmch is the machine precision."
98 '""
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))
105 ((> k n) nil)
106 (tagbody
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))
111 (setf sum zero)
112 (if (< n jp1) (go label20))
113 (f2cl-lib:fdo (i jp1 (f2cl-lib:int-add i 1))
114 ((> i n) nil)
115 (tagbody
116 (setf sum
117 (+ sum
118 (* (f2cl-lib:fref r-%data% (l) ((1 lr)) r-%offset%)
119 (f2cl-lib:fref x-%data%
121 ((1 n))
122 x-%offset%))))
123 (setf l (f2cl-lib:int-add l 1))
124 label10))
125 label20
126 (setf temp (f2cl-lib:fref r-%data% (jj) ((1 lr)) r-%offset%))
127 (if (/= temp zero) (go label40))
128 (setf l j)
129 (f2cl-lib:fdo (i 1 (f2cl-lib:int-add i 1))
130 ((> i j) nil)
131 (tagbody
132 (setf temp
133 (f2cl-lib:dmax1 temp
134 (f2cl-lib:dabs
135 (f2cl-lib:fref r-%data%
137 ((1 lr))
138 r-%offset%))))
139 (setf l (f2cl-lib:int-sub (f2cl-lib:int-add l n) i))
140 label30))
141 (setf temp (* epsmch temp))
142 (if (= temp zero) (setf temp epsmch))
143 label40
144 (setf (f2cl-lib:fref x-%data% (j) ((1 n)) x-%offset%)
146 (- (f2cl-lib:fref qtb-%data% (j) ((1 n)) qtb-%offset%)
147 sum)
148 temp))
149 label50))
151 '" test whether the gauss-newton direction is acceptable."
153 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
154 ((> j n) nil)
155 (tagbody
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%)))
160 label60))
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."
167 (setf l 1)
168 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
169 ((> j n) nil)
170 (tagbody
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))
173 ((> i n) nil)
174 (tagbody
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%)
178 temp)))
179 (setf l (f2cl-lib:int-add l 1))
180 label70))
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%)))
184 label80))
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))
190 (setf sgnorm zero)
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))
198 ((> j n) nil)
199 (tagbody
200 (setf (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
202 (/ (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
203 gnorm)
204 (f2cl-lib:fref diag-%data% (j) ((1 n)) diag-%offset%)))
205 label90))
206 (setf l 1)
207 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
208 ((> j n) nil)
209 (tagbody
210 (setf sum zero)
211 (f2cl-lib:fdo (i j (f2cl-lib:int-add i 1))
212 ((> i n) nil)
213 (tagbody
214 (setf sum
215 (+ sum
216 (* (f2cl-lib:fref r-%data% (l) ((1 lr)) r-%offset%)
217 (f2cl-lib:fref wa1-%data%
219 ((1 n))
220 wa1-%offset%))))
221 (setf l (f2cl-lib:int-add l 1))
222 label100))
223 (setf (f2cl-lib:fref wa2-%data% (j) ((1 n)) wa2-%offset%) sum)
224 label110))
225 (setf temp (enorm n wa2))
226 (setf sgnorm (/ (/ gnorm temp) temp))
228 '" test whether the scaled gradient direction is acceptable."
230 (setf alpha zero)
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)))
239 (setf temp
240 (+ (- temp (* (/ delta qnorm) (expt (/ sgnorm delta) 2)))
241 (f2cl-lib:dsqrt
242 (+ (expt (- temp (/ delta qnorm)) 2)
243 (* (- one (expt (/ delta qnorm) 2))
244 (- one (expt (/ sgnorm delta) 2)))))))
245 (setf alpha
246 (/ (* (/ delta qnorm) (- one (expt (/ sgnorm delta) 2))) temp))
247 label120
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))
254 ((> j n) nil)
255 (tagbody
256 (setf (f2cl-lib:fref x-%data% (j) ((1 n)) x-%offset%)
258 (* temp
259 (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%))
260 (* alpha
261 (f2cl-lib:fref x-%data% (j) ((1 n)) x-%offset%))))
262 label130))
263 label140
264 (go end_label)
266 '" last card of subroutine dogleg."
268 end_label
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))))