LAPACK  3.4.2
LAPACK: Linear Algebra PACKage
 All Files Functions Groups
sggbal.f
Go to the documentation of this file.
1 *> \brief \b SGGBAL
2 *
3 * =========== DOCUMENTATION ===========
4 *
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
7 *
8 *> \htmlonly
9 *> Download SGGBAL + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggbal.f">
11 *> [TGZ]</a>
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggbal.f">
13 *> [ZIP]</a>
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggbal.f">
15 *> [TXT]</a>
16 *> \endhtmlonly
17 *
18 * Definition:
19 * ===========
20 *
21 * SUBROUTINE SGGBAL( JOB, N, A, LDA, B, LDB, ILO, IHI, LSCALE,
22 * RSCALE, WORK, INFO )
23 *
24 * .. Scalar Arguments ..
25 * CHARACTER JOB
26 * INTEGER IHI, ILO, INFO, LDA, LDB, N
27 * ..
28 * .. Array Arguments ..
29 * REAL A( LDA, * ), B( LDB, * ), LSCALE( * ),
30 * $ RSCALE( * ), WORK( * )
31 * ..
32 *
33 *
34 *> \par Purpose:
35 * =============
36 *>
37 *> \verbatim
38 *>
39 *> SGGBAL balances a pair of general real matrices (A,B). This
40 *> involves, first, permuting A and B by similarity transformations to
41 *> isolate eigenvalues in the first 1 to ILO$-$1 and last IHI+1 to N
42 *> elements on the diagonal; and second, applying a diagonal similarity
43 *> transformation to rows and columns ILO to IHI to make the rows
44 *> and columns as close in norm as possible. Both steps are optional.
45 *>
46 *> Balancing may reduce the 1-norm of the matrices, and improve the
47 *> accuracy of the computed eigenvalues and/or eigenvectors in the
48 *> generalized eigenvalue problem A*x = lambda*B*x.
49 *> \endverbatim
50 *
51 * Arguments:
52 * ==========
53 *
54 *> \param[in] JOB
55 *> \verbatim
56 *> JOB is CHARACTER*1
57 *> Specifies the operations to be performed on A and B:
58 *> = 'N': none: simply set ILO = 1, IHI = N, LSCALE(I) = 1.0
59 *> and RSCALE(I) = 1.0 for i = 1,...,N.
60 *> = 'P': permute only;
61 *> = 'S': scale only;
62 *> = 'B': both permute and scale.
63 *> \endverbatim
64 *>
65 *> \param[in] N
66 *> \verbatim
67 *> N is INTEGER
68 *> The order of the matrices A and B. N >= 0.
69 *> \endverbatim
70 *>
71 *> \param[in,out] A
72 *> \verbatim
73 *> A is REAL array, dimension (LDA,N)
74 *> On entry, the input matrix A.
75 *> On exit, A is overwritten by the balanced matrix.
76 *> If JOB = 'N', A is not referenced.
77 *> \endverbatim
78 *>
79 *> \param[in] LDA
80 *> \verbatim
81 *> LDA is INTEGER
82 *> The leading dimension of the array A. LDA >= max(1,N).
83 *> \endverbatim
84 *>
85 *> \param[in,out] B
86 *> \verbatim
87 *> B is REAL array, dimension (LDB,N)
88 *> On entry, the input matrix B.
89 *> On exit, B is overwritten by the balanced matrix.
90 *> If JOB = 'N', B is not referenced.
91 *> \endverbatim
92 *>
93 *> \param[in] LDB
94 *> \verbatim
95 *> LDB is INTEGER
96 *> The leading dimension of the array B. LDB >= max(1,N).
97 *> \endverbatim
98 *>
99 *> \param[out] ILO
100 *> \verbatim
101 *> ILO is INTEGER
102 *> \endverbatim
103 *>
104 *> \param[out] IHI
105 *> \verbatim
106 *> IHI is INTEGER
107 *> ILO and IHI are set to integers such that on exit
108 *> A(i,j) = 0 and B(i,j) = 0 if i > j and
109 *> j = 1,...,ILO-1 or i = IHI+1,...,N.
110 *> If JOB = 'N' or 'S', ILO = 1 and IHI = N.
111 *> \endverbatim
112 *>
113 *> \param[out] LSCALE
114 *> \verbatim
115 *> LSCALE is REAL array, dimension (N)
116 *> Details of the permutations and scaling factors applied
117 *> to the left side of A and B. If P(j) is the index of the
118 *> row interchanged with row j, and D(j)
119 *> is the scaling factor applied to row j, then
120 *> LSCALE(j) = P(j) for J = 1,...,ILO-1
121 *> = D(j) for J = ILO,...,IHI
122 *> = P(j) for J = IHI+1,...,N.
123 *> The order in which the interchanges are made is N to IHI+1,
124 *> then 1 to ILO-1.
125 *> \endverbatim
126 *>
127 *> \param[out] RSCALE
128 *> \verbatim
129 *> RSCALE is REAL array, dimension (N)
130 *> Details of the permutations and scaling factors applied
131 *> to the right side of A and B. If P(j) is the index of the
132 *> column interchanged with column j, and D(j)
133 *> is the scaling factor applied to column j, then
134 *> LSCALE(j) = P(j) for J = 1,...,ILO-1
135 *> = D(j) for J = ILO,...,IHI
136 *> = P(j) for J = IHI+1,...,N.
137 *> The order in which the interchanges are made is N to IHI+1,
138 *> then 1 to ILO-1.
139 *> \endverbatim
140 *>
141 *> \param[out] WORK
142 *> \verbatim
143 *> WORK is REAL array, dimension (lwork)
144 *> lwork must be at least max(1,6*N) when JOB = 'S' or 'B', and
145 *> at least 1 when JOB = 'N' or 'P'.
146 *> \endverbatim
147 *>
148 *> \param[out] INFO
149 *> \verbatim
150 *> INFO is INTEGER
151 *> = 0: successful exit
152 *> < 0: if INFO = -i, the i-th argument had an illegal value.
153 *> \endverbatim
154 *
155 * Authors:
156 * ========
157 *
158 *> \author Univ. of Tennessee
159 *> \author Univ. of California Berkeley
160 *> \author Univ. of Colorado Denver
161 *> \author NAG Ltd.
162 *
163 *> \date November 2011
164 *
165 *> \ingroup realGBcomputational
166 *
167 *> \par Further Details:
168 * =====================
169 *>
170 *> \verbatim
171 *>
172 *> See R.C. WARD, Balancing the generalized eigenvalue problem,
173 *> SIAM J. Sci. Stat. Comp. 2 (1981), 141-152.
174 *> \endverbatim
175 *>
176 * =====================================================================
177  SUBROUTINE sggbal( JOB, N, A, LDA, B, LDB, ILO, IHI, LSCALE,
178  $ rscale, work, info )
179 *
180 * -- LAPACK computational routine (version 3.4.0) --
181 * -- LAPACK is a software package provided by Univ. of Tennessee, --
182 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
183 * November 2011
184 *
185 * .. Scalar Arguments ..
186  CHARACTER job
187  INTEGER ihi, ilo, info, lda, ldb, n
188 * ..
189 * .. Array Arguments ..
190  REAL a( lda, * ), b( ldb, * ), lscale( * ),
191  $ rscale( * ), work( * )
192 * ..
193 *
194 * =====================================================================
195 *
196 * .. Parameters ..
197  REAL zero, half, one
198  parameter( zero = 0.0e+0, half = 0.5e+0, one = 1.0e+0 )
199  REAL three, sclfac
200  parameter( three = 3.0e+0, sclfac = 1.0e+1 )
201 * ..
202 * .. Local Scalars ..
203  INTEGER i, icab, iflow, ip1, ir, irab, it, j, jc, jp1,
204  $ k, kount, l, lcab, lm1, lrab, lsfmax, lsfmin,
205  $ m, nr, nrp2
206  REAL alpha, basl, beta, cab, cmax, coef, coef2,
207  $ coef5, cor, ew, ewc, gamma, pgamma, rab, sfmax,
208  $ sfmin, sum, t, ta, tb, tc
209 * ..
210 * .. External Functions ..
211  LOGICAL lsame
212  INTEGER isamax
213  REAL sdot, slamch
214  EXTERNAL lsame, isamax, sdot, slamch
215 * ..
216 * .. External Subroutines ..
217  EXTERNAL saxpy, sscal, sswap, xerbla
218 * ..
219 * .. Intrinsic Functions ..
220  INTRINSIC abs, int, log10, max, min, REAL, sign
221 * ..
222 * .. Executable Statements ..
223 *
224 * Test the input parameters
225 *
226  info = 0
227  IF( .NOT.lsame( job, 'N' ) .AND. .NOT.lsame( job, 'P' ) .AND.
228  $ .NOT.lsame( job, 'S' ) .AND. .NOT.lsame( job, 'B' ) ) THEN
229  info = -1
230  ELSE IF( n.LT.0 ) THEN
231  info = -2
232  ELSE IF( lda.LT.max( 1, n ) ) THEN
233  info = -4
234  ELSE IF( ldb.LT.max( 1, n ) ) THEN
235  info = -6
236  END IF
237  IF( info.NE.0 ) THEN
238  CALL xerbla( 'SGGBAL', -info )
239  return
240  END IF
241 *
242 * Quick return if possible
243 *
244  IF( n.EQ.0 ) THEN
245  ilo = 1
246  ihi = n
247  return
248  END IF
249 *
250  IF( n.EQ.1 ) THEN
251  ilo = 1
252  ihi = n
253  lscale( 1 ) = one
254  rscale( 1 ) = one
255  return
256  END IF
257 *
258  IF( lsame( job, 'N' ) ) THEN
259  ilo = 1
260  ihi = n
261  DO 10 i = 1, n
262  lscale( i ) = one
263  rscale( i ) = one
264  10 continue
265  return
266  END IF
267 *
268  k = 1
269  l = n
270  IF( lsame( job, 'S' ) )
271  $ go to 190
272 *
273  go to 30
274 *
275 * Permute the matrices A and B to isolate the eigenvalues.
276 *
277 * Find row with one nonzero in columns 1 through L
278 *
279  20 continue
280  l = lm1
281  IF( l.NE.1 )
282  $ go to 30
283 *
284  rscale( 1 ) = one
285  lscale( 1 ) = one
286  go to 190
287 *
288  30 continue
289  lm1 = l - 1
290  DO 80 i = l, 1, -1
291  DO 40 j = 1, lm1
292  jp1 = j + 1
293  IF( a( i, j ).NE.zero .OR. b( i, j ).NE.zero )
294  $ go to 50
295  40 continue
296  j = l
297  go to 70
298 *
299  50 continue
300  DO 60 j = jp1, l
301  IF( a( i, j ).NE.zero .OR. b( i, j ).NE.zero )
302  $ go to 80
303  60 continue
304  j = jp1 - 1
305 *
306  70 continue
307  m = l
308  iflow = 1
309  go to 160
310  80 continue
311  go to 100
312 *
313 * Find column with one nonzero in rows K through N
314 *
315  90 continue
316  k = k + 1
317 *
318  100 continue
319  DO 150 j = k, l
320  DO 110 i = k, lm1
321  ip1 = i + 1
322  IF( a( i, j ).NE.zero .OR. b( i, j ).NE.zero )
323  $ go to 120
324  110 continue
325  i = l
326  go to 140
327  120 continue
328  DO 130 i = ip1, l
329  IF( a( i, j ).NE.zero .OR. b( i, j ).NE.zero )
330  $ go to 150
331  130 continue
332  i = ip1 - 1
333  140 continue
334  m = k
335  iflow = 2
336  go to 160
337  150 continue
338  go to 190
339 *
340 * Permute rows M and I
341 *
342  160 continue
343  lscale( m ) = i
344  IF( i.EQ.m )
345  $ go to 170
346  CALL sswap( n-k+1, a( i, k ), lda, a( m, k ), lda )
347  CALL sswap( n-k+1, b( i, k ), ldb, b( m, k ), ldb )
348 *
349 * Permute columns M and J
350 *
351  170 continue
352  rscale( m ) = j
353  IF( j.EQ.m )
354  $ go to 180
355  CALL sswap( l, a( 1, j ), 1, a( 1, m ), 1 )
356  CALL sswap( l, b( 1, j ), 1, b( 1, m ), 1 )
357 *
358  180 continue
359  go to( 20, 90 )iflow
360 *
361  190 continue
362  ilo = k
363  ihi = l
364 *
365  IF( lsame( job, 'P' ) ) THEN
366  DO 195 i = ilo, ihi
367  lscale( i ) = one
368  rscale( i ) = one
369  195 continue
370  return
371  END IF
372 *
373  IF( ilo.EQ.ihi )
374  $ return
375 *
376 * Balance the submatrix in rows ILO to IHI.
377 *
378  nr = ihi - ilo + 1
379  DO 200 i = ilo, ihi
380  rscale( i ) = zero
381  lscale( i ) = zero
382 *
383  work( i ) = zero
384  work( i+n ) = zero
385  work( i+2*n ) = zero
386  work( i+3*n ) = zero
387  work( i+4*n ) = zero
388  work( i+5*n ) = zero
389  200 continue
390 *
391 * Compute right side vector in resulting linear equations
392 *
393  basl = log10( sclfac )
394  DO 240 i = ilo, ihi
395  DO 230 j = ilo, ihi
396  tb = b( i, j )
397  ta = a( i, j )
398  IF( ta.EQ.zero )
399  $ go to 210
400  ta = log10( abs( ta ) ) / basl
401  210 continue
402  IF( tb.EQ.zero )
403  $ go to 220
404  tb = log10( abs( tb ) ) / basl
405  220 continue
406  work( i+4*n ) = work( i+4*n ) - ta - tb
407  work( j+5*n ) = work( j+5*n ) - ta - tb
408  230 continue
409  240 continue
410 *
411  coef = one / REAL( 2*nr )
412  coef2 = coef*coef
413  coef5 = half*coef2
414  nrp2 = nr + 2
415  beta = zero
416  it = 1
417 *
418 * Start generalized conjugate gradient iteration
419 *
420  250 continue
421 *
422  gamma = sdot( nr, work( ilo+4*n ), 1, work( ilo+4*n ), 1 ) +
423  $ sdot( nr, work( ilo+5*n ), 1, work( ilo+5*n ), 1 )
424 *
425  ew = zero
426  ewc = zero
427  DO 260 i = ilo, ihi
428  ew = ew + work( i+4*n )
429  ewc = ewc + work( i+5*n )
430  260 continue
431 *
432  gamma = coef*gamma - coef2*( ew**2+ewc**2 ) - coef5*( ew-ewc )**2
433  IF( gamma.EQ.zero )
434  $ go to 350
435  IF( it.NE.1 )
436  $ beta = gamma / pgamma
437  t = coef5*( ewc-three*ew )
438  tc = coef5*( ew-three*ewc )
439 *
440  CALL sscal( nr, beta, work( ilo ), 1 )
441  CALL sscal( nr, beta, work( ilo+n ), 1 )
442 *
443  CALL saxpy( nr, coef, work( ilo+4*n ), 1, work( ilo+n ), 1 )
444  CALL saxpy( nr, coef, work( ilo+5*n ), 1, work( ilo ), 1 )
445 *
446  DO 270 i = ilo, ihi
447  work( i ) = work( i ) + tc
448  work( i+n ) = work( i+n ) + t
449  270 continue
450 *
451 * Apply matrix to vector
452 *
453  DO 300 i = ilo, ihi
454  kount = 0
455  sum = zero
456  DO 290 j = ilo, ihi
457  IF( a( i, j ).EQ.zero )
458  $ go to 280
459  kount = kount + 1
460  sum = sum + work( j )
461  280 continue
462  IF( b( i, j ).EQ.zero )
463  $ go to 290
464  kount = kount + 1
465  sum = sum + work( j )
466  290 continue
467  work( i+2*n ) = REAL( kount )*work( i+n ) + sum
468  300 continue
469 *
470  DO 330 j = ilo, ihi
471  kount = 0
472  sum = zero
473  DO 320 i = ilo, ihi
474  IF( a( i, j ).EQ.zero )
475  $ go to 310
476  kount = kount + 1
477  sum = sum + work( i+n )
478  310 continue
479  IF( b( i, j ).EQ.zero )
480  $ go to 320
481  kount = kount + 1
482  sum = sum + work( i+n )
483  320 continue
484  work( j+3*n ) = REAL( kount )*work( j ) + sum
485  330 continue
486 *
487  sum = sdot( nr, work( ilo+n ), 1, work( ilo+2*n ), 1 ) +
488  $ sdot( nr, work( ilo ), 1, work( ilo+3*n ), 1 )
489  alpha = gamma / sum
490 *
491 * Determine correction to current iteration
492 *
493  cmax = zero
494  DO 340 i = ilo, ihi
495  cor = alpha*work( i+n )
496  IF( abs( cor ).GT.cmax )
497  $ cmax = abs( cor )
498  lscale( i ) = lscale( i ) + cor
499  cor = alpha*work( i )
500  IF( abs( cor ).GT.cmax )
501  $ cmax = abs( cor )
502  rscale( i ) = rscale( i ) + cor
503  340 continue
504  IF( cmax.LT.half )
505  $ go to 350
506 *
507  CALL saxpy( nr, -alpha, work( ilo+2*n ), 1, work( ilo+4*n ), 1 )
508  CALL saxpy( nr, -alpha, work( ilo+3*n ), 1, work( ilo+5*n ), 1 )
509 *
510  pgamma = gamma
511  it = it + 1
512  IF( it.LE.nrp2 )
513  $ go to 250
514 *
515 * End generalized conjugate gradient iteration
516 *
517  350 continue
518  sfmin = slamch( 'S' )
519  sfmax = one / sfmin
520  lsfmin = int( log10( sfmin ) / basl+one )
521  lsfmax = int( log10( sfmax ) / basl )
522  DO 360 i = ilo, ihi
523  irab = isamax( n-ilo+1, a( i, ilo ), lda )
524  rab = abs( a( i, irab+ilo-1 ) )
525  irab = isamax( n-ilo+1, b( i, ilo ), ldb )
526  rab = max( rab, abs( b( i, irab+ilo-1 ) ) )
527  lrab = int( log10( rab+sfmin ) / basl+one )
528  ir = lscale( i ) + sign( half, lscale( i ) )
529  ir = min( max( ir, lsfmin ), lsfmax, lsfmax-lrab )
530  lscale( i ) = sclfac**ir
531  icab = isamax( ihi, a( 1, i ), 1 )
532  cab = abs( a( icab, i ) )
533  icab = isamax( ihi, b( 1, i ), 1 )
534  cab = max( cab, abs( b( icab, i ) ) )
535  lcab = int( log10( cab+sfmin ) / basl+one )
536  jc = rscale( i ) + sign( half, rscale( i ) )
537  jc = min( max( jc, lsfmin ), lsfmax, lsfmax-lcab )
538  rscale( i ) = sclfac**jc
539  360 continue
540 *
541 * Row scaling of matrices A and B
542 *
543  DO 370 i = ilo, ihi
544  CALL sscal( n-ilo+1, lscale( i ), a( i, ilo ), lda )
545  CALL sscal( n-ilo+1, lscale( i ), b( i, ilo ), ldb )
546  370 continue
547 *
548 * Column scaling of matrices A and B
549 *
550  DO 380 j = ilo, ihi
551  CALL sscal( ihi, rscale( j ), a( 1, j ), 1 )
552  CALL sscal( ihi, rscale( j ), b( 1, j ), 1 )
553  380 continue
554 *
555  return
556 *
557 * End of SGGBAL
558 *
559  END