LAPACK 3.3.0

dgsvj0.f

Go to the documentation of this file.
00001       SUBROUTINE DGSVJ0( JOBV, M, N, A, LDA, D, SVA, MV, V, LDV, EPS,
00002      +                   SFMIN, TOL, NSWEEP, WORK, LWORK, INFO )
00003 *
00004 *  -- LAPACK routine (version 3.3.0)                                    --
00005 *
00006 *  -- Contributed by Zlatko Drmac of the University of Zagreb and     --
00007 *  -- Kresimir Veselic of the Fernuniversitaet Hagen                  --
00008 *     November 2010
00009 *
00010 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
00011 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
00012 *
00013 * This routine is also part of SIGMA (version 1.23, October 23. 2008.)
00014 * SIGMA is a library of algorithms for highly accurate algorithms for
00015 * computation of SVD, PSVD, QSVD, (H,K)-SVD, and for solution of the
00016 * eigenvalue problems Hx = lambda M x, H M x = lambda x with H, M > 0.
00017 *
00018       IMPLICIT NONE
00019 *     .. Scalar Arguments ..
00020       INTEGER            INFO, LDA, LDV, LWORK, M, MV, N, NSWEEP
00021       DOUBLE PRECISION   EPS, SFMIN, TOL
00022       CHARACTER*1        JOBV
00023 *     ..
00024 *     .. Array Arguments ..
00025       DOUBLE PRECISION   A( LDA, * ), SVA( N ), D( N ), V( LDV, * ),
00026      +                   WORK( LWORK )
00027 *     ..
00028 *
00029 *  Purpose
00030 *  =======
00031 *
00032 *  DGSVJ0 is called from DGESVJ as a pre-processor and that is its main
00033 *  purpose. It applies Jacobi rotations in the same way as DGESVJ does, but
00034 *  it does not check convergence (stopping criterion). Few tuning
00035 *  parameters (marked by [TP]) are available for the implementer.
00036 *
00037 *  Further Details
00038 *  ~~~~~~~~~~~~~~~
00039 *  DGSVJ0 is used just to enable SGESVJ to call a simplified version of
00040 *  itself to work on a submatrix of the original matrix.
00041 *
00042 *  Contributors
00043 *  ~~~~~~~~~~~~
00044 *  Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany)
00045 *
00046 *  Bugs, Examples and Comments
00047 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
00048 *  Please report all bugs and send interesting test examples and comments to
00049 *  drmac@math.hr. Thank you.
00050 *
00051 *  Arguments
00052 *  =========
00053 *
00054 *  JOBV    (input) CHARACTER*1
00055 *          Specifies whether the output from this procedure is used
00056 *          to compute the matrix V:
00057 *          = 'V': the product of the Jacobi rotations is accumulated
00058 *                 by postmulyiplying the N-by-N array V.
00059 *                (See the description of V.)
00060 *          = 'A': the product of the Jacobi rotations is accumulated
00061 *                 by postmulyiplying the MV-by-N array V.
00062 *                (See the descriptions of MV and V.)
00063 *          = 'N': the Jacobi rotations are not accumulated.
00064 *
00065 *  M       (input) INTEGER
00066 *          The number of rows of the input matrix A.  M >= 0.
00067 *
00068 *  N       (input) INTEGER
00069 *          The number of columns of the input matrix A.
00070 *          M >= N >= 0.
00071 *
00072 *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
00073 *          On entry, M-by-N matrix A, such that A*diag(D) represents
00074 *          the input matrix.
00075 *          On exit,
00076 *          A_onexit * D_onexit represents the input matrix A*diag(D)
00077 *          post-multiplied by a sequence of Jacobi rotations, where the
00078 *          rotation threshold and the total number of sweeps are given in
00079 *          TOL and NSWEEP, respectively.
00080 *          (See the descriptions of D, TOL and NSWEEP.)
00081 *
00082 *  LDA     (input) INTEGER
00083 *          The leading dimension of the array A.  LDA >= max(1,M).
00084 *
00085 *  D       (input/workspace/output) DOUBLE PRECISION array, dimension (N)
00086 *          The array D accumulates the scaling factors from the fast scaled
00087 *          Jacobi rotations.
00088 *          On entry, A*diag(D) represents the input matrix.
00089 *          On exit, A_onexit*diag(D_onexit) represents the input matrix
00090 *          post-multiplied by a sequence of Jacobi rotations, where the
00091 *          rotation threshold and the total number of sweeps are given in
00092 *          TOL and NSWEEP, respectively.
00093 *          (See the descriptions of A, TOL and NSWEEP.)
00094 *
00095 *  SVA     (input/workspace/output) DOUBLE PRECISION array, dimension (N)
00096 *          On entry, SVA contains the Euclidean norms of the columns of
00097 *          the matrix A*diag(D).
00098 *          On exit, SVA contains the Euclidean norms of the columns of
00099 *          the matrix onexit*diag(D_onexit).
00100 *
00101 *  MV      (input) INTEGER
00102 *          If JOBV .EQ. 'A', then MV rows of V are post-multipled by a
00103 *                           sequence of Jacobi rotations.
00104 *          If JOBV = 'N',   then MV is not referenced.
00105 *
00106 *  V       (input/output) DOUBLE PRECISION array, dimension (LDV,N)
00107 *          If JOBV .EQ. 'V' then N rows of V are post-multipled by a
00108 *                           sequence of Jacobi rotations.
00109 *          If JOBV .EQ. 'A' then MV rows of V are post-multipled by a
00110 *                           sequence of Jacobi rotations.
00111 *          If JOBV = 'N',   then V is not referenced.
00112 *
00113 *  LDV     (input) INTEGER
00114 *          The leading dimension of the array V,  LDV >= 1.
00115 *          If JOBV = 'V', LDV .GE. N.
00116 *          If JOBV = 'A', LDV .GE. MV.
00117 *
00118 *  EPS     (input) DOUBLE PRECISION
00119 *          EPS = DLAMCH('Epsilon')
00120 *
00121 *  SFMIN   (input) DOUBLE PRECISION
00122 *          SFMIN = DLAMCH('Safe Minimum')
00123 *
00124 *  TOL     (input) DOUBLE PRECISION
00125 *          TOL is the threshold for Jacobi rotations. For a pair
00126 *          A(:,p), A(:,q) of pivot columns, the Jacobi rotation is
00127 *          applied only if DABS(COS(angle(A(:,p),A(:,q)))) .GT. TOL.
00128 *
00129 *  NSWEEP  (input) INTEGER
00130 *          NSWEEP is the number of sweeps of Jacobi rotations to be
00131 *          performed.
00132 *
00133 *  WORK    (workspace) DOUBLE PRECISION array, dimension (LWORK)
00134 *
00135 *  LWORK   (input) INTEGER
00136 *          LWORK is the dimension of WORK. LWORK .GE. M.
00137 *
00138 *  INFO    (output) INTEGER
00139 *          = 0 : successful exit.
00140 *          < 0 : if INFO = -i, then the i-th argument had an illegal value
00141 *
00142 *  =====================================================================
00143 *
00144 *     .. Local Parameters ..
00145       DOUBLE PRECISION   ZERO, HALF, ONE, TWO
00146       PARAMETER          ( ZERO = 0.0D0, HALF = 0.5D0, ONE = 1.0D0,
00147      +                   TWO = 2.0D0 )
00148 *     ..
00149 *     .. Local Scalars ..
00150       DOUBLE PRECISION   AAPP, AAPP0, AAPQ, AAQQ, APOAQ, AQOAP, BIG,
00151      +                   BIGTHETA, CS, MXAAPQ, MXSINJ, ROOTBIG, ROOTEPS,
00152      +                   ROOTSFMIN, ROOTTOL, SMALL, SN, T, TEMP1, THETA,
00153      +                   THSIGN
00154       INTEGER            BLSKIP, EMPTSW, i, ibr, IERR, igl, IJBLSK, ir1,
00155      +                   ISWROT, jbc, jgl, KBL, LKAHEAD, MVL, NBL,
00156      +                   NOTROT, p, PSKIPPED, q, ROWSKIP, SWBAND
00157       LOGICAL            APPLV, ROTOK, RSVEC
00158 *     ..
00159 *     .. Local Arrays ..
00160       DOUBLE PRECISION   FASTR( 5 )
00161 *     ..
00162 *     .. Intrinsic Functions ..
00163       INTRINSIC          DABS, DMAX1, DBLE, MIN0, DSIGN, DSQRT
00164 *     ..
00165 *     .. External Functions ..
00166       DOUBLE PRECISION   DDOT, DNRM2
00167       INTEGER            IDAMAX
00168       LOGICAL            LSAME
00169       EXTERNAL           IDAMAX, LSAME, DDOT, DNRM2
00170 *     ..
00171 *     .. External Subroutines ..
00172       EXTERNAL           DAXPY, DCOPY, DLASCL, DLASSQ, DROTM, DSWAP
00173 *     ..
00174 *     .. Executable Statements ..
00175 *
00176       APPLV = LSAME( JOBV, 'A' )
00177       RSVEC = LSAME( JOBV, 'V' )
00178       IF( .NOT.( RSVEC .OR. APPLV .OR. LSAME( JOBV, 'N' ) ) ) THEN
00179          INFO = -1
00180       ELSE IF( M.LT.0 ) THEN
00181          INFO = -2
00182       ELSE IF( ( N.LT.0 ) .OR. ( N.GT.M ) ) THEN
00183          INFO = -3
00184       ELSE IF( LDA.LT.M ) THEN
00185          INFO = -5
00186       ELSE IF( ( RSVEC.OR.APPLV ) .AND. ( MV.LT.0 ) ) THEN
00187          INFO = -8
00188       ELSE IF( ( RSVEC.AND.( LDV.LT.N ) ).OR. 
00189      &         ( APPLV.AND.( LDV.LT.MV ) ) ) THEN
00190          INFO = -10
00191       ELSE IF( TOL.LE.EPS ) THEN
00192          INFO = -13
00193       ELSE IF( NSWEEP.LT.0 ) THEN
00194          INFO = -14
00195       ELSE IF( LWORK.LT.M ) THEN
00196          INFO = -16
00197       ELSE
00198          INFO = 0
00199       END IF
00200 *
00201 *     #:(
00202       IF( INFO.NE.0 ) THEN
00203          CALL XERBLA( 'DGSVJ0', -INFO )
00204          RETURN
00205       END IF
00206 *
00207       IF( RSVEC ) THEN
00208          MVL = N
00209       ELSE IF( APPLV ) THEN
00210          MVL = MV
00211       END IF
00212       RSVEC = RSVEC .OR. APPLV
00213 
00214       ROOTEPS = DSQRT( EPS )
00215       ROOTSFMIN = DSQRT( SFMIN )
00216       SMALL = SFMIN / EPS
00217       BIG = ONE / SFMIN
00218       ROOTBIG = ONE / ROOTSFMIN
00219       BIGTHETA = ONE / ROOTEPS
00220       ROOTTOL = DSQRT( TOL )
00221 *
00222 *
00223 *     -#- Row-cyclic Jacobi SVD algorithm with column pivoting -#-
00224 *
00225       EMPTSW = ( N*( N-1 ) ) / 2
00226       NOTROT = 0
00227       FASTR( 1 ) = ZERO
00228 *
00229 *     -#- Row-cyclic pivot strategy with de Rijk's pivoting -#-
00230 *
00231 
00232       SWBAND = 0
00233 *[TP] SWBAND is a tuning parameter. It is meaningful and effective
00234 *     if SGESVJ is used as a computational routine in the preconditioned
00235 *     Jacobi SVD algorithm SGESVJ. For sweeps i=1:SWBAND the procedure
00236 *     ......
00237 
00238       KBL = MIN0( 8, N )
00239 *[TP] KBL is a tuning parameter that defines the tile size in the
00240 *     tiling of the p-q loops of pivot pairs. In general, an optimal
00241 *     value of KBL depends on the matrix dimensions and on the
00242 *     parameters of the computer's memory.
00243 *
00244       NBL = N / KBL
00245       IF( ( NBL*KBL ).NE.N )NBL = NBL + 1
00246 
00247       BLSKIP = ( KBL**2 ) + 1
00248 *[TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL.
00249 
00250       ROWSKIP = MIN0( 5, KBL )
00251 *[TP] ROWSKIP is a tuning parameter.
00252 
00253       LKAHEAD = 1
00254 *[TP] LKAHEAD is a tuning parameter.
00255       SWBAND = 0
00256       PSKIPPED = 0
00257 *
00258       DO 1993 i = 1, NSWEEP
00259 *     .. go go go ...
00260 *
00261          MXAAPQ = ZERO
00262          MXSINJ = ZERO
00263          ISWROT = 0
00264 *
00265          NOTROT = 0
00266          PSKIPPED = 0
00267 *
00268          DO 2000 ibr = 1, NBL
00269 
00270             igl = ( ibr-1 )*KBL + 1
00271 *
00272             DO 1002 ir1 = 0, MIN0( LKAHEAD, NBL-ibr )
00273 *
00274                igl = igl + ir1*KBL
00275 *
00276                DO 2001 p = igl, MIN0( igl+KBL-1, N-1 )
00277 
00278 *     .. de Rijk's pivoting
00279                   q = IDAMAX( N-p+1, SVA( p ), 1 ) + p - 1
00280                   IF( p.NE.q ) THEN
00281                      CALL DSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
00282                      IF( RSVEC )CALL DSWAP( MVL, V( 1, p ), 1,
00283      +                                      V( 1, q ), 1 )
00284                      TEMP1 = SVA( p )
00285                      SVA( p ) = SVA( q )
00286                      SVA( q ) = TEMP1
00287                      TEMP1 = D( p )
00288                      D( p ) = D( q )
00289                      D( q ) = TEMP1
00290                   END IF
00291 *
00292                   IF( ir1.EQ.0 ) THEN
00293 *
00294 *        Column norms are periodically updated by explicit
00295 *        norm computation.
00296 *        Caveat:
00297 *        Some BLAS implementations compute DNRM2(M,A(1,p),1)
00298 *        as DSQRT(DDOT(M,A(1,p),1,A(1,p),1)), which may result in
00299 *        overflow for ||A(:,p)||_2 > DSQRT(overflow_threshold), and
00300 *        undeflow for ||A(:,p)||_2 < DSQRT(underflow_threshold).
00301 *        Hence, DNRM2 cannot be trusted, not even in the case when
00302 *        the true norm is far from the under(over)flow boundaries.
00303 *        If properly implemented DNRM2 is available, the IF-THEN-ELSE
00304 *        below should read "AAPP = DNRM2( M, A(1,p), 1 ) * D(p)".
00305 *
00306                      IF( ( SVA( p ).LT.ROOTBIG ) .AND.
00307      +                   ( SVA( p ).GT.ROOTSFMIN ) ) THEN
00308                         SVA( p ) = DNRM2( M, A( 1, p ), 1 )*D( p )
00309                      ELSE
00310                         TEMP1 = ZERO
00311                         AAPP = ONE
00312                         CALL DLASSQ( M, A( 1, p ), 1, TEMP1, AAPP )
00313                         SVA( p ) = TEMP1*DSQRT( AAPP )*D( p )
00314                      END IF
00315                      AAPP = SVA( p )
00316                   ELSE
00317                      AAPP = SVA( p )
00318                   END IF
00319 
00320 *
00321                   IF( AAPP.GT.ZERO ) THEN
00322 *
00323                      PSKIPPED = 0
00324 *
00325                      DO 2002 q = p + 1, MIN0( igl+KBL-1, N )
00326 *
00327                         AAQQ = SVA( q )
00328 
00329                         IF( AAQQ.GT.ZERO ) THEN
00330 *
00331                            AAPP0 = AAPP
00332                            IF( AAQQ.GE.ONE ) THEN
00333                               ROTOK = ( SMALL*AAPP ).LE.AAQQ
00334                               IF( AAPP.LT.( BIG / AAQQ ) ) THEN
00335                                  AAPQ = ( DDOT( M, A( 1, p ), 1, A( 1,
00336      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
00337      +                                  / AAPP
00338                               ELSE
00339                                  CALL DCOPY( M, A( 1, p ), 1, WORK, 1 )
00340                                  CALL DLASCL( 'G', 0, 0, AAPP, D( p ),
00341      +                                        M, 1, WORK, LDA, IERR )
00342                                  AAPQ = DDOT( M, WORK, 1, A( 1, q ),
00343      +                                  1 )*D( q ) / AAQQ
00344                               END IF
00345                            ELSE
00346                               ROTOK = AAPP.LE.( AAQQ / SMALL )
00347                               IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
00348                                  AAPQ = ( DDOT( M, A( 1, p ), 1, A( 1,
00349      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
00350      +                                  / AAPP
00351                               ELSE
00352                                  CALL DCOPY( M, A( 1, q ), 1, WORK, 1 )
00353                                  CALL DLASCL( 'G', 0, 0, AAQQ, D( q ),
00354      +                                        M, 1, WORK, LDA, IERR )
00355                                  AAPQ = DDOT( M, WORK, 1, A( 1, p ),
00356      +                                  1 )*D( p ) / AAPP
00357                               END IF
00358                            END IF
00359 *
00360                            MXAAPQ = DMAX1( MXAAPQ, DABS( AAPQ ) )
00361 *
00362 *        TO rotate or NOT to rotate, THAT is the question ...
00363 *
00364                            IF( DABS( AAPQ ).GT.TOL ) THEN
00365 *
00366 *           .. rotate
00367 *           ROTATED = ROTATED + ONE
00368 *
00369                               IF( ir1.EQ.0 ) THEN
00370                                  NOTROT = 0
00371                                  PSKIPPED = 0
00372                                  ISWROT = ISWROT + 1
00373                               END IF
00374 *
00375                               IF( ROTOK ) THEN
00376 *
00377                                  AQOAP = AAQQ / AAPP
00378                                  APOAQ = AAPP / AAQQ
00379                                  THETA = -HALF*DABS( AQOAP-APOAQ ) /
00380      +                                   AAPQ
00381 *
00382                                  IF( DABS( THETA ).GT.BIGTHETA ) THEN
00383 *
00384                                     T = HALF / THETA
00385                                     FASTR( 3 ) = T*D( p ) / D( q )
00386                                     FASTR( 4 ) = -T*D( q ) / D( p )
00387                                     CALL DROTM( M, A( 1, p ), 1,
00388      +                                          A( 1, q ), 1, FASTR )
00389                                     IF( RSVEC )CALL DROTM( MVL,
00390      +                                              V( 1, p ), 1,
00391      +                                              V( 1, q ), 1,
00392      +                                              FASTR )
00393                                     SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00394      +                                         ONE+T*APOAQ*AAPQ ) )
00395                                     AAPP = AAPP*DSQRT( DMAX1( ZERO, 
00396      +                                     ONE-T*AQOAP*AAPQ ) )
00397                                     MXSINJ = DMAX1( MXSINJ, DABS( T ) )
00398 *
00399                                  ELSE
00400 *
00401 *                 .. choose correct signum for THETA and rotate
00402 *
00403                                     THSIGN = -DSIGN( ONE, AAPQ )
00404                                     T = ONE / ( THETA+THSIGN*
00405      +                                  DSQRT( ONE+THETA*THETA ) )
00406                                     CS = DSQRT( ONE / ( ONE+T*T ) )
00407                                     SN = T*CS
00408 *
00409                                     MXSINJ = DMAX1( MXSINJ, DABS( SN ) )
00410                                     SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00411      +                                         ONE+T*APOAQ*AAPQ ) )
00412                                     AAPP = AAPP*DSQRT( DMAX1( ZERO,
00413      +                                     ONE-T*AQOAP*AAPQ ) )
00414 *
00415                                     APOAQ = D( p ) / D( q )
00416                                     AQOAP = D( q ) / D( p )
00417                                     IF( D( p ).GE.ONE ) THEN
00418                                        IF( D( q ).GE.ONE ) THEN
00419                                           FASTR( 3 ) = T*APOAQ
00420                                           FASTR( 4 ) = -T*AQOAP
00421                                           D( p ) = D( p )*CS
00422                                           D( q ) = D( q )*CS
00423                                           CALL DROTM( M, A( 1, p ), 1,
00424      +                                                A( 1, q ), 1,
00425      +                                                FASTR )
00426                                           IF( RSVEC )CALL DROTM( MVL,
00427      +                                        V( 1, p ), 1, V( 1, q ),
00428      +                                        1, FASTR )
00429                                        ELSE
00430                                           CALL DAXPY( M, -T*AQOAP,
00431      +                                                A( 1, q ), 1,
00432      +                                                A( 1, p ), 1 )
00433                                           CALL DAXPY( M, CS*SN*APOAQ,
00434      +                                                A( 1, p ), 1,
00435      +                                                A( 1, q ), 1 )
00436                                           D( p ) = D( p )*CS
00437                                           D( q ) = D( q ) / CS
00438                                           IF( RSVEC ) THEN
00439                                              CALL DAXPY( MVL, -T*AQOAP,
00440      +                                                   V( 1, q ), 1,
00441      +                                                   V( 1, p ), 1 )
00442                                              CALL DAXPY( MVL,
00443      +                                                   CS*SN*APOAQ,
00444      +                                                   V( 1, p ), 1,
00445      +                                                   V( 1, q ), 1 )
00446                                           END IF
00447                                        END IF
00448                                     ELSE
00449                                        IF( D( q ).GE.ONE ) THEN
00450                                           CALL DAXPY( M, T*APOAQ,
00451      +                                                A( 1, p ), 1,
00452      +                                                A( 1, q ), 1 )
00453                                           CALL DAXPY( M, -CS*SN*AQOAP,
00454      +                                                A( 1, q ), 1,
00455      +                                                A( 1, p ), 1 )
00456                                           D( p ) = D( p ) / CS
00457                                           D( q ) = D( q )*CS
00458                                           IF( RSVEC ) THEN
00459                                              CALL DAXPY( MVL, T*APOAQ,
00460      +                                                   V( 1, p ), 1,
00461      +                                                   V( 1, q ), 1 )
00462                                              CALL DAXPY( MVL,
00463      +                                                   -CS*SN*AQOAP,
00464      +                                                   V( 1, q ), 1,
00465      +                                                   V( 1, p ), 1 )
00466                                           END IF
00467                                        ELSE
00468                                           IF( D( p ).GE.D( q ) ) THEN
00469                                              CALL DAXPY( M, -T*AQOAP,
00470      +                                                   A( 1, q ), 1,
00471      +                                                   A( 1, p ), 1 )
00472                                              CALL DAXPY( M, CS*SN*APOAQ,
00473      +                                                   A( 1, p ), 1,
00474      +                                                   A( 1, q ), 1 )
00475                                              D( p ) = D( p )*CS
00476                                              D( q ) = D( q ) / CS
00477                                              IF( RSVEC ) THEN
00478                                                 CALL DAXPY( MVL,
00479      +                                               -T*AQOAP,
00480      +                                               V( 1, q ), 1,
00481      +                                               V( 1, p ), 1 )
00482                                                 CALL DAXPY( MVL,
00483      +                                               CS*SN*APOAQ,
00484      +                                               V( 1, p ), 1,
00485      +                                               V( 1, q ), 1 )
00486                                              END IF
00487                                           ELSE
00488                                              CALL DAXPY( M, T*APOAQ,
00489      +                                                   A( 1, p ), 1,
00490      +                                                   A( 1, q ), 1 )
00491                                              CALL DAXPY( M,
00492      +                                                   -CS*SN*AQOAP,
00493      +                                                   A( 1, q ), 1,
00494      +                                                   A( 1, p ), 1 )
00495                                              D( p ) = D( p ) / CS
00496                                              D( q ) = D( q )*CS
00497                                              IF( RSVEC ) THEN
00498                                                 CALL DAXPY( MVL,
00499      +                                               T*APOAQ, V( 1, p ),
00500      +                                               1, V( 1, q ), 1 )
00501                                                 CALL DAXPY( MVL,
00502      +                                               -CS*SN*AQOAP,
00503      +                                               V( 1, q ), 1,
00504      +                                               V( 1, p ), 1 )
00505                                              END IF
00506                                           END IF
00507                                        END IF
00508                                     END IF
00509                                  END IF
00510 *
00511                               ELSE
00512 *              .. have to use modified Gram-Schmidt like transformation
00513                                  CALL DCOPY( M, A( 1, p ), 1, WORK, 1 )
00514                                  CALL DLASCL( 'G', 0, 0, AAPP, ONE, M,
00515      +                                        1, WORK, LDA, IERR )
00516                                  CALL DLASCL( 'G', 0, 0, AAQQ, ONE, M,
00517      +                                        1, A( 1, q ), LDA, IERR )
00518                                  TEMP1 = -AAPQ*D( p ) / D( q )
00519                                  CALL DAXPY( M, TEMP1, WORK, 1,
00520      +                                       A( 1, q ), 1 )
00521                                  CALL DLASCL( 'G', 0, 0, ONE, AAQQ, M,
00522      +                                        1, A( 1, q ), LDA, IERR )
00523                                  SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00524      +                                      ONE-AAPQ*AAPQ ) )
00525                                  MXSINJ = DMAX1( MXSINJ, SFMIN )
00526                               END IF
00527 *           END IF ROTOK THEN ... ELSE
00528 *
00529 *           In the case of cancellation in updating SVA(q), SVA(p)
00530 *           recompute SVA(q), SVA(p).
00531                               IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
00532      +                            THEN
00533                                  IF( ( AAQQ.LT.ROOTBIG ) .AND.
00534      +                               ( AAQQ.GT.ROOTSFMIN ) ) THEN
00535                                     SVA( q ) = DNRM2( M, A( 1, q ), 1 )*
00536      +                                         D( q )
00537                                  ELSE
00538                                     T = ZERO
00539                                     AAQQ = ONE
00540                                     CALL DLASSQ( M, A( 1, q ), 1, T,
00541      +                                           AAQQ )
00542                                     SVA( q ) = T*DSQRT( AAQQ )*D( q )
00543                                  END IF
00544                               END IF
00545                               IF( ( AAPP / AAPP0 ).LE.ROOTEPS ) THEN
00546                                  IF( ( AAPP.LT.ROOTBIG ) .AND.
00547      +                               ( AAPP.GT.ROOTSFMIN ) ) THEN
00548                                     AAPP = DNRM2( M, A( 1, p ), 1 )*
00549      +                                     D( p )
00550                                  ELSE
00551                                     T = ZERO
00552                                     AAPP = ONE
00553                                     CALL DLASSQ( M, A( 1, p ), 1, T,
00554      +                                           AAPP )
00555                                     AAPP = T*DSQRT( AAPP )*D( p )
00556                                  END IF
00557                                  SVA( p ) = AAPP
00558                               END IF
00559 *
00560                            ELSE
00561 *        A(:,p) and A(:,q) already numerically orthogonal
00562                               IF( ir1.EQ.0 )NOTROT = NOTROT + 1
00563                               PSKIPPED = PSKIPPED + 1
00564                            END IF
00565                         ELSE
00566 *        A(:,q) is zero column
00567                            IF( ir1.EQ.0 )NOTROT = NOTROT + 1
00568                            PSKIPPED = PSKIPPED + 1
00569                         END IF
00570 *
00571                         IF( ( i.LE.SWBAND ) .AND.
00572      +                      ( PSKIPPED.GT.ROWSKIP ) ) THEN
00573                            IF( ir1.EQ.0 )AAPP = -AAPP
00574                            NOTROT = 0
00575                            GO TO 2103
00576                         END IF
00577 *
00578  2002                CONTINUE
00579 *     END q-LOOP
00580 *
00581  2103                CONTINUE
00582 *     bailed out of q-loop
00583 
00584                      SVA( p ) = AAPP
00585 
00586                   ELSE
00587                      SVA( p ) = AAPP
00588                      IF( ( ir1.EQ.0 ) .AND. ( AAPP.EQ.ZERO ) )
00589      +                   NOTROT = NOTROT + MIN0( igl+KBL-1, N ) - p
00590                   END IF
00591 *
00592  2001          CONTINUE
00593 *     end of the p-loop
00594 *     end of doing the block ( ibr, ibr )
00595  1002       CONTINUE
00596 *     end of ir1-loop
00597 *
00598 *........................................................
00599 * ... go to the off diagonal blocks
00600 *
00601             igl = ( ibr-1 )*KBL + 1
00602 *
00603             DO 2010 jbc = ibr + 1, NBL
00604 *
00605                jgl = ( jbc-1 )*KBL + 1
00606 *
00607 *        doing the block at ( ibr, jbc )
00608 *
00609                IJBLSK = 0
00610                DO 2100 p = igl, MIN0( igl+KBL-1, N )
00611 *
00612                   AAPP = SVA( p )
00613 *
00614                   IF( AAPP.GT.ZERO ) THEN
00615 *
00616                      PSKIPPED = 0
00617 *
00618                      DO 2200 q = jgl, MIN0( jgl+KBL-1, N )
00619 *
00620                         AAQQ = SVA( q )
00621 *
00622                         IF( AAQQ.GT.ZERO ) THEN
00623                            AAPP0 = AAPP
00624 *
00625 *     -#- M x 2 Jacobi SVD -#-
00626 *
00627 *        -#- Safe Gram matrix computation -#-
00628 *
00629                            IF( AAQQ.GE.ONE ) THEN
00630                               IF( AAPP.GE.AAQQ ) THEN
00631                                  ROTOK = ( SMALL*AAPP ).LE.AAQQ
00632                               ELSE
00633                                  ROTOK = ( SMALL*AAQQ ).LE.AAPP
00634                               END IF
00635                               IF( AAPP.LT.( BIG / AAQQ ) ) THEN
00636                                  AAPQ = ( DDOT( M, A( 1, p ), 1, A( 1,
00637      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
00638      +                                  / AAPP
00639                               ELSE
00640                                  CALL DCOPY( M, A( 1, p ), 1, WORK, 1 )
00641                                  CALL DLASCL( 'G', 0, 0, AAPP, D( p ),
00642      +                                        M, 1, WORK, LDA, IERR )
00643                                  AAPQ = DDOT( M, WORK, 1, A( 1, q ),
00644      +                                  1 )*D( q ) / AAQQ
00645                               END IF
00646                            ELSE
00647                               IF( AAPP.GE.AAQQ ) THEN
00648                                  ROTOK = AAPP.LE.( AAQQ / SMALL )
00649                               ELSE
00650                                  ROTOK = AAQQ.LE.( AAPP / SMALL )
00651                               END IF
00652                               IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
00653                                  AAPQ = ( DDOT( M, A( 1, p ), 1, A( 1,
00654      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
00655      +                                  / AAPP
00656                               ELSE
00657                                  CALL DCOPY( M, A( 1, q ), 1, WORK, 1 )
00658                                  CALL DLASCL( 'G', 0, 0, AAQQ, D( q ),
00659      +                                        M, 1, WORK, LDA, IERR )
00660                                  AAPQ = DDOT( M, WORK, 1, A( 1, p ),
00661      +                                  1 )*D( p ) / AAPP
00662                               END IF
00663                            END IF
00664 *
00665                            MXAAPQ = DMAX1( MXAAPQ, DABS( AAPQ ) )
00666 *
00667 *        TO rotate or NOT to rotate, THAT is the question ...
00668 *
00669                            IF( DABS( AAPQ ).GT.TOL ) THEN
00670                               NOTROT = 0
00671 *           ROTATED  = ROTATED + 1
00672                               PSKIPPED = 0
00673                               ISWROT = ISWROT + 1
00674 *
00675                               IF( ROTOK ) THEN
00676 *
00677                                  AQOAP = AAQQ / AAPP
00678                                  APOAQ = AAPP / AAQQ
00679                                  THETA = -HALF*DABS( AQOAP-APOAQ ) /
00680      +                                   AAPQ
00681                                  IF( AAQQ.GT.AAPP0 )THETA = -THETA
00682 *
00683                                  IF( DABS( THETA ).GT.BIGTHETA ) THEN
00684                                     T = HALF / THETA
00685                                     FASTR( 3 ) = T*D( p ) / D( q )
00686                                     FASTR( 4 ) = -T*D( q ) / D( p )
00687                                     CALL DROTM( M, A( 1, p ), 1,
00688      +                                          A( 1, q ), 1, FASTR )
00689                                     IF( RSVEC )CALL DROTM( MVL,
00690      +                                              V( 1, p ), 1,
00691      +                                              V( 1, q ), 1,
00692      +                                              FASTR )
00693                                     SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00694      +                                         ONE+T*APOAQ*AAPQ ) )
00695                                     AAPP = AAPP*DSQRT( DMAX1( ZERO,
00696      +                                     ONE-T*AQOAP*AAPQ ) )
00697                                     MXSINJ = DMAX1( MXSINJ, DABS( T ) )
00698                                  ELSE
00699 *
00700 *                 .. choose correct signum for THETA and rotate
00701 *
00702                                     THSIGN = -DSIGN( ONE, AAPQ )
00703                                     IF( AAQQ.GT.AAPP0 )THSIGN = -THSIGN
00704                                     T = ONE / ( THETA+THSIGN*
00705      +                                  DSQRT( ONE+THETA*THETA ) )
00706                                     CS = DSQRT( ONE / ( ONE+T*T ) )
00707                                     SN = T*CS
00708                                     MXSINJ = DMAX1( MXSINJ, DABS( SN ) )
00709                                     SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00710      +                                         ONE+T*APOAQ*AAPQ ) )
00711                                     AAPP = AAPP*DSQRT( DMAX1( ZERO, 
00712      +                                     ONE-T*AQOAP*AAPQ ) )
00713 *
00714                                     APOAQ = D( p ) / D( q )
00715                                     AQOAP = D( q ) / D( p )
00716                                     IF( D( p ).GE.ONE ) THEN
00717 *
00718                                        IF( D( q ).GE.ONE ) THEN
00719                                           FASTR( 3 ) = T*APOAQ
00720                                           FASTR( 4 ) = -T*AQOAP
00721                                           D( p ) = D( p )*CS
00722                                           D( q ) = D( q )*CS
00723                                           CALL DROTM( M, A( 1, p ), 1,
00724      +                                                A( 1, q ), 1,
00725      +                                                FASTR )
00726                                           IF( RSVEC )CALL DROTM( MVL,
00727      +                                        V( 1, p ), 1, V( 1, q ),
00728      +                                        1, FASTR )
00729                                        ELSE
00730                                           CALL DAXPY( M, -T*AQOAP,
00731      +                                                A( 1, q ), 1,
00732      +                                                A( 1, p ), 1 )
00733                                           CALL DAXPY( M, CS*SN*APOAQ,
00734      +                                                A( 1, p ), 1,
00735      +                                                A( 1, q ), 1 )
00736                                           IF( RSVEC ) THEN
00737                                              CALL DAXPY( MVL, -T*AQOAP,
00738      +                                                   V( 1, q ), 1,
00739      +                                                   V( 1, p ), 1 )
00740                                              CALL DAXPY( MVL,
00741      +                                                   CS*SN*APOAQ,
00742      +                                                   V( 1, p ), 1,
00743      +                                                   V( 1, q ), 1 )
00744                                           END IF
00745                                           D( p ) = D( p )*CS
00746                                           D( q ) = D( q ) / CS
00747                                        END IF
00748                                     ELSE
00749                                        IF( D( q ).GE.ONE ) THEN
00750                                           CALL DAXPY( M, T*APOAQ,
00751      +                                                A( 1, p ), 1,
00752      +                                                A( 1, q ), 1 )
00753                                           CALL DAXPY( M, -CS*SN*AQOAP,
00754      +                                                A( 1, q ), 1,
00755      +                                                A( 1, p ), 1 )
00756                                           IF( RSVEC ) THEN
00757                                              CALL DAXPY( MVL, T*APOAQ,
00758      +                                                   V( 1, p ), 1,
00759      +                                                   V( 1, q ), 1 )
00760                                              CALL DAXPY( MVL,
00761      +                                                   -CS*SN*AQOAP,
00762      +                                                   V( 1, q ), 1,
00763      +                                                   V( 1, p ), 1 )
00764                                           END IF
00765                                           D( p ) = D( p ) / CS
00766                                           D( q ) = D( q )*CS
00767                                        ELSE
00768                                           IF( D( p ).GE.D( q ) ) THEN
00769                                              CALL DAXPY( M, -T*AQOAP,
00770      +                                                   A( 1, q ), 1,
00771      +                                                   A( 1, p ), 1 )
00772                                              CALL DAXPY( M, CS*SN*APOAQ,
00773      +                                                   A( 1, p ), 1,
00774      +                                                   A( 1, q ), 1 )
00775                                              D( p ) = D( p )*CS
00776                                              D( q ) = D( q ) / CS
00777                                              IF( RSVEC ) THEN
00778                                                 CALL DAXPY( MVL,
00779      +                                               -T*AQOAP,
00780      +                                               V( 1, q ), 1,
00781      +                                               V( 1, p ), 1 )
00782                                                 CALL DAXPY( MVL,
00783      +                                               CS*SN*APOAQ,
00784      +                                               V( 1, p ), 1,
00785      +                                               V( 1, q ), 1 )
00786                                              END IF
00787                                           ELSE
00788                                              CALL DAXPY( M, T*APOAQ,
00789      +                                                   A( 1, p ), 1,
00790      +                                                   A( 1, q ), 1 )
00791                                              CALL DAXPY( M,
00792      +                                                   -CS*SN*AQOAP,
00793      +                                                   A( 1, q ), 1,
00794      +                                                   A( 1, p ), 1 )
00795                                              D( p ) = D( p ) / CS
00796                                              D( q ) = D( q )*CS
00797                                              IF( RSVEC ) THEN
00798                                                 CALL DAXPY( MVL,
00799      +                                               T*APOAQ, V( 1, p ),
00800      +                                               1, V( 1, q ), 1 )
00801                                                 CALL DAXPY( MVL,
00802      +                                               -CS*SN*AQOAP,
00803      +                                               V( 1, q ), 1,
00804      +                                               V( 1, p ), 1 )
00805                                              END IF
00806                                           END IF
00807                                        END IF
00808                                     END IF
00809                                  END IF
00810 *
00811                               ELSE
00812                                  IF( AAPP.GT.AAQQ ) THEN
00813                                     CALL DCOPY( M, A( 1, p ), 1, WORK,
00814      +                                          1 )
00815                                     CALL DLASCL( 'G', 0, 0, AAPP, ONE,
00816      +                                           M, 1, WORK, LDA, IERR )
00817                                     CALL DLASCL( 'G', 0, 0, AAQQ, ONE,
00818      +                                           M, 1, A( 1, q ), LDA,
00819      +                                           IERR )
00820                                     TEMP1 = -AAPQ*D( p ) / D( q )
00821                                     CALL DAXPY( M, TEMP1, WORK, 1,
00822      +                                          A( 1, q ), 1 )
00823                                     CALL DLASCL( 'G', 0, 0, ONE, AAQQ,
00824      +                                           M, 1, A( 1, q ), LDA,
00825      +                                           IERR )
00826                                     SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
00827      +                                         ONE-AAPQ*AAPQ ) )
00828                                     MXSINJ = DMAX1( MXSINJ, SFMIN )
00829                                  ELSE
00830                                     CALL DCOPY( M, A( 1, q ), 1, WORK,
00831      +                                          1 )
00832                                     CALL DLASCL( 'G', 0, 0, AAQQ, ONE,
00833      +                                           M, 1, WORK, LDA, IERR )
00834                                     CALL DLASCL( 'G', 0, 0, AAPP, ONE,
00835      +                                           M, 1, A( 1, p ), LDA,
00836      +                                           IERR )
00837                                     TEMP1 = -AAPQ*D( q ) / D( p )
00838                                     CALL DAXPY( M, TEMP1, WORK, 1,
00839      +                                          A( 1, p ), 1 )
00840                                     CALL DLASCL( 'G', 0, 0, ONE, AAPP,
00841      +                                           M, 1, A( 1, p ), LDA,
00842      +                                           IERR )
00843                                     SVA( p ) = AAPP*DSQRT( DMAX1( ZERO,
00844      +                                         ONE-AAPQ*AAPQ ) )
00845                                     MXSINJ = DMAX1( MXSINJ, SFMIN )
00846                                  END IF
00847                               END IF
00848 *           END IF ROTOK THEN ... ELSE
00849 *
00850 *           In the case of cancellation in updating SVA(q)
00851 *           .. recompute SVA(q)
00852                               IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
00853      +                            THEN
00854                                  IF( ( AAQQ.LT.ROOTBIG ) .AND.
00855      +                               ( AAQQ.GT.ROOTSFMIN ) ) THEN
00856                                     SVA( q ) = DNRM2( M, A( 1, q ), 1 )*
00857      +                                         D( q )
00858                                  ELSE
00859                                     T = ZERO
00860                                     AAQQ = ONE
00861                                     CALL DLASSQ( M, A( 1, q ), 1, T,
00862      +                                           AAQQ )
00863                                     SVA( q ) = T*DSQRT( AAQQ )*D( q )
00864                                  END IF
00865                               END IF
00866                               IF( ( AAPP / AAPP0 )**2.LE.ROOTEPS ) THEN
00867                                  IF( ( AAPP.LT.ROOTBIG ) .AND.
00868      +                               ( AAPP.GT.ROOTSFMIN ) ) THEN
00869                                     AAPP = DNRM2( M, A( 1, p ), 1 )*
00870      +                                     D( p )
00871                                  ELSE
00872                                     T = ZERO
00873                                     AAPP = ONE
00874                                     CALL DLASSQ( M, A( 1, p ), 1, T,
00875      +                                           AAPP )
00876                                     AAPP = T*DSQRT( AAPP )*D( p )
00877                                  END IF
00878                                  SVA( p ) = AAPP
00879                               END IF
00880 *              end of OK rotation
00881                            ELSE
00882                               NOTROT = NOTROT + 1
00883                               PSKIPPED = PSKIPPED + 1
00884                               IJBLSK = IJBLSK + 1
00885                            END IF
00886                         ELSE
00887                            NOTROT = NOTROT + 1
00888                            PSKIPPED = PSKIPPED + 1
00889                            IJBLSK = IJBLSK + 1
00890                         END IF
00891 *
00892                         IF( ( i.LE.SWBAND ) .AND. ( IJBLSK.GE.BLSKIP ) )
00893      +                      THEN
00894                            SVA( p ) = AAPP
00895                            NOTROT = 0
00896                            GO TO 2011
00897                         END IF
00898                         IF( ( i.LE.SWBAND ) .AND.
00899      +                      ( PSKIPPED.GT.ROWSKIP ) ) THEN
00900                            AAPP = -AAPP
00901                            NOTROT = 0
00902                            GO TO 2203
00903                         END IF
00904 *
00905  2200                CONTINUE
00906 *        end of the q-loop
00907  2203                CONTINUE
00908 *
00909                      SVA( p ) = AAPP
00910 *
00911                   ELSE
00912                      IF( AAPP.EQ.ZERO )NOTROT = NOTROT +
00913      +                   MIN0( jgl+KBL-1, N ) - jgl + 1
00914                      IF( AAPP.LT.ZERO )NOTROT = 0
00915                   END IF
00916 
00917  2100          CONTINUE
00918 *     end of the p-loop
00919  2010       CONTINUE
00920 *     end of the jbc-loop
00921  2011       CONTINUE
00922 *2011 bailed out of the jbc-loop
00923             DO 2012 p = igl, MIN0( igl+KBL-1, N )
00924                SVA( p ) = DABS( SVA( p ) )
00925  2012       CONTINUE
00926 *
00927  2000    CONTINUE
00928 *2000 :: end of the ibr-loop
00929 *
00930 *     .. update SVA(N)
00931          IF( ( SVA( N ).LT.ROOTBIG ) .AND. ( SVA( N ).GT.ROOTSFMIN ) )
00932      +       THEN
00933             SVA( N ) = DNRM2( M, A( 1, N ), 1 )*D( N )
00934          ELSE
00935             T = ZERO
00936             AAPP = ONE
00937             CALL DLASSQ( M, A( 1, N ), 1, T, AAPP )
00938             SVA( N ) = T*DSQRT( AAPP )*D( N )
00939          END IF
00940 *
00941 *     Additional steering devices
00942 *
00943          IF( ( i.LT.SWBAND ) .AND. ( ( MXAAPQ.LE.ROOTTOL ) .OR.
00944      +       ( ISWROT.LE.N ) ) )SWBAND = i
00945 *
00946          IF( ( i.GT.SWBAND+1 ) .AND. ( MXAAPQ.LT.DBLE( N )*TOL ) .AND.
00947      +       ( DBLE( N )*MXAAPQ*MXSINJ.LT.TOL ) ) THEN
00948             GO TO 1994
00949          END IF
00950 *
00951          IF( NOTROT.GE.EMPTSW )GO TO 1994
00952 
00953  1993 CONTINUE
00954 *     end i=1:NSWEEP loop
00955 * #:) Reaching this point means that the procedure has comleted the given
00956 *     number of iterations.
00957       INFO = NSWEEP - 1
00958       GO TO 1995
00959  1994 CONTINUE
00960 * #:) Reaching this point means that during the i-th sweep all pivots were
00961 *     below the given tolerance, causing early exit.
00962 *
00963       INFO = 0
00964 * #:) INFO = 0 confirms successful iterations.
00965  1995 CONTINUE
00966 *
00967 *     Sort the vector D.
00968       DO 5991 p = 1, N - 1
00969          q = IDAMAX( N-p+1, SVA( p ), 1 ) + p - 1
00970          IF( p.NE.q ) THEN
00971             TEMP1 = SVA( p )
00972             SVA( p ) = SVA( q )
00973             SVA( q ) = TEMP1
00974             TEMP1 = D( p )
00975             D( p ) = D( q )
00976             D( q ) = TEMP1
00977             CALL DSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
00978             IF( RSVEC )CALL DSWAP( MVL, V( 1, p ), 1, V( 1, q ), 1 )
00979          END IF
00980  5991 CONTINUE
00981 *
00982       RETURN
00983 *     ..
00984 *     .. END OF DGSVJ0
00985 *     ..
00986       END
 All Files Functions