```001:       SUBROUTINE ZUNGTR( UPLO, N, A, LDA, TAU, WORK, LWORK, 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, LWORK, N
010: *     ..
011: *     .. Array Arguments ..
012:       COMPLEX*16         A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  ZUNGTR generates a complex unitary matrix Q which is defined as the
019: *  product of n-1 elementary reflectors of order N, as returned by
020: *  ZHETRD:
021: *
022: *  if UPLO = 'U', Q = H(n-1) . . . H(2) H(1),
023: *
024: *  if UPLO = 'L', Q = H(1) H(2) . . . H(n-1).
025: *
026: *  Arguments
027: *  =========
028: *
029: *  UPLO    (input) CHARACTER*1
030: *          = 'U': Upper triangle of A contains elementary reflectors
031: *                 from ZHETRD;
032: *          = 'L': Lower triangle of A contains elementary reflectors
033: *                 from ZHETRD.
034: *
035: *  N       (input) INTEGER
036: *          The order of the matrix Q. N >= 0.
037: *
038: *  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
039: *          On entry, the vectors which define the elementary reflectors,
040: *          as returned by ZHETRD.
041: *          On exit, the N-by-N unitary matrix Q.
042: *
043: *  LDA     (input) INTEGER
044: *          The leading dimension of the array A. LDA >= N.
045: *
046: *  TAU     (input) COMPLEX*16 array, dimension (N-1)
047: *          TAU(i) must contain the scalar factor of the elementary
048: *          reflector H(i), as returned by ZHETRD.
049: *
050: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
051: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
052: *
053: *  LWORK   (input) INTEGER
054: *          The dimension of the array WORK. LWORK >= N-1.
055: *          For optimum performance LWORK >= (N-1)*NB, where NB is
056: *          the optimal blocksize.
057: *
058: *          If LWORK = -1, then a workspace query is assumed; the routine
059: *          only calculates the optimal size of the WORK array, returns
060: *          this value as the first entry of the WORK array, and no error
061: *          message related to LWORK is issued by XERBLA.
062: *
063: *  INFO    (output) INTEGER
064: *          = 0:  successful exit
065: *          < 0:  if INFO = -i, the i-th argument had an illegal value
066: *
067: *  =====================================================================
068: *
069: *     .. Parameters ..
070:       COMPLEX*16         ZERO, ONE
071:       PARAMETER          ( ZERO = ( 0.0D+0, 0.0D+0 ),
072:      \$                   ONE = ( 1.0D+0, 0.0D+0 ) )
073: *     ..
074: *     .. Local Scalars ..
075:       LOGICAL            LQUERY, UPPER
076:       INTEGER            I, IINFO, J, LWKOPT, NB
077: *     ..
078: *     .. External Functions ..
079:       LOGICAL            LSAME
080:       INTEGER            ILAENV
081:       EXTERNAL           LSAME, ILAENV
082: *     ..
083: *     .. External Subroutines ..
084:       EXTERNAL           XERBLA, ZUNGQL, ZUNGQR
085: *     ..
086: *     .. Intrinsic Functions ..
087:       INTRINSIC          MAX
088: *     ..
089: *     .. Executable Statements ..
090: *
091: *     Test the input arguments
092: *
093:       INFO = 0
094:       LQUERY = ( LWORK.EQ.-1 )
095:       UPPER = LSAME( UPLO, 'U' )
096:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
097:          INFO = -1
098:       ELSE IF( N.LT.0 ) THEN
099:          INFO = -2
100:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
101:          INFO = -4
102:       ELSE IF( LWORK.LT.MAX( 1, N-1 ) .AND. .NOT.LQUERY ) THEN
103:          INFO = -7
104:       END IF
105: *
106:       IF( INFO.EQ.0 ) THEN
107:          IF( UPPER ) THEN
108:             NB = ILAENV( 1, 'ZUNGQL', ' ', N-1, N-1, N-1, -1 )
109:          ELSE
110:             NB = ILAENV( 1, 'ZUNGQR', ' ', N-1, N-1, N-1, -1 )
111:          END IF
112:          LWKOPT = MAX( 1, N-1 )*NB
113:          WORK( 1 ) = LWKOPT
114:       END IF
115: *
116:       IF( INFO.NE.0 ) THEN
117:          CALL XERBLA( 'ZUNGTR', -INFO )
118:          RETURN
119:       ELSE IF( LQUERY ) THEN
120:          RETURN
121:       END IF
122: *
123: *     Quick return if possible
124: *
125:       IF( N.EQ.0 ) THEN
126:          WORK( 1 ) = 1
127:          RETURN
128:       END IF
129: *
130:       IF( UPPER ) THEN
131: *
132: *        Q was determined by a call to ZHETRD with UPLO = 'U'
133: *
134: *        Shift the vectors which define the elementary reflectors one
135: *        column to the left, and set the last row and column of Q to
136: *        those of the unit matrix
137: *
138:          DO 20 J = 1, N - 1
139:             DO 10 I = 1, J - 1
140:                A( I, J ) = A( I, J+1 )
141:    10       CONTINUE
142:             A( N, J ) = ZERO
143:    20    CONTINUE
144:          DO 30 I = 1, N - 1
145:             A( I, N ) = ZERO
146:    30    CONTINUE
147:          A( N, N ) = ONE
148: *
149: *        Generate Q(1:n-1,1:n-1)
150: *
151:          CALL ZUNGQL( N-1, N-1, N-1, A, LDA, TAU, WORK, LWORK, IINFO )
152: *
153:       ELSE
154: *
155: *        Q was determined by a call to ZHETRD with UPLO = 'L'.
156: *
157: *        Shift the vectors which define the elementary reflectors one
158: *        column to the right, and set the first row and column of Q to
159: *        those of the unit matrix
160: *
161:          DO 50 J = N, 2, -1
162:             A( 1, J ) = ZERO
163:             DO 40 I = J + 1, N
164:                A( I, J ) = A( I, J-1 )
165:    40       CONTINUE
166:    50    CONTINUE
167:          A( 1, 1 ) = ONE
168:          DO 60 I = 2, N
169:             A( I, 1 ) = ZERO
170:    60    CONTINUE
171:          IF( N.GT.1 ) THEN
172: *
173: *           Generate Q(2:n,2:n)
174: *
175:             CALL ZUNGQR( N-1, N-1, N-1, A( 2, 2 ), LDA, TAU, WORK,
176:      \$                   LWORK, IINFO )
177:          END IF
178:       END IF
179:       WORK( 1 ) = LWKOPT
180:       RETURN
181: *
182: *     End of ZUNGTR
183: *
184:       END
185: ```