share/tensor/itensor.lisp: make X and D shared lexical variables for the functions...
[maxima.git] / share / minpack / lisp / lmpar.lisp
blob083f0829ba180a932bd44937ae5a9c7c1fcf1fba
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 ((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
41 dxnorm))
42 '" **********"
43 '""
44 '" subroutine lmpar"
45 '""
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"
50 '""
51 '" a*x = b , sqrt(par)*d*x = 0 ,"
52 '""
53 '" in the least squares sense, and dxnorm is the euclidean"
54 '" norm of d*x, then either par is zero and"
55 '""
56 '" (dxnorm-delta) .le. 0.1*delta ,"
57 '""
58 '" or par is positive and"
59 '""
60 '" abs(dxnorm-delta) .le. 0.1*delta ."
61 '""
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"
71 '""
72 '" t t t"
73 '" p *(a *a + par*d*d)*p = s *s ."
74 '""
75 '" s is employed within lmpar and may be of separate interest."
76 '""
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."
81 '""
82 '" the subroutine statement is"
83 '""
84 '" subroutine lmpar(n,r,ldr,ipvt,diag,qtb,delta,par,x,sdiag,"
85 '" wa1,wa2)"
86 '""
87 '" where"
88 '""
89 '" n is a positive integer input variable set to the order of r."
90 '""
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."
96 '""
97 '" ldr is a positive integer input variable not less than n"
98 '" which specifies the leading dimension of the array r."
99 '""
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"
135 '" **********"
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."
144 (setf nsing n)
145 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
146 ((> j n) nil)
147 (tagbody
148 (setf (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
149 (f2cl-lib:fref qtb-%data% (j) ((1 n)) qtb-%offset%))
151 (and
152 (= (f2cl-lib:fref r-%data% (j j) ((1 ldr) (1 n)) r-%offset%)
153 zero)
154 (= nsing n))
155 (setf nsing (f2cl-lib:int-sub j 1)))
156 (if (< nsing n)
157 (setf (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
158 zero))
159 label10))
160 (if (< nsing 1) (go label50))
161 (f2cl-lib:fdo (k 1 (f2cl-lib:int-add k 1))
162 ((> k nsing) nil)
163 (tagbody
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%
168 (j j)
169 ((1 ldr) (1 n))
170 r-%offset%)))
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))
175 ((> i jm1) nil)
176 (tagbody
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%
181 (i j)
182 ((1 ldr) (1 n))
183 r-%offset%)
184 temp)))
185 label20))
186 label30
187 label40))
188 label50
189 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
190 ((> j n) nil)
191 (tagbody
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%))
195 label60))
197 '" initialize the iteration counter."
198 '" evaluate the function at the origin, and test"
199 '" for acceptance of the gauss-newton direction."
201 (setf iter 0)
202 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
203 ((> j n) nil)
204 (tagbody
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%)))
208 label70))
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."
217 (setf parl zero)
218 (if (< nsing n) (go label120))
219 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
220 ((> j n) nil)
221 (tagbody
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%)
226 dxnorm)))
227 label80))
228 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
229 ((> j n) nil)
230 (tagbody
231 (setf sum zero)
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))
235 ((> i jm1) nil)
236 (tagbody
237 (setf sum
238 (+ sum
240 (f2cl-lib:fref r-%data%
241 (i j)
242 ((1 ldr) (1 n))
243 r-%offset%)
244 (f2cl-lib:fref wa1-%data%
246 ((1 n))
247 wa1-%offset%))))
248 label90))
249 label100
250 (setf (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
252 (- (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
253 sum)
254 (f2cl-lib:fref r-%data%
255 (j j)
256 ((1 ldr) (1 n))
257 r-%offset%)))
258 label110))
259 (setf temp (enorm n wa1))
260 (setf parl (/ (/ (/ fp delta) temp) temp))
261 label120
263 '" calculate an upper bound, paru, for the zero of the function."
265 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
266 ((> j n) nil)
267 (tagbody
268 (setf sum zero)
269 (f2cl-lib:fdo (i 1 (f2cl-lib:int-add i 1))
270 ((> i j) nil)
271 (tagbody
272 (setf sum
273 (+ sum
275 (f2cl-lib:fref r-%data%
276 (i j)
277 ((1 ldr) (1 n))
278 r-%offset%)
279 (f2cl-lib:fref qtb-%data%
281 ((1 n))
282 qtb-%offset%))))
283 label130))
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%)
286 (/ sum
287 (f2cl-lib:fref diag-%data% (l) ((1 n)) diag-%offset%)))
288 label140))
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."
302 label150
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))
310 ((> j n) nil)
311 (tagbody
312 (setf (f2cl-lib:fref wa1-%data% (j) ((1 n)) wa1-%offset%)
313 (* temp
314 (f2cl-lib:fref diag-%data% (j) ((1 n)) diag-%offset%)))
315 label160))
316 (qrsolv n r ldr ipvt wa1 qtb x sdiag wa2)
317 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
318 ((> j n) nil)
319 (tagbody
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%)))
323 label170))
324 (setf dxnorm (enorm n wa2))
325 (setf temp fp)
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))
335 (= iter 10))
336 (go label220))
338 '" compute the newton correction."
340 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
341 ((> j n) nil)
342 (tagbody
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%)
347 dxnorm)))
348 label180))
349 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
350 ((> j n) nil)
351 (tagbody
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%
356 ((1 n))
357 sdiag-%offset%)))
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))
362 ((> i n) nil)
363 (tagbody
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%
368 (i j)
369 ((1 ldr) (1 n))
370 r-%offset%)
371 temp)))
372 label190))
373 label200
374 label210))
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."
389 (go label150)
390 label220
392 '" termination."
394 (if (= iter 0) (setf par zero))
395 (go end_label)
397 '" last card of subroutine lmpar."
399 end_label
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
415 nil nil nil nil)
416 :calls '(fortran-to-lisp::qrsolv fortran-to-lisp::enorm
417 fortran-to-lisp::dpmpar))))