You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgebal.f 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. *> \ingroup realGEcomputational
  125. *
  126. *> \par Further Details:
  127. * =====================
  128. *>
  129. *> \verbatim
  130. *>
  131. *> The permutations consist of row and column interchanges which put
  132. *> the matrix in the form
  133. *>
  134. *> ( T1 X Y )
  135. *> P A P = ( 0 B Z )
  136. *> ( 0 0 T2 )
  137. *>
  138. *> where T1 and T2 are upper triangular matrices whose eigenvalues lie
  139. *> along the diagonal. The column indices ILO and IHI mark the starting
  140. *> and ending columns of the submatrix B. Balancing consists of applying
  141. *> a diagonal similarity transformation inv(D) * B * D to make the
  142. *> 1-norms of each row of B and its corresponding column nearly equal.
  143. *> The output matrix is
  144. *>
  145. *> ( T1 X*D Y )
  146. *> ( 0 inv(D)*B*D inv(D)*Z ).
  147. *> ( 0 0 T2 )
  148. *>
  149. *> Information about the permutations P and the diagonal matrix D is
  150. *> returned in the vector SCALE.
  151. *>
  152. *> This subroutine is based on the EISPACK routine BALANC.
  153. *>
  154. *> Modified by Tzu-Yi Chen, Computer Science Division, University of
  155. *> California at Berkeley, USA
  156. *>
  157. *> Refactored by Evert Provoost, Department of Computer Science,
  158. *> KU Leuven, Belgium
  159. *> \endverbatim
  160. *>
  161. * =====================================================================
  162. SUBROUTINE SGEBAL( JOB, N, A, LDA, ILO, IHI, SCALE, INFO )
  163. *
  164. * -- LAPACK computational routine --
  165. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  166. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  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, CANSWAP
  188. INTEGER I, ICA, IRA, J, K, L
  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, SNRM2
  196. EXTERNAL SISNAN, LSAME, ISAMAX, SLAMCH, SNRM2
  197. * ..
  198. * .. External Subroutines ..
  199. EXTERNAL SSCAL, SSWAP, XERBLA
  200. * ..
  201. * .. Intrinsic Functions ..
  202. INTRINSIC ABS, MAX, MIN
  203. * ..
  204. * Test the input parameters
  205. *
  206. INFO = 0
  207. IF( .NOT.LSAME( JOB, 'N' ) .AND. .NOT.LSAME( JOB, 'P' ) .AND.
  208. $ .NOT.LSAME( JOB, 'S' ) .AND. .NOT.LSAME( JOB, 'B' ) ) THEN
  209. INFO = -1
  210. ELSE IF( N.LT.0 ) THEN
  211. INFO = -2
  212. ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
  213. INFO = -4
  214. END IF
  215. IF( INFO.NE.0 ) THEN
  216. CALL XERBLA( 'SGEBAL', -INFO )
  217. RETURN
  218. END IF
  219. *
  220. * Quick returns.
  221. *
  222. IF( N.EQ.0 ) THEN
  223. ILO = 1
  224. IHI = 0
  225. RETURN
  226. END IF
  227. *
  228. IF( LSAME( JOB, 'N' ) ) THEN
  229. DO I = 1, N
  230. SCALE( I ) = ONE
  231. END DO
  232. ILO = 1
  233. IHI = N
  234. RETURN
  235. END IF
  236. *
  237. * Permutation to isolate eigenvalues if possible.
  238. *
  239. K = 1
  240. L = N
  241. *
  242. IF( .NOT.LSAME( JOB, 'S' ) ) THEN
  243. *
  244. * Row and column exchange.
  245. *
  246. NOCONV = .TRUE.
  247. DO WHILE( NOCONV )
  248. *
  249. * Search for rows isolating an eigenvalue and push them down.
  250. *
  251. NOCONV = .FALSE.
  252. DO I = L, 1, -1
  253. CANSWAP = .TRUE.
  254. DO J = 1, L
  255. IF( I.NE.J .AND. A( I, J ).NE.ZERO ) THEN
  256. CANSWAP = .FALSE.
  257. EXIT
  258. END IF
  259. END DO
  260. *
  261. IF( CANSWAP ) THEN
  262. SCALE( L ) = I
  263. IF( I.NE.L ) THEN
  264. CALL SSWAP( L, A( 1, I ), 1, A( 1, L ), 1 )
  265. CALL SSWAP( N-K+1, A( I, K ), LDA, A( L, K ), LDA )
  266. END IF
  267. NOCONV = .TRUE.
  268. *
  269. IF( L.EQ.1 ) THEN
  270. ILO = 1
  271. IHI = 1
  272. RETURN
  273. END IF
  274. *
  275. L = L - 1
  276. END IF
  277. END DO
  278. *
  279. END DO
  280. NOCONV = .TRUE.
  281. DO WHILE( NOCONV )
  282. *
  283. * Search for columns isolating an eigenvalue and push them left.
  284. *
  285. NOCONV = .FALSE.
  286. DO J = K, L
  287. CANSWAP = .TRUE.
  288. DO I = K, L
  289. IF( I.NE.J .AND. A( I, J ).NE.ZERO ) THEN
  290. CANSWAP = .FALSE.
  291. EXIT
  292. END IF
  293. END DO
  294. *
  295. IF( CANSWAP ) THEN
  296. SCALE( K ) = J
  297. IF( J.NE.K ) THEN
  298. CALL SSWAP( L, A( 1, J ), 1, A( 1, K ), 1 )
  299. CALL SSWAP( N-K+1, A( J, K ), LDA, A( K, K ), LDA )
  300. END IF
  301. NOCONV = .TRUE.
  302. *
  303. K = K + 1
  304. END IF
  305. END DO
  306. *
  307. END DO
  308. *
  309. END IF
  310. *
  311. * Initialize SCALE for non-permuted submatrix.
  312. *
  313. DO I = K, L
  314. SCALE( I ) = ONE
  315. END DO
  316. *
  317. * If we only had to permute, we are done.
  318. *
  319. IF( LSAME( JOB, 'P' ) ) THEN
  320. ILO = K
  321. IHI = L
  322. RETURN
  323. END IF
  324. *
  325. * Balance the submatrix in rows K to L.
  326. *
  327. * Iterative loop for norm reduction.
  328. *
  329. SFMIN1 = SLAMCH( 'S' ) / SLAMCH( 'P' )
  330. SFMAX1 = ONE / SFMIN1
  331. SFMIN2 = SFMIN1*SCLFAC
  332. SFMAX2 = ONE / SFMIN2
  333. *
  334. NOCONV = .TRUE.
  335. DO WHILE( NOCONV )
  336. NOCONV = .FALSE.
  337. *
  338. DO I = K, L
  339. *
  340. C = SNRM2( L-K+1, A( K, I ), 1 )
  341. R = SNRM2( L-K+1, A( I, K ), LDA )
  342. ICA = ISAMAX( L, A( 1, I ), 1 )
  343. CA = ABS( A( ICA, I ) )
  344. IRA = ISAMAX( N-K+1, A( I, K ), LDA )
  345. RA = ABS( A( I, IRA+K-1 ) )
  346. *
  347. * Guard against zero C or R due to underflow.
  348. *
  349. IF( C.EQ.ZERO .OR. R.EQ.ZERO ) CYCLE
  350. *
  351. * Exit if NaN to avoid infinite loop
  352. *
  353. IF( SISNAN( C+CA+R+RA ) ) THEN
  354. INFO = -3
  355. CALL XERBLA( 'SGEBAL', -INFO )
  356. RETURN
  357. END IF
  358. *
  359. G = R / SCLFAC
  360. F = ONE
  361. S = C + R
  362. *
  363. DO WHILE( C.LT.G .AND. MAX( F, C, CA ).LT.SFMAX2 .AND.
  364. $ MIN( R, G, RA ).GT.SFMIN2 )
  365. F = F*SCLFAC
  366. C = C*SCLFAC
  367. CA = CA*SCLFAC
  368. R = R / SCLFAC
  369. G = G / SCLFAC
  370. RA = RA / SCLFAC
  371. END DO
  372. *
  373. G = C / SCLFAC
  374. *
  375. DO WHILE( G.GE.R .AND. MAX( R, RA ).LT.SFMAX2 .AND.
  376. $ MIN( F, C, G, CA ).GT.SFMIN2 )
  377. F = F / SCLFAC
  378. C = C / SCLFAC
  379. G = G / SCLFAC
  380. CA = CA / SCLFAC
  381. R = R*SCLFAC
  382. RA = RA*SCLFAC
  383. END DO
  384. *
  385. * Now balance.
  386. *
  387. IF( ( C+R ).GE.FACTOR*S ) CYCLE
  388. IF( F.LT.ONE .AND. SCALE( I ).LT.ONE ) THEN
  389. IF( F*SCALE( I ).LE.SFMIN1 ) CYCLE
  390. END IF
  391. IF( F.GT.ONE .AND. SCALE( I ).GT.ONE ) THEN
  392. IF( SCALE( I ).GE.SFMAX1 / F ) CYCLE
  393. END IF
  394. G = ONE / F
  395. SCALE( I ) = SCALE( I )*F
  396. NOCONV = .TRUE.
  397. *
  398. CALL SSCAL( N-K+1, G, A( I, K ), LDA )
  399. CALL SSCAL( L, F, A( 1, I ), 1 )
  400. *
  401. END DO
  402. *
  403. END DO
  404. *
  405. ILO = K
  406. IHI = L
  407. *
  408. RETURN
  409. *
  410. * End of SGEBAL
  411. *
  412. END