```001:       SUBROUTINE CSYEQUB( UPLO, N, A, LDA, S, SCOND, AMAX, WORK, INFO )
002: *
003: *     -- LAPACK routine (version 3.2)                                 --
004: *     -- Contributed by James Demmel, Deaglan Halligan, Yozo Hida and --
005: *     -- Jason Riedy of Univ. of California Berkeley.                 --
006: *     -- November 2008                                                --
007: *
008: *     -- LAPACK is a software package provided by Univ. of Tennessee, --
009: *     -- Univ. of California Berkeley and NAG Ltd.                    --
010: *
011:       IMPLICIT NONE
012: *     ..
013: *     .. Scalar Arguments ..
014:       INTEGER            INFO, LDA, N
015:       REAL               AMAX, SCOND
016:       CHARACTER          UPLO
017: *     ..
018: *     .. Array Arguments ..
019:       COMPLEX            A( LDA, * ), WORK( * )
020:       REAL               S( * )
021: *     ..
022: *
023: *  Purpose
024: *  =======
025: *
026: *  CSYEQUB computes row and column scalings intended to equilibrate a
027: *  symmetric matrix A and reduce its condition number
028: *  (with respect to the two-norm).  S contains the scale factors,
029: *  S(i) = 1/sqrt(A(i,i)), chosen so that the scaled matrix B with
030: *  elements B(i,j) = S(i)*A(i,j)*S(j) has ones on the diagonal.  This
031: *  choice of S puts the condition number of B within a factor N of the
032: *  smallest possible condition number over all possible diagonal
033: *  scalings.
034: *
035: *  Arguments
036: *  =========
037: *
038: *  N       (input) INTEGER
039: *          The order of the matrix A.  N >= 0.
040: *
041: *  A       (input) COMPLEX array, dimension (LDA,N)
042: *          The N-by-N symmetric matrix whose scaling
043: *          factors are to be computed.  Only the diagonal elements of A
044: *          are referenced.
045: *
046: *  LDA     (input) INTEGER
047: *          The leading dimension of the array A.  LDA >= max(1,N).
048: *
049: *  S       (output) REAL array, dimension (N)
050: *          If INFO = 0, S contains the scale factors for A.
051: *
052: *  SCOND   (output) REAL
053: *          If INFO = 0, S contains the ratio of the smallest S(i) to
054: *          the largest S(i).  If SCOND >= 0.1 and AMAX is neither too
055: *          large nor too small, it is not worth scaling by S.
056: *
057: *  AMAX    (output) REAL
058: *          Absolute value of largest matrix element.  If AMAX is very
059: *          close to overflow or very close to underflow, the matrix
060: *          should be scaled.
061: *  INFO    (output) INTEGER
062: *          = 0:  successful exit
063: *          < 0:  if INFO = -i, the i-th argument had an illegal value
064: *          > 0:  if INFO = i, the i-th diagonal element is nonpositive.
065: *
066: *  =====================================================================
067: *
068: *     .. Parameters ..
069:       REAL               ONE, ZERO
070:       PARAMETER          ( ONE = 1.0E0, ZERO = 0.0E0 )
071:       INTEGER            MAX_ITER
072:       PARAMETER          ( MAX_ITER = 100 )
073: *     ..
074: *     .. Local Scalars ..
075:       INTEGER            I, J, ITER
076:       REAL               AVG, STD, TOL, C0, C1, C2, T, U, SI, D, BASE,
077:      \$                   SMIN, SMAX, SMLNUM, BIGNUM, SCALE, SUMSQ
078:       LOGICAL            UP
079:       COMPLEX            ZDUM
080: *     ..
081: *     .. External Functions ..
082:       REAL               SLAMCH
083:       LOGICAL            LSAME
084: *     ..
085: *     .. External Subroutines ..
086:       EXTERNAL           CLASSQ
087: *     ..
088: *     .. Statement Functions ..
089:       REAL               CABS1
090: *     ..
091: *     Statement Function Definitions
092:       CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
093: *     ..
094: *     .. Executable Statements ..
095: *
096: *     Test the input parameters.
097: *
098:       INFO = 0
099:       IF ( .NOT. ( LSAME( UPLO, 'U' ) .OR. LSAME( UPLO, 'L' ) ) ) THEN
100:         INFO = -1
101:       ELSE IF ( N .LT. 0 ) THEN
102:         INFO = -2
103:       ELSE IF ( LDA .LT. MAX( 1, N ) ) THEN
104:         INFO = -4
105:       END IF
106:       IF ( INFO .NE. 0 ) THEN
107:         CALL XERBLA( 'CSYEQUB', -INFO )
108:         RETURN
109:       END IF
110:
111:       UP = LSAME( UPLO, 'U' )
112:       AMAX = ZERO
113: *
114: *     Quick return if possible.
115: *
116:       IF ( N .EQ. 0 ) THEN
117:         SCOND = ONE
118:         RETURN
119:       END IF
120:
121:       DO I = 1, N
122:         S( I ) = ZERO
123:       END DO
124:
125:       AMAX = ZERO
126:       IF ( UP ) THEN
127:          DO J = 1, N
128:             DO I = 1, J-1
129:                S( I ) = MAX( S( I ), CABS1( A( I, J ) ) )
130:                S( J ) = MAX( S( J ), CABS1( A( I, J ) ) )
131:                AMAX = MAX( AMAX, CABS1( A( I, J ) ) )
132:             END DO
133:             S( J ) = MAX( S( J ), CABS1( A( J, J) ) )
134:             AMAX = MAX( AMAX, CABS1( A( J, J ) ) )
135:          END DO
136:       ELSE
137:          DO J = 1, N
138:             S( J ) = MAX( S( J ), CABS1( A( J, J ) ) )
139:             AMAX = MAX( AMAX, CABS1( A( J, J ) ) )
140:             DO I = J+1, N
141:                S( I ) = MAX( S( I ), CABS1( A( I, J ) ) )
142:                S( J ) = MAX( S( J ), CABS1 (A( I, J ) ) )
143:                AMAX = MAX( AMAX, CABS1( A( I, J ) ) )
144:             END DO
145:          END DO
146:       END IF
147:       DO J = 1, N
148:          S( J ) = 1.0 / S( J )
149:       END DO
150:
151:       TOL = ONE / SQRT( 2.0E0 * N )
152:
153:       DO ITER = 1, MAX_ITER
154:          SCALE = 0.0
155:          SUMSQ = 0.0
156: *       beta = |A|s
157:         DO I = 1, N
158:            WORK( I ) = ZERO
159:         END DO
160:         IF ( UP ) THEN
161:            DO J = 1, N
162:               DO I = 1, J-1
163:                  T = CABS1( A( I, J ) )
164:                  WORK( I ) = WORK( I ) + CABS1( A( I, J ) ) * S( J )
165:                  WORK( J ) = WORK( J ) + CABS1( A( I, J ) ) * S( I )
166:               END DO
167:               WORK( J ) = WORK( J ) + CABS1( A( J, J ) ) * S( J )
168:            END DO
169:         ELSE
170:            DO J = 1, N
171:               WORK( J ) = WORK( J ) + CABS1( A( J, J ) ) * S( J )
172:               DO I = J+1, N
173:                  T = CABS1( A( I, J ) )
174:                  WORK( I ) = WORK( I ) + CABS1( A( I, J ) ) * S( J )
175:                  WORK( J ) = WORK( J ) + CABS1( A( I, J ) ) * S( I )
176:               END DO
177:            END DO
178:         END IF
179:
180: *       avg = s^T beta / n
181:         AVG = 0.0
182:         DO I = 1, N
183:           AVG = AVG + S( I )*WORK( I )
184:         END DO
185:         AVG = AVG / N
186:
187:         STD = 0.0
188:         DO I = 2*N+1, 3*N
189:            WORK( I ) = S( I-2*N ) * WORK( I-2*N ) - AVG
190:         END DO
191:         CALL CLASSQ( N, WORK( 2*N+1 ), 1, SCALE, SUMSQ )
192:         STD = SCALE * SQRT( SUMSQ / N )
193:
194:         IF ( STD .LT. TOL * AVG ) GOTO 999
195:
196:         DO I = 1, N
197:           T = CABS1( A( I, I ) )
198:           SI = S( I )
199:           C2 = ( N-1 ) * T
200:           C1 = ( N-2 ) * ( WORK( I ) - T*SI )
201:           C0 = -(T*SI)*SI + 2*WORK( I )*SI - N*AVG
202:           D = C1*C1 - 4*C0*C2
203:
204:           IF ( D .LE. 0 ) THEN
205:             INFO = -1
206:             RETURN
207:           END IF
208:           SI = -2*C0 / ( C1 + SQRT( D ) )
209:
210:           D = SI - S( I )
211:           U = ZERO
212:           IF ( UP ) THEN
213:             DO J = 1, I
214:               T = CABS1( A( J, I ) )
215:               U = U + S( J )*T
216:               WORK( J ) = WORK( J ) + D*T
217:             END DO
218:             DO J = I+1,N
219:               T = CABS1( A( I, J ) )
220:               U = U + S( J )*T
221:               WORK( J ) = WORK( J ) + D*T
222:             END DO
223:           ELSE
224:             DO J = 1, I
225:               T = CABS1( A( I, J ) )
226:               U = U + S( J )*T
227:               WORK( J ) = WORK( J ) + D*T
228:             END DO
229:             DO J = I+1,N
230:               T = CABS1( A( J, I ) )
231:               U = U + S( J )*T
232:               WORK( J ) = WORK( J ) + D*T
233:             END DO
234:           END IF
235:           AVG = AVG + ( U + WORK( I ) ) * D / N
236:           S( I ) = SI
237:         END DO
238:       END DO
239:
240:  999  CONTINUE
241:
242:       SMLNUM = SLAMCH( 'SAFEMIN' )
243:       BIGNUM = ONE / SMLNUM
244:       SMIN = BIGNUM
245:       SMAX = ZERO
246:       T = ONE / SQRT( AVG )
247:       BASE = SLAMCH( 'B' )
248:       U = ONE / LOG( BASE )
249:       DO I = 1, N
250:         S( I ) = BASE ** INT( U * LOG( S( I ) * T ) )
251:         SMIN = MIN( SMIN, S( I ) )
252:         SMAX = MAX( SMAX, S( I ) )
253:       END DO
254:       SCOND = MAX( SMIN, SMLNUM ) / MIN( SMAX, BIGNUM )
255: *
256:       END
257: ```