3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download ZGESVJ + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zgesvj.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zgesvj.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zgesvj.f">
21 * SUBROUTINE ZGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V,
22 * LDV, CWORK, LWORK, RWORK, LRWORK, INFO )
24 * .. Scalar Arguments ..
25 * INTEGER INFO, LDA, LDV, LWORK, LRWORK, M, MV, N
26 * CHARACTER*1 JOBA, JOBU, JOBV
28 * .. Array Arguments ..
29 * COMPLEX*16 A( LDA, * ), V( LDV, * ), CWORK( LWORK )
30 * DOUBLE PRECISION RWORK( LRWORK ), SVA( N )
39 *> ZGESVJ computes the singular value decomposition (SVD) of a complex
40 *> M-by-N matrix A, where M >= N. The SVD of A is written as
41 *> [++] [xx] [x0] [xx]
42 *> A = U * SIGMA * V^*, [++] = [xx] * [ox] * [xx]
44 *> where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal
45 *> matrix, and V is an N-by-N unitary matrix. The diagonal elements
46 *> of SIGMA are the singular values of A. The columns of U and V are the
47 *> left and the right singular vectors of A, respectively.
55 *> JOBA is CHARACTER* 1
56 *> Specifies the structure of A.
57 *> = 'L': The input matrix A is lower triangular;
58 *> = 'U': The input matrix A is upper triangular;
59 *> = 'G': The input matrix A is general M-by-N matrix, M >= N.
64 *> JOBU is CHARACTER*1
65 *> Specifies whether to compute the left singular vectors
67 *> = 'U': The left singular vectors corresponding to the nonzero
68 *> singular values are computed and returned in the leading
69 *> columns of A. See more details in the description of A.
70 *> The default numerical orthogonality threshold is set to
71 *> approximately TOL=CTOL*EPS, CTOL=DSQRT(M), EPS=DLAMCH('E').
72 *> = 'C': Analogous to JOBU='U', except that user can control the
73 *> level of numerical orthogonality of the computed left
74 *> singular vectors. TOL can be set to TOL = CTOL*EPS, where
75 *> CTOL is given on input in the array WORK.
76 *> No CTOL smaller than ONE is allowed. CTOL greater
77 *> than 1 / EPS is meaningless. The option 'C'
78 *> can be used if M*EPS is satisfactory orthogonality
79 *> of the computed left singular vectors, so CTOL=M could
80 *> save few sweeps of Jacobi rotations.
81 *> See the descriptions of A and WORK(1).
82 *> = 'N': The matrix U is not computed. However, see the
88 *> JOBV is CHARACTER*1
89 *> Specifies whether to compute the right singular vectors, that
91 *> = 'V' : the matrix V is computed and returned in the array V
92 *> = 'A' : the Jacobi rotations are applied to the MV-by-N
93 *> array V. In other words, the right singular vector
94 *> matrix V is not computed explicitly, instead it is
95 *> applied to an MV-by-N matrix initially stored in the
96 *> first MV rows of V.
97 *> = 'N' : the matrix V is not computed and the array V is not
104 *> The number of rows of the input matrix A. 1/DLAMCH('E') > M >= 0.
110 *> The number of columns of the input matrix A.
116 *> A is COMPLEX*16 array, dimension (LDA,N)
117 *> On entry, the M-by-N matrix A.
119 *> If JOBU .EQ. 'U' .OR. JOBU .EQ. 'C':
121 *> RANKA orthonormal columns of U are returned in the
122 *> leading RANKA columns of the array A. Here RANKA <= N
123 *> is the number of computed singular values of A that are
124 *> above the underflow threshold DLAMCH('S'). The singular
125 *> vectors corresponding to underflowed or zero singular
126 *> values are not computed. The value of RANKA is returned
127 *> in the array RWORK as RANKA=NINT(RWORK(2)). Also see the
128 *> descriptions of SVA and RWORK. The computed columns of U
129 *> are mutually numerically orthogonal up to approximately
130 *> TOL=SQRT(M)*EPS (default); or TOL=CTOL*EPS (JOBU.EQ.'C'),
131 *> see the description of JOBU.
133 *> the procedure ZGESVJ did not converge in the given number
134 *> of iterations (sweeps). In that case, the computed
135 *> columns of U may not be orthogonal up to TOL. The output
136 *> U (stored in A), SIGMA (given by the computed singular
137 *> values in SVA(1:N)) and V is still a decomposition of the
138 *> input matrix A in the sense that the residual
139 *> || A - SCALE * U * SIGMA * V^* ||_2 / ||A||_2 is small.
142 *> Note that the left singular vectors are 'for free' in the
143 *> one-sided Jacobi SVD algorithm. However, if only the
144 *> singular values are needed, the level of numerical
145 *> orthogonality of U is not an issue and iterations are
146 *> stopped when the columns of the iterated matrix are
147 *> numerically orthogonal up to approximately M*EPS. Thus,
148 *> on exit, A contains the columns of U scaled with the
149 *> corresponding singular values.
151 *> the procedure ZGESVJ did not converge in the given number
152 *> of iterations (sweeps).
158 *> The leading dimension of the array A. LDA >= max(1,M).
163 *> SVA is DOUBLE PRECISION array, dimension (N)
166 *> depending on the value SCALE = RWORK(1), we have:
167 *> If SCALE .EQ. ONE:
168 *> SVA(1:N) contains the computed singular values of A.
169 *> During the computation SVA contains the Euclidean column
170 *> norms of the iterated matrices in the array A.
171 *> If SCALE .NE. ONE:
172 *> The singular values of A are SCALE*SVA(1:N), and this
173 *> factored representation is due to the fact that some of the
174 *> singular values of A might underflow or overflow.
177 *> the procedure ZGESVJ did not converge in the given number of
178 *> iterations (sweeps) and SCALE*SVA(1:N) may not be accurate.
184 *> If JOBV .EQ. 'A', then the product of Jacobi rotations in ZGESVJ
185 *> is applied to the first MV rows of V. See the description of JOBV.
190 *> V is COMPLEX*16 array, dimension (LDV,N)
191 *> If JOBV = 'V', then V contains on exit the N-by-N matrix of
192 *> the right singular vectors;
193 *> If JOBV = 'A', then V contains the product of the computed right
194 *> singular vector matrix and the initial matrix in
196 *> If JOBV = 'N', then V is not referenced.
202 *> The leading dimension of the array V, LDV .GE. 1.
203 *> If JOBV .EQ. 'V', then LDV .GE. max(1,N).
204 *> If JOBV .EQ. 'A', then LDV .GE. max(1,MV) .
207 *> \param[in,out] CWORK
209 *> CWORK is COMPLEX*16 array, dimension M+N.
210 *> Used as work space.
216 *> Length of CWORK, LWORK >= M+N.
219 *> \param[in,out] RWORK
221 *> RWORK is DOUBLE PRECISION array, dimension max(6,M+N).
223 *> If JOBU .EQ. 'C' :
224 *> RWORK(1) = CTOL, where CTOL defines the threshold for convergence.
225 *> The process stops if all columns of A are mutually
226 *> orthogonal up to CTOL*EPS, EPS=DLAMCH('E').
227 *> It is required that CTOL >= ONE, i.e. it is not
228 *> allowed to force the routine to obtain orthogonality
231 *> RWORK(1) = SCALE is the scaling factor such that SCALE*SVA(1:N)
232 *> are the computed singular values of A.
233 *> (See description of SVA().)
234 *> RWORK(2) = NINT(RWORK(2)) is the number of the computed nonzero
236 *> RWORK(3) = NINT(RWORK(3)) is the number of the computed singular
237 *> values that are larger than the underflow threshold.
238 *> RWORK(4) = NINT(RWORK(4)) is the number of sweeps of Jacobi
239 *> rotations needed for numerical convergence.
240 *> RWORK(5) = max_{i.NE.j} |COS(A(:,i),A(:,j))| in the last sweep.
241 *> This is useful information in cases when ZGESVJ did
242 *> not converge, as it can be used to estimate whether
243 *> the output is stil useful and for post festum analysis.
244 *> RWORK(6) = the largest absolute value over all sines of the
245 *> Jacobi rotation angles in the last sweep. It can be
246 *> useful for a post festum analysis.
252 *> Length of RWORK, LRWORK >= MAX(6,N).
258 *> = 0 : successful exit.
259 *> < 0 : if INFO = -i, then the i-th argument had an illegal value
260 *> > 0 : ZGESVJ did not converge in the maximal allowed number
261 *> (NSWEEP=30) of sweeps. The output may still be useful.
262 *> See the description of RWORK.
268 *> \author Univ. of Tennessee
269 *> \author Univ. of California Berkeley
270 *> \author Univ. of Colorado Denver
275 *> \ingroup doubleGEcomputational
277 *> \par Further Details:
278 * =====================
282 *> The orthogonal N-by-N matrix V is obtained as a product of Jacobi plane
283 *> rotations. In the case of underflow of the tangent of the Jacobi angle, a
284 *> modified Jacobi transformation of Drmac [3] is used. Pivot strategy uses
285 *> column interchanges of de Rijk [1]. The relative accuracy of the computed
286 *> singular values and the accuracy of the computed singular vectors (in
287 *> angle metric) is as guaranteed by the theory of Demmel and Veselic [2].
288 *> The condition number that determines the accuracy in the full rank case
289 *> is essentially min_{D=diag} kappa(A*D), where kappa(.) is the
290 *> spectral condition number. The best performance of this Jacobi SVD
291 *> procedure is achieved if used in an accelerated version of Drmac and
292 *> Veselic [4,5], and it is the kernel routine in the SIGMA library [6].
293 *> Some tunning parameters (marked with [TP]) are available for the
295 *> The computational range for the nonzero singular values is the machine
296 *> number interval ( UNDERFLOW , OVERFLOW ). In extreme cases, even
297 *> denormalized singular values can be computed with the corresponding
298 *> gradual loss of accurate digits.
301 *> \par Contributors:
308 *> Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany)
314 *> [1] P. P. M. De Rijk: A one-sided Jacobi algorithm for computing the
315 *> singular value decomposition on a vector computer.
316 *> SIAM J. Sci. Stat. Comp., Vol. 10 (1998), pp. 359-371.
317 *> [2] J. Demmel and K. Veselic: Jacobi method is more accurate than QR.
318 *> [3] Z. Drmac: Implementation of Jacobi rotations for accurate singular
319 *> value computation in floating point arithmetic.
320 *> SIAM J. Sci. Comp., Vol. 18 (1997), pp. 1200-1222.
321 *> [4] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I.
322 *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342.
323 *> LAPACK Working note 169.
324 *> [5] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II.
325 *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362.
326 *> LAPACK Working note 170.
327 *> [6] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV,
328 *> QSVD, (H,K)-SVD computations.
329 *> Department of Mathematics, University of Zagreb, 2008, 2015.
332 *> \par Bugs, examples and comments:
333 * =================================
336 *> ===========================
337 *> Please report all bugs and send interesting test examples and comments to
338 *> drmac@math.hr. Thank you.
341 * =====================================================================
342 SUBROUTINE ZGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V,
343 $ LDV, CWORK, LWORK, RWORK, LRWORK, INFO )
345 * -- LAPACK computational routine (version 3.6.1) --
346 * -- LAPACK is a software package provided by Univ. of Tennessee, --
347 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
351 * .. Scalar Arguments ..
352 INTEGER INFO, LDA, LDV, LWORK, LRWORK, M, MV, N
353 CHARACTER*1 JOBA, JOBU, JOBV
355 * .. Array Arguments ..
356 COMPLEX*16 A( LDA, * ), V( LDV, * ), CWORK( LWORK )
357 DOUBLE PRECISION RWORK( LRWORK ), SVA( N )
360 * =====================================================================
362 * .. Local Parameters ..
363 DOUBLE PRECISION ZERO, HALF, ONE
364 PARAMETER ( ZERO = 0.0D0, HALF = 0.5D0, ONE = 1.0D0)
365 COMPLEX*16 CZERO, CONE
366 PARAMETER ( CZERO = (0.0D0, 0.0D0), CONE = (1.0D0, 0.0D0) )
368 PARAMETER ( NSWEEP = 30 )
370 * .. Local Scalars ..
371 COMPLEX*16 AAPQ, OMPQ
372 DOUBLE PRECISION AAPP, AAPP0, AAPQ1, AAQQ, APOAQ, AQOAP, BIG,
373 $ BIGTHETA, CS, CTOL, EPSLN, LARGE, MXAAPQ,
374 $ MXSINJ, ROOTBIG, ROOTEPS, ROOTSFMIN, ROOTTOL,
375 $ SKL, SFMIN, SMALL, SN, T, TEMP1, THETA, THSIGN, TOL
376 INTEGER BLSKIP, EMPTSW, i, ibr, IERR, igl, IJBLSK, ir1,
377 $ ISWROT, jbc, jgl, KBL, LKAHEAD, MVL, N2, N34,
378 $ N4, NBL, NOTROT, p, PSKIPPED, q, ROWSKIP, SWBAND
379 LOGICAL APPLV, GOSCALE, LOWER, LSVEC, NOSCALE, ROTOK,
380 $ RSVEC, UCTOL, UPPER
383 * .. Intrinsic Functions ..
384 INTRINSIC ABS, DMAX1, DMIN1, DCONJG, DBLE, MIN0, MAX0,
387 * .. External Functions ..
390 DOUBLE PRECISION DZNRM2
392 EXTERNAL ZDOTC, DZNRM2
396 DOUBLE PRECISION DLAMCH
401 * .. External Subroutines ..
404 EXTERNAL ZCOPY, ZROT, ZDSCAL, ZSWAP
406 EXTERNAL DLASCL, ZLASCL, ZLASET, ZLASSQ, XERBLA
407 EXTERNAL ZGSVJ0, ZGSVJ1
409 * .. Executable Statements ..
411 * Test the input arguments
413 LSVEC = LSAME( JOBU, 'U' )
414 UCTOL = LSAME( JOBU, 'C' )
415 RSVEC = LSAME( JOBV, 'V' )
416 APPLV = LSAME( JOBV, 'A' )
417 UPPER = LSAME( JOBA, 'U' )
418 LOWER = LSAME( JOBA, 'L' )
420 IF( .NOT.( UPPER .OR. LOWER .OR. LSAME( JOBA, 'G' ) ) ) THEN
422 ELSE IF( .NOT.( LSVEC .OR. UCTOL .OR. LSAME( JOBU, 'N' ) ) ) THEN
424 ELSE IF( .NOT.( RSVEC .OR. APPLV .OR. LSAME( JOBV, 'N' ) ) ) THEN
426 ELSE IF( M.LT.0 ) THEN
428 ELSE IF( ( N.LT.0 ) .OR. ( N.GT.M ) ) THEN
430 ELSE IF( LDA.LT.M ) THEN
432 ELSE IF( MV.LT.0 ) THEN
434 ELSE IF( ( RSVEC .AND. ( LDV.LT.N ) ) .OR.
435 $ ( APPLV .AND. ( LDV.LT.MV ) ) ) THEN
437 ELSE IF( UCTOL .AND. ( RWORK( 1 ).LE.ONE ) ) THEN
439 ELSE IF( LWORK.LT.( M+N ) ) THEN
441 ELSE IF( LRWORK.LT.MAX0( N, 6 ) ) THEN
449 CALL XERBLA( 'ZGESVJ', -INFO )
453 * #:) Quick return for void matrix
455 IF( ( M.EQ.0 ) .OR. ( N.EQ.0 ) )RETURN
457 * Set numerical parameters
458 * The stopping criterion for Jacobi rotations is
460 * max_{i<>j}|A(:,i)^* * A(:,j)| / (||A(:,i)||*||A(:,j)||) < CTOL*EPS
462 * where EPS is the round-off and CTOL is defined as follows:
465 * ... user controlled
469 IF( LSVEC .OR. RSVEC .OR. APPLV ) THEN
470 CTOL = DSQRT( DBLE( M ) )
475 * ... and the machine dependent parameters are
476 *[!] (Make sure that DLAMCH() works properly on the target machine.)
478 EPSLN = DLAMCH( 'Epsilon' )
479 ROOTEPS = DSQRT( EPSLN )
480 SFMIN = DLAMCH( 'SafeMinimum' )
481 ROOTSFMIN = DSQRT( SFMIN )
482 SMALL = SFMIN / EPSLN
483 BIG = DLAMCH( 'Overflow' )
485 ROOTBIG = ONE / ROOTSFMIN
486 LARGE = BIG / DSQRT( DBLE( M*N ) )
487 BIGTHETA = ONE / ROOTEPS
490 ROOTTOL = DSQRT( TOL )
492 IF( DBLE( M )*EPSLN.GE.ONE ) THEN
494 CALL XERBLA( 'ZGESVJ', -INFO )
498 * Initialize the right singular vector matrix.
502 CALL ZLASET( 'A', MVL, N, CZERO, CONE, V, LDV )
503 ELSE IF( APPLV ) THEN
506 RSVEC = RSVEC .OR. APPLV
508 * Initialize SVA( 1:N ) = ( ||A e_i||_2, i = 1:N )
509 *(!) If necessary, scale A to protect the largest singular value
510 * from overflow. It is possible that saving the largest singular
511 * value destroys the information about the small ones.
512 * This initial scaling is almost minimal in the sense that the
513 * goal is to make sure that no column norm overflows, and that
514 * SQRT(N)*max_i SVA(i) does not overflow. If INFinite entries
515 * in A are detected, the procedure returns with INFO=-6.
517 SKL = ONE / DSQRT( DBLE( M )*DBLE( N ) )
522 * the input matrix is M-by-N lower triangular (trapezoidal)
526 CALL ZLASSQ( M-p+1, A( p, p ), 1, AAPP, AAQQ )
527 IF( AAPP.GT.BIG ) THEN
529 CALL XERBLA( 'ZGESVJ', -INFO )
533 IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
537 SVA( p ) = AAPP*( AAQQ*SKL )
541 SVA( q ) = SVA( q )*SKL
546 ELSE IF( UPPER ) THEN
547 * the input matrix is M-by-N upper triangular (trapezoidal)
551 CALL ZLASSQ( p, A( 1, p ), 1, AAPP, AAQQ )
552 IF( AAPP.GT.BIG ) THEN
554 CALL XERBLA( 'ZGESVJ', -INFO )
558 IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
562 SVA( p ) = AAPP*( AAQQ*SKL )
566 SVA( q ) = SVA( q )*SKL
572 * the input matrix is M-by-N general dense
576 CALL ZLASSQ( M, A( 1, p ), 1, AAPP, AAQQ )
577 IF( AAPP.GT.BIG ) THEN
579 CALL XERBLA( 'ZGESVJ', -INFO )
583 IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
587 SVA( p ) = AAPP*( AAQQ*SKL )
591 SVA( q ) = SVA( q )*SKL
598 IF( NOSCALE )SKL = ONE
600 * Move the smaller part of the spectrum from the underflow threshold
601 *(!) Start by determining the position of the nonzero entries of the
602 * array SVA() relative to ( SFMIN, BIG ).
607 IF( SVA( p ).NE.ZERO )AAQQ = DMIN1( AAQQ, SVA( p ) )
608 AAPP = DMAX1( AAPP, SVA( p ) )
611 * #:) Quick return for zero matrix
613 IF( AAPP.EQ.ZERO ) THEN
614 IF( LSVEC )CALL ZLASET( 'G', M, N, CZERO, CONE, A, LDA )
624 * #:) Quick return for one-column matrix
627 IF( LSVEC )CALL ZLASCL( 'G', 0, 0, SVA( 1 ), SKL, M, 1,
628 $ A( 1, 1 ), LDA, IERR )
629 RWORK( 1 ) = ONE / SKL
630 IF( SVA( 1 ).GE.SFMIN ) THEN
642 * Protect small singular values from underflow, and try to
643 * avoid underflows/overflows in computing Jacobi rotations.
645 SN = DSQRT( SFMIN / EPSLN )
646 TEMP1 = DSQRT( BIG / DBLE( N ) )
647 IF( ( AAPP.LE.SN ) .OR. ( AAQQ.GE.TEMP1 ) .OR.
648 $ ( ( SN.LE.AAQQ ) .AND. ( AAPP.LE.TEMP1 ) ) ) THEN
649 TEMP1 = DMIN1( BIG, TEMP1 / AAPP )
652 ELSE IF( ( AAQQ.LE.SN ) .AND. ( AAPP.LE.TEMP1 ) ) THEN
653 TEMP1 = DMIN1( SN / AAQQ, BIG / (AAPP*DSQRT( DBLE(N)) ) )
656 ELSE IF( ( AAQQ.GE.SN ) .AND. ( AAPP.GE.TEMP1 ) ) THEN
657 TEMP1 = DMAX1( SN / AAQQ, TEMP1 / AAPP )
660 ELSE IF( ( AAQQ.LE.SN ) .AND. ( AAPP.GE.TEMP1 ) ) THEN
661 TEMP1 = DMIN1( SN / AAQQ, BIG / ( DSQRT( DBLE( N ) )*AAPP ) )
668 * Scale, if necessary
670 IF( TEMP1.NE.ONE ) THEN
671 CALL DLASCL( 'G', 0, 0, ONE, TEMP1, N, 1, SVA, N, IERR )
674 IF( SKL.NE.ONE ) THEN
675 CALL ZLASCL( JOBA, 0, 0, ONE, SKL, M, N, A, LDA, IERR )
679 * Row-cyclic Jacobi SVD algorithm with column pivoting
681 EMPTSW = ( N*( N-1 ) ) / 2
691 *[TP] SWBAND is a tuning parameter [TP]. It is meaningful and effective
692 * if ZGESVJ is used as a computational routine in the preconditioned
693 * Jacobi SVD algorithm ZGEJSV. For sweeps i=1:SWBAND the procedure
694 * works on pivots inside a band-like region around the diagonal.
695 * The boundaries are determined dynamically, based on the number of
696 * pivots above a threshold.
699 *[TP] KBL is a tuning parameter that defines the tile size in the
700 * tiling of the p-q loops of pivot pairs. In general, an optimal
701 * value of KBL depends on the matrix dimensions and on the
702 * parameters of the computer's memory.
705 IF( ( NBL*KBL ).NE.N )NBL = NBL + 1
708 *[TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL.
710 ROWSKIP = MIN0( 5, KBL )
711 *[TP] ROWSKIP is a tuning parameter.
714 *[TP] LKAHEAD is a tuning parameter.
716 * Quasi block transformations, using the lower (upper) triangular
717 * structure of the input matrix. The quasi-block-cycling usually
718 * invokes cubic convergence. Big part of this cycle is done inside
719 * canonical subspaces of dimensions less than M.
721 IF( ( LOWER .OR. UPPER ) .AND. ( N.GT.MAX0( 64, 4*KBL ) ) ) THEN
722 *[TP] The number of partition levels and the actual partition are
735 * This works very well on lower triangular matrices, in particular
736 * in the framework of the preconditioned Jacobi SVD (xGEJSV).
737 * The idea is simple:
738 * [+ 0 0 0] Note that Jacobi transformations of [0 0]
740 * [+ + x 0] actually work on [x 0] [x 0]
741 * [+ + x x] [x x]. [x x]
743 CALL ZGSVJ0( JOBV, M-N34, N-N34, A( N34+1, N34+1 ), LDA,
744 $ CWORK( N34+1 ), SVA( N34+1 ), MVL,
745 $ V( N34*q+1, N34+1 ), LDV, EPSLN, SFMIN, TOL,
746 $ 2, CWORK( N+1 ), LWORK-N, IERR )
748 CALL ZGSVJ0( JOBV, M-N2, N34-N2, A( N2+1, N2+1 ), LDA,
749 $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
750 $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 2,
751 $ CWORK( N+1 ), LWORK-N, IERR )
753 CALL ZGSVJ1( JOBV, M-N2, N-N2, N4, A( N2+1, N2+1 ), LDA,
754 $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
755 $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 1,
756 $ CWORK( N+1 ), LWORK-N, IERR )
758 CALL ZGSVJ0( JOBV, M-N4, N2-N4, A( N4+1, N4+1 ), LDA,
759 $ CWORK( N4+1 ), SVA( N4+1 ), MVL,
760 $ V( N4*q+1, N4+1 ), LDV, EPSLN, SFMIN, TOL, 1,
761 $ CWORK( N+1 ), LWORK-N, IERR )
763 CALL ZGSVJ0( JOBV, M, N4, A, LDA, CWORK, SVA, MVL, V, LDV,
764 $ EPSLN, SFMIN, TOL, 1, CWORK( N+1 ), LWORK-N,
767 CALL ZGSVJ1( JOBV, M, N2, N4, A, LDA, CWORK, SVA, MVL, V,
768 $ LDV, EPSLN, SFMIN, TOL, 1, CWORK( N+1 ),
772 ELSE IF( UPPER ) THEN
775 CALL ZGSVJ0( JOBV, N4, N4, A, LDA, CWORK, SVA, MVL, V, LDV,
776 $ EPSLN, SFMIN, TOL, 2, CWORK( N+1 ), LWORK-N,
779 CALL ZGSVJ0( JOBV, N2, N4, A( 1, N4+1 ), LDA, CWORK( N4+1 ),
780 $ SVA( N4+1 ), MVL, V( N4*q+1, N4+1 ), LDV,
781 $ EPSLN, SFMIN, TOL, 1, CWORK( N+1 ), LWORK-N,
784 CALL ZGSVJ1( JOBV, N2, N2, N4, A, LDA, CWORK, SVA, MVL, V,
785 $ LDV, EPSLN, SFMIN, TOL, 1, CWORK( N+1 ),
788 CALL ZGSVJ0( JOBV, N2+N4, N4, A( 1, N2+1 ), LDA,
789 $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
790 $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 1,
791 $ CWORK( N+1 ), LWORK-N, IERR )
797 * .. Row-cyclic pivot strategy with de Rijk's pivoting ..
799 DO 1993 i = 1, NSWEEP
810 * Each sweep is unrolled using KBL-by-KBL tiles over the pivot pairs
811 * 1 <= p < q <= N. This is the first step toward a blocked implementation
812 * of the rotations. New implementation, based on block transformations,
813 * is under development.
817 igl = ( ibr-1 )*KBL + 1
819 DO 1002 ir1 = 0, MIN0( LKAHEAD, NBL-ibr )
823 DO 2001 p = igl, MIN0( igl+KBL-1, N-1 )
825 * .. de Rijk's pivoting
827 q = IDAMAX( N-p+1, SVA( p ), 1 ) + p - 1
829 CALL ZSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
830 IF( RSVEC )CALL ZSWAP( MVL, V( 1, p ), 1,
842 * Column norms are periodically updated by explicit
845 * Unfortunately, some BLAS implementations compute DZNRM2(M,A(1,p),1)
846 * as SQRT(S=CDOTC(M,A(1,p),1,A(1,p),1)), which may cause the result to
847 * overflow for ||A(:,p)||_2 > SQRT(overflow_threshold), and to
848 * underflow for ||A(:,p)||_2 < SQRT(underflow_threshold).
849 * Hence, DZNRM2 cannot be trusted, not even in the case when
850 * the true norm is far from the under(over)flow boundaries.
851 * If properly implemented SCNRM2 is available, the IF-THEN-ELSE-END IF
852 * below should be replaced with "AAPP = DZNRM2( M, A(1,p), 1 )".
854 IF( ( SVA( p ).LT.ROOTBIG ) .AND.
855 $ ( SVA( p ).GT.ROOTSFMIN ) ) THEN
856 SVA( p ) = DZNRM2( M, A( 1, p ), 1 )
860 CALL ZLASSQ( M, A( 1, p ), 1, TEMP1, AAPP )
861 SVA( p ) = TEMP1*DSQRT( AAPP )
868 IF( AAPP.GT.ZERO ) THEN
872 DO 2002 q = p + 1, MIN0( igl+KBL-1, N )
876 IF( AAQQ.GT.ZERO ) THEN
879 IF( AAQQ.GE.ONE ) THEN
880 ROTOK = ( SMALL*AAPP ).LE.AAQQ
881 IF( AAPP.LT.( BIG / AAQQ ) ) THEN
882 AAPQ = ( ZDOTC( M, A( 1, p ), 1,
883 $ A( 1, q ), 1 ) / AAQQ ) / AAPP
885 CALL ZCOPY( M, A( 1, p ), 1,
887 CALL ZLASCL( 'G', 0, 0, AAPP, ONE,
888 $ M, 1, CWORK(N+1), LDA, IERR )
889 AAPQ = ZDOTC( M, CWORK(N+1), 1,
890 $ A( 1, q ), 1 ) / AAQQ
893 ROTOK = AAPP.LE.( AAQQ / SMALL )
894 IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
895 AAPQ = ( ZDOTC( M, A( 1, p ), 1,
896 $ A( 1, q ), 1 ) / AAQQ ) / AAPP
898 CALL ZCOPY( M, A( 1, q ), 1,
900 CALL ZLASCL( 'G', 0, 0, AAQQ,
902 $ CWORK(N+1), LDA, IERR )
903 AAPQ = ZDOTC( M, A(1, p ), 1,
904 $ CWORK(N+1), 1 ) / AAPP
908 * AAPQ = AAPQ * DCONJG( CWORK(p) ) * CWORK(q)
910 MXAAPQ = DMAX1( MXAAPQ, -AAPQ1 )
912 * TO rotate or NOT to rotate, THAT is the question ...
914 IF( ABS( AAPQ1 ).GT.TOL ) THEN
917 *[RTD] ROTATED = ROTATED + ONE
927 OMPQ = AAPQ / ABS(AAPQ)
930 THETA = -HALF*ABS( AQOAP-APOAQ )/AAPQ1
932 IF( ABS( THETA ).GT.BIGTHETA ) THEN
937 CALL ZROT( M, A(1,p), 1, A(1,q), 1,
938 $ CS, DCONJG(OMPQ)*T )
940 CALL ZROT( MVL, V(1,p), 1,
941 $ V(1,q), 1, CS, DCONJG(OMPQ)*T )
944 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
945 $ ONE+T*APOAQ*AAPQ1 ) )
946 AAPP = AAPP*DSQRT( DMAX1( ZERO,
947 $ ONE-T*AQOAP*AAPQ1 ) )
948 MXSINJ = DMAX1( MXSINJ, ABS( T ) )
952 * .. choose correct signum for THETA and rotate
954 THSIGN = -DSIGN( ONE, AAPQ1 )
955 T = ONE / ( THETA+THSIGN*
956 $ DSQRT( ONE+THETA*THETA ) )
957 CS = DSQRT( ONE / ( ONE+T*T ) )
960 MXSINJ = DMAX1( MXSINJ, ABS( SN ) )
961 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
962 $ ONE+T*APOAQ*AAPQ1 ) )
963 AAPP = AAPP*DSQRT( DMAX1( ZERO,
964 $ ONE-T*AQOAP*AAPQ1 ) )
966 CALL ZROT( M, A(1,p), 1, A(1,q), 1,
967 $ CS, DCONJG(OMPQ)*SN )
969 CALL ZROT( MVL, V(1,p), 1,
970 $ V(1,q), 1, CS, DCONJG(OMPQ)*SN )
973 CWORK(p) = -CWORK(q) * OMPQ
976 * .. have to use modified Gram-Schmidt like transformation
977 CALL ZCOPY( M, A( 1, p ), 1,
979 CALL ZLASCL( 'G', 0, 0, AAPP, ONE, M,
980 $ 1, CWORK(N+1), LDA,
982 CALL ZLASCL( 'G', 0, 0, AAQQ, ONE, M,
983 $ 1, A( 1, q ), LDA, IERR )
984 CALL ZAXPY( M, -AAPQ, CWORK(N+1), 1,
986 CALL ZLASCL( 'G', 0, 0, ONE, AAQQ, M,
987 $ 1, A( 1, q ), LDA, IERR )
988 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
989 $ ONE-AAPQ1*AAPQ1 ) )
990 MXSINJ = DMAX1( MXSINJ, SFMIN )
992 * END IF ROTOK THEN ... ELSE
994 * In the case of cancellation in updating SVA(q), SVA(p)
995 * recompute SVA(q), SVA(p).
997 IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
999 IF( ( AAQQ.LT.ROOTBIG ) .AND.
1000 $ ( AAQQ.GT.ROOTSFMIN ) ) THEN
1001 SVA( q ) = DZNRM2( M, A( 1, q ), 1 )
1005 CALL ZLASSQ( M, A( 1, q ), 1, T,
1007 SVA( q ) = T*DSQRT( AAQQ )
1010 IF( ( AAPP / AAPP0 ).LE.ROOTEPS ) THEN
1011 IF( ( AAPP.LT.ROOTBIG ) .AND.
1012 $ ( AAPP.GT.ROOTSFMIN ) ) THEN
1013 AAPP = DZNRM2( M, A( 1, p ), 1 )
1017 CALL ZLASSQ( M, A( 1, p ), 1, T,
1019 AAPP = T*DSQRT( AAPP )
1025 * A(:,p) and A(:,q) already numerically orthogonal
1026 IF( ir1.EQ.0 )NOTROT = NOTROT + 1
1027 *[RTD] SKIPPED = SKIPPED + 1
1028 PSKIPPED = PSKIPPED + 1
1031 * A(:,q) is zero column
1032 IF( ir1.EQ.0 )NOTROT = NOTROT + 1
1033 PSKIPPED = PSKIPPED + 1
1036 IF( ( i.LE.SWBAND ) .AND.
1037 $ ( PSKIPPED.GT.ROWSKIP ) ) THEN
1038 IF( ir1.EQ.0 )AAPP = -AAPP
1047 * bailed out of q-loop
1053 IF( ( ir1.EQ.0 ) .AND. ( AAPP.EQ.ZERO ) )
1054 $ NOTROT = NOTROT + MIN0( igl+KBL-1, N ) - p
1059 * end of doing the block ( ibr, ibr )
1063 * ... go to the off diagonal blocks
1065 igl = ( ibr-1 )*KBL + 1
1067 DO 2010 jbc = ibr + 1, NBL
1069 jgl = ( jbc-1 )*KBL + 1
1071 * doing the block at ( ibr, jbc )
1074 DO 2100 p = igl, MIN0( igl+KBL-1, N )
1077 IF( AAPP.GT.ZERO ) THEN
1081 DO 2200 q = jgl, MIN0( jgl+KBL-1, N )
1084 IF( AAQQ.GT.ZERO ) THEN
1087 * .. M x 2 Jacobi SVD ..
1089 * Safe Gram matrix computation
1091 IF( AAQQ.GE.ONE ) THEN
1092 IF( AAPP.GE.AAQQ ) THEN
1093 ROTOK = ( SMALL*AAPP ).LE.AAQQ
1095 ROTOK = ( SMALL*AAQQ ).LE.AAPP
1097 IF( AAPP.LT.( BIG / AAQQ ) ) THEN
1098 AAPQ = ( ZDOTC( M, A( 1, p ), 1,
1099 $ A( 1, q ), 1 ) / AAQQ ) / AAPP
1101 CALL ZCOPY( M, A( 1, p ), 1,
1103 CALL ZLASCL( 'G', 0, 0, AAPP,
1105 $ CWORK(N+1), LDA, IERR )
1106 AAPQ = ZDOTC( M, CWORK(N+1), 1,
1107 $ A( 1, q ), 1 ) / AAQQ
1110 IF( AAPP.GE.AAQQ ) THEN
1111 ROTOK = AAPP.LE.( AAQQ / SMALL )
1113 ROTOK = AAQQ.LE.( AAPP / SMALL )
1115 IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
1116 AAPQ = ( ZDOTC( M, A( 1, p ), 1,
1117 $ A( 1, q ), 1 ) / AAQQ ) / AAPP
1119 CALL ZCOPY( M, A( 1, q ), 1,
1121 CALL ZLASCL( 'G', 0, 0, AAQQ,
1123 $ CWORK(N+1), LDA, IERR )
1124 AAPQ = ZDOTC( M, A( 1, p ), 1,
1125 $ CWORK(N+1), 1 ) / AAPP
1129 * AAPQ = AAPQ * DCONJG(CWORK(p))*CWORK(q)
1131 MXAAPQ = DMAX1( MXAAPQ, -AAPQ1 )
1133 * TO rotate or NOT to rotate, THAT is the question ...
1135 IF( ABS( AAPQ1 ).GT.TOL ) THEN
1137 *[RTD] ROTATED = ROTATED + 1
1143 OMPQ = AAPQ / ABS(AAPQ)
1146 THETA = -HALF*ABS( AQOAP-APOAQ )/ AAPQ1
1147 IF( AAQQ.GT.AAPP0 )THETA = -THETA
1149 IF( ABS( THETA ).GT.BIGTHETA ) THEN
1152 CALL ZROT( M, A(1,p), 1, A(1,q), 1,
1153 $ CS, DCONJG(OMPQ)*T )
1155 CALL ZROT( MVL, V(1,p), 1,
1156 $ V(1,q), 1, CS, DCONJG(OMPQ)*T )
1158 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
1159 $ ONE+T*APOAQ*AAPQ1 ) )
1160 AAPP = AAPP*DSQRT( DMAX1( ZERO,
1161 $ ONE-T*AQOAP*AAPQ1 ) )
1162 MXSINJ = DMAX1( MXSINJ, ABS( T ) )
1165 * .. choose correct signum for THETA and rotate
1167 THSIGN = -DSIGN( ONE, AAPQ1 )
1168 IF( AAQQ.GT.AAPP0 )THSIGN = -THSIGN
1169 T = ONE / ( THETA+THSIGN*
1170 $ DSQRT( ONE+THETA*THETA ) )
1171 CS = DSQRT( ONE / ( ONE+T*T ) )
1173 MXSINJ = DMAX1( MXSINJ, ABS( SN ) )
1174 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
1175 $ ONE+T*APOAQ*AAPQ1 ) )
1176 AAPP = AAPP*DSQRT( DMAX1( ZERO,
1177 $ ONE-T*AQOAP*AAPQ1 ) )
1179 CALL ZROT( M, A(1,p), 1, A(1,q), 1,
1180 $ CS, DCONJG(OMPQ)*SN )
1182 CALL ZROT( MVL, V(1,p), 1,
1183 $ V(1,q), 1, CS, DCONJG(OMPQ)*SN )
1186 CWORK(p) = -CWORK(q) * OMPQ
1189 * .. have to use modified Gram-Schmidt like transformation
1190 IF( AAPP.GT.AAQQ ) THEN
1191 CALL ZCOPY( M, A( 1, p ), 1,
1193 CALL ZLASCL( 'G', 0, 0, AAPP, ONE,
1194 $ M, 1, CWORK(N+1),LDA,
1196 CALL ZLASCL( 'G', 0, 0, AAQQ, ONE,
1197 $ M, 1, A( 1, q ), LDA,
1199 CALL ZAXPY( M, -AAPQ, CWORK(N+1),
1201 CALL ZLASCL( 'G', 0, 0, ONE, AAQQ,
1202 $ M, 1, A( 1, q ), LDA,
1204 SVA( q ) = AAQQ*DSQRT( DMAX1( ZERO,
1205 $ ONE-AAPQ1*AAPQ1 ) )
1206 MXSINJ = DMAX1( MXSINJ, SFMIN )
1208 CALL ZCOPY( M, A( 1, q ), 1,
1210 CALL ZLASCL( 'G', 0, 0, AAQQ, ONE,
1211 $ M, 1, CWORK(N+1),LDA,
1213 CALL ZLASCL( 'G', 0, 0, AAPP, ONE,
1214 $ M, 1, A( 1, p ), LDA,
1216 CALL ZAXPY( M, -DCONJG(AAPQ),
1217 $ CWORK(N+1), 1, A( 1, p ), 1 )
1218 CALL ZLASCL( 'G', 0, 0, ONE, AAPP,
1219 $ M, 1, A( 1, p ), LDA,
1221 SVA( p ) = AAPP*DSQRT( DMAX1( ZERO,
1222 $ ONE-AAPQ1*AAPQ1 ) )
1223 MXSINJ = DMAX1( MXSINJ, SFMIN )
1226 * END IF ROTOK THEN ... ELSE
1228 * In the case of cancellation in updating SVA(q), SVA(p)
1229 * .. recompute SVA(q), SVA(p)
1230 IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
1232 IF( ( AAQQ.LT.ROOTBIG ) .AND.
1233 $ ( AAQQ.GT.ROOTSFMIN ) ) THEN
1234 SVA( q ) = DZNRM2( M, A( 1, q ), 1)
1238 CALL ZLASSQ( M, A( 1, q ), 1, T,
1240 SVA( q ) = T*DSQRT( AAQQ )
1243 IF( ( AAPP / AAPP0 )**2.LE.ROOTEPS ) THEN
1244 IF( ( AAPP.LT.ROOTBIG ) .AND.
1245 $ ( AAPP.GT.ROOTSFMIN ) ) THEN
1246 AAPP = DZNRM2( M, A( 1, p ), 1 )
1250 CALL ZLASSQ( M, A( 1, p ), 1, T,
1252 AAPP = T*DSQRT( AAPP )
1256 * end of OK rotation
1259 *[RTD] SKIPPED = SKIPPED + 1
1260 PSKIPPED = PSKIPPED + 1
1265 PSKIPPED = PSKIPPED + 1
1269 IF( ( i.LE.SWBAND ) .AND. ( IJBLSK.GE.BLSKIP ) )
1275 IF( ( i.LE.SWBAND ) .AND.
1276 $ ( PSKIPPED.GT.ROWSKIP ) ) THEN
1290 IF( AAPP.EQ.ZERO )NOTROT = NOTROT +
1291 $ MIN0( jgl+KBL-1, N ) - jgl + 1
1292 IF( AAPP.LT.ZERO )NOTROT = 0
1299 * end of the jbc-loop
1301 *2011 bailed out of the jbc-loop
1302 DO 2012 p = igl, MIN0( igl+KBL-1, N )
1303 SVA( p ) = ABS( SVA( p ) )
1307 *2000 :: end of the ibr-loop
1310 IF( ( SVA( N ).LT.ROOTBIG ) .AND. ( SVA( N ).GT.ROOTSFMIN ) )
1312 SVA( N ) = DZNRM2( M, A( 1, N ), 1 )
1316 CALL ZLASSQ( M, A( 1, N ), 1, T, AAPP )
1317 SVA( N ) = T*DSQRT( AAPP )
1320 * Additional steering devices
1322 IF( ( i.LT.SWBAND ) .AND. ( ( MXAAPQ.LE.ROOTTOL ) .OR.
1323 $ ( ISWROT.LE.N ) ) )SWBAND = i
1325 IF( ( i.GT.SWBAND+1 ) .AND. ( MXAAPQ.LT.DSQRT( DBLE( N ) )*
1326 $ TOL ) .AND. ( DBLE( N )*MXAAPQ*MXSINJ.LT.TOL ) ) THEN
1330 IF( NOTROT.GE.EMPTSW )GO TO 1994
1333 * end i=1:NSWEEP loop
1335 * #:( Reaching this point means that the procedure has not converged.
1340 * #:) Reaching this point means numerical convergence after the i-th
1344 * #:) INFO = 0 confirms successful iterations.
1347 * Sort the singular values and find how many are above
1348 * the underflow threshold.
1352 DO 5991 p = 1, N - 1
1353 q = IDAMAX( N-p+1, SVA( p ), 1 ) + p - 1
1358 CALL ZSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
1359 IF( RSVEC )CALL ZSWAP( MVL, V( 1, p ), 1, V( 1, q ), 1 )
1361 IF( SVA( p ).NE.ZERO ) THEN
1363 IF( SVA( p )*SKL.GT.SFMIN )N2 = N2 + 1
1366 IF( SVA( N ).NE.ZERO ) THEN
1368 IF( SVA( N )*SKL.GT.SFMIN )N2 = N2 + 1
1371 * Normalize the left singular vectors.
1373 IF( LSVEC .OR. UCTOL ) THEN
1375 CALL ZDSCAL( M, ONE / SVA( p ), A( 1, p ), 1 )
1379 * Scale the product of Jacobi rotations.
1383 TEMP1 = ONE / DZNRM2( MVL, V( 1, p ), 1 )
1384 CALL ZDSCAL( MVL, TEMP1, V( 1, p ), 1 )
1388 * Undo scaling, if necessary (and possible).
1389 IF( ( ( SKL.GT.ONE ) .AND. ( SVA( 1 ).LT.( BIG / SKL ) ) )
1390 $ .OR. ( ( SKL.LT.ONE ) .AND. ( SVA( MAX( N2, 1 ) ) .GT.
1391 $ ( SFMIN / SKL ) ) ) ) THEN
1393 SVA( P ) = SKL*SVA( P )
1399 * The singular values of A are SKL*SVA(1:N). If SKL.NE.ONE
1400 * then some of the singular values may overflow or underflow and
1401 * the spectrum is given in this factored representation.
1403 RWORK( 2 ) = DBLE( N4 )
1404 * N4 is the number of computed nonzero singular values of A.
1406 RWORK( 3 ) = DBLE( N2 )
1407 * N2 is the number of singular values of A greater than SFMIN.
1408 * If N2<N, SVA(N2:N) contains ZEROS and/or denormalized numbers
1409 * that may carry some information.
1411 RWORK( 4 ) = DBLE( i )
1412 * i is the index of the last sweep before declaring convergence.
1415 * MXAAPQ is the largest absolute value of scaled pivots in the
1419 * MXSINJ is the largest absolute value of the sines of Jacobi angles