```001:       SUBROUTINE CPBEQU( UPLO, N, KD, AB, LDAB, S, SCOND, AMAX, 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, KD, LDAB, N
011:       REAL               AMAX, SCOND
012: *     ..
013: *     .. Array Arguments ..
014:       REAL               S( * )
015:       COMPLEX            AB( LDAB, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  CPBEQU computes row and column scalings intended to equilibrate a
022: *  Hermitian positive definite band matrix A and reduce its condition
023: *  number (with respect to the two-norm).  S contains the scale factors,
024: *  S(i) = 1/sqrt(A(i,i)), chosen so that the scaled matrix B with
025: *  elements B(i,j) = S(i)*A(i,j)*S(j) has ones on the diagonal.  This
026: *  choice of S puts the condition number of B within a factor N of the
027: *  smallest possible condition number over all possible diagonal
028: *  scalings.
029: *
030: *  Arguments
031: *  =========
032: *
033: *  UPLO    (input) CHARACTER*1
034: *          = 'U':  Upper triangular of A is stored;
035: *          = 'L':  Lower triangular of A is stored.
036: *
037: *  N       (input) INTEGER
038: *          The order of the matrix A.  N >= 0.
039: *
040: *  KD      (input) INTEGER
041: *          The number of superdiagonals of the matrix A if UPLO = 'U',
042: *          or the number of subdiagonals if UPLO = 'L'.  KD >= 0.
043: *
044: *  AB      (input) COMPLEX array, dimension (LDAB,N)
045: *          The upper or lower triangle of the Hermitian band matrix A,
046: *          stored in the first KD+1 rows of the array.  The j-th column
047: *          of A is stored in the j-th column of the array AB as follows:
048: *          if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j;
049: *          if UPLO = 'L', AB(1+i-j,j)    = A(i,j) for j<=i<=min(n,j+kd).
050: *
051: *  LDAB     (input) INTEGER
052: *          The leading dimension of the array A.  LDAB >= KD+1.
053: *
054: *  S       (output) REAL array, dimension (N)
055: *          If INFO = 0, S contains the scale factors for A.
056: *
057: *  SCOND   (output) REAL
058: *          If INFO = 0, S contains the ratio of the smallest S(i) to
059: *          the largest S(i).  If SCOND >= 0.1 and AMAX is neither too
060: *          large nor too small, it is not worth scaling by S.
061: *
062: *  AMAX    (output) REAL
063: *          Absolute value of largest matrix element.  If AMAX is very
064: *          close to overflow or very close to underflow, the matrix
065: *          should be scaled.
066: *
067: *  INFO    (output) INTEGER
068: *          = 0:  successful exit
069: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
070: *          > 0:  if INFO = i, the i-th diagonal element is nonpositive.
071: *
072: *  =====================================================================
073: *
074: *     .. Parameters ..
075:       REAL               ZERO, ONE
076:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
077: *     ..
078: *     .. Local Scalars ..
079:       LOGICAL            UPPER
080:       INTEGER            I, J
081:       REAL               SMIN
082: *     ..
083: *     .. External Functions ..
084:       LOGICAL            LSAME
085:       EXTERNAL           LSAME
086: *     ..
087: *     .. External Subroutines ..
088:       EXTERNAL           XERBLA
089: *     ..
090: *     .. Intrinsic Functions ..
091:       INTRINSIC          MAX, MIN, REAL, SQRT
092: *     ..
093: *     .. Executable Statements ..
094: *
095: *     Test the input parameters.
096: *
097:       INFO = 0
098:       UPPER = LSAME( UPLO, 'U' )
099:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
100:          INFO = -1
101:       ELSE IF( N.LT.0 ) THEN
102:          INFO = -2
103:       ELSE IF( KD.LT.0 ) THEN
104:          INFO = -3
105:       ELSE IF( LDAB.LT.KD+1 ) THEN
106:          INFO = -5
107:       END IF
108:       IF( INFO.NE.0 ) THEN
109:          CALL XERBLA( 'CPBEQU', -INFO )
110:          RETURN
111:       END IF
112: *
113: *     Quick return if possible
114: *
115:       IF( N.EQ.0 ) THEN
116:          SCOND = ONE
117:          AMAX = ZERO
118:          RETURN
119:       END IF
120: *
121:       IF( UPPER ) THEN
122:          J = KD + 1
123:       ELSE
124:          J = 1
125:       END IF
126: *
127: *     Initialize SMIN and AMAX.
128: *
129:       S( 1 ) = REAL( AB( J, 1 ) )
130:       SMIN = S( 1 )
131:       AMAX = S( 1 )
132: *
133: *     Find the minimum and maximum diagonal elements.
134: *
135:       DO 10 I = 2, N
136:          S( I ) = REAL( AB( J, I ) )
137:          SMIN = MIN( SMIN, S( I ) )
138:          AMAX = MAX( AMAX, S( I ) )
139:    10 CONTINUE
140: *
141:       IF( SMIN.LE.ZERO ) THEN
142: *
143: *        Find the first non-positive diagonal element and return.
144: *
145:          DO 20 I = 1, N
146:             IF( S( I ).LE.ZERO ) THEN
147:                INFO = I
148:                RETURN
149:             END IF
150:    20    CONTINUE
151:       ELSE
152: *
153: *        Set the scale factors to the reciprocals
154: *        of the diagonal elements.
155: *
156:          DO 30 I = 1, N
157:             S( I ) = ONE / SQRT( S( I ) )
158:    30    CONTINUE
159: *
160: *        Compute SCOND = min(S(I)) / max(S(I))
161: *
162:          SCOND = SQRT( SMIN ) / SQRT( AMAX )
163:       END IF
164:       RETURN
165: *
166: *     End of CPBEQU
167: *
168:       END
169: ```