Rename *ll* and *ul* to ll and ul in defint
[maxima.git] / share / minpack / lisp / qrsolv.lisp
blobfcdcad61297e6f12e15e7a30a5fbf0ccfb516727
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 ((p5 0.5) (p25 0.25) (zero 0.0))
21 (declare (type (double-float) p5 p25 zero))
22 (defun qrsolv (n r ldr ipvt diag qtb x sdiag wa)
23 (declare (type (array f2cl-lib:integer4 (*)) ipvt)
24 (type (array double-float (*)) wa sdiag x qtb diag r)
25 (type (f2cl-lib:integer4) ldr 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 (sdiag double-float sdiag-%data% sdiag-%offset%)
32 (wa double-float wa-%data% wa-%offset%)
33 (ipvt f2cl-lib:integer4 ipvt-%data% ipvt-%offset%))
34 (prog ((cos 0.0) (cotan 0.0) (qtbpj 0.0) (sin 0.0) (sum 0.0) (tan 0.0)
35 (temp 0.0) (i 0) (j 0) (jp1 0) (k 0) (kp1 0) (l 0) (nsing 0))
36 (declare (type (f2cl-lib:integer4) nsing l kp1 k jp1 j i)
37 (type (double-float) temp tan sum sin qtbpj cotan cos))
38 '" **********"
39 '""
40 '" subroutine qrsolv"
41 '""
42 '" given an m by n matrix a, an n by n diagonal matrix d,"
43 '" and an m-vector b, the problem is to determine an x which"
44 '" solves the system"
45 '""
46 '" a*x = b , d*x = 0 ,"
47 '""
48 '" in the least squares sense."
49 '""
50 '" this subroutine completes the solution of the problem"
51 '" if it is provided with the necessary information from the"
52 '" qr factorization, with column pivoting, of a. that is, if"
53 '" a*p = q*r, where p is a permutation matrix, q has orthogonal"
54 '" columns, and r is an upper triangular matrix with diagonal"
55 '" elements of nonincreasing magnitude, then qrsolv expects"
56 '" the full upper triangle of r, the permutation matrix p,"
57 '" and the first n components of (q transpose)*b. the system"
58 '" a*x = b, d*x = 0, is then equivalent to"
59 '""
60 '" t t"
61 '" r*z = q *b , p *d*p*z = 0 ,"
62 '""
63 '" where x = p*z. if this system does not have full rank,"
64 '" then a least squares solution is obtained. on output qrsolv"
65 '" also provides an upper triangular matrix s such that"
66 '""
67 '" t t t"
68 '" p *(a *a + d*d)*p = s *s ."
69 '""
70 '" s is computed within qrsolv and may be of separate interest."
71 '""
72 '" the subroutine statement is"
73 '""
74 '" subroutine qrsolv(n,r,ldr,ipvt,diag,qtb,x,sdiag,wa)"
75 '""
76 '" where"
77 '""
78 '" n is a positive integer input variable set to the order of r."
79 '""
80 '" r is an n by n array. on input the full upper triangle"
81 '" must contain the full upper triangle of the matrix r."
82 '" on output the full upper triangle is unaltered, and the"
83 '" strict lower triangle contains the strict upper triangle"
84 '" (transposed) of the upper triangular matrix s."
85 '""
86 '" ldr is a positive integer input variable not less than n"
87 '" which specifies the leading dimension of the array r."
88 '""
89 '" ipvt is an integer input array of length n which defines the"
90 '" permutation matrix p such that a*p = q*r. column j of p"
91 '" is column ipvt(j) of the identity matrix."
92 '""
93 '" diag is an input array of length n which must contain the"
94 '" diagonal elements of the matrix d."
95 '""
96 '" qtb is an input array of length n which must contain the first"
97 '" n elements of the vector (q transpose)*b."
98 '""
99 '" x is an output array of length n which contains the least"
100 '" squares solution of the system a*x = b, d*x = 0."
102 '" sdiag is an output array of length n which contains the"
103 '" diagonal elements of the upper triangular matrix s."
105 '" wa is a work array of length n."
107 '" subprograms called"
109 '" fortran-supplied ... dabs,dsqrt"
111 '" argonne national laboratory. minpack project. march 1980."
112 '" burton s. garbow, kenneth e. hillstrom, jorge j. more"
114 '" **********"
116 '" copy r and (q transpose)*b to preserve input and initialize s."
117 '" in particular, save the diagonal elements of r in x."
119 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
120 ((> j n) nil)
121 (tagbody
122 (f2cl-lib:fdo (i j (f2cl-lib:int-add i 1))
123 ((> i n) nil)
124 (tagbody
125 (setf (f2cl-lib:fref r-%data% (i j) ((1 ldr) (1 n)) r-%offset%)
126 (f2cl-lib:fref r-%data%
127 (j i)
128 ((1 ldr) (1 n))
129 r-%offset%))
130 label10))
131 (setf (f2cl-lib:fref x-%data% (j) ((1 n)) x-%offset%)
132 (f2cl-lib:fref r-%data% (j j) ((1 ldr) (1 n)) r-%offset%))
133 (setf (f2cl-lib:fref wa-%data% (j) ((1 n)) wa-%offset%)
134 (f2cl-lib:fref qtb-%data% (j) ((1 n)) qtb-%offset%))
135 label20))
137 '" eliminate the diagonal matrix d using a givens rotation."
139 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
140 ((> j n) nil)
141 (tagbody
143 '" prepare the row of d to be eliminated, locating the"
144 '" diagonal element using p from the qr factorization."
146 (setf l (f2cl-lib:fref ipvt-%data% (j) ((1 n)) ipvt-%offset%))
147 (if (= (f2cl-lib:fref diag-%data% (l) ((1 n)) diag-%offset%) zero)
148 (go label90))
149 (f2cl-lib:fdo (k j (f2cl-lib:int-add k 1))
150 ((> k n) nil)
151 (tagbody
152 (setf (f2cl-lib:fref sdiag-%data% (k) ((1 n)) sdiag-%offset%)
153 zero)
154 label30))
155 (setf (f2cl-lib:fref sdiag-%data% (j) ((1 n)) sdiag-%offset%)
156 (f2cl-lib:fref diag-%data% (l) ((1 n)) diag-%offset%))
158 '" the transformations to eliminate the row of d"
159 '" modify only a single element of (q transpose)*b"
160 '" beyond the first n, which is initially zero."
162 (setf qtbpj zero)
163 (f2cl-lib:fdo (k j (f2cl-lib:int-add k 1))
164 ((> k n) nil)
165 (tagbody
167 '" determine a givens rotation which eliminates the"
168 '" appropriate element in the current row of d."
171 (= (f2cl-lib:fref sdiag-%data% (k) ((1 n)) sdiag-%offset%)
172 zero)
173 (go label70))
176 (f2cl-lib:dabs
177 (f2cl-lib:fref r-%data% (k k) ((1 ldr) (1 n)) r-%offset%))
178 (f2cl-lib:dabs
179 (f2cl-lib:fref sdiag-%data% (k) ((1 n)) sdiag-%offset%)))
180 (go label40))
181 (setf cotan
183 (f2cl-lib:fref r-%data%
184 (k k)
185 ((1 ldr) (1 n))
186 r-%offset%)
187 (f2cl-lib:fref sdiag-%data%
189 ((1 n))
190 sdiag-%offset%)))
191 (setf sin
192 (/ p5 (f2cl-lib:dsqrt (+ p25 (* p25 (expt cotan 2))))))
193 (setf cos (* sin cotan))
194 (go label50)
195 label40
196 (setf tan
198 (f2cl-lib:fref sdiag-%data%
200 ((1 n))
201 sdiag-%offset%)
202 (f2cl-lib:fref r-%data%
203 (k k)
204 ((1 ldr) (1 n))
205 r-%offset%)))
206 (setf cos (/ p5 (f2cl-lib:dsqrt (+ p25 (* p25 (expt tan 2))))))
207 (setf sin (* cos tan))
208 label50
210 '" compute the modified diagonal element of r and"
211 '" the modified element of ((q transpose)*b,0)."
213 (setf (f2cl-lib:fref r-%data% (k k) ((1 ldr) (1 n)) r-%offset%)
215 (* cos
216 (f2cl-lib:fref r-%data%
217 (k k)
218 ((1 ldr) (1 n))
219 r-%offset%))
220 (* sin
221 (f2cl-lib:fref sdiag-%data%
223 ((1 n))
224 sdiag-%offset%))))
225 (setf temp
227 (* cos
228 (f2cl-lib:fref wa-%data% (k) ((1 n)) wa-%offset%))
229 (* sin qtbpj)))
230 (setf qtbpj
232 (* (- sin)
233 (f2cl-lib:fref wa-%data% (k) ((1 n)) wa-%offset%))
234 (* cos qtbpj)))
235 (setf (f2cl-lib:fref wa-%data% (k) ((1 n)) wa-%offset%) temp)
237 '" accumulate the transformation in the row of s."
239 (setf kp1 (f2cl-lib:int-add k 1))
240 (if (< n kp1) (go label70))
241 (f2cl-lib:fdo (i kp1 (f2cl-lib:int-add i 1))
242 ((> i n) nil)
243 (tagbody
244 (setf temp
246 (* cos
247 (f2cl-lib:fref r-%data%
248 (i k)
249 ((1 ldr) (1 n))
250 r-%offset%))
251 (* sin
252 (f2cl-lib:fref sdiag-%data%
254 ((1 n))
255 sdiag-%offset%))))
256 (setf (f2cl-lib:fref sdiag-%data%
258 ((1 n))
259 sdiag-%offset%)
261 (* (- sin)
262 (f2cl-lib:fref r-%data%
263 (i k)
264 ((1 ldr) (1 n))
265 r-%offset%))
266 (* cos
267 (f2cl-lib:fref sdiag-%data%
269 ((1 n))
270 sdiag-%offset%))))
271 (setf (f2cl-lib:fref r-%data%
272 (i k)
273 ((1 ldr) (1 n))
274 r-%offset%)
275 temp)
276 label60))
277 label70
278 label80))
279 label90
281 '" store the diagonal element of s and restore"
282 '" the corresponding diagonal element of r."
284 (setf (f2cl-lib:fref sdiag-%data% (j) ((1 n)) sdiag-%offset%)
285 (f2cl-lib:fref r-%data% (j j) ((1 ldr) (1 n)) r-%offset%))
286 (setf (f2cl-lib:fref r-%data% (j j) ((1 ldr) (1 n)) r-%offset%)
287 (f2cl-lib:fref x-%data% (j) ((1 n)) x-%offset%))
288 label100))
290 '" solve the triangular system for z. if the system is"
291 '" singular, then obtain a least squares solution."
293 (setf nsing n)
294 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
295 ((> j n) nil)
296 (tagbody
298 (and
299 (= (f2cl-lib:fref sdiag-%data% (j) ((1 n)) sdiag-%offset%) zero)
300 (= nsing n))
301 (setf nsing (f2cl-lib:int-sub j 1)))
302 (if (< nsing n)
303 (setf (f2cl-lib:fref wa-%data% (j) ((1 n)) wa-%offset%) zero))
304 label110))
305 (if (< nsing 1) (go label150))
306 (f2cl-lib:fdo (k 1 (f2cl-lib:int-add k 1))
307 ((> k nsing) nil)
308 (tagbody
309 (setf j (f2cl-lib:int-add (f2cl-lib:int-sub nsing k) 1))
310 (setf sum zero)
311 (setf jp1 (f2cl-lib:int-add j 1))
312 (if (< nsing jp1) (go label130))
313 (f2cl-lib:fdo (i jp1 (f2cl-lib:int-add i 1))
314 ((> i nsing) nil)
315 (tagbody
316 (setf sum
317 (+ sum
319 (f2cl-lib:fref r-%data%
320 (i j)
321 ((1 ldr) (1 n))
322 r-%offset%)
323 (f2cl-lib:fref wa-%data%
325 ((1 n))
326 wa-%offset%))))
327 label120))
328 label130
329 (setf (f2cl-lib:fref wa-%data% (j) ((1 n)) wa-%offset%)
331 (- (f2cl-lib:fref wa-%data% (j) ((1 n)) wa-%offset%) sum)
332 (f2cl-lib:fref sdiag-%data% (j) ((1 n)) sdiag-%offset%)))
333 label140))
334 label150
336 '" permute the components of z back to components of x."
338 (f2cl-lib:fdo (j 1 (f2cl-lib:int-add j 1))
339 ((> j n) nil)
340 (tagbody
341 (setf l (f2cl-lib:fref ipvt-%data% (j) ((1 n)) ipvt-%offset%))
342 (setf (f2cl-lib:fref x-%data% (l) ((1 n)) x-%offset%)
343 (f2cl-lib:fref wa-%data% (j) ((1 n)) wa-%offset%))
344 label160))
345 (go end_label)
347 '" last card of subroutine qrsolv."
349 end_label
350 (return (values nil nil nil nil nil nil nil nil nil))))))
352 (in-package #:cl-user)
353 #+#.(cl:if (cl:find-package '#:f2cl) '(and) '(or))
354 (eval-when (:load-toplevel :compile-toplevel :execute)
355 (setf (gethash 'fortran-to-lisp::qrsolv
356 fortran-to-lisp::*f2cl-function-info*)
357 (fortran-to-lisp::make-f2cl-finfo
358 :arg-types '((fortran-to-lisp::integer4) (array double-float (*))
359 (fortran-to-lisp::integer4)
360 (array fortran-to-lisp::integer4 (*))
361 (array double-float (*)) (array double-float (*))
362 (array double-float (*)) (array double-float (*))
363 (array double-float (*)))
364 :return-values '(nil nil nil nil nil nil nil nil nil)
365 :calls 'nil)))