SPEED
QR_SOLVE.f90
Go to the documentation of this file.
1subroutine daxpy ( n, da, dx, incx, dy, incy )
2
3!*****************************************************************************80
4!
5!! DAXPY computes constant times a vector plus a vector.
6!
7! Discussion:
8!
9! This routine uses double precision real arithmetic.
10!
11! This routine uses unrolled loops for increments equal to one.
12!
13! Licensing:
14!
15! This code is distributed under the GNU LGPL license.
16!
17! Modified:
18!
19! 16 May 2005
20!
21! Author:
22!
23! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
24! David Kincaid, Fred Krogh.
25! FORTRAN90 version by John Burkardt.
26!
27! Reference:
28!
29! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
30! LINPACK User's Guide,
31! SIAM, 1979,
32! ISBN13: 978-0-898711-72-1,
33! LC: QA214.L56.
34!
35! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
36! Algorithm 539,
37! Basic Linear Algebra Subprograms for Fortran Usage,
38! ACM Transactions on Mathematical Software,
39! Volume 5, Number 3, September 1979, pages 308-323.
40!
41! Parameters:
42!
43! Input, integer ( kind = 4 ) N, the number of elements in DX and DY.
44!
45! Input, real ( kind = 8 ) DA, the multiplier of DX.
46!
47! Input, real ( kind = 8 ) DX(*), the first vector.
48!
49! Input, integer ( kind = 4 ) INCX, the increment between successive
50! entries of DX.
51!
52! Input/output, real ( kind = 8 ) DY(*), the second vector.
53! On output, DY(*) has been replaced by DY(*) + DA * DX(*).
54!
55! Input, integer ( kind = 4 ) INCY, the increment between successive
56! entries of DY.
57!
58 implicit none
59
60 real ( kind = 8 ) da
61 real ( kind = 8 ) dx(*)
62 real ( kind = 8 ) dy(*)
63 integer ( kind = 4 ) i
64 integer ( kind = 4 ) incx
65 integer ( kind = 4 ) incy
66 integer ( kind = 4 ) ix
67 integer ( kind = 4 ) iy
68 integer ( kind = 4 ) m
69 integer ( kind = 4 ) n
70
71 if ( n <= 0 ) then
72 return
73 end if
74
75 if ( da == 0.0d+00 ) then
76 return
77 end if
78!
79! Code for unequal increments or equal increments
80! not equal to 1.
81!
82 if ( incx /= 1 .or. incy /= 1 ) then
83
84 if ( 0 <= incx ) then
85 ix = 1
86 else
87 ix = ( - n + 1 ) * incx + 1
88 end if
89
90 if ( 0 <= incy ) then
91 iy = 1
92 else
93 iy = ( - n + 1 ) * incy + 1
94 end if
95
96 do i = 1, n
97 dy(iy) = dy(iy) + da * dx(ix)
98 ix = ix + incx
99 iy = iy + incy
100 end do
101!
102! Code for both increments equal to 1.
103!
104 else
105
106 m = mod( n, 4 )
107
108 dy(1:m) = dy(1:m) + da * dx(1:m)
109
110 do i = m+1, n, 4
111 dy(i ) = dy(i ) + da * dx(i )
112 dy(i+1) = dy(i+1) + da * dx(i+1)
113 dy(i+2) = dy(i+2) + da * dx(i+2)
114 dy(i+3) = dy(i+3) + da * dx(i+3)
115 end do
116
117 end if
118
119 return
120end
121function ddot ( n, dx, incx, dy, incy )
122
123!*****************************************************************************80
124!
125!! DDOT forms the dot product of two vectors.
126!
127! Discussion:
128!
129! This routine uses double precision real arithmetic.
130!
131! This routine uses unrolled loops for increments equal to one.
132!
133! Licensing:
134!
135! This code is distributed under the GNU LGPL license.
136!
137! Modified:
138!
139! 16 May 2005
140!
141! Author:
142!
143! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
144! David Kincaid, Fred Krogh.
145! FORTRAN90 version by John Burkardt.
146!
147! Reference:
148!
149! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
150! LINPACK User's Guide,
151! SIAM, 1979,
152! ISBN13: 978-0-898711-72-1,
153! LC: QA214.L56.
154!
155! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
156! Algorithm 539,
157! Basic Linear Algebra Subprograms for Fortran Usage,
158! ACM Transactions on Mathematical Software,
159! Volume 5, Number 3, September 1979, pages 308-323.
160!
161! Parameters:
162!
163! Input, integer ( kind = 4 ) N, the number of entries in the vectors.
164!
165! Input, real ( kind = 8 ) DX(*), the first vector.
166!
167! Input, integer ( kind = 4 ) INCX, the increment between successive
168! entries in DX.
169!
170! Input, real ( kind = 8 ) DY(*), the second vector.
171!
172! Input, integer ( kind = 4 ) INCY, the increment between successive
173! entries in DY.
174!
175! Output, real ( kind = 8 ) DDOT, the sum of the product of the
176! corresponding entries of DX and DY.
177!
178 implicit none
179
180 real ( kind = 8 ) ddot
181 real ( kind = 8 ) dtemp
182 real ( kind = 8 ) dx(*)
183 real ( kind = 8 ) dy(*)
184 integer ( kind = 4 ) i
185 integer ( kind = 4 ) incx
186 integer ( kind = 4 ) incy
187 integer ( kind = 4 ) ix
188 integer ( kind = 4 ) iy
189 integer ( kind = 4 ) m
190 integer ( kind = 4 ) n
191
192 ddot = 0.0d+00
193 dtemp = 0.0d+00
194
195 if ( n <= 0 ) then
196 return
197 end if
198!
199! Code for unequal increments or equal increments
200! not equal to 1.
201!
202 if ( incx /= 1 .or. incy /= 1 ) then
203
204 if ( 0 <= incx ) then
205 ix = 1
206 else
207 ix = ( - n + 1 ) * incx + 1
208 end if
209
210 if ( 0 <= incy ) then
211 iy = 1
212 else
213 iy = ( - n + 1 ) * incy + 1
214 end if
215
216 do i = 1, n
217 dtemp = dtemp + dx(ix) * dy(iy)
218 ix = ix + incx
219 iy = iy + incy
220 end do
221!
222! Code for both increments equal to 1.
223!
224 else
225
226 m = mod( n, 5 )
227
228 do i = 1, m
229 dtemp = dtemp + dx(i) * dy(i)
230 end do
231
232 do i = m+1, n, 5
233
234 dtemp = dtemp + dx(i ) * dy(i ) &
235 + dx(i+1) * dy(i+1) &
236 + dx(i+2) * dy(i+2) &
237 + dx(i+3) * dy(i+3) &
238 + dx(i+4) * dy(i+4)
239 end do
240
241 end if
242
243 ddot = dtemp
244
245 return
246end
247function dnrm2 ( n, x, incx )
248
249!*****************************************************************************80
250!
251!! DNRM2 returns the euclidean norm of a vector.
252!
253! Discussion:
254!
255! This routine uses double precision real arithmetic.
256!
257! DNRM2 ( X ) = sqrt ( X' * X )
258!
259! Licensing:
260!
261! This code is distributed under the GNU LGPL license.
262!
263! Modified:
264!
265! 16 May 2005
266!
267! Author:
268!
269! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
270! David Kincaid, Fred Krogh.
271! FORTRAN90 version by John Burkardt.
272!
273! Reference:
274!
275! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
276! LINPACK User's Guide,
277! SIAM, 1979,
278! ISBN13: 978-0-898711-72-1,
279! LC: QA214.L56.
280!
281! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
282! Algorithm 539,
283! Basic Linear Algebra Subprograms for Fortran Usage,
284! ACM Transactions on Mathematical Software,
285! Volume 5, Number 3, September 1979, pages 308-323.
286!
287! Parameters:
288!
289! Input, integer ( kind = 4 ) N, the number of entries in the vector.
290!
291! Input, real ( kind = 8 ) X(*), the vector whose norm is to be computed.
292!
293! Input, integer ( kind = 4 ) INCX, the increment between successive
294! entries of X.
295!
296! Output, real ( kind = 8 ) DNRM2, the Euclidean norm of X.
297!
298 implicit none
299
300 real ( kind = 8 ) absxi
301 real ( kind = 8 ) dnrm2
302 integer ( kind = 4 ) incx
303 integer ( kind = 4 ) ix
304 integer ( kind = 4 ) n
305 real ( kind = 8 ) norm
306 real ( kind = 8 ) scale
307 real ( kind = 8 ) ssq
308 real ( kind = 8 ) x(*)
309
310 if ( n < 1 .or. incx < 1 ) then
311
312 norm = 0.0d+00
313
314 else if ( n == 1 ) then
315
316 norm = abs( x(1) )
317
318 else
319
320 scale = 0.0d+00
321 ssq = 1.0d+00
322
323 do ix = 1, 1 + ( n - 1 ) * incx, incx
324 if ( x(ix) /= 0.0d+00 ) then
325 absxi = abs( x(ix) )
326 if ( scale < absxi ) then
327 ssq = 1.0d+00 + ssq * ( scale / absxi )**2
328 scale = absxi
329 else
330 ssq = ssq + ( absxi / scale )**2
331 end if
332 end if
333 end do
334 norm = scale * sqrt( ssq )
335 end if
336
337 dnrm2 = norm
338
339 return
340end
341subroutine dqrank ( a, lda, m, n, tol, kr, jpvt, qraux, work )
342
343!*****************************************************************************80
344!
345!! DQRANK computes the QR factorization of a rectangular matrix.
346!
347! Discussion:
348!
349! This routine is used in conjunction with sqrlss to solve
350! overdetermined, underdetermined and singular linear systems
351! in a least squares sense.
352!
353! DQRANK uses the LINPACK subroutine DQRDC to compute the QR
354! factorization, with column pivoting, of an M by N matrix A.
355! The numerical rank is determined using the tolerance TOL.
356!
357! Note that on output, ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
358! of the condition number of the matrix of independent columns,
359! and of R. This estimate will be <= 1/TOL.
360!
361! Reference:
362!
363! Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
364! LINPACK User's Guide,
365! SIAM, 1979,
366! ISBN13: 978-0-898711-72-1,
367! LC: QA214.L56.
368!
369! Parameters:
370!
371! Input/output, real ( kind = 8 ) A(LDA,N). On input, the matrix whose
372! decomposition is to be computed. On output, the information from DQRDC.
373! The triangular matrix R of the QR factorization is contained in the
374! upper triangle and information needed to recover the orthogonal
375! matrix Q is stored below the diagonal in A and in the vector QRAUX.
376!
377! Input, integer ( kind = 4 ) LDA, the leading dimension of A, which must
378! be at least M.
379!
380! Input, integer ( kind = 4 ) M, the number of rows of A.
381!
382! Input, integer ( kind = 4 ) N, the number of columns of A.
383!
384! Input, real ( kind = 8 ) TOL, a relative tolerance used to determine the
385! numerical rank. The problem should be scaled so that all the elements
386! of A have roughly the same absolute accuracy, EPS. Then a reasonable
387! value for TOL is roughly EPS divided by the magnitude of the largest
388! element.
389!
390! Output, integer ( kind = 4 ) KR, the numerical rank.
391!
392! Output, integer ( kind = 4 ) JPVT(N), the pivot information from DQRDC.
393! Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
394! independent to within the tolerance TOL and the remaining columns
395! are linearly dependent.
396!
397! Output, real ( kind = 8 ) QRAUX(N), will contain extra information defining
398! the QR factorization.
399!
400! Workspace, real ( kind = 8 ) WORK(N).
401!
402 implicit none
403
404 integer ( kind = 4 ) lda
405 integer ( kind = 4 ) n
406
407 real ( kind = 8 ) a(lda,n)
408 integer ( kind = 4 ) j
409 integer ( kind = 4 ) jpvt(n)
410 integer ( kind = 4 ) k
411 integer ( kind = 4 ) kr
412 integer ( kind = 4 ) m
413 real ( kind = 8 ) qraux(n)
414 real ( kind = 8 ) tol
415 real ( kind = 8 ) work(n)
416
417 jpvt(1:n) = 0
418
419 call dqrdc ( a, lda, m, n, qraux, jpvt, work, 1 )
420
421 kr = 0
422 k = min( m, n )
423
424 do j = 1, k
425 if ( abs( a(j,j) ) <= tol * abs( a(1,1) ) ) then
426 return
427 end if
428 kr = j
429 end do
430
431 return
432end
433subroutine dqrdc ( a, lda, n, p, qraux, jpvt, work, job )
434
435!*****************************************************************************80
436!
437!! DQRDC computes the QR factorization of a real rectangular matrix.
438!
439! Discussion:
440!
441! DQRDC uses Householder transformations.
442!
443! Column pivoting based on the 2-norms of the reduced columns may be
444! performed at the user's option.
445!
446! Reference:
447!
448! Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
449! LINPACK User's Guide,
450! SIAM, 1979,
451! ISBN13: 978-0-898711-72-1,
452! LC: QA214.L56.
453!
454! Parameters:
455!
456! Input/output, real ( kind = 8 ) A(LDA,P). On input, the N by P matrix
457! whose decomposition is to be computed. On output, A contains in
458! its upper triangle the upper triangular matrix R of the QR
459! factorization. Below its diagonal A contains information from
460! which the orthogonal part of the decomposition can be recovered.
461! Note that if pivoting has been requested, the decomposition is not that
462! of the original matrix A but that of A with its columns permuted
463! as described by JPVT.
464!
465! Input, integer ( kind = 4 ) LDA, the leading dimension of the array A.
466! LDA must be at least N.
467!
468! Input, integer ( kind = 4 ) N, the number of rows of the matrix A.
469!
470! Input, integer ( kind = 4 ) P, the number of columns of the matrix A.
471!
472! Output, real ( kind = 8 ) QRAUX(P), contains further information required
473! to recover the orthogonal part of the decomposition.
474!
475! Input/output, integer ( kind = 4 ) JPVT(P). On input, JPVT contains
476! integers that control the selection of the pivot columns. The K-th
477! column A(*,K) of A is placed in one of three classes according to the
478! value of JPVT(K).
479! > 0, then A(K) is an initial column.
480! = 0, then A(K) is a free column.
481! < 0, then A(K) is a final column.
482! Before the decomposition is computed, initial columns are moved to
483! the beginning of the array A and final columns to the end. Both
484! initial and final columns are frozen in place during the computation
485! and only free columns are moved. At the K-th stage of the
486! reduction, if A(*,K) is occupied by a free column it is interchanged
487! with the free column of largest reduced norm. JPVT is not referenced
488! if JOB == 0. On output, JPVT(K) contains the index of the column of the
489! original matrix that has been interchanged into the K-th column, if
490! pivoting was requested.
491!
492! Workspace, real ( kind = 8 ) WORK(P). WORK is not referenced if JOB == 0.
493!
494! Input, integer ( kind = 4 ) JOB, initiates column pivoting.
495! 0, no pivoting is done.
496! nonzero, pivoting is done.
497!
498 implicit none
499
500 integer ( kind = 4 ) lda
501 integer ( kind = 4 ) n
502 integer ( kind = 4 ) p
503
504 real ( kind = 8 ) a(lda,p)
505 integer ( kind = 4 ) jpvt(p)
506 real ( kind = 8 ) qraux(p)
507 real ( kind = 8 ) work(p)
508 integer ( kind = 4 ) j
509 integer ( kind = 4 ) job
510 integer ( kind = 4 ) jp
511 integer ( kind = 4 ) l
512 integer ( kind = 4 ) lup
513 integer ( kind = 4 ) maxj
514 real ( kind = 8 ) maxnrm
515 real ( kind = 8 ) nrmxl
516 integer ( kind = 4 ) pl
517 integer ( kind = 4 ) pu
518 real ( kind = 8 ) ddot
519 real ( kind = 8 ) dnrm2
520 logical swapj
521 real ( kind = 8 ) t
522 real ( kind = 8 ) tt
523
524 pl = 1
525 pu = 0
526!
527! If pivoting is requested, rearrange the columns.
528!
529 if ( job /= 0 ) then
530
531 do j = 1, p
532
533 swapj = 0 < jpvt(j)
534
535 if ( jpvt(j) < 0 ) then
536 jpvt(j) = - j
537 else
538 jpvt(j) = j
539 end if
540
541 if ( swapj ) then
542
543 if ( j /= pl ) then
544 call dswap ( n, a(1,pl), 1, a(1,j), 1 )
545 end if
546
547 jpvt(j) = jpvt(pl)
548 jpvt(pl) = j
549 pl = pl + 1
550
551 end if
552
553 end do
554
555 pu = p
556
557 do j = p, 1, -1
558
559 if ( jpvt(j) < 0 ) then
560
561 jpvt(j) = - jpvt(j)
562
563 if ( j /= pu ) then
564 call dswap ( n, a(1,pu), 1, a(1,j), 1 )
565 jp = jpvt(pu)
566 jpvt(pu) = jpvt(j)
567 jpvt(j) = jp
568 end if
569
570 pu = pu - 1
571
572 end if
573
574 end do
575
576 end if
577!
578! Compute the norms of the free columns.
579!
580 do j = pl, pu
581 qraux(j) = dnrm2( n, a(1,j), 1 )
582 end do
583
584 work(pl:pu) = qraux(pl:pu)
585!
586! Perform the Householder reduction of A.
587!
588 lup = min( n, p )
589
590 do l = 1, lup
591!
592! Bring the column of largest norm into the pivot position.
593!
594 if ( pl <= l .and. l < pu ) then
595
596 maxnrm = 0.0d+00
597 maxj = l
598 do j = l, pu
599 if ( maxnrm < qraux(j) ) then
600 maxnrm = qraux(j)
601 maxj = j
602 end if
603 end do
604
605 if ( maxj /= l ) then
606 call dswap ( n, a(1,l), 1, a(1,maxj), 1 )
607 qraux(maxj) = qraux(l)
608 work(maxj) = work(l)
609 jp = jpvt(maxj)
610 jpvt(maxj) = jpvt(l)
611 jpvt(l) = jp
612 end if
613
614 end if
615!
616! Compute the Householder transformation for column L.
617!
618 qraux(l) = 0.0d+00
619
620 if ( l /= n ) then
621
622 nrmxl = dnrm2( n-l+1, a(l,l), 1 )
623
624 if ( nrmxl /= 0.0d+00 ) then
625
626 if ( a(l,l) /= 0.0d+00 ) then
627 nrmxl = sign( nrmxl, a(l,l) )
628 end if
629
630 call dscal ( n-l+1, 1.0d+00 / nrmxl, a(l,l), 1 )
631 a(l,l) = 1.0d+00 + a(l,l)
632!
633! Apply the transformation to the remaining columns, updating the norms.
634!
635 do j = l + 1, p
636
637 t = - ddot( n-l+1, a(l,l), 1, a(l,j), 1 ) / a(l,l)
638 call daxpy ( n-l+1, t, a(l,l), 1, a(l,j), 1 )
639
640 if ( pl <= j .and. j <= pu ) then
641
642 if ( qraux(j) /= 0.0d+00 ) then
643
644 tt = 1.0d+00 - ( abs( a(l,j) ) / qraux(j) )**2
645 tt = max( tt, 0.0d+00 )
646 t = tt
647 tt = 1.0d+00 + 0.05d+00 * tt * ( qraux(j) / work(j) )**2
648
649 if ( tt /= 1.0d+00 ) then
650 qraux(j) = qraux(j) * sqrt( t )
651 else
652 qraux(j) = dnrm2( n-l, a(l+1,j), 1 )
653 work(j) = qraux(j)
654 end if
655
656 end if
657
658 end if
659
660 end do
661!
662! Save the transformation.
663!
664 qraux(l) = a(l,l)
665 a(l,l) = - nrmxl
666
667 end if
668
669 end if
670
671 end do
672
673 return
674end
675subroutine dqrls ( a, lda, m, n, tol, kr, b, x, rsd, jpvt, qraux, work, &
676 itask, ind )
677
678!*****************************************************************************80
679!
680!! DQRLS factors and solves a linear system in the least squares sense.
681!
682! Discussion:
683!
684! The linear system may be overdetermined, underdetermined or singular.
685! The solution is obtained using a QR factorization of the
686! coefficient matrix.
687!
688! DQRLS can be efficiently used to solve several least squares
689! problems with the same matrix A. The first system is solved
690! with ITASK = 1. The subsequent systems are solved with
691! ITASK = 2, to avoid the recomputation of the matrix factors.
692! The parameters KR, JPVT, and QRAUX must not be modified
693! between calls to DQRLS.
694!
695! DQRLS is used to solve in a least squares sense
696! overdetermined, underdetermined and singular linear systems.
697! The system is A*X approximates B where A is M by N.
698! B is a given M-vector, and X is the N-vector to be computed.
699! A solution X is found which minimimzes the sum of squares (2-norm)
700! of the residual, A*X - B.
701!
702! The numerical rank of A is determined using the tolerance TOL.
703!
704! DQRLS uses the LINPACK subroutine DQRDC to compute the QR
705! factorization, with column pivoting, of an M by N matrix A.
706!
707! Modified:
708!
709! 15 April 2012
710!
711! Reference:
712!
713! David Kahaner, Cleve Moler, Steven Nash,
714! Numerical Methods and Software,
715! Prentice Hall, 1989,
716! ISBN: 0-13-627258-4,
717! LC: TA345.K34.
718!
719! Parameters:
720!
721! Input/output, real ( kind = 8 ) A(LDA,N), an M by N matrix.
722! On input, the matrix whose decomposition is to be computed.
723! In a least squares data fitting problem, A(I,J) is the
724! value of the J-th basis (model) function at the I-th data point.
725! On output, A contains the output from DQRDC. The triangular matrix R
726! of the QR factorization is contained in the upper triangle and
727! information needed to recover the orthogonal matrix Q is stored
728! below the diagonal in A and in the vector QRAUX.
729!
730! Input, integer ( kind = 4 ) LDA, the leading dimension of A.
731!
732! Input, integer ( kind = 4 ) M, the number of rows of A.
733!
734! Input, integer ( kind = 4 ) N, the number of columns of A.
735!
736! Input, real ( kind = 8 ) TOL, a relative tolerance used to determine the
737! numerical rank. The problem should be scaled so that all the elements
738! of A have roughly the same absolute accuracy EPS. Then a reasonable
739! value for TOL is roughly EPS divided by the magnitude of the largest
740! element.
741!
742! Output, integer ( kind = 4 ) KR, the numerical rank.
743!
744! Input, real ( kind = 8 ) B(M), the right hand side of the linear system.
745!
746! Output, real ( kind = 8 ) X(N), a least squares solution to the linear
747! system.
748!
749! Output, real ( kind = 8 ) RSD(M), the residual, B - A*X. RSD may
750! overwrite B.
751!
752! Workspace, integer ( kind = 4 ) JPVT(N), required if ITASK = 1.
753! Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
754! independent to within the tolerance TOL and the remaining columns
755! are linearly dependent. ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
756! of the condition number of the matrix of independent columns,
757! and of R. This estimate will be <= 1/TOL.
758!
759! Workspace, real ( kind = 8 ) QRAUX(N), required if ITASK = 1.
760!
761! Workspace, real ( kind = 8 ) WORK(N), required if ITASK = 1.
762!
763! Input, integer ( kind = 4 ) ITASK.
764! 1, DQRLS factors the matrix A and solves the least squares problem.
765! 2, DQRLS assumes that the matrix A was factored with an earlier
766! call to DQRLS, and only solves the least squares problem.
767!
768! Output, integer ( kind = 4 ) IND, error code.
769! 0: no error
770! -1: LDA < M (fatal error)
771! -2: N < 1 (fatal error)
772! -3: ITASK < 1 (fatal error)
773!
774 implicit none
775
776 integer ( kind = 4 ) lda
777 integer ( kind = 4 ) m
778 integer ( kind = 4 ) n
779
780 real ( kind = 8 ) a(lda,n)
781 real ( kind = 8 ) b(m)
782 integer ( kind = 4 ) ind
783 integer ( kind = 4 ) itask
784 integer ( kind = 4 ) jpvt(n)
785 integer ( kind = 4 ) kr
786 real ( kind = 8 ) qraux(n)
787 real ( kind = 8 ) rsd(m)
788 real ( kind = 8 ) tol
789 real ( kind = 8 ) work(n)
790 real ( kind = 8 ) x(n)
791
792 if ( lda < m ) then
793 write ( *, '(a)' ) ' '
794 write ( *, '(a)' ) 'DQRLS - Fatal error!'
795 write ( *, '(a)' ) ' LDA < M.'
796 stop
797 end if
798
799 if ( n <= 0 ) then
800 write ( *, '(a)' ) ' '
801 write ( *, '(a)' ) 'DQRLS - Fatal error!'
802 write ( *, '(a)' ) ' N <= 0.'
803 stop
804 end if
805
806 if ( itask < 1 ) then
807 write ( *, '(a)' ) ' '
808 write ( *, '(a)' ) 'DQRLS - Fatal error!'
809 write ( *, '(a)' ) ' ITASK < 1.'
810 stop
811 end if
812
813 ind = 0
814!
815! Factor the matrix.
816!
817 if ( itask == 1 ) then
818 call dqrank ( a, lda, m, n, tol, kr, jpvt, qraux, work )
819 end if
820!
821! Solve the least-squares problem.
822!
823 call dqrlss ( a, lda, m, n, kr, b, x, rsd, jpvt, qraux )
824
825 return
826end
827subroutine dqrlss ( a, lda, m, n, kr, b, x, rsd, jpvt, qraux )
828
829!*****************************************************************************80
830!
831!! DQRLSS solves a linear system in a least squares sense.
832!
833! Discussion:
834!
835! DQRLSS must be preceeded by a call to DQRANK.
836!
837! The system is to be solved is
838! A * X = B
839! where
840! A is an M by N matrix with rank KR, as determined by DQRANK,
841! B is a given M-vector,
842! X is the N-vector to be computed.
843!
844! A solution X, with at most KR nonzero components, is found which
845! minimizes the 2-norm of the residual (A*X-B).
846!
847! Once the matrix A has been formed, DQRANK should be
848! called once to decompose it. Then, for each right hand
849! side B, DQRLSS should be called once to obtain the
850! solution and residual.
851!
852! Modified:
853!
854! 15 April 2012
855!
856! Parameters:
857!
858! Input, real ( kind = 8 ) A(LDA,N), the QR factorization information
859! from DQRANK. The triangular matrix R of the QR factorization is
860! contained in the upper triangle and information needed to recover
861! the orthogonal matrix Q is stored below the diagonal in A and in
862! the vector QRAUX.
863!
864! Input, integer ( kind = 4 ) LDA, the leading dimension of A, which must
865! be at least M.
866!
867! Input, integer ( kind = 4 ) M, the number of rows of A.
868!
869! Input, integer ( kind = 4 ) N, the number of columns of A.
870!
871! Input, integer ( kind = 4 ) KR, the rank of the matrix, as estimated
872! by DQRANK.
873!
874! Input, real ( kind = 8 ) B(M), the right hand side of the linear system.
875!
876! Output, real ( kind = 8 ) X(N), a least squares solution to the
877! linear system.
878!
879! Output, real ( kind = 8 ) RSD(M), the residual, B - A*X. RSD may
880! overwite B.
881!
882! Input, integer ( kind = 4 ) JPVT(N), the pivot information from DQRANK.
883! Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
884! independent to within the tolerance TOL and the remaining columns
885! are linearly dependent.
886!
887! Input, real ( kind = 8 ) QRAUX(N), auxiliary information from DQRANK
888! defining the QR factorization.
889!
890 implicit none
891
892 integer ( kind = 4 ) lda
893 integer ( kind = 4 ) m
894 integer ( kind = 4 ) n
895
896 real ( kind = 8 ) a(lda,n)
897 real ( kind = 8 ) b(m)
898 integer ( kind = 4 ) info
899 integer ( kind = 4 ) j
900 integer ( kind = 4 ) jpvt(n)
901 integer ( kind = 4 ) k
902 integer ( kind = 4 ) kr
903 real ( kind = 8 ) qraux(n)
904 real ( kind = 8 ) rsd(m)
905 real ( kind = 8 ) t
906 real ( kind = 8 ) x(n)
907
908 if ( kr /= 0 ) then
909 call dqrsl ( a, lda, m, kr, qraux, b, rsd, rsd, x, rsd, rsd, 110, info )
910 end if
911
912 jpvt(1:n) = - jpvt(1:n)
913
914 x(kr+1:n) = 0.0d+00
915
916 do j = 1, n
917
918 if ( jpvt(j) <= 0 ) then
919
920 k = -jpvt(j)
921 jpvt(j) = k
922
923 do while ( k /= j )
924 t = x(j)
925 x(j) = x(k)
926 x(k) = t
927 jpvt(k) = -jpvt(k)
928 k = jpvt(k)
929 end do
930
931 end if
932
933 end do
934
935 return
936end
937subroutine dqrsl ( a, lda, n, k, qraux, y, qy, qty, b, rsd, ab, job, info )
938
939!*****************************************************************************80
940!
941!! DQRSL computes transformations, projections, and least squares solutions.
942!
943! Discussion:
944!
945! DQRSL requires the output of DQRDC.
946!
947! For K <= min(N,P), let AK be the matrix
948!
949! AK = ( A(JPVT(1)), A(JPVT(2)), ..., A(JPVT(K)) )
950!
951! formed from columns JPVT(1), ..., JPVT(K) of the original
952! N by P matrix A that was input to DQRDC. If no pivoting was
953! done, AK consists of the first K columns of A in their
954! original order. DQRDC produces a factored orthogonal matrix Q
955! and an upper triangular matrix R such that
956!
957! AK = Q * (R)
958! (0)
959!
960! This information is contained in coded form in the arrays
961! A and QRAUX.
962!
963! The parameters QY, QTY, B, RSD, and AB are not referenced
964! if their computation is not requested and in this case
965! can be replaced by dummy variables in the calling program.
966! To save storage, the user may in some cases use the same
967! array for different parameters in the calling sequence. A
968! frequently occuring example is when one wishes to compute
969! any of B, RSD, or AB and does not need Y or QTY. In this
970! case one may identify Y, QTY, and one of B, RSD, or AB, while
971! providing separate arrays for anything else that is to be
972! computed.
973!
974! Thus the calling sequence
975!
976! call dqrsl ( a, lda, n, k, qraux, y, dum, y, b, y, dum, 110, info )
977!
978! will result in the computation of B and RSD, with RSD
979! overwriting Y. More generally, each item in the following
980! list contains groups of permissible identifications for
981! a single calling sequence.
982!
983! 1. (Y,QTY,B) (RSD) (AB) (QY)
984!
985! 2. (Y,QTY,RSD) (B) (AB) (QY)
986!
987! 3. (Y,QTY,AB) (B) (RSD) (QY)
988!
989! 4. (Y,QY) (QTY,B) (RSD) (AB)
990!
991! 5. (Y,QY) (QTY,RSD) (B) (AB)
992!
993! 6. (Y,QY) (QTY,AB) (B) (RSD)
994!
995! In any group the value returned in the array allocated to
996! the group corresponds to the last member of the group.
997!
998! Modified:
999!
1000! 15 April 2012
1001!
1002! Reference:
1003!
1004! Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
1005! LINPACK User's Guide,
1006! SIAM, 1979,
1007! ISBN13: 978-0-898711-72-1,
1008! LC: QA214.L56.
1009!
1010! Parameters:
1011!
1012! Input, real ( kind = 8 ) A(LDA,P), contains the output of DQRDC.
1013!
1014! Input, integer ( kind = 4 ) LDA, the leading dimension of the array A.
1015!
1016! Input, integer ( kind = 4 ) N, the number of rows of the matrix AK. It
1017! must have the same value as N in DQRDC.
1018!
1019! Input, integer ( kind = 4 ) K, the number of columns of the matrix AK. K
1020! must not be greater than min(N,P), where P is the same as in the
1021! calling sequence to DQRDC.
1022!
1023! Input, real ( kind = 8 ) QRAUX(P), the auxiliary output from DQRDC.
1024!
1025! Input, real ( kind = 8 ) Y(N), a vector to be manipulated by DQRSL.
1026!
1027! Output, real ( kind = 8 ) QY(N), contains Q * Y, if requested.
1028!
1029! Output, real ( kind = 8 ) QTY(N), contains Q' * Y, if requested.
1030!
1031! Output, real ( kind = 8 ) B(K), the solution of the least squares problem
1032! minimize norm2 ( Y - AK * B),
1033! if its computation has been requested. Note that if pivoting was
1034! requested in DQRDC, the J-th component of B will be associated with
1035! column JPVT(J) of the original matrix A that was input into DQRDC.
1036!
1037! Output, real ( kind = 8 ) RSD(N), the least squares residual Y - AK * B,
1038! if its computation has been requested. RSD is also the orthogonal
1039! projection of Y onto the orthogonal complement of the column space
1040! of AK.
1041!
1042! Output, real ( kind = 8 ) AB(N), the least squares approximation Ak * B,
1043! if its computation has been requested. AB is also the orthogonal
1044! projection of Y onto the column space of A.
1045!
1046! Input, integer ( kind = 4 ) JOB, specifies what is to be computed. JOB has
1047! the decimal expansion ABCDE, with the following meaning:
1048!
1049! if A /= 0, compute QY.
1050! if B /= 0, compute QTY.
1051! if C /= 0, compute QTY and B.
1052! if D /= 0, compute QTY and RSD.
1053! if E /= 0, compute QTY and AB.
1054!
1055! Note that a request to compute B, RSD, or AB automatically triggers
1056! the computation of QTY, for which an array must be provided in the
1057! calling sequence.
1058!
1059! Output, integer ( kind = 4 ) INFO, is zero unless the computation of B has
1060! been requested and R is exactly singular. In this case, INFO is the
1061! index of the first zero diagonal element of R, and B is left unaltered.
1062!
1063 implicit none
1064
1065 integer ( kind = 4 ) k
1066 integer ( kind = 4 ) lda
1067 integer ( kind = 4 ) n
1068
1069 real ( kind = 8 ) a(lda,*)
1070 real ( kind = 8 ) ab(n)
1071 real ( kind = 8 ) b(k)
1072 logical cab
1073 logical cb
1074 logical cqty
1075 logical cqy
1076 logical cr
1077 integer ( kind = 4 ) info
1078 integer ( kind = 4 ) j
1079 integer ( kind = 4 ) jj
1080 integer ( kind = 4 ) job
1081 integer ( kind = 4 ) ju
1082 integer ( kind = 4 ) kp1
1083 real ( kind = 8 ) qraux(*)
1084 real ( kind = 8 ) qty(n)
1085 real ( kind = 8 ) qy(n)
1086 real ( kind = 8 ) rsd(n)
1087 real ( kind = 8 ) ddot
1088 real ( kind = 8 ) t
1089 real ( kind = 8 ) temp
1090 real ( kind = 8 ) y(n)
1091!
1092! set info flag.
1093!
1094 info = 0
1095!
1096! Determine what is to be computed.
1097!
1098 cqy = job / 10000 /= 0
1099 cqty = mod( job, 10000 ) /= 0
1100 cb = mod( job, 1000 ) / 100 /= 0
1101 cr = mod( job, 100 ) / 10 /= 0
1102 cab = mod( job, 10 ) /= 0
1103
1104 ju = min( k, n-1 )
1105!
1106! Special action when N = 1.
1107!
1108 if ( ju == 0 ) then
1109
1110 if ( cqy ) then
1111 qy(1) = y(1)
1112 end if
1113
1114 if ( cqty ) then
1115 qty(1) = y(1)
1116 end if
1117
1118 if ( cab ) then
1119 ab(1) = y(1)
1120 end if
1121
1122 if ( cb ) then
1123
1124 if ( a(1,1) == 0.0d+00 ) then
1125 info = 1
1126 else
1127 b(1) = y(1) / a(1,1)
1128 end if
1129
1130 end if
1131
1132 if ( cr ) then
1133 rsd(1) = 0.0d+00
1134 end if
1135
1136 return
1137
1138 end if
1139!
1140! Set up to compute QY or QTY.
1141!
1142 if ( cqy ) then
1143 qy(1:n) = y(1:n)
1144 end if
1145
1146 if ( cqty ) then
1147 qty(1:n) = y(1:n)
1148 end if
1149!
1150! Compute QY.
1151!
1152 if ( cqy ) then
1153
1154 do jj = 1, ju
1155
1156 j = ju - jj + 1
1157
1158 if ( qraux(j) /= 0.0d+00 ) then
1159 temp = a(j,j)
1160 a(j,j) = qraux(j)
1161 t = - ddot( n-j+1, a(j,j), 1, qy(j), 1 ) / a(j,j)
1162 call daxpy ( n-j+1, t, a(j,j), 1, qy(j), 1 )
1163 a(j,j) = temp
1164 end if
1165
1166 end do
1167
1168 end if
1169!
1170! Compute Q'*Y.
1171!
1172 if ( cqty ) then
1173
1174 do j = 1, ju
1175 if ( qraux(j) /= 0.0d+00 ) then
1176 temp = a(j,j)
1177 a(j,j) = qraux(j)
1178 t = - ddot( n-j+1, a(j,j), 1, qty(j), 1 ) / a(j,j)
1179 call daxpy ( n-j+1, t, a(j,j), 1, qty(j), 1 )
1180 a(j,j) = temp
1181 end if
1182 end do
1183
1184 end if
1185!
1186! Set up to compute B, RSD, or AB.
1187!
1188 if ( cb ) then
1189 b(1:k) = qty(1:k)
1190 end if
1191
1192 kp1 = k + 1
1193
1194 if ( cab ) then
1195 ab(1:k) = qty(1:k)
1196 end if
1197
1198 if ( cr .and. k < n ) then
1199 rsd(k+1:n) = qty(k+1:n)
1200 end if
1201
1202 if ( cab .and. k+1 <= n ) then
1203 ab(k+1:n) = 0.0d+00
1204 end if
1205
1206 if ( cr ) then
1207 rsd(1:k) = 0.0d+00
1208 end if
1209!
1210! Compute B.
1211!
1212 if ( cb ) then
1213
1214 do jj = 1, k
1215
1216 j = k - jj + 1
1217
1218 if ( a(j,j) == 0.0d+00 ) then
1219 info = j
1220 exit
1221 end if
1222
1223 b(j) = b(j)/a(j,j)
1224
1225 if ( j /= 1 ) then
1226 t = -b(j)
1227 call daxpy ( j-1, t, a(1,j), 1, b, 1 )
1228 end if
1229
1230 end do
1231
1232 end if
1233
1234 if ( cr .or. cab ) then
1235!
1236! Compute RSD or AB as required.
1237!
1238 do jj = 1, ju
1239
1240 j = ju - jj + 1
1241
1242 if ( qraux(j) /= 0.0d+00 ) then
1243
1244 temp = a(j,j)
1245 a(j,j) = qraux(j)
1246
1247 if ( cr ) then
1248 t = - ddot( n-j+1, a(j,j), 1, rsd(j), 1 ) / a(j,j)
1249 call daxpy ( n-j+1, t, a(j,j), 1, rsd(j), 1 )
1250 end if
1251
1252 if ( cab ) then
1253 t = - ddot( n-j+1, a(j,j), 1, ab(j), 1 ) / a(j,j)
1254 call daxpy ( n-j+1, t, a(j,j), 1, ab(j), 1 )
1255 end if
1256
1257 a(j,j) = temp
1258
1259 end if
1260
1261 end do
1262
1263 end if
1264
1265 return
1266end
1267subroutine drot ( n, x, incx, y, incy, c, s )
1268
1269!*****************************************************************************80
1270!
1271!! DROT applies a plane rotation.
1272!
1273! Discussion:
1274!
1275! This routine uses double precision real arithmetic.
1276!
1277! Licensing:
1278!
1279! This code is distributed under the GNU LGPL license.
1280!
1281! Modified:
1282!
1283! 08 April 1999
1284!
1285! Author:
1286!
1287! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
1288! David Kincaid, Fred Krogh.
1289! FORTRAN90 version by John Burkardt.
1290!
1291! Reference:
1292!
1293! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
1294! LINPACK User's Guide,
1295! SIAM, 1979,
1296! ISBN13: 978-0-898711-72-1,
1297! LC: QA214.L56.
1298!
1299! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
1300! Algorithm 539,
1301! Basic Linear Algebra Subprograms for Fortran Usage,
1302! ACM Transactions on Mathematical Software,
1303! Volume 5, Number 3, September 1979, pages 308-323.
1304!
1305! Parameters:
1306!
1307! Input, integer ( kind = 4 ) N, the number of entries in the vectors.
1308!
1309! Input/output, real ( kind = 8 ) X(*), one of the vectors to be rotated.
1310!
1311! Input, integer ( kind = 4 ) INCX, the increment between successive
1312! entries of X.
1313!
1314! Input/output, real ( kind = 8 ) Y(*), one of the vectors to be rotated.
1315!
1316! Input, integer ( kind = 4 ) INCY, the increment between successive
1317! elements of Y.
1318!
1319! Input, real ( kind = 8 ) C, S, parameters (presumably the cosine and
1320! sine of some angle) that define a plane rotation.
1321!
1322 implicit none
1323
1324 real ( kind = 8 ) c
1325 integer ( kind = 4 ) i
1326 integer ( kind = 4 ) incx
1327 integer ( kind = 4 ) incy
1328 integer ( kind = 4 ) ix
1329 integer ( kind = 4 ) iy
1330 integer ( kind = 4 ) n
1331 real ( kind = 8 ) s
1332 real ( kind = 8 ) stemp
1333 real ( kind = 8 ) x(*)
1334 real ( kind = 8 ) y(*)
1335
1336 if ( n <= 0 ) then
1337
1338 else if ( incx == 1 .and. incy == 1 ) then
1339
1340 do i = 1, n
1341 stemp = c * x(i) + s * y(i)
1342 y(i) = c * y(i) - s * x(i)
1343 x(i) = stemp
1344 end do
1345
1346 else
1347
1348 if ( 0 <= incx ) then
1349 ix = 1
1350 else
1351 ix = ( - n + 1 ) * incx + 1
1352 end if
1353
1354 if ( 0 <= incy ) then
1355 iy = 1
1356 else
1357 iy = ( - n + 1 ) * incy + 1
1358 end if
1359
1360 do i = 1, n
1361 stemp = c * x(ix) + s * y(iy)
1362 y(iy) = c * y(iy) - s * x(ix)
1363 x(ix) = stemp
1364 ix = ix + incx
1365 iy = iy + incy
1366 end do
1367
1368 end if
1369
1370 return
1371end
1372subroutine drotg ( sa, sb, c, s )
1373
1374!*****************************************************************************80
1375!
1376!! DROTG constructs a Givens plane rotation.
1377!
1378! Discussion:
1379!
1380! This routine uses double precision real arithmetic.
1381!
1382! Given values A and B, this routine computes
1383!
1384! SIGMA = sign ( A ) if abs ( A ) > abs ( B )
1385! = sign ( B ) if abs ( A ) <= abs ( B );
1386!
1387! R = SIGMA * ( A * A + B * B );
1388!
1389! C = A / R if R is not 0
1390! = 1 if R is 0;
1391!
1392! S = B / R if R is not 0,
1393! 0 if R is 0.
1394!
1395! The computed numbers then satisfy the equation
1396!
1397! ( C S ) ( A ) = ( R )
1398! ( -S C ) ( B ) = ( 0 )
1399!
1400! The routine also computes
1401!
1402! Z = S if abs ( A ) > abs ( B ),
1403! = 1 / C if abs ( A ) <= abs ( B ) and C is not 0,
1404! = 1 if C is 0.
1405!
1406! The single value Z encodes C and S, and hence the rotation:
1407!
1408! If Z = 1, set C = 0 and S = 1;
1409! If abs ( Z ) < 1, set C = sqrt ( 1 - Z * Z ) and S = Z;
1410! if abs ( Z ) > 1, set C = 1/ Z and S = sqrt ( 1 - C * C );
1411!
1412! Licensing:
1413!
1414! This code is distributed under the GNU LGPL license.
1415!
1416! Modified:
1417!
1418! 15 May 2006
1419!
1420! Author:
1421!
1422! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
1423! David Kincaid, Fred Krogh.
1424! FORTRAN90 version by John Burkardt.
1425!
1426! Reference:
1427!
1428! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
1429! LINPACK User's Guide,
1430! SIAM, 1979,
1431! ISBN13: 978-0-898711-72-1,
1432! LC: QA214.L56.
1433!
1434! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
1435! Algorithm 539,
1436! Basic Linear Algebra Subprograms for Fortran Usage,
1437! ACM Transactions on Mathematical Software,
1438! Volume 5, Number 3, September 1979, pages 308-323.
1439!
1440! Parameters:
1441!
1442! Input/output, real ( kind = 8 ) SA, SB. On input, SA and SB are the values
1443! A and B. On output, SA is overwritten with R, and SB is
1444! overwritten with Z.
1445!
1446! Output, real ( kind = 8 ) C, S, the cosine and sine of the
1447! Givens rotation.
1448!
1449 implicit none
1450
1451 real ( kind = 8 ) c
1452 real ( kind = 8 ) r
1453 real ( kind = 8 ) roe
1454 real ( kind = 8 ) s
1455 real ( kind = 8 ) sa
1456 real ( kind = 8 ) sb
1457 real ( kind = 8 ) scale
1458 real ( kind = 8 ) z
1459
1460 if ( abs( sb ) < abs( sa ) ) then
1461 roe = sa
1462 else
1463 roe = sb
1464 end if
1465
1466 scale = abs( sa ) + abs( sb )
1467
1468 if ( scale == 0.0d+00 ) then
1469 c = 1.0d+00
1470 s = 0.0d+00
1471 r = 0.0d+00
1472 else
1473 r = scale * sqrt( ( sa / scale )**2 + ( sb / scale )**2 )
1474 r = sign( 1.0d+00, roe ) * r
1475 c = sa / r
1476 s = sb / r
1477 end if
1478
1479 if ( 0.0d+00 < abs( c ) .and. abs( c ) <= s ) then
1480 z = 1.0d+00 / c
1481 else
1482 z = s
1483 end if
1484
1485 sa = r
1486 sb = z
1487
1488 return
1489end
1490subroutine dscal ( n, sa, x, incx )
1491
1492!*****************************************************************************80
1493!
1494!! DSCAL scales a vector by a constant.
1495!
1496! Discussion:
1497!
1498! This routine uses double precision real arithmetic.
1499!
1500! Licensing:
1501!
1502! This code is distributed under the GNU LGPL license.
1503!
1504! Modified:
1505!
1506! 08 April 1999
1507!
1508! Author:
1509!
1510! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
1511! David Kincaid, Fred Krogh.
1512! FORTRAN90 version by John Burkardt.
1513!
1514! Reference:
1515!
1516! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
1517! LINPACK User's Guide,
1518! SIAM, 1979,
1519! ISBN13: 978-0-898711-72-1,
1520! LC: QA214.L56.
1521!
1522! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
1523! Algorithm 539,
1524! Basic Linear Algebra Subprograms for Fortran Usage,
1525! ACM Transactions on Mathematical Software,
1526! Volume 5, Number 3, September 1979, pages 308-323.
1527!
1528! Parameters:
1529!
1530! Input, integer ( kind = 4 ) N, the number of entries in the vector.
1531!
1532! Input, real ( kind = 8 ) SA, the multiplier.
1533!
1534! Input/output, real ( kind = 8 ) X(*), the vector to be scaled.
1535!
1536! Input, integer ( kind = 4 ) INCX, the increment between successive
1537! entries of X.
1538!
1539 implicit none
1540
1541 integer ( kind = 4 ) i
1542 integer ( kind = 4 ) incx
1543 integer ( kind = 4 ) ix
1544 integer ( kind = 4 ) m
1545 integer ( kind = 4 ) n
1546 real ( kind = 8 ) sa
1547 real ( kind = 8 ) x(*)
1548
1549 if ( n <= 0 ) then
1550
1551 else if ( incx == 1 ) then
1552
1553 m = mod( n, 5 )
1554
1555 x(1:m) = sa * x(1:m)
1556
1557 do i = m+1, n, 5
1558 x(i) = sa * x(i)
1559 x(i+1) = sa * x(i+1)
1560 x(i+2) = sa * x(i+2)
1561 x(i+3) = sa * x(i+3)
1562 x(i+4) = sa * x(i+4)
1563 end do
1564
1565 else
1566
1567 if ( 0 <= incx ) then
1568 ix = 1
1569 else
1570 ix = ( - n + 1 ) * incx + 1
1571 end if
1572
1573 do i = 1, n
1574 x(ix) = sa * x(ix)
1575 ix = ix + incx
1576 end do
1577
1578 end if
1579
1580 return
1581end
1582subroutine dsvdc ( a, lda, m, n, s, e, u, ldu, v, ldv, work, job, info )
1583
1584!*****************************************************************************80
1585!
1586!! DSVDC computes the singular value decomposition of a real rectangular matrix.
1587!
1588! Discussion:
1589!
1590! This routine reduces an M by N matrix A to diagonal form by orthogonal
1591! transformations U and V. The diagonal elements S(I) are the singular
1592! values of A. The columns of U are the corresponding left singular
1593! vectors, and the columns of V the right singular vectors.
1594!
1595! The form of the singular value decomposition is then
1596!
1597! A(MxN) = U(MxM) * S(MxN) * V(NxN)'
1598!
1599! Licensing:
1600!
1601! This code is distributed under the GNU LGPL license.
1602!
1603! Modified:
1604!
1605! 16 September 2006
1606!
1607! Author:
1608!
1609! FORTRAN90 version by John Burkardt.
1610!
1611! Reference:
1612!
1613! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
1614! LINPACK User's Guide,
1615! SIAM, 1979,
1616! ISBN13: 978-0-898711-72-1,
1617! LC: QA214.L56.
1618!
1619! Parameters:
1620!
1621! Input/output, real ( kind = 8 ) A(LDA,N). On input, the M by N
1622! matrix whose singular value decomposition is to be computed.
1623! On output, the matrix has been destroyed. Depending on the user's
1624! requests, the matrix may contain other useful information.
1625!
1626! Input, integer ( kind = 4 ) LDA, the leading dimension of the array A.
1627! LDA must be at least N.
1628!
1629! Input, integer ( kind = 4 ) M, the number of rows of the matrix.
1630!
1631! Input, integer ( kind = 4 ) N, the number of columns of the matrix A.
1632!
1633! Output, real ( kind = 8 ) S(MM), where MM = max(M+1,N). The first
1634! min(M,N) entries of S contain the singular values of A arranged in
1635! descending order of magnitude.
1636!
1637! Output, real ( kind = 8 ) E(MM), where MM = max(M+1,N). Ordinarily
1638! contains zeros. However see the discussion of INFO for exceptions.
1639!
1640! Output, real ( kind = 8 ) U(LDU,K). If JOBA = 1 then K = M;
1641! if 2 <= JOBA, then K = min(M,N). U contains the M by M matrix of
1642! left singular vectors. U is not referenced if JOBA = 0. If M <= N
1643! or if JOBA = 2, then U may be identified with A in the subroutine call.
1644!
1645! Input, integer ( kind = 4 ) LDU, the leading dimension of the array U.
1646! LDU must be at least M.
1647!
1648! Output, real ( kind = 8 ) V(LDV,N), the N by N matrix of right singular
1649! vectors. V is not referenced if JOB is 0. If N <= M, then V may be
1650! identified with A in the subroutine call.
1651!
1652! Input, integer ( kind = 4 ) LDV, the leading dimension of the array V.
1653! LDV must be at least N.
1654!
1655! Workspace, real ( kind = 8 ) WORK(M).
1656!
1657! Input, integer ( kind = 4 ) JOB, controls the computation of the singular
1658! vectors. It has the decimal expansion AB with the following meaning:
1659! A = 0, do not compute the left singular vectors.
1660! A = 1, return the M left singular vectors in U.
1661! A >= 2, return the first min(M,N) singular vectors in U.
1662! B = 0, do not compute the right singular vectors.
1663! B = 1, return the right singular vectors in V.
1664!
1665! Output, integer ( kind = 4 ) INFO, status indicator.
1666! The singular values (and their corresponding singular vectors)
1667! S(INFO+1), S(INFO+2),...,S(MN) are correct. Here MN = min ( M, N ).
1668! Thus if INFO is 0, all the singular values and their vectors are
1669! correct. In any event, the matrix B = U' * A * V is the bidiagonal
1670! matrix with the elements of S on its diagonal and the elements of E on
1671! its superdiagonal. Thus the singular values of A and B are the same.
1672!
1673 implicit none
1674
1675 integer ( kind = 4 ) lda
1676 integer ( kind = 4 ) ldu
1677 integer ( kind = 4 ) ldv
1678 integer ( kind = 4 ) m
1679 integer ( kind = 4 ) n
1680
1681 real ( kind = 8 ) a(lda,n)
1682 real ( kind = 8 ) b
1683 real ( kind = 8 ) c
1684 real ( kind = 8 ) cs
1685 real ( kind = 8 ) e(*)
1686 real ( kind = 8 ) el
1687 real ( kind = 8 ) emm1
1688 real ( kind = 8 ) f
1689 real ( kind = 8 ) g
1690 integer ( kind = 4 ) info
1691 integer ( kind = 4 ) iter
1692 integer ( kind = 4 ) j
1693 integer ( kind = 4 ) job
1694 integer ( kind = 4 ) jobu
1695 integer ( kind = 4 ) k
1696 integer ( kind = 4 ) kase
1697 integer ( kind = 4 ) kk
1698 integer ( kind = 4 ) l
1699 integer ( kind = 4 ) ll
1700 integer ( kind = 4 ) lls
1701 integer ( kind = 4 ) ls
1702 integer ( kind = 4 ) lu
1703 integer ( kind = 4 ), parameter :: maxit = 30
1704 integer ( kind = 4 ) mm
1705 integer ( kind = 4 ) mm1
1706 integer ( kind = 4 ) mn
1707 integer ( kind = 4 ) nct
1708 integer ( kind = 4 ) nctp1
1709 integer ( kind = 4 ) ncu
1710 integer ( kind = 4 ) nrt
1711 integer ( kind = 4 ) nrtp1
1712 real ( kind = 8 ) s(*)
1713 real ( kind = 8 ) scale
1714 real ( kind = 8 ) ddot
1715 real ( kind = 8 ) shift
1716 real ( kind = 8 ) sl
1717 real ( kind = 8 ) sm
1718 real ( kind = 8 ) smm1
1719 real ( kind = 8 ) sn
1720 real ( kind = 8 ) dnrm2
1721 real ( kind = 8 ) t
1722 real ( kind = 8 ) t1
1723 real ( kind = 8 ) test
1724 real ( kind = 8 ) u(ldu,m)
1725 real ( kind = 8 ) v(ldv,n)
1726 logical wantu
1727 logical wantv
1728 real ( kind = 8 ) work(m)
1729 real ( kind = 8 ) ztest
1730!
1731! Determine what is to be computed.
1732!
1733 wantu = .false.
1734 wantv = .false.
1735 jobu = mod( job, 100 ) / 10
1736
1737 if ( 1 < jobu ) then
1738 ncu = min( m, n )
1739 else
1740 ncu = m
1741 end if
1742
1743 if ( jobu /= 0 ) then
1744 wantu = .true.
1745 end if
1746
1747 if ( mod( job, 10 ) /= 0 ) then
1748 wantv = .true.
1749 end if
1750!
1751! Reduce A to bidiagonal form, storing the diagonal elements
1752! in S and the super-diagonal elements in E.
1753!
1754 info = 0
1755 nct = min( m-1, n )
1756 nrt = max( 0, min( m, n-2 ) )
1757 lu = max( nct, nrt )
1758
1759 do l = 1, lu
1760!
1761! Compute the transformation for the L-th column and
1762! place the L-th diagonal in S(L).
1763!
1764 if ( l <= nct ) then
1765
1766 s(l) = dnrm2( m-l+1, a(l,l), 1 )
1767
1768 if ( s(l) /= 0.0d+00 ) then
1769 if ( a(l,l) /= 0.0d+00 ) then
1770 s(l) = sign( s(l), a(l,l) )
1771 end if
1772 call dscal ( m-l+1, 1.0d+00 / s(l), a(l,l), 1 )
1773 a(l,l) = 1.0d+00 + a(l,l)
1774 end if
1775
1776 s(l) = -s(l)
1777
1778 end if
1779
1780 do j = l+1, n
1781!
1782! Apply the transformation.
1783!
1784 if ( l <= nct .and. s(l) /= 0.0d+00 ) then
1785 t = -ddot( m-l+1, a(l,l), 1, a(l,j), 1 ) / a(l,l)
1786 call daxpy ( m-l+1, t, a(l,l), 1, a(l,j), 1 )
1787 end if
1788!
1789! Place the L-th row of A into E for the
1790! subsequent calculation of the row transformation.
1791!
1792 e(j) = a(l,j)
1793
1794 end do
1795!
1796! Place the transformation in U for subsequent back multiplication.
1797!
1798 if ( wantu .and. l <= nct ) then
1799 u(l:m,l) = a(l:m,l)
1800 end if
1801!
1802! Compute the L-th row transformation and place the
1803! L-th superdiagonal in E(L).
1804!
1805 if ( l <= nrt ) then
1806
1807 e(l) = dnrm2( n-l, e(l+1), 1 )
1808
1809 if ( e(l) /= 0.0d+00 ) then
1810 if ( e(l+1) /= 0.0d+00 ) then
1811 e(l) = sign( e(l), e(l+1) )
1812 end if
1813 call dscal ( n-l, 1.0d+00 / e(l), e(l+1), 1 )
1814 e(l+1) = 1.0d+00 + e(l+1)
1815 end if
1816
1817 e(l) = -e(l)
1818!
1819! Apply the transformation.
1820!
1821 if ( l + 1 <= m .and. e(l) /= 0.0d+00 ) then
1822
1823 work(l+1:m) = 0.0d+00
1824
1825 do j = l+1, n
1826 call daxpy ( m-l, e(j), a(l+1,j), 1, work(l+1), 1 )
1827 end do
1828
1829 do j = l+1, n
1830 call daxpy ( m-l, -e(j)/e(l+1), work(l+1), 1, a(l+1,j), 1 )
1831 end do
1832
1833 end if
1834!
1835! Place the transformation in V for subsequent back multiplication.
1836!
1837 if ( wantv ) then
1838 v(l+1:n,l) = e(l+1:n)
1839 end if
1840
1841 end if
1842
1843 end do
1844!
1845! Set up the final bidiagonal matrix of order MN.
1846!
1847 mn = min( m + 1, n )
1848 nctp1 = nct + 1
1849 nrtp1 = nrt + 1
1850
1851 if ( nct < n ) then
1852 s(nctp1) = a(nctp1,nctp1)
1853 end if
1854
1855 if ( m < mn ) then
1856 s(mn) = 0.0d+00
1857 end if
1858
1859 if ( nrtp1 < mn ) then
1860 e(nrtp1) = a(nrtp1,mn)
1861 end if
1862
1863 e(mn) = 0.0d+00
1864!
1865! If required, generate U.
1866!
1867 if ( wantu ) then
1868
1869 u(1:m,nctp1:ncu) = 0.0d+00
1870
1871 do j = nctp1, ncu
1872 u(j,j) = 1.0d+00
1873 end do
1874
1875 do ll = 1, nct
1876
1877 l = nct - ll + 1
1878
1879 if ( s(l) /= 0.0d+00 ) then
1880
1881 do j = l+1, ncu
1882 t = -ddot( m-l+1, u(l,l), 1, u(l,j), 1 ) / u(l,l)
1883 call daxpy ( m-l+1, t, u(l,l), 1, u(l,j), 1 )
1884 end do
1885
1886 u(l:m,l) = -u(l:m,l)
1887 u(l,l) = 1.0d+00 + u(l,l)
1888 u(1:l-1,l) = 0.0d+00
1889
1890 else
1891
1892 u(1:m,l) = 0.0d+00
1893 u(l,l) = 1.0d+00
1894
1895 end if
1896
1897 end do
1898
1899 end if
1900!
1901! If it is required, generate V.
1902!
1903 if ( wantv ) then
1904
1905 do ll = 1, n
1906
1907 l = n - ll + 1
1908
1909 if ( l <= nrt .and. e(l) /= 0.0d+00 ) then
1910
1911 do j = l + 1, n
1912 t = -ddot( n-l, v(l+1,l), 1, v(l+1,j), 1 ) / v(l+1,l)
1913 call daxpy ( n-l, t, v(l+1,l), 1, v(l+1,j), 1 )
1914 end do
1915
1916 end if
1917
1918 v(1:n,l) = 0.0d+00
1919 v(l,l) = 1.0d+00
1920
1921 end do
1922
1923 end if
1924!
1925! Main iteration loop for the singular values.
1926!
1927 mm = mn
1928 iter = 0
1929
1930 do while ( 0 < mn )
1931!
1932! If too many iterations have been performed, set flag and return.
1933!
1934 if ( maxit <= iter ) then
1935 info = mn
1936 return
1937 end if
1938!
1939! This section of the program inspects for
1940! negligible elements in the S and E arrays.
1941!
1942! On completion the variables KASE and L are set as follows:
1943!
1944! KASE = 1 if S(MN) and E(L-1) are negligible and L < MN
1945! KASE = 2 if S(L) is negligible and L < MN
1946! KASE = 3 if E(L-1) is negligible, L < MN, and
1947! S(L), ..., S(MN) are not negligible (QR step).
1948! KASE = 4 if E(MN-1) is negligible (convergence).
1949!
1950 do ll = 1, mn
1951
1952 l = mn - ll
1953
1954 if ( l == 0 ) then
1955 exit
1956 end if
1957
1958 test = abs( s(l) ) + abs( s(l+1) )
1959 ztest = test + abs( e(l) )
1960
1961 if ( ztest == test ) then
1962 e(l) = 0.0d+00
1963 exit
1964 end if
1965
1966 end do
1967
1968 if ( l == mn - 1 ) then
1969
1970 kase = 4
1971
1972 else
1973
1974 do lls = l + 1, mn + 1
1975
1976 ls = mn - lls + l + 1
1977
1978 if ( ls == l ) then
1979 exit
1980 end if
1981
1982 test = 0.0d+00
1983 if ( ls /= mn ) then
1984 test = test + abs( e(ls) )
1985 end if
1986
1987 if ( ls /= l + 1 ) then
1988 test = test + abs( e(ls-1) )
1989 end if
1990
1991 ztest = test + abs( s(ls) )
1992
1993 if ( ztest == test ) then
1994 s(ls) = 0.0d+00
1995 exit
1996 end if
1997
1998 end do
1999
2000 if ( ls == l ) then
2001 kase = 3
2002 else if ( ls == mn ) then
2003 kase = 1
2004 else
2005 kase = 2
2006 l = ls
2007 end if
2008
2009 end if
2010
2011 l = l + 1
2012!
2013! Deflate negligible S(MN).
2014!
2015 if ( kase == 1 ) then
2016
2017 mm1 = mn - 1
2018 f = e(mn-1)
2019 e(mn-1) = 0.0d+00
2020
2021 do kk = l, mm1
2022
2023 k = mm1 - kk + l
2024 t1 = s(k)
2025 call drotg ( t1, f, cs, sn )
2026 s(k) = t1
2027
2028 if ( k /= l ) then
2029 f = -sn * e(k-1)
2030 e(k-1) = cs * e(k-1)
2031 end if
2032
2033 if ( wantv ) then
2034 call drot ( n, v(1,k), 1, v(1,mn), 1, cs, sn )
2035 end if
2036
2037 end do
2038!
2039! Split at negligible S(L).
2040!
2041 else if ( kase == 2 ) then
2042
2043 f = e(l-1)
2044 e(l-1) = 0.0d+00
2045
2046 do k = l, mn
2047
2048 t1 = s(k)
2049 call drotg ( t1, f, cs, sn )
2050 s(k) = t1
2051 f = -sn * e(k)
2052 e(k) = cs * e(k)
2053 if ( wantu ) then
2054 call drot ( m, u(1,k), 1, u(1,l-1), 1, cs, sn )
2055 end if
2056
2057 end do
2058!
2059! Perform one QR step.
2060!
2061 else if ( kase == 3 ) then
2062!
2063! Calculate the shift.
2064!
2065 scale = max( abs( s(mn) ), abs( s(mn-1) ), abs( e(mn-1) ), &
2066 abs( s(l) ), abs( e(l) ) )
2067
2068 sm = s(mn) / scale
2069 smm1 = s(mn-1) / scale
2070 emm1 = e(mn-1) / scale
2071 sl = s(l) / scale
2072 el = e(l) / scale
2073 b = ( ( smm1 + sm ) * ( smm1 - sm ) + emm1 * emm1 ) / 2.0d+00
2074 c = sm * sm * emm1 * emm1
2075 shift = 0.0d+00
2076
2077 if ( b /= 0.0d+00 .or. c /= 0.0d+00 ) then
2078 shift = sqrt( b * b + c )
2079 if ( b < 0.0d+00 ) then
2080 shift = -shift
2081 end if
2082 shift = c / ( b + shift )
2083 end if
2084
2085 f = ( sl + sm ) * ( sl - sm ) + shift
2086 g = sl * el
2087!
2088! Chase zeros.
2089!
2090 mm1 = mn - 1
2091
2092 do k = l, mm1
2093
2094 call drotg ( f, g, cs, sn )
2095
2096 if ( k /= l ) then
2097 e(k-1) = f
2098 end if
2099
2100 f = cs * s(k) + sn * e(k)
2101 e(k) = cs * e(k) - sn * s(k)
2102 g = sn * s(k+1)
2103 s(k+1) = cs * s(k+1)
2104
2105 if ( wantv ) then
2106 call drot ( n, v(1,k), 1, v(1,k+1), 1, cs, sn )
2107 end if
2108
2109 call drotg ( f, g, cs, sn )
2110 s(k) = f
2111 f = cs * e(k) + sn * s(k+1)
2112 s(k+1) = -sn * e(k) + cs * s(k+1)
2113 g = sn * e(k+1)
2114 e(k+1) = cs * e(k+1)
2115
2116 if ( wantu .and. k < m ) then
2117 call drot ( m, u(1,k), 1, u(1,k+1), 1, cs, sn )
2118 end if
2119
2120 end do
2121
2122 e(mn-1) = f
2123 iter = iter + 1
2124!
2125! Convergence.
2126!
2127 else if ( kase == 4 ) then
2128!
2129! Make the singular value nonnegative.
2130!
2131 if ( s(l) < 0.0d+00 ) then
2132 s(l) = -s(l)
2133 if ( wantv ) then
2134 v(1:n,l) = -v(1:n,l)
2135 end if
2136 end if
2137!
2138! Order the singular value.
2139!
2140 do
2141
2142 if ( l == mm ) then
2143 exit
2144 end if
2145
2146 if ( s(l+1) <= s(l) ) then
2147 exit
2148 end if
2149
2150 t = s(l)
2151 s(l) = s(l+1)
2152 s(l+1) = t
2153
2154 if ( wantv .and. l < n ) then
2155 call dswap ( n, v(1,l), 1, v(1,l+1), 1 )
2156 end if
2157
2158 if ( wantu .and. l < m ) then
2159 call dswap ( m, u(1,l), 1, u(1,l+1), 1 )
2160 end if
2161
2162 l = l + 1
2163
2164 end do
2165
2166 iter = 0
2167 mn = mn - 1
2168
2169 end if
2170
2171 end do
2172
2173 return
2174end
2175subroutine dswap ( n, x, incx, y, incy )
2176
2177!*****************************************************************************80
2178!
2179!! DSWAP interchanges two vectors.
2180!
2181! Discussion:
2182!
2183! This routine uses double precision real arithmetic.
2184!
2185! Licensing:
2186!
2187! This code is distributed under the GNU LGPL license.
2188!
2189! Modified:
2190!
2191! 08 April 1999
2192!
2193! Author:
2194!
2195! Original FORTRAN77 version by Charles Lawson, Richard Hanson,
2196! David Kincaid, Fred Krogh.
2197! FORTRAN90 version by John Burkardt.
2198!
2199! Reference:
2200!
2201! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
2202! LINPACK User's Guide,
2203! SIAM, 1979,
2204! ISBN13: 978-0-898711-72-1,
2205! LC: QA214.L56.
2206!
2207! Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
2208! Algorithm 539,
2209! Basic Linear Algebra Subprograms for Fortran Usage,
2210! ACM Transactions on Mathematical Software,
2211! Volume 5, Number 3, September 1979, pages 308-323.
2212!
2213! Parameters:
2214!
2215! Input, integer ( kind = 4 ) N, the number of entries in the vectors.
2216!
2217! Input/output, real ( kind = 8 ) X(*), one of the vectors to swap.
2218!
2219! Input, integer ( kind = 4 ) INCX, the increment between successive
2220! entries of X.
2221!
2222! Input/output, real ( kind = 8 ) Y(*), one of the vectors to swap.
2223!
2224! Input, integer ( kind = 4 ) INCY, the increment between successive
2225! elements of Y.
2226!
2227 implicit none
2228
2229 integer ( kind = 4 ) i
2230 integer ( kind = 4 ) incx
2231 integer ( kind = 4 ) incy
2232 integer ( kind = 4 ) ix
2233 integer ( kind = 4 ) iy
2234 integer ( kind = 4 ) m
2235 integer ( kind = 4 ) n
2236 real ( kind = 8 ) temp
2237 real ( kind = 8 ) x(*)
2238 real ( kind = 8 ) y(*)
2239
2240 if ( n <= 0 ) then
2241
2242 else if ( incx == 1 .and. incy == 1 ) then
2243
2244 m = mod( n, 3 )
2245
2246 do i = 1, m
2247 temp = x(i)
2248 x(i) = y(i)
2249 y(i) = temp
2250 end do
2251
2252 do i = m + 1, n, 3
2253
2254 temp = x(i)
2255 x(i) = y(i)
2256 y(i) = temp
2257
2258 temp = x(i+1)
2259 x(i+1) = y(i+1)
2260 y(i+1) = temp
2261
2262 temp = x(i+2)
2263 x(i+2) = y(i+2)
2264 y(i+2) = temp
2265
2266 end do
2267
2268 else
2269
2270 if ( 0 <= incx ) then
2271 ix = 1
2272 else
2273 ix = ( - n + 1 ) * incx + 1
2274 end if
2275
2276 if ( 0 <= incy ) then
2277 iy = 1
2278 else
2279 iy = ( - n + 1 ) * incy + 1
2280 end if
2281
2282 do i = 1, n
2283 temp = x(ix)
2284 x(ix) = y(iy)
2285 y(iy) = temp
2286 ix = ix + incx
2287 iy = iy + incy
2288 end do
2289
2290 end if
2291
2292 return
2293end
2294subroutine normal_solve ( m, n, a, b, x, flag )
2295
2296!*****************************************************************************80
2297!
2298!! NORMAL_SOLVE solves a linear system using the normal equations.
2299!
2300! Discussion:
2301!
2302! Given a presumably rectangular MxN system of equations A*x=b, this routine
2303! sets up the NxN system A'*A*x=A'b. Assuming N <= M, and that A has full
2304! column rank, the system will be solvable, and the vector x that is returned
2305! will minimize the Euclidean norm of the residual.
2306!
2307! One drawback to this approach is that the condition number of the linear
2308! system A'*A is effectively the square of the condition number of A,
2309! meaning that there is a substantial loss of accuracy.
2310!
2311! Licensing:
2312!
2313! This code is distributed under the GNU LGPL license.
2314!
2315! Modified:
2316!
2317! 17 April 2012
2318!
2319! Author:
2320!
2321! John Burkardt
2322!
2323! Reference:
2324!
2325! David Kahaner, Cleve Moler, Steven Nash,
2326! Numerical Methods and Software,
2327! Prentice Hall, 1989,
2328! ISBN: 0-13-627258-4,
2329! LC: TA345.K34.
2330!
2331! Parameters:
2332!
2333! Input, integer ( kind = 4 ) M, the number of rows of A.
2334!
2335! Input, integer ( kind = 4 ) N, the number of columns of A.
2336! It must be the case that N <= M.
2337!
2338! Input, real ( kind = 8 ) A(M,N), the matrix.
2339! The matrix must have full column rank.
2340!
2341! Input, real ( kind = 8 ) B(M), the right hand side.
2342!
2343! Output, real ( kind = 8 ) X(N), the least squares solution.
2344!
2345! Output, integer ( kind = 4 ) FLAG,
2346! 0, no error was detected.
2347! 1, an error occurred.
2348!
2349 implicit none
2350
2351 integer ( kind = 4 ) m
2352 integer ( kind = 4 ) n
2353
2354 real ( kind = 8 ) a(m,n)
2355 real ( kind = 8 ) ata(n,n)
2356 real ( kind = 8 ) ata_c(n,n)
2357 real ( kind = 8 ) atb(n)
2358 real ( kind = 8 ) b(m)
2359 integer ( kind = 4 ) flag
2360 real ( kind = 8 ) x(n)
2361
2362 flag = 0
2363
2364 if ( m < n ) then
2365 flag = 1
2366 return
2367 end if
2368
2369 ata(1:n,1:n) = matmul( transpose( a(1:m,1:n) ), a(1:m,1:n) )
2370
2371 atb(1:n) = matmul( transpose( a(1:m,1:n) ), b(1:m) )
2372
2373 call r8mat_cholesky_factor ( n, ata, ata_c, flag )
2374
2375 if ( flag /= 0 ) then
2376 return
2377 end if
2378
2379 call r8mat_cholesky_solve ( n, ata_c, atb, x )
2380
2381 return
2382end
2383subroutine qr_solve ( m, n, a, b, x )
2384
2385!*****************************************************************************80
2386!
2387!! QR_SOLVE solves a linear system in the least squares sense.
2388!
2389! Discussion:
2390!
2391! If the matrix A has full column rank, then the solution X should be the
2392! unique vector that minimizes the Euclidean norm of the residual.
2393!
2394! If the matrix A does not have full column rank, then the solution is
2395! not unique; the vector X will minimize the residual norm, but so will
2396! various other vectors.
2397!
2398! Licensing:
2399!
2400! This code is distributed under the GNU LGPL license.
2401!
2402! Modified:
2403!
2404! 15 April 2012
2405!
2406! Author:
2407!
2408! John Burkardt
2409!
2410! Reference:
2411!
2412! David Kahaner, Cleve Moler, Steven Nash,
2413! Numerical Methods and Software,
2414! Prentice Hall, 1989,
2415! ISBN: 0-13-627258-4,
2416! LC: TA345.K34.
2417!
2418! Parameters:
2419!
2420! Input, integer ( kind = 4 ) M, the number of rows of A.
2421!
2422! Input, integer ( kind = 4 ) N, the number of columns of A.
2423!
2424! Input, real ( kind = 8 ) A(M,N), the matrix.
2425!
2426! Input, real ( kind = 8 ) B(M), the right hand side.
2427!
2428! Output, real ( kind = 8 ) X(N), the least squares solution.
2429!
2430 implicit none
2431
2432 integer ( kind = 4 ) m
2433 integer ( kind = 4 ) n
2434
2435 real ( kind = 8 ) a(m,n)
2436 real ( kind = 8 ) a_qr(m,n)
2437 real ( kind = 8 ) b(m)
2438 integer ( kind = 4 ) ind
2439 integer ( kind = 4 ) itask
2440 integer ( kind = 4 ) jpvt(n)
2441 integer ( kind = 4 ) kr
2442 integer ( kind = 4 ) lda
2443 real ( kind = 8 ) qraux(n)
2444 real ( kind = 8 ) r(m)
2445 real ( kind = 8 ) tol
2446 real ( kind = 8 ) work(n)
2447 real ( kind = 8 ) x(n)
2448
2449 a_qr(1:m,1:n) = a(1:m,1:n)
2450
2451 lda = m
2452
2453 tol = epsilon( tol ) / maxval( abs( a_qr(1:m,1:n) ) )
2454
2455 itask = 1
2456
2457 call dqrls ( a_qr, lda, m, n, tol, kr, b, x, r, jpvt, qraux, work, &
2458 itask, ind )
2459
2460 return
2461end
2462subroutine svd_solve ( m, n, a, b, x )
2463
2464!*****************************************************************************80
2465!
2466!! SVD_SOLVE solves a linear system in the least squares sense.
2467!
2468! Discussion:
2469!
2470! The vector X returned by this routine should always minimize the
2471! Euclidean norm of the residual ||A*x-b||.
2472!
2473! If the matrix A does not have full column rank, then there are multiple
2474! vectors that attain the minimum residual. In that case, the vector
2475! X returned by this routine is the unique such minimizer that has the
2476! the minimum possible Euclidean norm, that is, ||A*x-b|| and ||x||
2477! are both minimized.
2478!
2479! Licensing:
2480!
2481! This code is distributed under the GNU LGPL license.
2482!
2483! Modified:
2484!
2485! 07 September 2012
2486!
2487! Reference:
2488!
2489! David Kahaner, Cleve Moler, Steven Nash,
2490! Numerical Methods and Software,
2491! Prentice Hall, 1989,
2492! ISBN: 0-13-627258-4,
2493! LC: TA345.K34.
2494!
2495! Parameters:
2496!
2497! Input, integer ( kind = 4 ) M, the number of rows of A.
2498!
2499! Input, integer ( kind = 4 ) N, the number of columns of A.
2500!
2501! Input, real ( kind = 8 ) A(M,N), the matrix.
2502!
2503! Input, real ( kind = 8 ) B(M), the right hand side.
2504!
2505! Output, real ( kind = 8 ) X(N), the least squares solution.
2506!
2507 implicit none
2508
2509 integer ( kind = 4 ) m
2510 integer ( kind = 4 ) n
2511
2512 real ( kind = 8 ) a(m,n)
2513 real ( kind = 8 ) a_copy(m,n)
2514 real ( kind = 8 ) b(m)
2515 real ( kind = 8 ) e(max(m+1,n))
2516 integer ( kind = 4 ) i
2517 integer ( kind = 4 ) info
2518 integer ( kind = 4 ) lda
2519 integer ( kind = 4 ) ldu
2520 integer ( kind = 4 ) ldv
2521 integer ( kind = 4 ) job
2522 real ( kind = 8 ) sdiag(max(m+1,n))
2523 real ( kind = 8 ) smax
2524 real ( kind = 8 ) stol
2525 real ( kind = 8 ) sub(n)
2526 real ( kind = 8 ) u(m,m)
2527 real ( kind = 8 ) ub(m)
2528 real ( kind = 8 ) v(n,n)
2529 real ( kind = 8 ) work(m)
2530 real ( kind = 8 ) x(n)
2531!
2532! Get the SVD.
2533!
2534 a_copy(1:m,1:n) = a(1:m,1:n)
2535 lda = m
2536 ldu = m
2537 ldv = n
2538 job = 11
2539
2540 call dsvdc ( a_copy, lda, m, n, sdiag, e, u, ldu, v, ldv, work, job, info )
2541
2542 if ( info /= 0 ) then
2543 write ( *, '(a)' ) ' '
2544 write ( *, '(a)' ) 'SVD_SOLVE - Failure!'
2545 write ( *, '(a)' ) ' The SVD could not be calculated.'
2546 write ( *, '(a)' ) ' LINPACK routine DSVDC returned a nonzero'
2547 write ( *, '(a,i8)' ) ' value of the error flag, INFO = ', info
2548 stop
2549 end if
2550
2551 ub(1:m) = matmul( transpose( u(1:m,1:m) ), b(1:m) )
2552
2553 sub(1:n) = 0.0d+00
2554!
2555! For singular problems, there may be tiny but nonzero singular values
2556! that should be ignored. This is a reasonable attempt to avoid such
2557! problems, although in general, the user might wish to control the tolerance.
2558!
2559 smax = maxval( sdiag(1:n) )
2560 if ( smax <= epsilon( smax ) ) then
2561 smax = 1.0d+00
2562 end if
2563
2564 stol = epsilon( smax ) * smax
2565
2566 do i = 1, n
2567 if ( i <= m ) then
2568 if ( stol <= sdiag(i) ) then
2569 sub(i) = ub(i) / sdiag(i)
2570 end if
2571 end if
2572 end do
2573
2574 x(1:n) = matmul( v(1:n,1:n), sub(1:n) )
2575
2576 return
2577end
subroutine svd_solve(m, n, a, b, x)
real(kind=8) function ddot(n, dx, incx, dy, incy)
Definition QR_SOLVE.f90:122
subroutine dqrdc(a, lda, n, p, qraux, jpvt, work, job)
Definition QR_SOLVE.f90:434
real(kind=8) function dnrm2(n, x, incx)
Definition QR_SOLVE.f90:248
subroutine dscal(n, sa, x, incx)
subroutine qr_solve(m, n, a, b, x)
subroutine dswap(n, x, incx, y, incy)
subroutine dqrlss(a, lda, m, n, kr, b, x, rsd, jpvt, qraux)
Definition QR_SOLVE.f90:828
subroutine dqrls(a, lda, m, n, tol, kr, b, x, rsd, jpvt, qraux, work, itask, ind)
Definition QR_SOLVE.f90:677
subroutine normal_solve(m, n, a, b, x, flag)
subroutine dqrank(a, lda, m, n, tol, kr, jpvt, qraux, work)
Definition QR_SOLVE.f90:342
subroutine drot(n, x, incx, y, incy, c, s)
subroutine daxpy(n, da, dx, incx, dy, incy)
Definition QR_SOLVE.f90:2
subroutine dsvdc(a, lda, m, n, s, e, u, ldu, v, ldv, work, job, info)
subroutine drotg(sa, sb, c, s)
subroutine dqrsl(a, lda, n, k, qraux, y, qy, qty, b, rsd, ab, job, info)
Definition QR_SOLVE.f90:938
subroutine r8mat_cholesky_factor(n, a, c, flag)
Definition R8LIB.f90:12849
subroutine r8mat_cholesky_solve(n, l, b, x)
Definition R8LIB.f90:13143