001:       SUBROUTINE SGBEQU( M, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
002:      $                   AMAX, 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: *     .. Scalar Arguments ..
010:       INTEGER            INFO, KL, KU, LDAB, M, N
011:       REAL               AMAX, COLCND, ROWCND
012: *     ..
013: *     .. Array Arguments ..
014:       REAL               AB( LDAB, * ), C( * ), R( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  SGBEQU computes row and column scalings intended to equilibrate an
021: *  M-by-N band matrix A and reduce its condition number.  R returns the
022: *  row scale factors and C the column scale factors, chosen to try to
023: *  make the largest element in each row and column of the matrix B with
024: *  elements B(i,j)=R(i)*A(i,j)*C(j) have absolute value 1.
025: *
026: *  R(i) and C(j) are restricted to be between SMLNUM = smallest safe
027: *  number and BIGNUM = largest safe number.  Use of these scaling
028: *  factors is not guaranteed to reduce the condition number of A but
029: *  works well in practice.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  M       (input) INTEGER
035: *          The number of rows of the matrix A.  M >= 0.
036: *
037: *  N       (input) INTEGER
038: *          The number of columns of the matrix A.  N >= 0.
039: *
040: *  KL      (input) INTEGER
041: *          The number of subdiagonals within the band of A.  KL >= 0.
042: *
043: *  KU      (input) INTEGER
044: *          The number of superdiagonals within the band of A.  KU >= 0.
045: *
046: *  AB      (input) REAL array, dimension (LDAB,N)
047: *          The band matrix A, stored in rows 1 to KL+KU+1.  The j-th
048: *          column of A is stored in the j-th column of the array AB as
049: *          follows:
050: *          AB(ku+1+i-j,j) = A(i,j) for max(1,j-ku)<=i<=min(m,j+kl).
051: *
052: *  LDAB    (input) INTEGER
053: *          The leading dimension of the array AB.  LDAB >= KL+KU+1.
054: *
055: *  R       (output) REAL array, dimension (M)
056: *          If INFO = 0, or INFO > M, R contains the row scale factors
057: *          for A.
058: *
059: *  C       (output) REAL array, dimension (N)
060: *          If INFO = 0, C contains the column scale factors for A.
061: *
062: *  ROWCND  (output) REAL
063: *          If INFO = 0 or INFO > M, ROWCND contains the ratio of the
064: *          smallest R(i) to the largest R(i).  If ROWCND >= 0.1 and
065: *          AMAX is neither too large nor too small, it is not worth
066: *          scaling by R.
067: *
068: *  COLCND  (output) REAL
069: *          If INFO = 0, COLCND contains the ratio of the smallest
070: *          C(i) to the largest C(i).  If COLCND >= 0.1, it is not
071: *          worth scaling by C.
072: *
073: *  AMAX    (output) REAL
074: *          Absolute value of largest matrix element.  If AMAX is very
075: *          close to overflow or very close to underflow, the matrix
076: *          should be scaled.
077: *
078: *  INFO    (output) INTEGER
079: *          = 0:  successful exit
080: *          < 0:  if INFO = -i, the i-th argument had an illegal value
081: *          > 0:  if INFO = i, and i is
082: *                <= M:  the i-th row of A is exactly zero
083: *                >  M:  the (i-M)-th column of A is exactly zero
084: *
085: *  =====================================================================
086: *
087: *     .. Parameters ..
088:       REAL               ONE, ZERO
089:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
090: *     ..
091: *     .. Local Scalars ..
092:       INTEGER            I, J, KD
093:       REAL               BIGNUM, RCMAX, RCMIN, SMLNUM
094: *     ..
095: *     .. External Functions ..
096:       REAL               SLAMCH
097:       EXTERNAL           SLAMCH
098: *     ..
099: *     .. External Subroutines ..
100:       EXTERNAL           XERBLA
101: *     ..
102: *     .. Intrinsic Functions ..
103:       INTRINSIC          ABS, MAX, MIN
104: *     ..
105: *     .. Executable Statements ..
106: *
107: *     Test the input parameters
108: *
109:       INFO = 0
110:       IF( M.LT.0 ) THEN
111:          INFO = -1
112:       ELSE IF( N.LT.0 ) THEN
113:          INFO = -2
114:       ELSE IF( KL.LT.0 ) THEN
115:          INFO = -3
116:       ELSE IF( KU.LT.0 ) THEN
117:          INFO = -4
118:       ELSE IF( LDAB.LT.KL+KU+1 ) THEN
119:          INFO = -6
120:       END IF
121:       IF( INFO.NE.0 ) THEN
122:          CALL XERBLA( 'SGBEQU', -INFO )
123:          RETURN
124:       END IF
125: *
126: *     Quick return if possible
127: *
128:       IF( M.EQ.0 .OR. N.EQ.0 ) THEN
129:          ROWCND = ONE
130:          COLCND = ONE
131:          AMAX = ZERO
132:          RETURN
133:       END IF
134: *
135: *     Get machine constants.
136: *
137:       SMLNUM = SLAMCH( 'S' )
138:       BIGNUM = ONE / SMLNUM
139: *
140: *     Compute row scale factors.
141: *
142:       DO 10 I = 1, M
143:          R( I ) = ZERO
144:    10 CONTINUE
145: *
146: *     Find the maximum element in each row.
147: *
148:       KD = KU + 1
149:       DO 30 J = 1, N
150:          DO 20 I = MAX( J-KU, 1 ), MIN( J+KL, M )
151:             R( I ) = MAX( R( I ), ABS( AB( KD+I-J, J ) ) )
152:    20    CONTINUE
153:    30 CONTINUE
154: *
155: *     Find the maximum and minimum scale factors.
156: *
157:       RCMIN = BIGNUM
158:       RCMAX = ZERO
159:       DO 40 I = 1, M
160:          RCMAX = MAX( RCMAX, R( I ) )
161:          RCMIN = MIN( RCMIN, R( I ) )
162:    40 CONTINUE
163:       AMAX = RCMAX
164: *
165:       IF( RCMIN.EQ.ZERO ) THEN
166: *
167: *        Find the first zero scale factor and return an error code.
168: *
169:          DO 50 I = 1, M
170:             IF( R( I ).EQ.ZERO ) THEN
171:                INFO = I
172:                RETURN
173:             END IF
174:    50    CONTINUE
175:       ELSE
176: *
177: *        Invert the scale factors.
178: *
179:          DO 60 I = 1, M
180:             R( I ) = ONE / MIN( MAX( R( I ), SMLNUM ), BIGNUM )
181:    60    CONTINUE
182: *
183: *        Compute ROWCND = min(R(I)) / max(R(I))
184: *
185:          ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
186:       END IF
187: *
188: *     Compute column scale factors
189: *
190:       DO 70 J = 1, N
191:          C( J ) = ZERO
192:    70 CONTINUE
193: *
194: *     Find the maximum element in each column,
195: *     assuming the row scaling computed above.
196: *
197:       KD = KU + 1
198:       DO 90 J = 1, N
199:          DO 80 I = MAX( J-KU, 1 ), MIN( J+KL, M )
200:             C( J ) = MAX( C( J ), ABS( AB( KD+I-J, J ) )*R( I ) )
201:    80    CONTINUE
202:    90 CONTINUE
203: *
204: *     Find the maximum and minimum scale factors.
205: *
206:       RCMIN = BIGNUM
207:       RCMAX = ZERO
208:       DO 100 J = 1, N
209:          RCMIN = MIN( RCMIN, C( J ) )
210:          RCMAX = MAX( RCMAX, C( J ) )
211:   100 CONTINUE
212: *
213:       IF( RCMIN.EQ.ZERO ) THEN
214: *
215: *        Find the first zero scale factor and return an error code.
216: *
217:          DO 110 J = 1, N
218:             IF( C( J ).EQ.ZERO ) THEN
219:                INFO = M + J
220:                RETURN
221:             END IF
222:   110    CONTINUE
223:       ELSE
224: *
225: *        Invert the scale factors.
226: *
227:          DO 120 J = 1, N
228:             C( J ) = ONE / MIN( MAX( C( J ), SMLNUM ), BIGNUM )
229:   120    CONTINUE
230: *
231: *        Compute COLCND = min(C(J)) / max(C(J))
232: *
233:          COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
234:       END IF
235: *
236:       RETURN
237: *
238: *     End of SGBEQU
239: *
240:       END
241: