001:       SUBROUTINE SGSVJ1( JOBV, M, N, N1, A, LDA, D, SVA, MV, V, LDV,
002:      +                   EPS, SFMIN, TOL, NSWEEP, WORK, LWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2.1)                                    --
005: *
006: *  -- Contributed by Zlatko Drmac of the University of Zagreb and     --
007: *  -- Kresimir Veselic of the Fernuniversitaet Hagen                  --
008: *  -- April 2009                                                      --
009: *
010: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
011: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
012: *
013: * This routine is also part of SIGMA (version 1.23, October 23. 2008.)
014: * SIGMA is a library of algorithms for highly accurate algorithms for
015: * computation of SVD, PSVD, QSVD, (H,K)-SVD, and for solution of the
016: * eigenvalue problems Hx = lambda M x, H M x = lambda x with H, M > 0.
017: *
018:       IMPLICIT           NONE
019: *     ..
020: *     .. Scalar Arguments ..
021:       REAL               EPS, SFMIN, TOL
022:       INTEGER            INFO, LDA, LDV, LWORK, M, MV, N, N1, NSWEEP
023:       CHARACTER*1        JOBV
024: *     ..
025: *     .. Array Arguments ..
026:       REAL               A( LDA, * ), D( N ), SVA( N ), V( LDV, * ),
027:      +                   WORK( LWORK )
028: *     ..
029: *
030: *  Purpose
031: *  =======
032: *
033: *  SGSVJ1 is called from SGESVJ as a pre-processor and that is its main
034: *  purpose. It applies Jacobi rotations in the same way as SGESVJ does, but
035: *  it targets only particular pivots and it does not check convergence
036: *  (stopping criterion). Few tunning parameters (marked by [TP]) are
037: *  available for the implementer.
038: *
039: *  Further Details
040: *  ~~~~~~~~~~~~~~~
041: *  SGSVJ1 applies few sweeps of Jacobi rotations in the column space of
042: *  the input M-by-N matrix A. The pivot pairs are taken from the (1,2)
043: *  off-diagonal block in the corresponding N-by-N Gram matrix A^T * A. The
044: *  block-entries (tiles) of the (1,2) off-diagonal block are marked by the
045: *  [x]'s in the following scheme:
046: *
047: *     | *   *   * [x] [x] [x]|
048: *     | *   *   * [x] [x] [x]|    Row-cycling in the nblr-by-nblc [x] blocks.
049: *     | *   *   * [x] [x] [x]|    Row-cyclic pivoting inside each [x] block.
050: *     |[x] [x] [x] *   *   * |
051: *     |[x] [x] [x] *   *   * |
052: *     |[x] [x] [x] *   *   * |
053: *
054: *  In terms of the columns of A, the first N1 columns are rotated 'against'
055: *  the remaining N-N1 columns, trying to increase the angle between the
056: *  corresponding subspaces. The off-diagonal block is N1-by(N-N1) and it is
057: *  tiled using quadratic tiles of side KBL. Here, KBL is a tunning parmeter.
058: *  The number of sweeps is given in NSWEEP and the orthogonality threshold
059: *  is given in TOL.
060: *
061: *  Contributors
062: *  ~~~~~~~~~~~~
063: *  Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany)
064: *
065: *  Arguments
066: *  =========
067: *
068: *  JOBV    (input) CHARACTER*1
069: *          Specifies whether the output from this procedure is used
070: *          to compute the matrix V:
071: *          = 'V': the product of the Jacobi rotations is accumulated
072: *                 by postmulyiplying the N-by-N array V.
073: *                (See the description of V.)
074: *          = 'A': the product of the Jacobi rotations is accumulated
075: *                 by postmulyiplying the MV-by-N array V.
076: *                (See the descriptions of MV and V.)
077: *          = 'N': the Jacobi rotations are not accumulated.
078: *
079: *  M       (input) INTEGER
080: *          The number of rows of the input matrix A.  M >= 0.
081: *
082: *  N       (input) INTEGER
083: *          The number of columns of the input matrix A.
084: *          M >= N >= 0.
085: *
086: *  N1      (input) INTEGER
087: *          N1 specifies the 2 x 2 block partition, the first N1 columns are
088: *          rotated 'against' the remaining N-N1 columns of A.
089: *
090: *  A       (input/output) REAL array, dimension (LDA,N)
091: *          On entry, M-by-N matrix A, such that A*diag(D) represents
092: *          the input matrix.
093: *          On exit,
094: *          A_onexit * D_onexit represents the input matrix A*diag(D)
095: *          post-multiplied by a sequence of Jacobi rotations, where the
096: *          rotation threshold and the total number of sweeps are given in
097: *          TOL and NSWEEP, respectively.
098: *          (See the descriptions of N1, D, TOL and NSWEEP.)
099: *
100: *  LDA     (input) INTEGER
101: *          The leading dimension of the array A.  LDA >= max(1,M).
102: *
103: *  D       (input/workspace/output) REAL array, dimension (N)
104: *          The array D accumulates the scaling factors from the fast scaled
105: *          Jacobi rotations.
106: *          On entry, A*diag(D) represents the input matrix.
107: *          On exit, A_onexit*diag(D_onexit) represents the input matrix
108: *          post-multiplied by a sequence of Jacobi rotations, where the
109: *          rotation threshold and the total number of sweeps are given in
110: *          TOL and NSWEEP, respectively.
111: *          (See the descriptions of N1, A, TOL and NSWEEP.)
112: *
113: *  SVA     (input/workspace/output) REAL array, dimension (N)
114: *          On entry, SVA contains the Euclidean norms of the columns of
115: *          the matrix A*diag(D).
116: *          On exit, SVA contains the Euclidean norms of the columns of
117: *          the matrix onexit*diag(D_onexit).
118: *
119: *  MV      (input) INTEGER
120: *          If JOBV .EQ. 'A', then MV rows of V are post-multipled by a
121: *                           sequence of Jacobi rotations.
122: *          If JOBV = 'N',   then MV is not referenced.
123: *
124: *  V       (input/output) REAL array, dimension (LDV,N)
125: *          If JOBV .EQ. 'V' then N rows of V are post-multipled by a
126: *                           sequence of Jacobi rotations.
127: *          If JOBV .EQ. 'A' then MV rows of V are post-multipled by a
128: *                           sequence of Jacobi rotations.
129: *          If JOBV = 'N',   then V is not referenced.
130: *
131: *  LDV     (input) INTEGER
132: *          The leading dimension of the array V,  LDV >= 1.
133: *          If JOBV = 'V', LDV .GE. N.
134: *          If JOBV = 'A', LDV .GE. MV.
135: *
136: *  EPS     (input) INTEGER
137: *          EPS = SLAMCH('Epsilon')
138: *
139: *  SFMIN   (input) INTEGER
140: *          SFMIN = SLAMCH('Safe Minimum')
141: *
142: *  TOL     (input) REAL
143: *          TOL is the threshold for Jacobi rotations. For a pair
144: *          A(:,p), A(:,q) of pivot columns, the Jacobi rotation is
145: *          applied only if ABS(COS(angle(A(:,p),A(:,q)))) .GT. TOL.
146: *
147: *  NSWEEP  (input) INTEGER
148: *          NSWEEP is the number of sweeps of Jacobi rotations to be
149: *          performed.
150: *
151: *  WORK    (workspace) REAL array, dimension LWORK.
152: *
153: *  LWORK   (input) INTEGER
154: *          LWORK is the dimension of WORK. LWORK .GE. M.
155: *
156: *  INFO    (output) INTEGER
157: *          = 0 : successful exit.
158: *          < 0 : if INFO = -i, then the i-th argument had an illegal value
159: *
160: *  =====================================================================
161: *
162: *     .. Local Parameters ..
163:       REAL               ZERO, HALF, ONE, TWO
164:       PARAMETER          ( ZERO = 0.0E0, HALF = 0.5E0, ONE = 1.0E0,
165:      +                   TWO = 2.0E0 )
166: *     ..
167: *     .. Local Scalars ..
168:       REAL               AAPP, AAPP0, AAPQ, AAQQ, APOAQ, AQOAP, BIG,
169:      +                   BIGTHETA, CS, LARGE, MXAAPQ, MXSINJ, ROOTBIG,
170:      +                   ROOTEPS, ROOTSFMIN, ROOTTOL, SMALL, SN, T,
171:      +                   TEMP1, THETA, THSIGN
172:       INTEGER            BLSKIP, EMPTSW, i, ibr, igl, IERR, IJBLSK,
173:      +                   ISWROT, jbc, jgl, KBL, MVL, NOTROT, nblc, nblr,
174:      +                   p, PSKIPPED, q, ROWSKIP, SWBAND
175:       LOGICAL            APPLV, ROTOK, RSVEC
176: *     ..
177: *     .. Local Arrays ..
178:       REAL               FASTR( 5 )
179: *     ..
180: *     .. Intrinsic Functions ..
181:       INTRINSIC          ABS, AMAX1, FLOAT, MIN0, SIGN, SQRT
182: *     ..
183: *     .. External Functions ..
184:       REAL               SDOT, SNRM2
185:       INTEGER            ISAMAX
186:       LOGICAL            LSAME
187:       EXTERNAL           ISAMAX, LSAME, SDOT, SNRM2
188: *     ..
189: *     .. External Subroutines ..
190:       EXTERNAL           SAXPY, SCOPY, SLASCL, SLASSQ, SROTM, SSWAP
191: *     ..
192: *     .. Executable Statements ..
193: *
194: *     Test the input parameters.
195: *
196:       APPLV = LSAME( JOBV, 'A' )
197:       RSVEC = LSAME( JOBV, 'V' )
198:       IF( .NOT.( RSVEC .OR. APPLV .OR. LSAME( JOBV, 'N' ) ) ) THEN
199:          INFO = -1
200:       ELSE IF( M.LT.0 ) THEN
201:          INFO = -2
202:       ELSE IF( ( N.LT.0 ) .OR. ( N.GT.M ) ) THEN
203:          INFO = -3
204:       ELSE IF( N1.LT.0 ) THEN
205:          INFO = -4
206:       ELSE IF( LDA.LT.M ) THEN
207:          INFO = -6
208:       ELSE IF( MV.LT.0 ) THEN
209:          INFO = -9
210:       ELSE IF( LDV.LT.M ) THEN
211:          INFO = -11
212:       ELSE IF( TOL.LE.EPS ) THEN
213:          INFO = -14
214:       ELSE IF( NSWEEP.LT.0 ) THEN
215:          INFO = -15
216:       ELSE IF( LWORK.LT.M ) THEN
217:          INFO = -17
218:       ELSE
219:          INFO = 0
220:       END IF
221: *
222: *     #:(
223:       IF( INFO.NE.0 ) THEN
224:          CALL XERBLA( 'SGSVJ1', -INFO )
225:          RETURN
226:       END IF
227: *
228:       IF( RSVEC ) THEN
229:          MVL = N
230:       ELSE IF( APPLV ) THEN
231:          MVL = MV
232:       END IF
233:       RSVEC = RSVEC .OR. APPLV
234: 
235:       ROOTEPS = SQRT( EPS )
236:       ROOTSFMIN = SQRT( SFMIN )
237:       SMALL = SFMIN / EPS
238:       BIG = ONE / SFMIN
239:       ROOTBIG = ONE / ROOTSFMIN
240:       LARGE = BIG / SQRT( FLOAT( M*N ) )
241:       BIGTHETA = ONE / ROOTEPS
242:       ROOTTOL = SQRT( TOL )
243: *
244: *     .. Initialize the right singular vector matrix ..
245: *
246: *     RSVEC = LSAME( JOBV, 'Y' )
247: *
248:       EMPTSW = N1*( N-N1 )
249:       NOTROT = 0
250:       FASTR( 1 ) = ZERO
251: *
252: *     .. Row-cyclic pivot strategy with de Rijk's pivoting ..
253: *
254:       KBL = MIN0( 8, N )
255:       NBLR = N1 / KBL
256:       IF( ( NBLR*KBL ).NE.N1 )NBLR = NBLR + 1
257: 
258: *     .. the tiling is nblr-by-nblc [tiles]
259: 
260:       NBLC = ( N-N1 ) / KBL
261:       IF( ( NBLC*KBL ).NE.( N-N1 ) )NBLC = NBLC + 1
262:       BLSKIP = ( KBL**2 ) + 1
263: *[TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL.
264: 
265:       ROWSKIP = MIN0( 5, KBL )
266: *[TP] ROWSKIP is a tuning parameter.
267:       SWBAND = 0
268: *[TP] SWBAND is a tuning parameter. It is meaningful and effective
269: *     if SGESVJ is used as a computational routine in the preconditioned
270: *     Jacobi SVD algorithm SGESVJ.
271: *
272: *
273: *     | *   *   * [x] [x] [x]|
274: *     | *   *   * [x] [x] [x]|    Row-cycling in the nblr-by-nblc [x] blocks.
275: *     | *   *   * [x] [x] [x]|    Row-cyclic pivoting inside each [x] block.
276: *     |[x] [x] [x] *   *   * |
277: *     |[x] [x] [x] *   *   * |
278: *     |[x] [x] [x] *   *   * |
279: *
280: *
281:       DO 1993 i = 1, NSWEEP
282: *     .. go go go ...
283: *
284:          MXAAPQ = ZERO
285:          MXSINJ = ZERO
286:          ISWROT = 0
287: *
288:          NOTROT = 0
289:          PSKIPPED = 0
290: *
291:          DO 2000 ibr = 1, NBLR
292: 
293:             igl = ( ibr-1 )*KBL + 1
294: *
295: *
296: *........................................................
297: * ... go to the off diagonal blocks
298: 
299:             igl = ( ibr-1 )*KBL + 1
300: 
301:             DO 2010 jbc = 1, NBLC
302: 
303:                jgl = N1 + ( jbc-1 )*KBL + 1
304: 
305: *        doing the block at ( ibr, jbc )
306: 
307:                IJBLSK = 0
308:                DO 2100 p = igl, MIN0( igl+KBL-1, N1 )
309: 
310:                   AAPP = SVA( p )
311: 
312:                   IF( AAPP.GT.ZERO ) THEN
313: 
314:                      PSKIPPED = 0
315: 
316:                      DO 2200 q = jgl, MIN0( jgl+KBL-1, N )
317: *
318:                         AAQQ = SVA( q )
319: 
320:                         IF( AAQQ.GT.ZERO ) THEN
321:                            AAPP0 = AAPP
322: *
323: *     .. M x 2 Jacobi SVD ..
324: *
325: *        .. Safe Gram matrix computation ..
326: *
327:                            IF( AAQQ.GE.ONE ) THEN
328:                               IF( AAPP.GE.AAQQ ) THEN
329:                                  ROTOK = ( SMALL*AAPP ).LE.AAQQ
330:                               ELSE
331:                                  ROTOK = ( SMALL*AAQQ ).LE.AAPP
332:                               END IF
333:                               IF( AAPP.LT.( BIG / AAQQ ) ) THEN
334:                                  AAPQ = ( SDOT( M, A( 1, p ), 1, A( 1,
335:      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
336:      +                                  / AAPP
337:                               ELSE
338:                                  CALL SCOPY( M, A( 1, p ), 1, WORK, 1 )
339:                                  CALL SLASCL( 'G', 0, 0, AAPP, D( p ),
340:      +                                        M, 1, WORK, LDA, IERR )
341:                                  AAPQ = SDOT( M, WORK, 1, A( 1, q ),
342:      +                                  1 )*D( q ) / AAQQ
343:                               END IF
344:                            ELSE
345:                               IF( AAPP.GE.AAQQ ) THEN
346:                                  ROTOK = AAPP.LE.( AAQQ / SMALL )
347:                               ELSE
348:                                  ROTOK = AAQQ.LE.( AAPP / SMALL )
349:                               END IF
350:                               IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
351:                                  AAPQ = ( SDOT( M, A( 1, p ), 1, A( 1,
352:      +                                  q ), 1 )*D( p )*D( q ) / AAQQ )
353:      +                                  / AAPP
354:                               ELSE
355:                                  CALL SCOPY( M, A( 1, q ), 1, WORK, 1 )
356:                                  CALL SLASCL( 'G', 0, 0, AAQQ, D( q ),
357:      +                                        M, 1, WORK, LDA, IERR )
358:                                  AAPQ = SDOT( M, WORK, 1, A( 1, p ),
359:      +                                  1 )*D( p ) / AAPP
360:                               END IF
361:                            END IF
362: 
363:                            MXAAPQ = AMAX1( MXAAPQ, ABS( AAPQ ) )
364: 
365: *        TO rotate or NOT to rotate, THAT is the question ...
366: *
367:                            IF( ABS( AAPQ ).GT.TOL ) THEN
368:                               NOTROT = 0
369: *           ROTATED  = ROTATED + 1
370:                               PSKIPPED = 0
371:                               ISWROT = ISWROT + 1
372: *
373:                               IF( ROTOK ) THEN
374: *
375:                                  AQOAP = AAQQ / AAPP
376:                                  APOAQ = AAPP / AAQQ
377:                                  THETA = -HALF*ABS( AQOAP-APOAQ ) / AAPQ
378:                                  IF( AAQQ.GT.AAPP0 )THETA = -THETA
379: 
380:                                  IF( ABS( THETA ).GT.BIGTHETA ) THEN
381:                                     T = HALF / THETA
382:                                     FASTR( 3 ) = T*D( p ) / D( q )
383:                                     FASTR( 4 ) = -T*D( q ) / D( p )
384:                                     CALL SROTM( M, A( 1, p ), 1,
385:      +                                          A( 1, q ), 1, FASTR )
386:                                     IF( RSVEC )CALL SROTM( MVL,
387:      +                                              V( 1, p ), 1,
388:      +                                              V( 1, q ), 1,
389:      +                                              FASTR )
390:                                     SVA( q ) = AAQQ*SQRT( AMAX1( ZERO,
391:      +                                         ONE+T*APOAQ*AAPQ ) )
392:                                     AAPP = AAPP*SQRT( AMAX1( ZERO,
393:      +                                     ONE-T*AQOAP*AAPQ ) )
394:                                     MXSINJ = AMAX1( MXSINJ, ABS( T ) )
395:                                  ELSE
396: *
397: *                 .. choose correct signum for THETA and rotate
398: *
399:                                     THSIGN = -SIGN( ONE, AAPQ )
400:                                     IF( AAQQ.GT.AAPP0 )THSIGN = -THSIGN
401:                                     T = ONE / ( THETA+THSIGN*
402:      +                                  SQRT( ONE+THETA*THETA ) )
403:                                     CS = SQRT( ONE / ( ONE+T*T ) )
404:                                     SN = T*CS
405:                                     MXSINJ = AMAX1( MXSINJ, ABS( SN ) )
406:                                     SVA( q ) = AAQQ*SQRT( AMAX1( ZERO,
407:      +                                         ONE+T*APOAQ*AAPQ ) )
408:                                     AAPP = AAPP*SQRT( ONE-T*AQOAP*AAPQ )
409: 
410:                                     APOAQ = D( p ) / D( q )
411:                                     AQOAP = D( q ) / D( p )
412:                                     IF( D( p ).GE.ONE ) THEN
413: *
414:                                        IF( D( q ).GE.ONE ) THEN
415:                                           FASTR( 3 ) = T*APOAQ
416:                                           FASTR( 4 ) = -T*AQOAP
417:                                           D( p ) = D( p )*CS
418:                                           D( q ) = D( q )*CS
419:                                           CALL SROTM( M, A( 1, p ), 1,
420:      +                                                A( 1, q ), 1,
421:      +                                                FASTR )
422:                                           IF( RSVEC )CALL SROTM( MVL,
423:      +                                        V( 1, p ), 1, V( 1, q ),
424:      +                                        1, FASTR )
425:                                        ELSE
426:                                           CALL SAXPY( M, -T*AQOAP,
427:      +                                                A( 1, q ), 1,
428:      +                                                A( 1, p ), 1 )
429:                                           CALL SAXPY( M, CS*SN*APOAQ,
430:      +                                                A( 1, p ), 1,
431:      +                                                A( 1, q ), 1 )
432:                                           IF( RSVEC ) THEN
433:                                              CALL SAXPY( MVL, -T*AQOAP,
434:      +                                                   V( 1, q ), 1,
435:      +                                                   V( 1, p ), 1 )
436:                                              CALL SAXPY( MVL,
437:      +                                                   CS*SN*APOAQ,
438:      +                                                   V( 1, p ), 1,
439:      +                                                   V( 1, q ), 1 )
440:                                           END IF
441:                                           D( p ) = D( p )*CS
442:                                           D( q ) = D( q ) / CS
443:                                        END IF
444:                                     ELSE
445:                                        IF( D( q ).GE.ONE ) THEN
446:                                           CALL SAXPY( M, T*APOAQ,
447:      +                                                A( 1, p ), 1,
448:      +                                                A( 1, q ), 1 )
449:                                           CALL SAXPY( M, -CS*SN*AQOAP,
450:      +                                                A( 1, q ), 1,
451:      +                                                A( 1, p ), 1 )
452:                                           IF( RSVEC ) THEN
453:                                              CALL SAXPY( MVL, T*APOAQ,
454:      +                                                   V( 1, p ), 1,
455:      +                                                   V( 1, q ), 1 )
456:                                              CALL SAXPY( MVL,
457:      +                                                   -CS*SN*AQOAP,
458:      +                                                   V( 1, q ), 1,
459:      +                                                   V( 1, p ), 1 )
460:                                           END IF
461:                                           D( p ) = D( p ) / CS
462:                                           D( q ) = D( q )*CS
463:                                        ELSE
464:                                           IF( D( p ).GE.D( q ) ) THEN
465:                                              CALL SAXPY( M, -T*AQOAP,
466:      +                                                   A( 1, q ), 1,
467:      +                                                   A( 1, p ), 1 )
468:                                              CALL SAXPY( M, CS*SN*APOAQ,
469:      +                                                   A( 1, p ), 1,
470:      +                                                   A( 1, q ), 1 )
471:                                              D( p ) = D( p )*CS
472:                                              D( q ) = D( q ) / CS
473:                                              IF( RSVEC ) THEN
474:                                                 CALL SAXPY( MVL,
475:      +                                               -T*AQOAP,
476:      +                                               V( 1, q ), 1,
477:      +                                               V( 1, p ), 1 )
478:                                                 CALL SAXPY( MVL,
479:      +                                               CS*SN*APOAQ,
480:      +                                               V( 1, p ), 1,
481:      +                                               V( 1, q ), 1 )
482:                                              END IF
483:                                           ELSE
484:                                              CALL SAXPY( M, T*APOAQ,
485:      +                                                   A( 1, p ), 1,
486:      +                                                   A( 1, q ), 1 )
487:                                              CALL SAXPY( M,
488:      +                                                   -CS*SN*AQOAP,
489:      +                                                   A( 1, q ), 1,
490:      +                                                   A( 1, p ), 1 )
491:                                              D( p ) = D( p ) / CS
492:                                              D( q ) = D( q )*CS
493:                                              IF( RSVEC ) THEN
494:                                                 CALL SAXPY( MVL,
495:      +                                               T*APOAQ, V( 1, p ),
496:      +                                               1, V( 1, q ), 1 )
497:                                                 CALL SAXPY( MVL,
498:      +                                               -CS*SN*AQOAP,
499:      +                                               V( 1, q ), 1,
500:      +                                               V( 1, p ), 1 )
501:                                              END IF
502:                                           END IF
503:                                        END IF
504:                                     END IF
505:                                  END IF
506: 
507:                               ELSE
508:                                  IF( AAPP.GT.AAQQ ) THEN
509:                                     CALL SCOPY( M, A( 1, p ), 1, WORK,
510:      +                                          1 )
511:                                     CALL SLASCL( 'G', 0, 0, AAPP, ONE,
512:      +                                           M, 1, WORK, LDA, IERR )
513:                                     CALL SLASCL( 'G', 0, 0, AAQQ, ONE,
514:      +                                           M, 1, A( 1, q ), LDA,
515:      +                                           IERR )
516:                                     TEMP1 = -AAPQ*D( p ) / D( q )
517:                                     CALL SAXPY( M, TEMP1, WORK, 1,
518:      +                                          A( 1, q ), 1 )
519:                                     CALL SLASCL( 'G', 0, 0, ONE, AAQQ,
520:      +                                           M, 1, A( 1, q ), LDA,
521:      +                                           IERR )
522:                                     SVA( q ) = AAQQ*SQRT( AMAX1( ZERO,
523:      +                                         ONE-AAPQ*AAPQ ) )
524:                                     MXSINJ = AMAX1( MXSINJ, SFMIN )
525:                                  ELSE
526:                                     CALL SCOPY( M, A( 1, q ), 1, WORK,
527:      +                                          1 )
528:                                     CALL SLASCL( 'G', 0, 0, AAQQ, ONE,
529:      +                                           M, 1, WORK, LDA, IERR )
530:                                     CALL SLASCL( 'G', 0, 0, AAPP, ONE,
531:      +                                           M, 1, A( 1, p ), LDA,
532:      +                                           IERR )
533:                                     TEMP1 = -AAPQ*D( q ) / D( p )
534:                                     CALL SAXPY( M, TEMP1, WORK, 1,
535:      +                                          A( 1, p ), 1 )
536:                                     CALL SLASCL( 'G', 0, 0, ONE, AAPP,
537:      +                                           M, 1, A( 1, p ), LDA,
538:      +                                           IERR )
539:                                     SVA( p ) = AAPP*SQRT( AMAX1( ZERO,
540:      +                                         ONE-AAPQ*AAPQ ) )
541:                                     MXSINJ = AMAX1( MXSINJ, SFMIN )
542:                                  END IF
543:                               END IF
544: *           END IF ROTOK THEN ... ELSE
545: *
546: *           In the case of cancellation in updating SVA(q)
547: *           .. recompute SVA(q)
548:                               IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
549:      +                            THEN
550:                                  IF( ( AAQQ.LT.ROOTBIG ) .AND.
551:      +                               ( AAQQ.GT.ROOTSFMIN ) ) THEN
552:                                     SVA( q ) = SNRM2( M, A( 1, q ), 1 )*
553:      +                                         D( q )
554:                                  ELSE
555:                                     T = ZERO
556:                                     AAQQ = ZERO
557:                                     CALL SLASSQ( M, A( 1, q ), 1, T,
558:      +                                           AAQQ )
559:                                     SVA( q ) = T*SQRT( AAQQ )*D( q )
560:                                  END IF
561:                               END IF
562:                               IF( ( AAPP / AAPP0 )**2.LE.ROOTEPS ) THEN
563:                                  IF( ( AAPP.LT.ROOTBIG ) .AND.
564:      +                               ( AAPP.GT.ROOTSFMIN ) ) THEN
565:                                     AAPP = SNRM2( M, A( 1, p ), 1 )*
566:      +                                     D( p )
567:                                  ELSE
568:                                     T = ZERO
569:                                     AAPP = ZERO
570:                                     CALL SLASSQ( M, A( 1, p ), 1, T,
571:      +                                           AAPP )
572:                                     AAPP = T*SQRT( AAPP )*D( p )
573:                                  END IF
574:                                  SVA( p ) = AAPP
575:                               END IF
576: *              end of OK rotation
577:                            ELSE
578:                               NOTROT = NOTROT + 1
579: *           SKIPPED  = SKIPPED  + 1
580:                               PSKIPPED = PSKIPPED + 1
581:                               IJBLSK = IJBLSK + 1
582:                            END IF
583:                         ELSE
584:                            NOTROT = NOTROT + 1
585:                            PSKIPPED = PSKIPPED + 1
586:                            IJBLSK = IJBLSK + 1
587:                         END IF
588: 
589: *      IF ( NOTROT .GE. EMPTSW )  GO TO 2011
590:                         IF( ( i.LE.SWBAND ) .AND. ( IJBLSK.GE.BLSKIP ) )
591:      +                      THEN
592:                            SVA( p ) = AAPP
593:                            NOTROT = 0
594:                            GO TO 2011
595:                         END IF
596:                         IF( ( i.LE.SWBAND ) .AND.
597:      +                      ( PSKIPPED.GT.ROWSKIP ) ) THEN
598:                            AAPP = -AAPP
599:                            NOTROT = 0
600:                            GO TO 2203
601:                         END IF
602: 
603: *
604:  2200                CONTINUE
605: *        end of the q-loop
606:  2203                CONTINUE
607: 
608:                      SVA( p ) = AAPP
609: *
610:                   ELSE
611:                      IF( AAPP.EQ.ZERO )NOTROT = NOTROT +
612:      +                   MIN0( jgl+KBL-1, N ) - jgl + 1
613:                      IF( AAPP.LT.ZERO )NOTROT = 0
614: ***      IF ( NOTROT .GE. EMPTSW )  GO TO 2011
615:                   END IF
616: 
617:  2100          CONTINUE
618: *     end of the p-loop
619:  2010       CONTINUE
620: *     end of the jbc-loop
621:  2011       CONTINUE
622: *2011 bailed out of the jbc-loop
623:             DO 2012 p = igl, MIN0( igl+KBL-1, N )
624:                SVA( p ) = ABS( SVA( p ) )
625:  2012       CONTINUE
626: ***   IF ( NOTROT .GE. EMPTSW ) GO TO 1994
627:  2000    CONTINUE
628: *2000 :: end of the ibr-loop
629: *
630: *     .. update SVA(N)
631:          IF( ( SVA( N ).LT.ROOTBIG ) .AND. ( SVA( N ).GT.ROOTSFMIN ) )
632:      +       THEN
633:             SVA( N ) = SNRM2( M, A( 1, N ), 1 )*D( N )
634:          ELSE
635:             T = ZERO
636:             AAPP = ZERO
637:             CALL SLASSQ( M, A( 1, N ), 1, T, AAPP )
638:             SVA( N ) = T*SQRT( AAPP )*D( N )
639:          END IF
640: *
641: *     Additional steering devices
642: *
643:          IF( ( i.LT.SWBAND ) .AND. ( ( MXAAPQ.LE.ROOTTOL ) .OR.
644:      +       ( ISWROT.LE.N ) ) )SWBAND = i
645: 
646:          IF( ( i.GT.SWBAND+1 ) .AND. ( MXAAPQ.LT.FLOAT( N )*TOL ) .AND.
647:      +       ( FLOAT( N )*MXAAPQ*MXSINJ.LT.TOL ) ) THEN
648:             GO TO 1994
649:          END IF
650: 
651: *
652:          IF( NOTROT.GE.EMPTSW )GO TO 1994
653: 
654:  1993 CONTINUE
655: *     end i=1:NSWEEP loop
656: * #:) Reaching this point means that the procedure has completed the given
657: *     number of sweeps.
658:       INFO = NSWEEP - 1
659:       GO TO 1995
660:  1994 CONTINUE
661: * #:) Reaching this point means that during the i-th sweep all pivots were
662: *     below the given threshold, causing early exit.
663: 
664:       INFO = 0
665: * #:) INFO = 0 confirms successful iterations.
666:  1995 CONTINUE
667: *
668: *     Sort the vector D
669: *
670:       DO 5991 p = 1, N - 1
671:          q = ISAMAX( N-p+1, SVA( p ), 1 ) + p - 1
672:          IF( p.NE.q ) THEN
673:             TEMP1 = SVA( p )
674:             SVA( p ) = SVA( q )
675:             SVA( q ) = TEMP1
676:             TEMP1 = D( p )
677:             D( p ) = D( q )
678:             D( q ) = TEMP1
679:             CALL SSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
680:             IF( RSVEC )CALL SSWAP( MVL, V( 1, p ), 1, V( 1, q ), 1 )
681:          END IF
682:  5991 CONTINUE
683: *
684:       RETURN
685: *     ..
686: *     .. END OF SGSVJ1
687: *     ..
688:       END
689: