LAPACK  3.4.2
LAPACK: Linear Algebra PACKage
 All Files Functions Groups
sgebal.f
Go to the documentation of this file.
1 *> \brief \b SGEBAL
2 *
3 * =========== DOCUMENTATION ===========
4 *
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
7 *
8 *> \htmlonly
9 *> Download SGEBAL + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgebal.f">
11 *> [TGZ]</a>
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgebal.f">
13 *> [ZIP]</a>
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgebal.f">
15 *> [TXT]</a>
16 *> \endhtmlonly
17 *
18 * Definition:
19 * ===========
20 *
21 * SUBROUTINE SGEBAL( JOB, N, A, LDA, ILO, IHI, SCALE, INFO )
22 *
23 * .. Scalar Arguments ..
24 * CHARACTER JOB
25 * INTEGER IHI, ILO, INFO, LDA, N
26 * ..
27 * .. Array Arguments ..
28 * REAL A( LDA, * ), SCALE( * )
29 * ..
30 *
31 *
32 *> \par Purpose:
33 * =============
34 *>
35 *> \verbatim
36 *>
37 *> SGEBAL balances a general real matrix A. This involves, first,
38 *> permuting A by a similarity transformation to isolate eigenvalues
39 *> in the first 1 to ILO-1 and last IHI+1 to N elements on the
40 *> diagonal; and second, applying a diagonal similarity transformation
41 *> to rows and columns ILO to IHI to make the rows and columns as
42 *> close in norm as possible. Both steps are optional.
43 *>
44 *> Balancing may reduce the 1-norm of the matrix, and improve the
45 *> accuracy of the computed eigenvalues and/or eigenvectors.
46 *> \endverbatim
47 *
48 * Arguments:
49 * ==========
50 *
51 *> \param[in] JOB
52 *> \verbatim
53 *> JOB is CHARACTER*1
54 *> Specifies the operations to be performed on A:
55 *> = 'N': none: simply set ILO = 1, IHI = N, SCALE(I) = 1.0
56 *> for i = 1,...,N;
57 *> = 'P': permute only;
58 *> = 'S': scale only;
59 *> = 'B': both permute and scale.
60 *> \endverbatim
61 *>
62 *> \param[in] N
63 *> \verbatim
64 *> N is INTEGER
65 *> The order of the matrix A. N >= 0.
66 *> \endverbatim
67 *>
68 *> \param[in,out] A
69 *> \verbatim
70 *> A is REAL array, dimension (LDA,N)
71 *> On entry, the input matrix A.
72 *> On exit, A is overwritten by the balanced matrix.
73 *> If JOB = 'N', A is not referenced.
74 *> See Further Details.
75 *> \endverbatim
76 *>
77 *> \param[in] LDA
78 *> \verbatim
79 *> LDA is INTEGER
80 *> The leading dimension of the array A. LDA >= max(1,N).
81 *> \endverbatim
82 *>
83 *> \param[out] ILO
84 *> \verbatim
85 *> ILO is INTEGER
86 *> \endverbatim
87 *> \param[out] IHI
88 *> \verbatim
89 *> IHI is INTEGER
90 *> ILO and IHI are set to integers such that on exit
91 *> A(i,j) = 0 if i > j and j = 1,...,ILO-1 or I = IHI+1,...,N.
92 *> If JOB = 'N' or 'S', ILO = 1 and IHI = N.
93 *> \endverbatim
94 *>
95 *> \param[out] SCALE
96 *> \verbatim
97 *> SCALE is REAL array, dimension (N)
98 *> Details of the permutations and scaling factors applied to
99 *> A. If P(j) is the index of the row and column interchanged
100 *> with row and column j and D(j) is the scaling factor
101 *> applied to row and column j, then
102 *> SCALE(j) = P(j) for j = 1,...,ILO-1
103 *> = D(j) for j = ILO,...,IHI
104 *> = P(j) for j = IHI+1,...,N.
105 *> The order in which the interchanges are made is N to IHI+1,
106 *> then 1 to ILO-1.
107 *> \endverbatim
108 *>
109 *> \param[out] INFO
110 *> \verbatim
111 *> INFO is INTEGER
112 *> = 0: successful exit.
113 *> < 0: if INFO = -i, the i-th argument had an illegal value.
114 *> \endverbatim
115 *
116 * Authors:
117 * ========
118 *
119 *> \author Univ. of Tennessee
120 *> \author Univ. of California Berkeley
121 *> \author Univ. of Colorado Denver
122 *> \author NAG Ltd.
123 *
124 *> \date November 2011
125 *
126 *> \ingroup realGEcomputational
127 *
128 *> \par Further Details:
129 * =====================
130 *>
131 *> \verbatim
132 *>
133 *> The permutations consist of row and column interchanges which put
134 *> the matrix in the form
135 *>
136 *> ( T1 X Y )
137 *> P A P = ( 0 B Z )
138 *> ( 0 0 T2 )
139 *>
140 *> where T1 and T2 are upper triangular matrices whose eigenvalues lie
141 *> along the diagonal. The column indices ILO and IHI mark the starting
142 *> and ending columns of the submatrix B. Balancing consists of applying
143 *> a diagonal similarity transformation inv(D) * B * D to make the
144 *> 1-norms of each row of B and its corresponding column nearly equal.
145 *> The output matrix is
146 *>
147 *> ( T1 X*D Y )
148 *> ( 0 inv(D)*B*D inv(D)*Z ).
149 *> ( 0 0 T2 )
150 *>
151 *> Information about the permutations P and the diagonal matrix D is
152 *> returned in the vector SCALE.
153 *>
154 *> This subroutine is based on the EISPACK routine BALANC.
155 *>
156 *> Modified by Tzu-Yi Chen, Computer Science Division, University of
157 *> California at Berkeley, USA
158 *> \endverbatim
159 *>
160 * =====================================================================
161  SUBROUTINE sgebal( JOB, N, A, LDA, ILO, IHI, SCALE, INFO )
162 *
163 * -- LAPACK computational routine (version 3.4.0) --
164 * -- LAPACK is a software package provided by Univ. of Tennessee, --
165 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
166 * November 2011
167 *
168 * .. Scalar Arguments ..
169  CHARACTER job
170  INTEGER ihi, ilo, info, lda, n
171 * ..
172 * .. Array Arguments ..
173  REAL a( lda, * ), scale( * )
174 * ..
175 *
176 * =====================================================================
177 *
178 * .. Parameters ..
179  REAL zero, one
180  parameter( zero = 0.0e+0, one = 1.0e+0 )
181  REAL sclfac
182  parameter( sclfac = 2.0e+0 )
183  REAL factor
184  parameter( factor = 0.95e+0 )
185 * ..
186 * .. Local Scalars ..
187  LOGICAL noconv
188  INTEGER i, ica, iexc, ira, j, k, l, m
189  REAL c, ca, f, g, r, ra, s, sfmax1, sfmax2, sfmin1,
190  $ sfmin2
191 * ..
192 * .. External Functions ..
193  LOGICAL sisnan, lsame
194  INTEGER isamax
195  REAL slamch
196  EXTERNAL sisnan, lsame, isamax, slamch
197 * ..
198 * .. External Subroutines ..
199  EXTERNAL sscal, sswap, xerbla
200 * ..
201 * .. Intrinsic Functions ..
202  INTRINSIC abs, max, min
203 * ..
204 * .. Executable Statements ..
205 *
206 * Test the input parameters
207 *
208  info = 0
209  IF( .NOT.lsame( job, 'N' ) .AND. .NOT.lsame( job, 'P' ) .AND.
210  $ .NOT.lsame( job, 'S' ) .AND. .NOT.lsame( job, 'B' ) ) THEN
211  info = -1
212  ELSE IF( n.LT.0 ) THEN
213  info = -2
214  ELSE IF( lda.LT.max( 1, n ) ) THEN
215  info = -4
216  END IF
217  IF( info.NE.0 ) THEN
218  CALL xerbla( 'SGEBAL', -info )
219  return
220  END IF
221 *
222  k = 1
223  l = n
224 *
225  IF( n.EQ.0 )
226  $ go to 210
227 *
228  IF( lsame( job, 'N' ) ) THEN
229  DO 10 i = 1, n
230  scale( i ) = one
231  10 continue
232  go to 210
233  END IF
234 *
235  IF( lsame( job, 'S' ) )
236  $ go to 120
237 *
238 * Permutation to isolate eigenvalues if possible
239 *
240  go to 50
241 *
242 * Row and column exchange.
243 *
244  20 continue
245  scale( m ) = j
246  IF( j.EQ.m )
247  $ go to 30
248 *
249  CALL sswap( l, a( 1, j ), 1, a( 1, m ), 1 )
250  CALL sswap( n-k+1, a( j, k ), lda, a( m, k ), lda )
251 *
252  30 continue
253  go to( 40, 80 )iexc
254 *
255 * Search for rows isolating an eigenvalue and push them down.
256 *
257  40 continue
258  IF( l.EQ.1 )
259  $ go to 210
260  l = l - 1
261 *
262  50 continue
263  DO 70 j = l, 1, -1
264 *
265  DO 60 i = 1, l
266  IF( i.EQ.j )
267  $ go to 60
268  IF( a( j, i ).NE.zero )
269  $ go to 70
270  60 continue
271 *
272  m = l
273  iexc = 1
274  go to 20
275  70 continue
276 *
277  go to 90
278 *
279 * Search for columns isolating an eigenvalue and push them left.
280 *
281  80 continue
282  k = k + 1
283 *
284  90 continue
285  DO 110 j = k, l
286 *
287  DO 100 i = k, l
288  IF( i.EQ.j )
289  $ go to 100
290  IF( a( i, j ).NE.zero )
291  $ go to 110
292  100 continue
293 *
294  m = k
295  iexc = 2
296  go to 20
297  110 continue
298 *
299  120 continue
300  DO 130 i = k, l
301  scale( i ) = one
302  130 continue
303 *
304  IF( lsame( job, 'P' ) )
305  $ go to 210
306 *
307 * Balance the submatrix in rows K to L.
308 *
309 * Iterative loop for norm reduction
310 *
311  sfmin1 = slamch( 'S' ) / slamch( 'P' )
312  sfmax1 = one / sfmin1
313  sfmin2 = sfmin1*sclfac
314  sfmax2 = one / sfmin2
315  140 continue
316  noconv = .false.
317 *
318  DO 200 i = k, l
319  c = zero
320  r = zero
321 *
322  DO 150 j = k, l
323  IF( j.EQ.i )
324  $ go to 150
325  c = c + abs( a( j, i ) )
326  r = r + abs( a( i, j ) )
327  150 continue
328  ica = isamax( l, a( 1, i ), 1 )
329  ca = abs( a( ica, i ) )
330  ira = isamax( n-k+1, a( i, k ), lda )
331  ra = abs( a( i, ira+k-1 ) )
332 *
333 * Guard against zero C or R due to underflow.
334 *
335  IF( c.EQ.zero .OR. r.EQ.zero )
336  $ go to 200
337  g = r / sclfac
338  f = one
339  s = c + r
340  160 continue
341  IF( c.GE.g .OR. max( f, c, ca ).GE.sfmax2 .OR.
342  $ min( r, g, ra ).LE.sfmin2 )go to 170
343  f = f*sclfac
344  c = c*sclfac
345  ca = ca*sclfac
346  r = r / sclfac
347  g = g / sclfac
348  ra = ra / sclfac
349  go to 160
350 *
351  170 continue
352  g = c / sclfac
353  180 continue
354  IF( g.LT.r .OR. max( r, ra ).GE.sfmax2 .OR.
355  $ min( f, c, g, ca ).LE.sfmin2 )go to 190
356  IF( sisnan( c+f+ca+r+g+ra ) ) THEN
357 *
358 * Exit if NaN to avoid infinite loop
359 *
360  info = -3
361  CALL xerbla( 'SGEBAL', -info )
362  return
363  END IF
364  f = f / sclfac
365  c = c / sclfac
366  g = g / sclfac
367  ca = ca / sclfac
368  r = r*sclfac
369  ra = ra*sclfac
370  go to 180
371 *
372 * Now balance.
373 *
374  190 continue
375  IF( ( c+r ).GE.factor*s )
376  $ go to 200
377  IF( f.LT.one .AND. scale( i ).LT.one ) THEN
378  IF( f*scale( i ).LE.sfmin1 )
379  $ go to 200
380  END IF
381  IF( f.GT.one .AND. scale( i ).GT.one ) THEN
382  IF( scale( i ).GE.sfmax1 / f )
383  $ go to 200
384  END IF
385  g = one / f
386  scale( i ) = scale( i )*f
387  noconv = .true.
388 *
389  CALL sscal( n-k+1, g, a( i, k ), lda )
390  CALL sscal( l, f, a( 1, i ), 1 )
391 *
392  200 continue
393 *
394  IF( noconv )
395  $ go to 140
396 *
397  210 continue
398  ilo = k
399  ihi = l
400 *
401  return
402 *
403 * End of SGEBAL
404 *
405  END