```001:       SUBROUTINE CTRCON( NORM, UPLO, DIAG, N, A, LDA, RCOND, WORK,
002:      \$                   RWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2) --
005: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
006: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
007: *     November 2006
008: *
009: *     Modified to call CLACN2 in place of CLACON, 10 Feb 03, SJH.
010: *
011: *     .. Scalar Arguments ..
012:       CHARACTER          DIAG, NORM, UPLO
013:       INTEGER            INFO, LDA, N
014:       REAL               RCOND
015: *     ..
016: *     .. Array Arguments ..
017:       REAL               RWORK( * )
018:       COMPLEX            A( LDA, * ), WORK( * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CTRCON estimates the reciprocal of the condition number of a
025: *  triangular matrix A, in either the 1-norm or the infinity-norm.
026: *
027: *  The norm of A is computed and an estimate is obtained for
028: *  norm(inv(A)), then the reciprocal of the condition number is
029: *  computed as
030: *     RCOND = 1 / ( norm(A) * norm(inv(A)) ).
031: *
032: *  Arguments
033: *  =========
034: *
035: *  NORM    (input) CHARACTER*1
036: *          Specifies whether the 1-norm condition number or the
037: *          infinity-norm condition number is required:
038: *          = '1' or 'O':  1-norm;
039: *          = 'I':         Infinity-norm.
040: *
041: *  UPLO    (input) CHARACTER*1
042: *          = 'U':  A is upper triangular;
043: *          = 'L':  A is lower triangular.
044: *
045: *  DIAG    (input) CHARACTER*1
046: *          = 'N':  A is non-unit triangular;
047: *          = 'U':  A is unit triangular.
048: *
049: *  N       (input) INTEGER
050: *          The order of the matrix A.  N >= 0.
051: *
052: *  A       (input) COMPLEX array, dimension (LDA,N)
053: *          The triangular matrix A.  If UPLO = 'U', the leading N-by-N
054: *          upper triangular part of the array A contains the upper
055: *          triangular matrix, and the strictly lower triangular part of
056: *          A is not referenced.  If UPLO = 'L', the leading N-by-N lower
057: *          triangular part of the array A contains the lower triangular
058: *          matrix, and the strictly upper triangular part of A is not
059: *          referenced.  If DIAG = 'U', the diagonal elements of A are
060: *          also not referenced and are assumed to be 1.
061: *
062: *  LDA     (input) INTEGER
063: *          The leading dimension of the array A.  LDA >= max(1,N).
064: *
065: *  RCOND   (output) REAL
066: *          The reciprocal of the condition number of the matrix A,
067: *          computed as RCOND = 1/(norm(A) * norm(inv(A))).
068: *
069: *  WORK    (workspace) COMPLEX array, dimension (2*N)
070: *
071: *  RWORK   (workspace) REAL array, dimension (N)
072: *
073: *  INFO    (output) INTEGER
074: *          = 0:  successful exit
075: *          < 0:  if INFO = -i, the i-th argument had an illegal value
076: *
077: *  =====================================================================
078: *
079: *     .. Parameters ..
080:       REAL               ONE, ZERO
081:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
082: *     ..
083: *     .. Local Scalars ..
084:       LOGICAL            NOUNIT, ONENRM, UPPER
085:       CHARACTER          NORMIN
086:       INTEGER            IX, KASE, KASE1
087:       REAL               AINVNM, ANORM, SCALE, SMLNUM, XNORM
088:       COMPLEX            ZDUM
089: *     ..
090: *     .. Local Arrays ..
091:       INTEGER            ISAVE( 3 )
092: *     ..
093: *     .. External Functions ..
094:       LOGICAL            LSAME
095:       INTEGER            ICAMAX
096:       REAL               CLANTR, SLAMCH
097:       EXTERNAL           LSAME, ICAMAX, CLANTR, SLAMCH
098: *     ..
099: *     .. External Subroutines ..
100:       EXTERNAL           CLACN2, CLATRS, CSRSCL, XERBLA
101: *     ..
102: *     .. Intrinsic Functions ..
103:       INTRINSIC          ABS, AIMAG, MAX, REAL
104: *     ..
105: *     .. Statement Functions ..
106:       REAL               CABS1
107: *     ..
108: *     .. Statement Function definitions ..
109:       CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
110: *     ..
111: *     .. Executable Statements ..
112: *
113: *     Test the input parameters.
114: *
115:       INFO = 0
116:       UPPER = LSAME( UPLO, 'U' )
117:       ONENRM = NORM.EQ.'1' .OR. LSAME( NORM, 'O' )
118:       NOUNIT = LSAME( DIAG, 'N' )
119: *
120:       IF( .NOT.ONENRM .AND. .NOT.LSAME( NORM, 'I' ) ) THEN
121:          INFO = -1
122:       ELSE IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
123:          INFO = -2
124:       ELSE IF( .NOT.NOUNIT .AND. .NOT.LSAME( DIAG, 'U' ) ) THEN
125:          INFO = -3
126:       ELSE IF( N.LT.0 ) THEN
127:          INFO = -4
128:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
129:          INFO = -6
130:       END IF
131:       IF( INFO.NE.0 ) THEN
132:          CALL XERBLA( 'CTRCON', -INFO )
133:          RETURN
134:       END IF
135: *
136: *     Quick return if possible
137: *
138:       IF( N.EQ.0 ) THEN
139:          RCOND = ONE
140:          RETURN
141:       END IF
142: *
143:       RCOND = ZERO
144:       SMLNUM = SLAMCH( 'Safe minimum' )*REAL( MAX( 1, N ) )
145: *
146: *     Compute the norm of the triangular matrix A.
147: *
148:       ANORM = CLANTR( NORM, UPLO, DIAG, N, N, A, LDA, RWORK )
149: *
150: *     Continue only if ANORM > 0.
151: *
152:       IF( ANORM.GT.ZERO ) THEN
153: *
154: *        Estimate the norm of the inverse of A.
155: *
156:          AINVNM = ZERO
157:          NORMIN = 'N'
158:          IF( ONENRM ) THEN
159:             KASE1 = 1
160:          ELSE
161:             KASE1 = 2
162:          END IF
163:          KASE = 0
164:    10    CONTINUE
165:          CALL CLACN2( N, WORK( N+1 ), WORK, AINVNM, KASE, ISAVE )
166:          IF( KASE.NE.0 ) THEN
167:             IF( KASE.EQ.KASE1 ) THEN
168: *
169: *              Multiply by inv(A).
170: *
171:                CALL CLATRS( UPLO, 'No transpose', DIAG, NORMIN, N, A,
172:      \$                      LDA, WORK, SCALE, RWORK, INFO )
173:             ELSE
174: *
175: *              Multiply by inv(A').
176: *
177:                CALL CLATRS( UPLO, 'Conjugate transpose', DIAG, NORMIN,
178:      \$                      N, A, LDA, WORK, SCALE, RWORK, INFO )
179:             END IF
180:             NORMIN = 'Y'
181: *
182: *           Multiply by 1/SCALE if doing so will not cause overflow.
183: *
184:             IF( SCALE.NE.ONE ) THEN
185:                IX = ICAMAX( N, WORK, 1 )
186:                XNORM = CABS1( WORK( IX ) )
187:                IF( SCALE.LT.XNORM*SMLNUM .OR. SCALE.EQ.ZERO )
188:      \$            GO TO 20
189:                CALL CSRSCL( N, SCALE, WORK, 1 )
190:             END IF
191:             GO TO 10
192:          END IF
193: *
194: *        Compute the estimate of the reciprocal condition number.
195: *
196:          IF( AINVNM.NE.ZERO )
197:      \$      RCOND = ( ONE / ANORM ) / AINVNM
198:       END IF
199: *
200:    20 CONTINUE
201:       RETURN
202: *
203: *     End of CTRCON
204: *
205:       END
206: ```