| 1 | subroutine dqrsl(x,ldx,n,k,qraux,y,qy,qty,b,rsd,xb,job,info) |
| 2 | integer ldx,n,k,job,info |
| 3 | double precision x(ldx,1),qraux(1),y(1),qy(1),qty(1),b(1),rsd(1), |
| 4 | * xb(1) |
| 5 | c |
| 6 | c dqrsl applies the output of dqrdc to compute coordinate |
| 7 | c transformations, projections, and least squares solutions. |
| 8 | c for k .le. min(n,p), let xk be the matrix |
| 9 | c |
| 10 | c xk = (x(jpvt(1)),x(jpvt(2)), ... ,x(jpvt(k))) |
| 11 | c |
| 12 | c formed from columnns jpvt(1), ... ,jpvt(k) of the original |
| 13 | c n x p matrix x that was input to dqrdc (if no pivoting was |
| 14 | c done, xk consists of the first k columns of x in their |
| 15 | c original order). dqrdc produces a factored orthogonal matrix q |
| 16 | c and an upper triangular matrix r such that |
| 17 | c |
| 18 | c xk = q * (r) |
| 19 | c (0) |
| 20 | c |
| 21 | c this information is contained in coded form in the arrays |
| 22 | c x and qraux. |
| 23 | c |
| 24 | c on entry |
| 25 | c |
| 26 | c x double precision(ldx,p). |
| 27 | c x contains the output of dqrdc. |
| 28 | c |
| 29 | c ldx integer. |
| 30 | c ldx is the leading dimension of the array x. |
| 31 | c |
| 32 | c n integer. |
| 33 | c n is the number of rows of the matrix xk. it must |
| 34 | c have the same value as n in dqrdc. |
| 35 | c |
| 36 | c k integer. |
| 37 | c k is the number of columns of the matrix xk. k |
| 38 | c must nnot be greater than min(n,p), where p is the |
| 39 | c same as in the calling sequence to dqrdc. |
| 40 | c |
| 41 | c qraux double precision(p). |
| 42 | c qraux contains the auxiliary output from dqrdc. |
| 43 | c |
| 44 | c y double precision(n) |
| 45 | c y contains an n-vector that is to be manipulated |
| 46 | c by dqrsl. |
| 47 | c |
| 48 | c job integer. |
| 49 | c job specifies what is to be computed. job has |
| 50 | c the decimal expansion abcde, with the following |
| 51 | c meaning. |
| 52 | c |
| 53 | c if a.ne.0, compute qy. |
| 54 | c if b,c,d, or e .ne. 0, compute qty. |
| 55 | c if c.ne.0, compute b. |
| 56 | c if d.ne.0, compute rsd. |
| 57 | c if e.ne.0, compute xb. |
| 58 | c |
| 59 | c note that a request to compute b, rsd, or xb |
| 60 | c automatically triggers the computation of qty, for |
| 61 | c which an array must be provided in the calling |
| 62 | c sequence. |
| 63 | c |
| 64 | c on return |
| 65 | c |
| 66 | c qy double precision(n). |
| 67 | c qy conntains q*y, if its computation has been |
| 68 | c requested. |
| 69 | c |
| 70 | c qty double precision(n). |
| 71 | c qty contains trans(q)*y, if its computation has |
| 72 | c been requested. here trans(q) is the |
| 73 | c transpose of the matrix q. |
| 74 | c |
| 75 | c b double precision(k) |
| 76 | c b contains the solution of the least squares problem |
| 77 | c |
| 78 | c minimize norm2(y - xk*b), |
| 79 | c |
| 80 | c if its computation has been requested. (note that |
| 81 | c if pivoting was requested in dqrdc, the j-th |
| 82 | c component of b will be associated with column jpvt(j) |
| 83 | c of the original matrix x that was input into dqrdc.) |
| 84 | c |
| 85 | c rsd double precision(n). |
| 86 | c rsd contains the least squares residual y - xk*b, |
| 87 | c if its computation has been requested. rsd is |
| 88 | c also the orthogonal projection of y onto the |
| 89 | c orthogonal complement of the column space of xk. |
| 90 | c |
| 91 | c xb double precision(n). |
| 92 | c xb contains the least squares approximation xk*b, |
| 93 | c if its computation has been requested. xb is also |
| 94 | c the orthogonal projection of y onto the column space |
| 95 | c of x. |
| 96 | c |
| 97 | c info integer. |
| 98 | c info is zero unless the computation of b has |
| 99 | c been requested and r is exactly singular. in |
| 100 | c this case, info is the index of the first zero |
| 101 | c diagonal element of r and b is left unaltered. |
| 102 | c |
| 103 | c the parameters qy, qty, b, rsd, and xb are not referenced |
| 104 | c if their computation is not requested and in this case |
| 105 | c can be replaced by dummy variables in the calling program. |
| 106 | c to save storage, the user may in some cases use the same |
| 107 | c array for different parameters in the calling sequence. a |
| 108 | c frequently occuring example is when one wishes to compute |
| 109 | c any of b, rsd, or xb and does not need y or qty. in this |
| 110 | c case one may identify y, qty, and one of b, rsd, or xb, while |
| 111 | c providing separate arrays for anything else that is to be |
| 112 | c computed. thus the calling sequence |
| 113 | c |
| 114 | c call dqrsl(x,ldx,n,k,qraux,y,dum,y,b,y,dum,110,info) |
| 115 | c |
| 116 | c will result in the computation of b and rsd, with rsd |
| 117 | c overwriting y. more generally, each item in the following |
| 118 | c list contains groups of permissible identifications for |
| 119 | c a single callinng sequence. |
| 120 | c |
| 121 | c 1. (y,qty,b) (rsd) (xb) (qy) |
| 122 | c |
| 123 | c 2. (y,qty,rsd) (b) (xb) (qy) |
| 124 | c |
| 125 | c 3. (y,qty,xb) (b) (rsd) (qy) |
| 126 | c |
| 127 | c 4. (y,qy) (qty,b) (rsd) (xb) |
| 128 | c |
| 129 | c 5. (y,qy) (qty,rsd) (b) (xb) |
| 130 | c |
| 131 | c 6. (y,qy) (qty,xb) (b) (rsd) |
| 132 | c |
| 133 | c in any group the value returned in the array allocated to |
| 134 | c the group corresponds to the last member of the group. |
| 135 | c |
| 136 | c linpack. this version dated 08/14/78 . |
| 137 | c g.w. stewart, university of maryland, argonne national lab. |
| 138 | c |
| 139 | c dqrsl uses the following functions and subprograms. |
| 140 | c |
| 141 | c blas daxpy,dcopy,ddot |
| 142 | c fortran dabs,min0,mod |
| 143 | c |
| 144 | c internal variables |
| 145 | c |
| 146 | integer i,j,jj,ju,kp1 |
| 147 | double precision ddot,t,temp |
| 148 | logical cb,cqy,cqty,cr,cxb |
| 149 | c |
| 150 | c |
| 151 | c set info flag. |
| 152 | c |
| 153 | info = 0 |
| 154 | c |
| 155 | c determine what is to be computed. |
| 156 | c |
| 157 | cqy = job/10000 .ne. 0 |
| 158 | cqty = mod(job,10000) .ne. 0 |
| 159 | cb = mod(job,1000)/100 .ne. 0 |
| 160 | cr = mod(job,100)/10 .ne. 0 |
| 161 | cxb = mod(job,10) .ne. 0 |
| 162 | ju = min0(k,n-1) |
| 163 | c |
| 164 | c special action when n=1. |
| 165 | c |
| 166 | if (ju .ne. 0) go to 40 |
| 167 | if (cqy) qy(1) = y(1) |
| 168 | if (cqty) qty(1) = y(1) |
| 169 | if (cxb) xb(1) = y(1) |
| 170 | if (.not.cb) go to 30 |
| 171 | if (x(1,1) .ne. 0.0d0) go to 10 |
| 172 | info = 1 |
| 173 | go to 20 |
| 174 | 10 continue |
| 175 | b(1) = y(1)/x(1,1) |
| 176 | 20 continue |
| 177 | 30 continue |
| 178 | if (cr) rsd(1) = 0.0d0 |
| 179 | go to 250 |
| 180 | 40 continue |
| 181 | c |
| 182 | c set up to compute qy or qty. |
| 183 | c |
| 184 | if (cqy) call dcopy(n,y,1,qy,1) |
| 185 | if (cqty) call dcopy(n,y,1,qty,1) |
| 186 | if (.not.cqy) go to 70 |
| 187 | c |
| 188 | c compute qy. |
| 189 | c |
| 190 | do 60 jj = 1, ju |
| 191 | j = ju - jj + 1 |
| 192 | if (qraux(j) .eq. 0.0d0) go to 50 |
| 193 | temp = x(j,j) |
| 194 | x(j,j) = qraux(j) |
| 195 | t = -ddot(n-j+1,x(j,j),1,qy(j),1)/x(j,j) |
| 196 | call daxpy(n-j+1,t,x(j,j),1,qy(j),1) |
| 197 | x(j,j) = temp |
| 198 | 50 continue |
| 199 | 60 continue |
| 200 | 70 continue |
| 201 | if (.not.cqty) go to 100 |
| 202 | c |
| 203 | c compute trans(q)*y. |
| 204 | c |
| 205 | do 90 j = 1, ju |
| 206 | if (qraux(j) .eq. 0.0d0) go to 80 |
| 207 | temp = x(j,j) |
| 208 | x(j,j) = qraux(j) |
| 209 | t = -ddot(n-j+1,x(j,j),1,qty(j),1)/x(j,j) |
| 210 | call daxpy(n-j+1,t,x(j,j),1,qty(j),1) |
| 211 | x(j,j) = temp |
| 212 | 80 continue |
| 213 | 90 continue |
| 214 | 100 continue |
| 215 | c |
| 216 | c set up to compute b, rsd, or xb. |
| 217 | c |
| 218 | if (cb) call dcopy(k,qty,1,b,1) |
| 219 | kp1 = k + 1 |
| 220 | if (cxb) call dcopy(k,qty,1,xb,1) |
| 221 | if (cr .and. k .lt. n) call dcopy(n-k,qty(kp1),1,rsd(kp1),1) |
| 222 | if (.not.cxb .or. kp1 .gt. n) go to 120 |
| 223 | do 110 i = kp1, n |
| 224 | xb(i) = 0.0d0 |
| 225 | 110 continue |
| 226 | 120 continue |
| 227 | if (.not.cr) go to 140 |
| 228 | do 130 i = 1, k |
| 229 | rsd(i) = 0.0d0 |
| 230 | 130 continue |
| 231 | 140 continue |
| 232 | if (.not.cb) go to 190 |
| 233 | c |
| 234 | c compute b. |
| 235 | c |
| 236 | do 170 jj = 1, k |
| 237 | j = k - jj + 1 |
| 238 | if (x(j,j) .ne. 0.0d0) go to 150 |
| 239 | info = j |
| 240 | c ......exit |
| 241 | go to 180 |
| 242 | 150 continue |
| 243 | b(j) = b(j)/x(j,j) |
| 244 | if (j .eq. 1) go to 160 |
| 245 | t = -b(j) |
| 246 | call daxpy(j-1,t,x(1,j),1,b,1) |
| 247 | 160 continue |
| 248 | 170 continue |
| 249 | 180 continue |
| 250 | 190 continue |
| 251 | if (.not.cr .and. .not.cxb) go to 240 |
| 252 | c |
| 253 | c compute rsd or xb as required. |
| 254 | c |
| 255 | do 230 jj = 1, ju |
| 256 | j = ju - jj + 1 |
| 257 | if (qraux(j) .eq. 0.0d0) go to 220 |
| 258 | temp = x(j,j) |
| 259 | x(j,j) = qraux(j) |
| 260 | if (.not.cr) go to 200 |
| 261 | t = -ddot(n-j+1,x(j,j),1,rsd(j),1)/x(j,j) |
| 262 | call daxpy(n-j+1,t,x(j,j),1,rsd(j),1) |
| 263 | 200 continue |
| 264 | if (.not.cxb) go to 210 |
| 265 | t = -ddot(n-j+1,x(j,j),1,xb(j),1)/x(j,j) |
| 266 | call daxpy(n-j+1,t,x(j,j),1,xb(j),1) |
| 267 | 210 continue |
| 268 | x(j,j) = temp |
| 269 | 220 continue |
| 270 | 230 continue |
| 271 | 240 continue |
| 272 | 250 continue |
| 273 | return |
| 274 | end |
| 275 | c...................................................................... |
| 276 | subroutine dsvdc(x,ldx,n,p,s,e,u,ldu,v,ldv,work,job,info) |
| 277 | integer ldx,n,p,ldu,ldv,job,info |
| 278 | double precision x(ldx,1),s(1),e(1),u(ldu,1),v(ldv,1),work(1) |
| 279 | c |
| 280 | c |
| 281 | c dsvdc is a subroutine to reduce a double precision nxp matrix x |
| 282 | c by orthogonal transformations u and v to diagonal form. the |
| 283 | c diagonal elements s(i) are the singular values of x. the |
| 284 | c columns of u are the corresponding left singular vectors, |
| 285 | c and the columns of v the right singular vectors. |
| 286 | c |
| 287 | c on entry |
| 288 | c |
| 289 | c x double precision(ldx,p), where ldx.ge.n. |
| 290 | c x contains the matrix whose singular value |
| 291 | c decomposition is to be computed. x is |
| 292 | c destroyed by dsvdc. |
| 293 | c |
| 294 | c ldx integer. |
| 295 | c ldx is the leading dimension of the array x. |
| 296 | c |
| 297 | c n integer. |
| 298 | c n is the number of rows of the matrix x. |
| 299 | c |
| 300 | c p integer. |
| 301 | c p is the number of columns of the matrix x. |
| 302 | c |
| 303 | c ldu integer. |
| 304 | c ldu is the leading dimension of the array u. |
| 305 | c (see below). |
| 306 | c |
| 307 | c ldv integer. |
| 308 | c ldv is the leading dimension of the array v. |
| 309 | c (see below). |
| 310 | c |
| 311 | c work double precision(n). |
| 312 | c work is a scratch array. |
| 313 | c |
| 314 | c job integer. |
| 315 | c job controls the computation of the singular |
| 316 | c vectors. it has the decimal expansion ab |
| 317 | c with the following meaning |
| 318 | c |
| 319 | c a.eq.0 do not compute the left singular |
| 320 | c vectors. |
| 321 | c a.eq.1 return the n left singular vectors |
| 322 | c in u. |
| 323 | c a.ge.2 return the first min(n,p) singular |
| 324 | c vectors in u. |
| 325 | c b.eq.0 do not compute the right singular |
| 326 | c vectors. |
| 327 | c b.eq.1 return the right singular vectors |
| 328 | c in v. |
| 329 | c |
| 330 | c on return |
| 331 | c |
| 332 | c s double precision(mm), where mm=min(n+1,p). |
| 333 | c the first min(n,p) entries of s contain the |
| 334 | c singular values of x arranged in descending |
| 335 | c order of magnitude. |
| 336 | c |
| 337 | c e double precision(p), |
| 338 | c e ordinarily contains zeros. however see the |
| 339 | c discussion of info for exceptions. |
| 340 | c |
| 341 | c u double precision(ldu,k), where ldu.ge.n. if |
| 342 | c joba.eq.1 then k.eq.n, if joba.ge.2 |
| 343 | c then k.eq.min(n,p). |
| 344 | c u contains the matrix of left singular vectors. |
| 345 | c u is not referenced if joba.eq.0. if n.le.p |
| 346 | c or if joba.eq.2, then u may be identified with x |
| 347 | c in the subroutine call. |
| 348 | c |
| 349 | c v double precision(ldv,p), where ldv.ge.p. |
| 350 | c v contains the matrix of right singular vectors. |
| 351 | c v is not referenced if job.eq.0. if p.le.n, |
| 352 | c then v may be identified with x in the |
| 353 | c subroutine call. |
| 354 | c |
| 355 | c info integer. |
| 356 | c the singular values (and their corresponding |
| 357 | c singular vectors) s(info+1),s(info+2),...,s(m) |
| 358 | c are correct (here m=min(n,p)). thus if |
| 359 | c info.eq.0, all the singular values and their |
| 360 | c vectors are correct. in any event, the matrix |
| 361 | c b = trans(u)*x*v is the bidiagonal matrix |
| 362 | c with the elements of s on its diagonal and the |
| 363 | c elements of e on its super-diagonal (trans(u) |
| 364 | c is the transpose of u). thus the singular |
| 365 | c values of x and b are the same. |
| 366 | c |
| 367 | c linpack. this version dated 08/14/78 . |
| 368 | c correction made to shift 2/84. |
| 369 | c g.w. stewart, university of maryland, argonne national lab. |
| 370 | c |
| 371 | c dsvdc uses the following functions and subprograms. |
| 372 | c |
| 373 | c external drot |
| 374 | c blas daxpy,ddot,dscal,dswap,dnrm2,drotg |
| 375 | c fortran dabs,dmax1,max0,min0,mod,dsqrt |
| 376 | c |
| 377 | c internal variables |
| 378 | c |
| 379 | integer i,iter,j,jobu,k,kase,kk,l,ll,lls,lm1,lp1,ls,lu,m,maxit, |
| 380 | * mm,mm1,mp1,nct,nctp1,ncu,nrt,nrtp1 |
| 381 | double precision ddot,t,r |
| 382 | double precision b,c,cs,el,emm1,f,g,dnrm2,scale,shift,sl,sm,sn, |
| 383 | * smm1,t1,test,ztest |
| 384 | logical wantu,wantv |
| 385 | c |
| 386 | c |
| 387 | c set the maximum number of iterations. |
| 388 | c |
| 389 | maxit = 30 |
| 390 | c |
| 391 | c determine what is to be computed. |
| 392 | c |
| 393 | wantu = .false. |
| 394 | wantv = .false. |
| 395 | jobu = mod(job,100)/10 |
| 396 | ncu = n |
| 397 | if (jobu .gt. 1) ncu = min0(n,p) |
| 398 | if (jobu .ne. 0) wantu = .true. |
| 399 | if (mod(job,10) .ne. 0) wantv = .true. |
| 400 | c |
| 401 | c reduce x to bidiagonal form, storing the diagonal elements |
| 402 | c in s and the super-diagonal elements in e. |
| 403 | c |
| 404 | info = 0 |
| 405 | nct = min0(n-1,p) |
| 406 | nrt = max0(0,min0(p-2,n)) |
| 407 | lu = max0(nct,nrt) |
| 408 | if (lu .lt. 1) go to 170 |
| 409 | do 160 l = 1, lu |
| 410 | lp1 = l + 1 |
| 411 | if (l .gt. nct) go to 20 |
| 412 | c |
| 413 | c compute the transformation for the l-th column and |
| 414 | c place the l-th diagonal in s(l). |
| 415 | c |
| 416 | s(l) = dnrm2(n-l+1,x(l,l),1) |
| 417 | if (s(l) .eq. 0.0d0) go to 10 |
| 418 | if (x(l,l) .ne. 0.0d0) s(l) = dsign(s(l),x(l,l)) |
| 419 | call dscal(n-l+1,1.0d0/s(l),x(l,l),1) |
| 420 | x(l,l) = 1.0d0 + x(l,l) |
| 421 | 10 continue |
| 422 | s(l) = -s(l) |
| 423 | 20 continue |
| 424 | if (p .lt. lp1) go to 50 |
| 425 | do 40 j = lp1, p |
| 426 | if (l .gt. nct) go to 30 |
| 427 | if (s(l) .eq. 0.0d0) go to 30 |
| 428 | c |
| 429 | c apply the transformation. |
| 430 | c |
| 431 | t = -ddot(n-l+1,x(l,l),1,x(l,j),1)/x(l,l) |
| 432 | call daxpy(n-l+1,t,x(l,l),1,x(l,j),1) |
| 433 | 30 continue |
| 434 | c |
| 435 | c place the l-th row of x into e for the |
| 436 | c subsequent calculation of the row transformation. |
| 437 | c |
| 438 | e(j) = x(l,j) |
| 439 | 40 continue |
| 440 | 50 continue |
| 441 | if (.not.wantu .or. l .gt. nct) go to 70 |
| 442 | c |
| 443 | c place the transformation in u for subsequent back |
| 444 | c multiplication. |
| 445 | c |
| 446 | do 60 i = l, n |
| 447 | u(i,l) = x(i,l) |
| 448 | 60 continue |
| 449 | 70 continue |
| 450 | if (l .gt. nrt) go to 150 |
| 451 | c |
| 452 | c compute the l-th row transformation and place the |
| 453 | c l-th super-diagonal in e(l). |
| 454 | c |
| 455 | e(l) = dnrm2(p-l,e(lp1),1) |
| 456 | if (e(l) .eq. 0.0d0) go to 80 |
| 457 | if (e(lp1) .ne. 0.0d0) e(l) = dsign(e(l),e(lp1)) |
| 458 | call dscal(p-l,1.0d0/e(l),e(lp1),1) |
| 459 | e(lp1) = 1.0d0 + e(lp1) |
| 460 | 80 continue |
| 461 | e(l) = -e(l) |
| 462 | if (lp1 .gt. n .or. e(l) .eq. 0.0d0) go to 120 |
| 463 | c |
| 464 | c apply the transformation. |
| 465 | c |
| 466 | do 90 i = lp1, n |
| 467 | work(i) = 0.0d0 |
| 468 | 90 continue |
| 469 | do 100 j = lp1, p |
| 470 | call daxpy(n-l,e(j),x(lp1,j),1,work(lp1),1) |
| 471 | 100 continue |
| 472 | do 110 j = lp1, p |
| 473 | call daxpy(n-l,-e(j)/e(lp1),work(lp1),1,x(lp1,j),1) |
| 474 | 110 continue |
| 475 | 120 continue |
| 476 | if (.not.wantv) go to 140 |
| 477 | c |
| 478 | c place the transformation in v for subsequent |
| 479 | c back multiplication. |
| 480 | c |
| 481 | do 130 i = lp1, p |
| 482 | v(i,l) = e(i) |
| 483 | 130 continue |
| 484 | 140 continue |
| 485 | 150 continue |
| 486 | 160 continue |
| 487 | 170 continue |
| 488 | c |
| 489 | c set up the final bidiagonal matrix or order m. |
| 490 | c |
| 491 | m = min0(p,n+1) |
| 492 | nctp1 = nct + 1 |
| 493 | nrtp1 = nrt + 1 |
| 494 | if (nct .lt. p) s(nctp1) = x(nctp1,nctp1) |
| 495 | if (n .lt. m) s(m) = 0.0d0 |
| 496 | if (nrtp1 .lt. m) e(nrtp1) = x(nrtp1,m) |
| 497 | e(m) = 0.0d0 |
| 498 | c |
| 499 | c if required, generate u. |
| 500 | c |
| 501 | if (.not.wantu) go to 300 |
| 502 | if (ncu .lt. nctp1) go to 200 |
| 503 | do 190 j = nctp1, ncu |
| 504 | do 180 i = 1, n |
| 505 | u(i,j) = 0.0d0 |
| 506 | 180 continue |
| 507 | u(j,j) = 1.0d0 |
| 508 | 190 continue |
| 509 | 200 continue |
| 510 | if (nct .lt. 1) go to 290 |
| 511 | do 280 ll = 1, nct |
| 512 | l = nct - ll + 1 |
| 513 | if (s(l) .eq. 0.0d0) go to 250 |
| 514 | lp1 = l + 1 |
| 515 | if (ncu .lt. lp1) go to 220 |
| 516 | do 210 j = lp1, ncu |
| 517 | t = -ddot(n-l+1,u(l,l),1,u(l,j),1)/u(l,l) |
| 518 | call daxpy(n-l+1,t,u(l,l),1,u(l,j),1) |
| 519 | 210 continue |
| 520 | 220 continue |
| 521 | call dscal(n-l+1,-1.0d0,u(l,l),1) |
| 522 | u(l,l) = 1.0d0 + u(l,l) |
| 523 | lm1 = l - 1 |
| 524 | if (lm1 .lt. 1) go to 240 |
| 525 | do 230 i = 1, lm1 |
| 526 | u(i,l) = 0.0d0 |
| 527 | 230 continue |
| 528 | 240 continue |
| 529 | go to 270 |
| 530 | 250 continue |
| 531 | do 260 i = 1, n |
| 532 | u(i,l) = 0.0d0 |
| 533 | 260 continue |
| 534 | u(l,l) = 1.0d0 |
| 535 | 270 continue |
| 536 | 280 continue |
| 537 | 290 continue |
| 538 | 300 continue |
| 539 | c |
| 540 | c if it is required, generate v. |
| 541 | c |
| 542 | if (.not.wantv) go to 350 |
| 543 | do 340 ll = 1, p |
| 544 | l = p - ll + 1 |
| 545 | lp1 = l + 1 |
| 546 | if (l .gt. nrt) go to 320 |
| 547 | if (e(l) .eq. 0.0d0) go to 320 |
| 548 | do 310 j = lp1, p |
| 549 | t = -ddot(p-l,v(lp1,l),1,v(lp1,j),1)/v(lp1,l) |
| 550 | call daxpy(p-l,t,v(lp1,l),1,v(lp1,j),1) |
| 551 | 310 continue |
| 552 | 320 continue |
| 553 | do 330 i = 1, p |
| 554 | v(i,l) = 0.0d0 |
| 555 | 330 continue |
| 556 | v(l,l) = 1.0d0 |
| 557 | 340 continue |
| 558 | 350 continue |
| 559 | c |
| 560 | c main iteration loop for the singular values. |
| 561 | c |
| 562 | mm = m |
| 563 | iter = 0 |
| 564 | 360 continue |
| 565 | c |
| 566 | c quit if all the singular values have been found. |
| 567 | c |
| 568 | c ...exit |
| 569 | if (m .eq. 0) go to 620 |
| 570 | c |
| 571 | c if too many iterations have been performed, set |
| 572 | c flag and return. |
| 573 | c |
| 574 | if (iter .lt. maxit) go to 370 |
| 575 | info = m |
| 576 | c ......exit |
| 577 | go to 620 |
| 578 | 370 continue |
| 579 | c |
| 580 | c this section of the program inspects for |
| 581 | c negligible elements in the s and e arrays. on |
| 582 | c completion the variables kase and l are set as follows. |
| 583 | c |
| 584 | c kase = 1 if s(m) and e(l-1) are negligible and l.lt.m |
| 585 | c kase = 2 if s(l) is negligible and l.lt.m |
| 586 | c kase = 3 if e(l-1) is negligible, l.lt.m, and |
| 587 | c s(l), ..., s(m) are not negligible (qr step). |
| 588 | c kase = 4 if e(m-1) is negligible (convergence). |
| 589 | c |
| 590 | do 390 ll = 1, m |
| 591 | l = m - ll |
| 592 | c ...exit |
| 593 | if (l .eq. 0) go to 400 |
| 594 | test = dabs(s(l)) + dabs(s(l+1)) |
| 595 | ztest = test + dabs(e(l)) |
| 596 | if (ztest .ne. test) go to 380 |
| 597 | e(l) = 0.0d0 |
| 598 | c ......exit |
| 599 | go to 400 |
| 600 | 380 continue |
| 601 | 390 continue |
| 602 | 400 continue |
| 603 | if (l .ne. m - 1) go to 410 |
| 604 | kase = 4 |
| 605 | go to 480 |
| 606 | 410 continue |
| 607 | lp1 = l + 1 |
| 608 | mp1 = m + 1 |
| 609 | do 430 lls = lp1, mp1 |
| 610 | ls = m - lls + lp1 |
| 611 | c ...exit |
| 612 | if (ls .eq. l) go to 440 |
| 613 | test = 0.0d0 |
| 614 | if (ls .ne. m) test = test + dabs(e(ls)) |
| 615 | if (ls .ne. l + 1) test = test + dabs(e(ls-1)) |
| 616 | ztest = test + dabs(s(ls)) |
| 617 | if (ztest .ne. test) go to 420 |
| 618 | s(ls) = 0.0d0 |
| 619 | c ......exit |
| 620 | go to 440 |
| 621 | 420 continue |
| 622 | 430 continue |
| 623 | 440 continue |
| 624 | if (ls .ne. l) go to 450 |
| 625 | kase = 3 |
| 626 | go to 470 |
| 627 | 450 continue |
| 628 | if (ls .ne. m) go to 460 |
| 629 | kase = 1 |
| 630 | go to 470 |
| 631 | 460 continue |
| 632 | kase = 2 |
| 633 | l = ls |
| 634 | 470 continue |
| 635 | 480 continue |
| 636 | l = l + 1 |
| 637 | c |
| 638 | c perform the task indicated by kase. |
| 639 | c |
| 640 | go to (490,520,540,570), kase |
| 641 | c |
| 642 | c deflate negligible s(m). |
| 643 | c |
| 644 | 490 continue |
| 645 | mm1 = m - 1 |
| 646 | f = e(m-1) |
| 647 | e(m-1) = 0.0d0 |
| 648 | do 510 kk = l, mm1 |
| 649 | k = mm1 - kk + l |
| 650 | t1 = s(k) |
| 651 | call drotg(t1,f,cs,sn) |
| 652 | s(k) = t1 |
| 653 | if (k .eq. l) go to 500 |
| 654 | f = -sn*e(k-1) |
| 655 | e(k-1) = cs*e(k-1) |
| 656 | 500 continue |
| 657 | if (wantv) call drot(p,v(1,k),1,v(1,m),1,cs,sn) |
| 658 | 510 continue |
| 659 | go to 610 |
| 660 | c |
| 661 | c split at negligible s(l). |
| 662 | c |
| 663 | 520 continue |
| 664 | f = e(l-1) |
| 665 | e(l-1) = 0.0d0 |
| 666 | do 530 k = l, m |
| 667 | t1 = s(k) |
| 668 | call drotg(t1,f,cs,sn) |
| 669 | s(k) = t1 |
| 670 | f = -sn*e(k) |
| 671 | e(k) = cs*e(k) |
| 672 | if (wantu) call drot(n,u(1,k),1,u(1,l-1),1,cs,sn) |
| 673 | 530 continue |
| 674 | go to 610 |
| 675 | c |
| 676 | c perform one qr step. |
| 677 | c |
| 678 | 540 continue |
| 679 | c |
| 680 | c calculate the shift. |
| 681 | c |
| 682 | scale = dmax1(dabs(s(m)),dabs(s(m-1)),dabs(e(m-1)), |
| 683 | * dabs(s(l)),dabs(e(l))) |
| 684 | sm = s(m)/scale |
| 685 | smm1 = s(m-1)/scale |
| 686 | emm1 = e(m-1)/scale |
| 687 | sl = s(l)/scale |
| 688 | el = e(l)/scale |
| 689 | b = ((smm1 + sm)*(smm1 - sm) + emm1**2)/2.0d0 |
| 690 | c = (sm*emm1)**2 |
| 691 | shift = 0.0d0 |
| 692 | if (b .eq. 0.0d0 .and. c .eq. 0.0d0) go to 550 |
| 693 | shift = dsqrt(b**2+c) |
| 694 | if (b .lt. 0.0d0) shift = -shift |
| 695 | shift = c/(b + shift) |
| 696 | 550 continue |
| 697 | f = (sl + sm)*(sl - sm) + shift |
| 698 | g = sl*el |
| 699 | c |
| 700 | c chase zeros. |
| 701 | c |
| 702 | mm1 = m - 1 |
| 703 | do 560 k = l, mm1 |
| 704 | call drotg(f,g,cs,sn) |
| 705 | if (k .ne. l) e(k-1) = f |
| 706 | f = cs*s(k) + sn*e(k) |
| 707 | e(k) = cs*e(k) - sn*s(k) |
| 708 | g = sn*s(k+1) |
| 709 | s(k+1) = cs*s(k+1) |
| 710 | if (wantv) call drot(p,v(1,k),1,v(1,k+1),1,cs,sn) |
| 711 | call drotg(f,g,cs,sn) |
| 712 | s(k) = f |
| 713 | f = cs*e(k) + sn*s(k+1) |
| 714 | s(k+1) = -sn*e(k) + cs*s(k+1) |
| 715 | g = sn*e(k+1) |
| 716 | e(k+1) = cs*e(k+1) |
| 717 | if (wantu .and. k .lt. n) |
| 718 | * call drot(n,u(1,k),1,u(1,k+1),1,cs,sn) |
| 719 | 560 continue |
| 720 | e(m-1) = f |
| 721 | iter = iter + 1 |
| 722 | go to 610 |
| 723 | c |
| 724 | c convergence. |
| 725 | c |
| 726 | 570 continue |
| 727 | c |
| 728 | c make the singular value positive. |
| 729 | c |
| 730 | if (s(l) .ge. 0.0d0) go to 580 |
| 731 | s(l) = -s(l) |
| 732 | if (wantv) call dscal(p,-1.0d0,v(1,l),1) |
| 733 | 580 continue |
| 734 | c |
| 735 | c order the singular value. |
| 736 | c |
| 737 | 590 if (l .eq. mm) go to 600 |
| 738 | c ...exit |
| 739 | if (s(l) .ge. s(l+1)) go to 600 |
| 740 | t = s(l) |
| 741 | s(l) = s(l+1) |
| 742 | s(l+1) = t |
| 743 | if (wantv .and. l .lt. p) |
| 744 | * call dswap(p,v(1,l),1,v(1,l+1),1) |
| 745 | if (wantu .and. l .lt. n) |
| 746 | * call dswap(n,u(1,l),1,u(1,l+1),1) |
| 747 | l = l + 1 |
| 748 | go to 590 |
| 749 | 600 continue |
| 750 | iter = 0 |
| 751 | m = m - 1 |
| 752 | 610 continue |
| 753 | go to 360 |
| 754 | 620 continue |
| 755 | return |
| 756 | end |
| 757 | |
| 758 | c...................................................................... |
| 759 | |
| 760 | subroutine dqrdc(x,ldx,n,p,qraux,jpvt,work,job) |
| 761 | integer ldx,n,p,job |
| 762 | integer jpvt(1) |
| 763 | double precision x(ldx,1),qraux(1),work(1) |
| 764 | c |
| 765 | c dqrdc uses householder transformations to compute the qr |
| 766 | c factorization of an n by p matrix x. column pivoting |
| 767 | c based on the 2-norms of the reduced columns may be |
| 768 | c performed at the users option. |
| 769 | c |
| 770 | c on entry |
| 771 | c |
| 772 | c x double precision(ldx,p), where ldx .ge. n. |
| 773 | c x contains the matrix whose decomposition is to be |
| 774 | c computed. |
| 775 | c |
| 776 | c ldx integer. |
| 777 | c ldx is the leading dimension of the array x. |
| 778 | c |
| 779 | c n integer. |
| 780 | c n is the number of rows of the matrix x. |
| 781 | c |
| 782 | c p integer. |
| 783 | c p is the number of columns of the matrix x. |
| 784 | c |
| 785 | c jpvt integer(p). |
| 786 | c jpvt contains integers that control the selection |
| 787 | c of the pivot columns. the k-th column x(k) of x |
| 788 | c is placed in one of three classes according to the |
| 789 | c value of jpvt(k). |
| 790 | c |
| 791 | c if jpvt(k) .gt. 0, then x(k) is an initial |
| 792 | c column. |
| 793 | c |
| 794 | c if jpvt(k) .eq. 0, then x(k) is a free column. |
| 795 | c |
| 796 | c if jpvt(k) .lt. 0, then x(k) is a final column. |
| 797 | c |
| 798 | c before the decomposition is computed, initial columns |
| 799 | c are moved to the beginning of the array x and final |
| 800 | c columns to the end. both initial and final columns |
| 801 | c are frozen in place during the computation and only |
| 802 | c free columns are moved. at the k-th stage of the |
| 803 | c reduction, if x(k) is occupied by a free column |
| 804 | c it is interchanged with the free column of largest |
| 805 | c reduced norm. jpvt is not referenced if |
| 806 | c job .eq. 0. |
| 807 | c |
| 808 | c work double precision(p). |
| 809 | c work is a work array. work is not referenced if |
| 810 | c job .eq. 0. |
| 811 | c |
| 812 | c job integer. |
| 813 | c job is an integer that initiates column pivoting. |
| 814 | c if job .eq. 0, no pivoting is done. |
| 815 | c if job .ne. 0, pivoting is done. |
| 816 | c |
| 817 | c on return |
| 818 | c |
| 819 | c x x contains in its upper triangle the upper |
| 820 | c triangular matrix r of the qr factorization. |
| 821 | c below its diagonal x contains information from |
| 822 | c which the orthogonal part of the decomposition |
| 823 | c can be recovered. note that if pivoting has |
| 824 | c been requested, the decomposition is not that |
| 825 | c of the original matrix x but that of x |
| 826 | c with its columns permuted as described by jpvt. |
| 827 | c |
| 828 | c qraux double precision(p). |
| 829 | c qraux contains further information required to recover |
| 830 | c the orthogonal part of the decomposition. |
| 831 | c |
| 832 | c jpvt jpvt(k) contains the index of the column of the |
| 833 | c original matrix that has been interchanged into |
| 834 | c the k-th column, if pivoting was requested. |
| 835 | c |
| 836 | c linpack. this version dated 08/14/78 . |
| 837 | c g.w. stewart, university of maryland, argonne national lab. |
| 838 | c |
| 839 | c dqrdc uses the following functions and subprograms. |
| 840 | c |
| 841 | c blas daxpy,ddot,dscal,dswap,dnrm2 |
| 842 | c fortran dabs,dmax1,min0,dsqrt |
| 843 | c |
| 844 | c internal variables |
| 845 | c |
| 846 | integer j,jp,l,lp1,lup,maxj,pl,pu |
| 847 | double precision maxnrm,dnrm2,tt |
| 848 | double precision ddot,nrmxl,t |
| 849 | logical negj,swapj |
| 850 | c |
| 851 | c |
| 852 | pl = 1 |
| 853 | pu = 0 |
| 854 | if (job .eq. 0) go to 60 |
| 855 | c |
| 856 | c pivoting has been requested. rearrange the columns |
| 857 | c according to jpvt. |
| 858 | c |
| 859 | do 20 j = 1, p |
| 860 | swapj = jpvt(j) .gt. 0 |
| 861 | negj = jpvt(j) .lt. 0 |
| 862 | jpvt(j) = j |
| 863 | if (negj) jpvt(j) = -j |
| 864 | if (.not.swapj) go to 10 |
| 865 | if (j .ne. pl) call dswap(n,x(1,pl),1,x(1,j),1) |
| 866 | jpvt(j) = jpvt(pl) |
| 867 | jpvt(pl) = j |
| 868 | pl = pl + 1 |
| 869 | 10 continue |
| 870 | 20 continue |
| 871 | pu = p |
| 872 | do 50 jj = 1, p |
| 873 | j = p - jj + 1 |
| 874 | if (jpvt(j) .ge. 0) go to 40 |
| 875 | jpvt(j) = -jpvt(j) |
| 876 | if (j .eq. pu) go to 30 |
| 877 | call dswap(n,x(1,pu),1,x(1,j),1) |
| 878 | jp = jpvt(pu) |
| 879 | jpvt(pu) = jpvt(j) |
| 880 | jpvt(j) = jp |
| 881 | 30 continue |
| 882 | pu = pu - 1 |
| 883 | 40 continue |
| 884 | 50 continue |
| 885 | 60 continue |
| 886 | c |
| 887 | c compute the norms of the free columns. |
| 888 | c |
| 889 | if (pu .lt. pl) go to 80 |
| 890 | do 70 j = pl, pu |
| 891 | qraux(j) = dnrm2(n,x(1,j),1) |
| 892 | work(j) = qraux(j) |
| 893 | 70 continue |
| 894 | 80 continue |
| 895 | c |
| 896 | c perform the householder reduction of x. |
| 897 | c |
| 898 | lup = min0(n,p) |
| 899 | do 200 l = 1, lup |
| 900 | if (l .lt. pl .or. l .ge. pu) go to 120 |
| 901 | c |
| 902 | c locate the column of largest norm and bring it |
| 903 | c into the pivot position. |
| 904 | c |
| 905 | maxnrm = 0.0d0 |
| 906 | maxj = l |
| 907 | do 100 j = l, pu |
| 908 | if (qraux(j) .le. maxnrm) go to 90 |
| 909 | maxnrm = qraux(j) |
| 910 | maxj = j |
| 911 | 90 continue |
| 912 | 100 continue |
| 913 | if (maxj .eq. l) go to 110 |
| 914 | call dswap(n,x(1,l),1,x(1,maxj),1) |
| 915 | qraux(maxj) = qraux(l) |
| 916 | work(maxj) = work(l) |
| 917 | jp = jpvt(maxj) |
| 918 | jpvt(maxj) = jpvt(l) |
| 919 | jpvt(l) = jp |
| 920 | 110 continue |
| 921 | 120 continue |
| 922 | qraux(l) = 0.0d0 |
| 923 | if (l .eq. n) go to 190 |
| 924 | c |
| 925 | c compute the householder transformation for column l. |
| 926 | c |
| 927 | nrmxl = dnrm2(n-l+1,x(l,l),1) |
| 928 | if (nrmxl .eq. 0.0d0) go to 180 |
| 929 | if (x(l,l) .ne. 0.0d0) nrmxl = dsign(nrmxl,x(l,l)) |
| 930 | call dscal(n-l+1,1.0d0/nrmxl,x(l,l),1) |
| 931 | x(l,l) = 1.0d0 + x(l,l) |
| 932 | c |
| 933 | c apply the transformation to the remaining columns, |
| 934 | c updating the norms. |
| 935 | c |
| 936 | lp1 = l + 1 |
| 937 | if (p .lt. lp1) go to 170 |
| 938 | do 160 j = lp1, p |
| 939 | t = -ddot(n-l+1,x(l,l),1,x(l,j),1)/x(l,l) |
| 940 | call daxpy(n-l+1,t,x(l,l),1,x(l,j),1) |
| 941 | if (j .lt. pl .or. j .gt. pu) go to 150 |
| 942 | if (qraux(j) .eq. 0.0d0) go to 150 |
| 943 | tt = 1.0d0 - (dabs(x(l,j))/qraux(j))**2 |
| 944 | tt = dmax1(tt,0.0d0) |
| 945 | t = tt |
| 946 | tt = 1.0d0 + 0.05d0*tt*(qraux(j)/work(j))**2 |
| 947 | if (tt .eq. 1.0d0) go to 130 |
| 948 | qraux(j) = qraux(j)*dsqrt(t) |
| 949 | go to 140 |
| 950 | 130 continue |
| 951 | qraux(j) = dnrm2(n-l,x(l+1,j),1) |
| 952 | work(j) = qraux(j) |
| 953 | 140 continue |
| 954 | 150 continue |
| 955 | 160 continue |
| 956 | 170 continue |
| 957 | c |
| 958 | c save the transformation. |
| 959 | c |
| 960 | qraux(l) = x(l,l) |
| 961 | x(l,l) = -nrmxl |
| 962 | 180 continue |
| 963 | 190 continue |
| 964 | 200 continue |
| 965 | return |
| 966 | end |