```001:       SUBROUTINE SSYEQUB( 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:       REAL               A( LDA, * ), S( * ), WORK( * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  SSYEQUB computes row and column scalings intended to equilibrate a
026: *  symmetric matrix A and reduce its condition number
027: *  (with respect to the two-norm).  S contains the scale factors,
028: *  S(i) = 1/sqrt(A(i,i)), chosen so that the scaled matrix B with
029: *  elements B(i,j) = S(i)*A(i,j)*S(j) has ones on the diagonal.  This
030: *  choice of S puts the condition number of B within a factor N of the
031: *  smallest possible condition number over all possible diagonal
032: *  scalings.
033: *
034: *  Arguments
035: *  =========
036: *
037: *  N       (input) INTEGER
038: *          The order of the matrix A.  N >= 0.
039: *
040: *  A       (input) REAL array, dimension (LDA,N)
041: *          The N-by-N symmetric matrix whose scaling
042: *          factors are to be computed.  Only the diagonal elements of A
043: *          are referenced.
044: *
045: *  LDA     (input) INTEGER
046: *          The leading dimension of the array A.  LDA >= max(1,N).
047: *
048: *  S       (output) REAL array, dimension (N)
049: *          If INFO = 0, S contains the scale factors for A.
050: *
051: *  SCOND   (output) REAL
052: *          If INFO = 0, S contains the ratio of the smallest S(i) to
053: *          the largest S(i).  If SCOND >= 0.1 and AMAX is neither too
054: *          large nor too small, it is not worth scaling by S.
055: *
056: *  AMAX    (output) REAL
057: *          Absolute value of largest matrix element.  If AMAX is very
058: *          close to overflow or very close to underflow, the matrix
059: *          should be scaled.
060: *  INFO    (output) INTEGER
061: *          = 0:  successful exit
062: *          < 0:  if INFO = -i, the i-th argument had an illegal value
063: *          > 0:  if INFO = i, the i-th diagonal element is nonpositive.
064: *
065: *  Further Details
066: *  ======= =======
067: *
068: *  Reference: Livne, O.E. and Golub, G.H., "Scaling by Binormalization",
069: *  Numerical Algorithms, vol. 35, no. 1, pp. 97-120, January 2004.
070: *  DOI 10.1023/B:NUMA.0000016606.32820.69
071: *  Tech report version: http://ruready.utah.edu/archive/papers/bin.pdf
072: *
073: *  =====================================================================
074: *
075: *     .. Parameters ..
076:       REAL               ONE, ZERO
077:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
078:       INTEGER            MAX_ITER
079:       PARAMETER          ( MAX_ITER = 100 )
080: *     ..
081: *     .. Local Scalars ..
082:       INTEGER            I, J, ITER
083:       REAL               AVG, STD, TOL, C0, C1, C2, T, U, SI, D, BASE,
084:      \$                   SMIN, SMAX, SMLNUM, BIGNUM, SCALE, SUMSQ
085:       LOGICAL            UP
086: *     ..
087: *     .. External Functions ..
088:       REAL               SLAMCH
089:       LOGICAL            LSAME
090: *     ..
091: *     .. External Subroutines ..
092:       EXTERNAL           SLASSQ
093: *     ..
094: *     .. Executable Statements ..
095: *
096: *     Test 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( 'SSYEQUB', -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 ), ABS( A( I, J ) ) )
130:                S( J ) = MAX( S( J ), ABS( A( I, J ) ) )
131:                AMAX = MAX( AMAX, ABS( A(I, J) ) )
132:             END DO
133:             S( J ) = MAX( S( J ), ABS( A( J, J ) ) )
134:             AMAX = MAX( AMAX, ABS( A( J, J ) ) )
135:          END DO
136:       ELSE
137:          DO J = 1, N
138:             S( J ) = MAX( S( J ), ABS( A( J, J ) ) )
139:             AMAX = MAX( AMAX, ABS( A( J, J ) ) )
140:             DO I = J+1, N
141:                S( I ) = MAX( S( I ), ABS( A( I, J ) ) )
142:                S( J ) = MAX( S( J ), ABS( A( I, J ) ) )
143:                AMAX = MAX( AMAX, ABS( 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 = ABS( A( I, J ) )
164:                  WORK( I ) = WORK( I ) + ABS( A( I, J ) ) * S( J )
165:                  WORK( J ) = WORK( J ) + ABS( A( I, J ) ) * S( I )
166:               END DO
167:               WORK( J ) = WORK( J ) + ABS( A( J, J ) ) * S( J )
168:            END DO
169:         ELSE
170:            DO J = 1, N
171:               WORK( J ) = WORK( J ) + ABS( A( J, J ) ) * S( J )
172:               DO I = J+1, N
173:                  T = ABS( A( I, J ) )
174:                  WORK( I ) = WORK( I ) + ABS( A( I, J ) ) * S( J )
175:                  WORK( J ) = WORK( J ) + ABS( 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 SLASSQ( 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 = ABS( 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 = ABS( 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 = ABS( 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 = ABS( 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 = ABS( A( J, I ) )
231:               U = U + S( J )*T
232:               WORK( J ) = WORK( J ) + D*T
233:             END DO
234:           END IF
235:
236:           AVG = AVG + ( U + WORK( I ) ) * D / N
237:           S( I ) = SI
238:
239:         END DO
240:
241:       END DO
242:
243:  999  CONTINUE
244:
245:       SMLNUM = SLAMCH( 'SAFEMIN' )
246:       BIGNUM = ONE / SMLNUM
247:       SMIN = BIGNUM
248:       SMAX = ZERO
249:       T = ONE / SQRT(AVG)
250:       BASE = SLAMCH( 'B' )
251:       U = ONE / LOG( BASE )
252:       DO I = 1, N
253:         S( I ) = BASE ** INT( U * LOG( S( I ) * T ) )
254:         SMIN = MIN( SMIN, S( I ) )
255:         SMAX = MAX( SMAX, S( I ) )
256:       END DO
257:       SCOND = MAX( SMIN, SMLNUM ) / MIN( SMAX, BIGNUM )
258: *
259:       END
260: ```