001:       SUBROUTINE ZHETRF( UPLO, N, A, LDA, IPIV, WORK, LWORK, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          UPLO
010:       INTEGER            INFO, LDA, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IPIV( * )
014:       COMPLEX*16         A( LDA, * ), WORK( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  ZHETRF computes the factorization of a complex Hermitian matrix A
021: *  using the Bunch-Kaufman diagonal pivoting method.  The form of the
022: *  factorization is
023: *
024: *     A = U*D*U**H  or  A = L*D*L**H
025: *
026: *  where U (or L) is a product of permutation and unit upper (lower)
027: *  triangular matrices, and D is Hermitian and block diagonal with
028: *  1-by-1 and 2-by-2 diagonal blocks.
029: *
030: *  This is the blocked version of the algorithm, calling Level 3 BLAS.
031: *
032: *  Arguments
033: *  =========
034: *
035: *  UPLO    (input) CHARACTER*1
036: *          = 'U':  Upper triangle of A is stored;
037: *          = 'L':  Lower triangle of A is stored.
038: *
039: *  N       (input) INTEGER
040: *          The order of the matrix A.  N >= 0.
041: *
042: *  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
043: *          On entry, the Hermitian 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: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
068: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
069: *
070: *  LWORK   (input) INTEGER
071: *          The length of WORK.  LWORK >=1.  For best performance
072: *          LWORK >= N*NB, where NB is the block size returned by ILAENV.
073: *
074: *  INFO    (output) INTEGER
075: *          = 0:  successful exit
076: *          < 0:  if INFO = -i, the i-th argument had an illegal value
077: *          > 0:  if INFO = i, D(i,i) is exactly zero.  The factorization
078: *                has been completed, but the block diagonal matrix D is
079: *                exactly singular, and division by zero will occur if it
080: *                is used to solve a system of equations.
081: *
082: *  Further Details
083: *  ===============
084: *
085: *  If UPLO = 'U', then A = U*D*U', where
086: *     U = P(n)*U(n)* ... *P(k)U(k)* ...,
087: *  i.e., U is a product of terms P(k)*U(k), where k decreases from n to
088: *  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
089: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
090: *  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such
091: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
092: *
093: *             (   I    v    0   )   k-s
094: *     U(k) =  (   0    I    0   )   s
095: *             (   0    0    I   )   n-k
096: *                k-s   s   n-k
097: *
098: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).
099: *  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),
100: *  and A(k,k), and v overwrites A(1:k-2,k-1:k).
101: *
102: *  If UPLO = 'L', then A = L*D*L', where
103: *     L = P(1)*L(1)* ... *P(k)*L(k)* ...,
104: *  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to
105: *  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
106: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
107: *  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such
108: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
109: *
110: *             (   I    0     0   )  k-1
111: *     L(k) =  (   0    I     0   )  s
112: *             (   0    v     I   )  n-k-s+1
113: *                k-1   s  n-k-s+1
114: *
115: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).
116: *  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),
117: *  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).
118: *
119: *  =====================================================================
120: *
121: *     .. Local Scalars ..
122:       LOGICAL            LQUERY, UPPER
123:       INTEGER            IINFO, IWS, J, K, KB, LDWORK, LWKOPT, NB, NBMIN
124: *     ..
125: *     .. External Functions ..
126:       LOGICAL            LSAME
127:       INTEGER            ILAENV
128:       EXTERNAL           LSAME, ILAENV
129: *     ..
130: *     .. External Subroutines ..
131:       EXTERNAL           XERBLA, ZHETF2, ZLAHEF
132: *     ..
133: *     .. Intrinsic Functions ..
134:       INTRINSIC          MAX
135: *     ..
136: *     .. Executable Statements ..
137: *
138: *     Test the input parameters.
139: *
140:       INFO = 0
141:       UPPER = LSAME( UPLO, 'U' )
142:       LQUERY = ( LWORK.EQ.-1 )
143:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
144:          INFO = -1
145:       ELSE IF( N.LT.0 ) THEN
146:          INFO = -2
147:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
148:          INFO = -4
149:       ELSE IF( LWORK.LT.1 .AND. .NOT.LQUERY ) THEN
150:          INFO = -7
151:       END IF
152: *
153:       IF( INFO.EQ.0 ) THEN
154: *
155: *        Determine the block size
156: *
157:          NB = ILAENV( 1, 'ZHETRF', UPLO, N, -1, -1, -1 )
158:          LWKOPT = N*NB
159:          WORK( 1 ) = LWKOPT
160:       END IF
161: *
162:       IF( INFO.NE.0 ) THEN
163:          CALL XERBLA( 'ZHETRF', -INFO )
164:          RETURN
165:       ELSE IF( LQUERY ) THEN
166:          RETURN
167:       END IF
168: *
169:       NBMIN = 2
170:       LDWORK = N
171:       IF( NB.GT.1 .AND. NB.LT.N ) THEN
172:          IWS = LDWORK*NB
173:          IF( LWORK.LT.IWS ) THEN
174:             NB = MAX( LWORK / LDWORK, 1 )
175:             NBMIN = MAX( 2, ILAENV( 2, 'ZHETRF', UPLO, N, -1, -1, -1 ) )
176:          END IF
177:       ELSE
178:          IWS = 1
179:       END IF
180:       IF( NB.LT.NBMIN )
181:      $   NB = N
182: *
183:       IF( UPPER ) THEN
184: *
185: *        Factorize A as U*D*U' using the upper triangle of A
186: *
187: *        K is the main loop index, decreasing from N to 1 in steps of
188: *        KB, where KB is the number of columns factorized by ZLAHEF;
189: *        KB is either NB or NB-1, or K for the last block
190: *
191:          K = N
192:    10    CONTINUE
193: *
194: *        If K < 1, exit from loop
195: *
196:          IF( K.LT.1 )
197:      $      GO TO 40
198: *
199:          IF( K.GT.NB ) THEN
200: *
201: *           Factorize columns k-kb+1:k of A and use blocked code to
202: *           update columns 1:k-kb
203: *
204:             CALL ZLAHEF( UPLO, K, NB, KB, A, LDA, IPIV, WORK, N, IINFO )
205:          ELSE
206: *
207: *           Use unblocked code to factorize columns 1:k of A
208: *
209:             CALL ZHETF2( UPLO, K, A, LDA, IPIV, IINFO )
210:             KB = K
211:          END IF
212: *
213: *        Set INFO on the first occurrence of a zero pivot
214: *
215:          IF( INFO.EQ.0 .AND. IINFO.GT.0 )
216:      $      INFO = IINFO
217: *
218: *        Decrease K and return to the start of the main loop
219: *
220:          K = K - KB
221:          GO TO 10
222: *
223:       ELSE
224: *
225: *        Factorize A as L*D*L' using the lower triangle of A
226: *
227: *        K is the main loop index, increasing from 1 to N in steps of
228: *        KB, where KB is the number of columns factorized by ZLAHEF;
229: *        KB is either NB or NB-1, or N-K+1 for the last block
230: *
231:          K = 1
232:    20    CONTINUE
233: *
234: *        If K > N, exit from loop
235: *
236:          IF( K.GT.N )
237:      $      GO TO 40
238: *
239:          IF( K.LE.N-NB ) THEN
240: *
241: *           Factorize columns k:k+kb-1 of A and use blocked code to
242: *           update columns k+kb:n
243: *
244:             CALL ZLAHEF( UPLO, N-K+1, NB, KB, A( K, K ), LDA, IPIV( K ),
245:      $                   WORK, N, IINFO )
246:          ELSE
247: *
248: *           Use unblocked code to factorize columns k:n of A
249: *
250:             CALL ZHETF2( UPLO, N-K+1, A( K, K ), LDA, IPIV( K ), IINFO )
251:             KB = N - K + 1
252:          END IF
253: *
254: *        Set INFO on the first occurrence of a zero pivot
255: *
256:          IF( INFO.EQ.0 .AND. IINFO.GT.0 )
257:      $      INFO = IINFO + K - 1
258: *
259: *        Adjust IPIV
260: *
261:          DO 30 J = K, K + KB - 1
262:             IF( IPIV( J ).GT.0 ) THEN
263:                IPIV( J ) = IPIV( J ) + K - 1
264:             ELSE
265:                IPIV( J ) = IPIV( J ) - K + 1
266:             END IF
267:    30    CONTINUE
268: *
269: *        Increase K and return to the start of the main loop
270: *
271:          K = K + KB
272:          GO TO 20
273: *
274:       END IF
275: *
276:    40 CONTINUE
277:       WORK( 1 ) = LWKOPT
278:       RETURN
279: *
280: *     End of ZHETRF
281: *
282:       END
283: