001:       SUBROUTINE CSYTF2( UPLO, N, A, LDA, IPIV, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       CHARACTER          UPLO
009:       INTEGER            INFO, LDA, N
010: *     ..
011: *     .. Array Arguments ..
012:       INTEGER            IPIV( * )
013:       COMPLEX            A( LDA, * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  CSYTF2 computes the factorization of a complex symmetric matrix A
020: *  using the Bunch-Kaufman diagonal pivoting method:
021: *
022: *     A = U*D*U'  or  A = L*D*L'
023: *
024: *  where U (or L) is a product of permutation and unit upper (lower)
025: *  triangular matrices, U' is the transpose of U, and D is symmetric and
026: *  block diagonal with 1-by-1 and 2-by-2 diagonal blocks.
027: *
028: *  This is the unblocked version of the algorithm, calling Level 2 BLAS.
029: *
030: *  Arguments
031: *  =========
032: *
033: *  UPLO    (input) CHARACTER*1
034: *          Specifies whether the upper or lower triangular part of the
035: *          symmetric matrix A is stored:
036: *          = 'U':  Upper triangular
037: *          = 'L':  Lower triangular
038: *
039: *  N       (input) INTEGER
040: *          The order of the matrix A.  N >= 0.
041: *
042: *  A       (input/output) COMPLEX array, dimension (LDA,N)
043: *          On entry, the symmetric matrix A.  If UPLO = 'U', the leading
044: *          n-by-n upper triangular part of A contains the upper
045: *          triangular part of the matrix A, and the strictly lower
046: *          triangular part of A is not referenced.  If UPLO = 'L', the
047: *          leading n-by-n lower triangular part of A contains the lower
048: *          triangular part of the matrix A, and the strictly upper
049: *          triangular part of A is not referenced.
050: *
051: *          On exit, the block diagonal matrix D and the multipliers used
052: *          to obtain the factor U or L (see below for further details).
053: *
054: *  LDA     (input) INTEGER
055: *          The leading dimension of the array A.  LDA >= max(1,N).
056: *
057: *  IPIV    (output) INTEGER array, dimension (N)
058: *          Details of the interchanges and the block structure of D.
059: *          If IPIV(k) > 0, then rows and columns k and IPIV(k) were
060: *          interchanged and D(k,k) is a 1-by-1 diagonal block.
061: *          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and
062: *          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)
063: *          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =
064: *          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were
065: *          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.
066: *
067: *  INFO    (output) INTEGER
068: *          = 0: successful exit
069: *          < 0: if INFO = -k, the k-th argument had an illegal value
070: *          > 0: if INFO = k, D(k,k) is exactly zero.  The factorization
071: *               has been completed, but the block diagonal matrix D is
072: *               exactly singular, and division by zero will occur if it
073: *               is used to solve a system of equations.
074: *
075: *  Further Details
076: *  ===============
077: *
078: *  09-29-06 - patch from
079: *    Bobby Cheng, MathWorks
080: *
081: *    Replace l.209 and l.377
082: *         IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN
083: *    by
084: *         IF( (MAX( ABSAKK, COLMAX ).EQ.ZERO) .OR. SISNAN(ABSAKK) ) THEN
085: *
086: *  1-96 - Based on modifications by J. Lewis, Boeing Computer Services
087: *         Company
088: *
089: *  If UPLO = 'U', then A = U*D*U', where
090: *     U = P(n)*U(n)* ... *P(k)U(k)* ...,
091: *  i.e., U is a product of terms P(k)*U(k), where k decreases from n to
092: *  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
093: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
094: *  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such
095: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
096: *
097: *             (   I    v    0   )   k-s
098: *     U(k) =  (   0    I    0   )   s
099: *             (   0    0    I   )   n-k
100: *                k-s   s   n-k
101: *
102: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).
103: *  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),
104: *  and A(k,k), and v overwrites A(1:k-2,k-1:k).
105: *
106: *  If UPLO = 'L', then A = L*D*L', where
107: *     L = P(1)*L(1)* ... *P(k)*L(k)* ...,
108: *  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to
109: *  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
110: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
111: *  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such
112: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
113: *
114: *             (   I    0     0   )  k-1
115: *     L(k) =  (   0    I     0   )  s
116: *             (   0    v     I   )  n-k-s+1
117: *                k-1   s  n-k-s+1
118: *
119: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).
120: *  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),
121: *  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).
122: *
123: *  =====================================================================
124: *
125: *     .. Parameters ..
126:       REAL               ZERO, ONE
127:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
128:       REAL               EIGHT, SEVTEN
129:       PARAMETER          ( EIGHT = 8.0E+0, SEVTEN = 17.0E+0 )
130:       COMPLEX            CONE
131:       PARAMETER          ( CONE = ( 1.0E+0, 0.0E+0 ) )
132: *     ..
133: *     .. Local Scalars ..
134:       LOGICAL            UPPER
135:       INTEGER            I, IMAX, J, JMAX, K, KK, KP, KSTEP
136:       REAL               ABSAKK, ALPHA, COLMAX, ROWMAX
137:       COMPLEX            D11, D12, D21, D22, R1, T, WK, WKM1, WKP1, Z
138: *     ..
139: *     .. External Functions ..
140:       LOGICAL            LSAME, SISNAN
141:       INTEGER            ICAMAX
142:       EXTERNAL           LSAME, ICAMAX, SISNAN
143: *     ..
144: *     .. External Subroutines ..
145:       EXTERNAL           CSCAL, CSWAP, CSYR, XERBLA
146: *     ..
147: *     .. Intrinsic Functions ..
148:       INTRINSIC          ABS, AIMAG, MAX, REAL, SQRT
149: *     ..
150: *     .. Statement Functions ..
151:       REAL               CABS1
152: *     ..
153: *     .. Statement Function definitions ..
154:       CABS1( Z ) = ABS( REAL( Z ) ) + ABS( AIMAG( Z ) )
155: *     ..
156: *     .. Executable Statements ..
157: *
158: *     Test the input parameters.
159: *
160:       INFO = 0
161:       UPPER = LSAME( UPLO, 'U' )
162:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
163:          INFO = -1
164:       ELSE IF( N.LT.0 ) THEN
165:          INFO = -2
166:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
167:          INFO = -4
168:       END IF
169:       IF( INFO.NE.0 ) THEN
170:          CALL XERBLA( 'CSYTF2', -INFO )
171:          RETURN
172:       END IF
173: *
174: *     Initialize ALPHA for use in choosing pivot block size.
175: *
176:       ALPHA = ( ONE+SQRT( SEVTEN ) ) / EIGHT
177: *
178:       IF( UPPER ) THEN
179: *
180: *        Factorize A as U*D*U' using the upper triangle of A
181: *
182: *        K is the main loop index, decreasing from N to 1 in steps of
183: *        1 or 2
184: *
185:          K = N
186:    10    CONTINUE
187: *
188: *        If K < 1, exit from loop
189: *
190:          IF( K.LT.1 )
191:      $      GO TO 70
192:          KSTEP = 1
193: *
194: *        Determine rows and columns to be interchanged and whether
195: *        a 1-by-1 or 2-by-2 pivot block will be used
196: *
197:          ABSAKK = CABS1( A( K, K ) )
198: *
199: *        IMAX is the row-index of the largest off-diagonal element in
200: *        column K, and COLMAX is its absolute value
201: *
202:          IF( K.GT.1 ) THEN
203:             IMAX = ICAMAX( K-1, A( 1, K ), 1 )
204:             COLMAX = CABS1( A( IMAX, K ) )
205:          ELSE
206:             COLMAX = ZERO
207:          END IF
208: *
209:          IF( MAX( ABSAKK, COLMAX ).EQ.ZERO .OR. SISNAN(ABSAKK) ) THEN
210: *
211: *           Column K is zero or NaN: set INFO and continue
212: *
213:             IF( INFO.EQ.0 )
214:      $         INFO = K
215:             KP = K
216:          ELSE
217:             IF( ABSAKK.GE.ALPHA*COLMAX ) THEN
218: *
219: *              no interchange, use 1-by-1 pivot block
220: *
221:                KP = K
222:             ELSE
223: *
224: *              JMAX is the column-index of the largest off-diagonal
225: *              element in row IMAX, and ROWMAX is its absolute value
226: *
227:                JMAX = IMAX + ICAMAX( K-IMAX, A( IMAX, IMAX+1 ), LDA )
228:                ROWMAX = CABS1( A( IMAX, JMAX ) )
229:                IF( IMAX.GT.1 ) THEN
230:                   JMAX = ICAMAX( IMAX-1, A( 1, IMAX ), 1 )
231:                   ROWMAX = MAX( ROWMAX, CABS1( A( JMAX, IMAX ) ) )
232:                END IF
233: *
234:                IF( ABSAKK.GE.ALPHA*COLMAX*( COLMAX / ROWMAX ) ) THEN
235: *
236: *                 no interchange, use 1-by-1 pivot block
237: *
238:                   KP = K
239:                ELSE IF( CABS1( A( IMAX, IMAX ) ).GE.ALPHA*ROWMAX ) THEN
240: *
241: *                 interchange rows and columns K and IMAX, use 1-by-1
242: *                 pivot block
243: *
244:                   KP = IMAX
245:                ELSE
246: *
247: *                 interchange rows and columns K-1 and IMAX, use 2-by-2
248: *                 pivot block
249: *
250:                   KP = IMAX
251:                   KSTEP = 2
252:                END IF
253:             END IF
254: *
255:             KK = K - KSTEP + 1
256:             IF( KP.NE.KK ) THEN
257: *
258: *              Interchange rows and columns KK and KP in the leading
259: *              submatrix A(1:k,1:k)
260: *
261:                CALL CSWAP( KP-1, A( 1, KK ), 1, A( 1, KP ), 1 )
262:                CALL CSWAP( KK-KP-1, A( KP+1, KK ), 1, A( KP, KP+1 ),
263:      $                     LDA )
264:                T = A( KK, KK )
265:                A( KK, KK ) = A( KP, KP )
266:                A( KP, KP ) = T
267:                IF( KSTEP.EQ.2 ) THEN
268:                   T = A( K-1, K )
269:                   A( K-1, K ) = A( KP, K )
270:                   A( KP, K ) = T
271:                END IF
272:             END IF
273: *
274: *           Update the leading submatrix
275: *
276:             IF( KSTEP.EQ.1 ) THEN
277: *
278: *              1-by-1 pivot block D(k): column k now holds
279: *
280: *              W(k) = U(k)*D(k)
281: *
282: *              where U(k) is the k-th column of U
283: *
284: *              Perform a rank-1 update of A(1:k-1,1:k-1) as
285: *
286: *              A := A - U(k)*D(k)*U(k)' = A - W(k)*1/D(k)*W(k)'
287: *
288:                R1 = CONE / A( K, K )
289:                CALL CSYR( UPLO, K-1, -R1, A( 1, K ), 1, A, LDA )
290: *
291: *              Store U(k) in column k
292: *
293:                CALL CSCAL( K-1, R1, A( 1, K ), 1 )
294:             ELSE
295: *
296: *              2-by-2 pivot block D(k): columns k and k-1 now hold
297: *
298: *              ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k)
299: *
300: *              where U(k) and U(k-1) are the k-th and (k-1)-th columns
301: *              of U
302: *
303: *              Perform a rank-2 update of A(1:k-2,1:k-2) as
304: *
305: *              A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )'
306: *                 = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )'
307: *
308:                IF( K.GT.2 ) THEN
309: *
310:                   D12 = A( K-1, K )
311:                   D22 = A( K-1, K-1 ) / D12
312:                   D11 = A( K, K ) / D12
313:                   T = CONE / ( D11*D22-CONE )
314:                   D12 = T / D12
315: *
316:                   DO 30 J = K - 2, 1, -1
317:                      WKM1 = D12*( D11*A( J, K-1 )-A( J, K ) )
318:                      WK = D12*( D22*A( J, K )-A( J, K-1 ) )
319:                      DO 20 I = J, 1, -1
320:                         A( I, J ) = A( I, J ) - A( I, K )*WK -
321:      $                              A( I, K-1 )*WKM1
322:    20                CONTINUE
323:                      A( J, K ) = WK
324:                      A( J, K-1 ) = WKM1
325:    30             CONTINUE
326: *
327:                END IF
328: *
329:             END IF
330:          END IF
331: *
332: *        Store details of the interchanges in IPIV
333: *
334:          IF( KSTEP.EQ.1 ) THEN
335:             IPIV( K ) = KP
336:          ELSE
337:             IPIV( K ) = -KP
338:             IPIV( K-1 ) = -KP
339:          END IF
340: *
341: *        Decrease K and return to the start of the main loop
342: *
343:          K = K - KSTEP
344:          GO TO 10
345: *
346:       ELSE
347: *
348: *        Factorize A as L*D*L' using the lower triangle of A
349: *
350: *        K is the main loop index, increasing from 1 to N in steps of
351: *        1 or 2
352: *
353:          K = 1
354:    40    CONTINUE
355: *
356: *        If K > N, exit from loop
357: *
358:          IF( K.GT.N )
359:      $      GO TO 70
360:          KSTEP = 1
361: *
362: *        Determine rows and columns to be interchanged and whether
363: *        a 1-by-1 or 2-by-2 pivot block will be used
364: *
365:          ABSAKK = CABS1( A( K, K ) )
366: *
367: *        IMAX is the row-index of the largest off-diagonal element in
368: *        column K, and COLMAX is its absolute value
369: *
370:          IF( K.LT.N ) THEN
371:             IMAX = K + ICAMAX( N-K, A( K+1, K ), 1 )
372:             COLMAX = CABS1( A( IMAX, K ) )
373:          ELSE
374:             COLMAX = ZERO
375:          END IF
376: *
377:          IF( MAX( ABSAKK, COLMAX ).EQ.ZERO .OR. SISNAN(ABSAKK) ) THEN
378: *
379: *           Column K is zero or NaN: set INFO and continue
380: *
381:             IF( INFO.EQ.0 )
382:      $         INFO = K
383:             KP = K
384:          ELSE
385:             IF( ABSAKK.GE.ALPHA*COLMAX ) THEN
386: *
387: *              no interchange, use 1-by-1 pivot block
388: *
389:                KP = K
390:             ELSE
391: *
392: *              JMAX is the column-index of the largest off-diagonal
393: *              element in row IMAX, and ROWMAX is its absolute value
394: *
395:                JMAX = K - 1 + ICAMAX( IMAX-K, A( IMAX, K ), LDA )
396:                ROWMAX = CABS1( A( IMAX, JMAX ) )
397:                IF( IMAX.LT.N ) THEN
398:                   JMAX = IMAX + ICAMAX( N-IMAX, A( IMAX+1, IMAX ), 1 )
399:                   ROWMAX = MAX( ROWMAX, CABS1( A( JMAX, IMAX ) ) )
400:                END IF
401: *
402:                IF( ABSAKK.GE.ALPHA*COLMAX*( COLMAX / ROWMAX ) ) THEN
403: *
404: *                 no interchange, use 1-by-1 pivot block
405: *
406:                   KP = K
407:                ELSE IF( CABS1( A( IMAX, IMAX ) ).GE.ALPHA*ROWMAX ) THEN
408: *
409: *                 interchange rows and columns K and IMAX, use 1-by-1
410: *                 pivot block
411: *
412:                   KP = IMAX
413:                ELSE
414: *
415: *                 interchange rows and columns K+1 and IMAX, use 2-by-2
416: *                 pivot block
417: *
418:                   KP = IMAX
419:                   KSTEP = 2
420:                END IF
421:             END IF
422: *
423:             KK = K + KSTEP - 1
424:             IF( KP.NE.KK ) THEN
425: *
426: *              Interchange rows and columns KK and KP in the trailing
427: *              submatrix A(k:n,k:n)
428: *
429:                IF( KP.LT.N )
430:      $            CALL CSWAP( N-KP, A( KP+1, KK ), 1, A( KP+1, KP ), 1 )
431:                CALL CSWAP( KP-KK-1, A( KK+1, KK ), 1, A( KP, KK+1 ),
432:      $                     LDA )
433:                T = A( KK, KK )
434:                A( KK, KK ) = A( KP, KP )
435:                A( KP, KP ) = T
436:                IF( KSTEP.EQ.2 ) THEN
437:                   T = A( K+1, K )
438:                   A( K+1, K ) = A( KP, K )
439:                   A( KP, K ) = T
440:                END IF
441:             END IF
442: *
443: *           Update the trailing submatrix
444: *
445:             IF( KSTEP.EQ.1 ) THEN
446: *
447: *              1-by-1 pivot block D(k): column k now holds
448: *
449: *              W(k) = L(k)*D(k)
450: *
451: *              where L(k) is the k-th column of L
452: *
453:                IF( K.LT.N ) THEN
454: *
455: *                 Perform a rank-1 update of A(k+1:n,k+1:n) as
456: *
457: *                 A := A - L(k)*D(k)*L(k)' = A - W(k)*(1/D(k))*W(k)'
458: *
459:                   R1 = CONE / A( K, K )
460:                   CALL CSYR( UPLO, N-K, -R1, A( K+1, K ), 1,
461:      $                       A( K+1, K+1 ), LDA )
462: *
463: *                 Store L(k) in column K
464: *
465:                   CALL CSCAL( N-K, R1, A( K+1, K ), 1 )
466:                END IF
467:             ELSE
468: *
469: *              2-by-2 pivot block D(k)
470: *
471:                IF( K.LT.N-1 ) THEN
472: *
473: *                 Perform a rank-2 update of A(k+2:n,k+2:n) as
474: *
475: *                 A := A - ( L(k) L(k+1) )*D(k)*( L(k) L(k+1) )'
476: *                    = A - ( W(k) W(k+1) )*inv(D(k))*( W(k) W(k+1) )'
477: *
478: *                 where L(k) and L(k+1) are the k-th and (k+1)-th
479: *                 columns of L
480: *
481:                   D21 = A( K+1, K )
482:                   D11 = A( K+1, K+1 ) / D21
483:                   D22 = A( K, K ) / D21
484:                   T = CONE / ( D11*D22-CONE )
485:                   D21 = T / D21
486: *
487:                   DO 60 J = K + 2, N
488:                      WK = D21*( D11*A( J, K )-A( J, K+1 ) )
489:                      WKP1 = D21*( D22*A( J, K+1 )-A( J, K ) )
490:                      DO 50 I = J, N
491:                         A( I, J ) = A( I, J ) - A( I, K )*WK -
492:      $                              A( I, K+1 )*WKP1
493:    50                CONTINUE
494:                      A( J, K ) = WK
495:                      A( J, K+1 ) = WKP1
496:    60             CONTINUE
497:                END IF
498:             END IF
499:          END IF
500: *
501: *        Store details of the interchanges in IPIV
502: *
503:          IF( KSTEP.EQ.1 ) THEN
504:             IPIV( K ) = KP
505:          ELSE
506:             IPIV( K ) = -KP
507:             IPIV( K+1 ) = -KP
508:          END IF
509: *
510: *        Increase K and return to the start of the main loop
511: *
512:          K = K + KSTEP
513:          GO TO 40
514: *
515:       END IF
516: *
517:    70 CONTINUE
518:       RETURN
519: *
520: *     End of CSYTF2
521: *
522:       END
523: