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 ((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
))
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"
46 '" a*x = b , d*x = 0 ,"
48 '" in the least squares sense."
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"
61 '" r*z = q *b , p *d*p*z = 0 ,"
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"
68 '" p *(a *a + d*d)*p = s *s ."
70 '" s is computed within qrsolv and may be of separate interest."
72 '" the subroutine statement is"
74 '" subroutine qrsolv(n,r,ldr,ipvt,diag,qtb,x,sdiag,wa)"
78 '" n is a positive integer input variable set to the order of r."
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."
86 '" ldr is a positive integer input variable not less than n"
87 '" which specifies the leading dimension of the array r."
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."
93 '" diag is an input array of length n which must contain the"
94 '" diagonal elements of the matrix d."
96 '" qtb is an input array of length n which must contain the first"
97 '" n elements of the vector (q transpose)*b."
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"
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))
122 (f2cl-lib:fdo
(i j
(f2cl-lib:int-add i
1))
125 (setf (f2cl-lib:fref r-%data%
(i j
) ((1 ldr
) (1 n
)) r-%offset%
)
126 (f2cl-lib:fref r-%data%
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%
))
137 '" eliminate the diagonal matrix d using a givens rotation."
139 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
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
)
149 (f2cl-lib:fdo
(k j
(f2cl-lib:int-add k
1))
152 (setf (f2cl-lib:fref sdiag-%data%
(k) ((1 n
)) sdiag-%offset%
)
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."
163 (f2cl-lib:fdo
(k j
(f2cl-lib:int-add k
1))
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%
)
177 (f2cl-lib:fref r-%data%
(k k
) ((1 ldr
) (1 n
)) r-%offset%
))
179 (f2cl-lib:fref sdiag-%data%
(k) ((1 n
)) sdiag-%offset%
)))
183 (f2cl-lib:fref r-%data%
187 (f2cl-lib:fref sdiag-%data%
192 (/ p5
(f2cl-lib:dsqrt
(+ p25
(* p25
(expt cotan
2))))))
193 (setf cos
(* sin cotan
))
198 (f2cl-lib:fref sdiag-%data%
202 (f2cl-lib:fref r-%data%
206 (setf cos
(/ p5
(f2cl-lib:dsqrt
(+ p25
(* p25
(expt tan
2))))))
207 (setf sin
(* cos tan
))
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%
)
216 (f2cl-lib:fref r-%data%
221 (f2cl-lib:fref sdiag-%data%
228 (f2cl-lib:fref wa-%data%
(k) ((1 n
)) wa-%offset%
))
233 (f2cl-lib:fref wa-%data%
(k) ((1 n
)) wa-%offset%
))
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))
247 (f2cl-lib:fref r-%data%
252 (f2cl-lib:fref sdiag-%data%
256 (setf (f2cl-lib:fref sdiag-%data%
262 (f2cl-lib:fref r-%data%
267 (f2cl-lib:fref sdiag-%data%
271 (setf (f2cl-lib:fref r-%data%
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%
))
290 '" solve the triangular system for z. if the system is"
291 '" singular, then obtain a least squares solution."
294 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
299 (= (f2cl-lib:fref sdiag-%data%
(j) ((1 n
)) sdiag-%offset%
) zero
)
301 (setf nsing
(f2cl-lib:int-sub j
1)))
303 (setf (f2cl-lib:fref wa-%data%
(j) ((1 n
)) wa-%offset%
) zero
))
305 (if (< nsing
1) (go label150
))
306 (f2cl-lib:fdo
(k 1 (f2cl-lib:int-add k
1))
309 (setf j
(f2cl-lib:int-add
(f2cl-lib:int-sub nsing k
) 1))
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))
319 (f2cl-lib:fref r-%data%
323 (f2cl-lib:fref wa-%data%
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%
)))
336 '" permute the components of z back to components of x."
338 (f2cl-lib:fdo
(j 1 (f2cl-lib:int-add j
1))
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%
))
347 '" last card of subroutine qrsolv."
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
)