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.

spstrf.f 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. *> \brief \b SPSTRF computes the Cholesky factorization with complete pivoting of a real symmetric positive semidefinite matrix.
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download SPSTRF + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/spstrf.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/spstrf.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/spstrf.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE SPSTRF( UPLO, N, A, LDA, PIV, RANK, TOL, WORK, INFO )
  22. *
  23. * .. Scalar Arguments ..
  24. * REAL TOL
  25. * INTEGER INFO, LDA, N, RANK
  26. * CHARACTER UPLO
  27. * ..
  28. * .. Array Arguments ..
  29. * REAL A( LDA, * ), WORK( 2*N )
  30. * INTEGER PIV( N )
  31. * ..
  32. *
  33. *
  34. *> \par Purpose:
  35. * =============
  36. *>
  37. *> \verbatim
  38. *>
  39. *> SPSTRF computes the Cholesky factorization with complete
  40. *> pivoting of a real symmetric positive semidefinite matrix A.
  41. *>
  42. *> The factorization has the form
  43. *> P**T * A * P = U**T * U , if UPLO = 'U',
  44. *> P**T * A * P = L * L**T, if UPLO = 'L',
  45. *> where U is an upper triangular matrix and L is lower triangular, and
  46. *> P is stored as vector PIV.
  47. *>
  48. *> This algorithm does not attempt to check that A is positive
  49. *> semidefinite. This version of the algorithm calls level 3 BLAS.
  50. *> \endverbatim
  51. *
  52. * Arguments:
  53. * ==========
  54. *
  55. *> \param[in] UPLO
  56. *> \verbatim
  57. *> UPLO is CHARACTER*1
  58. *> Specifies whether the upper or lower triangular part of the
  59. *> symmetric matrix A is stored.
  60. *> = 'U': Upper triangular
  61. *> = 'L': Lower triangular
  62. *> \endverbatim
  63. *>
  64. *> \param[in] N
  65. *> \verbatim
  66. *> N is INTEGER
  67. *> The order of the matrix A. N >= 0.
  68. *> \endverbatim
  69. *>
  70. *> \param[in,out] A
  71. *> \verbatim
  72. *> A is REAL array, dimension (LDA,N)
  73. *> On entry, the symmetric matrix A. If UPLO = 'U', the leading
  74. *> n by n upper triangular part of A contains the upper
  75. *> triangular part of the matrix A, and the strictly lower
  76. *> triangular part of A is not referenced. If UPLO = 'L', the
  77. *> leading n by n lower triangular part of A contains the lower
  78. *> triangular part of the matrix A, and the strictly upper
  79. *> triangular part of A is not referenced.
  80. *>
  81. *> On exit, if INFO = 0, the factor U or L from the Cholesky
  82. *> factorization as above.
  83. *> \endverbatim
  84. *>
  85. *> \param[in] LDA
  86. *> \verbatim
  87. *> LDA is INTEGER
  88. *> The leading dimension of the array A. LDA >= max(1,N).
  89. *> \endverbatim
  90. *>
  91. *> \param[out] PIV
  92. *> \verbatim
  93. *> PIV is INTEGER array, dimension (N)
  94. *> PIV is such that the nonzero entries are P( PIV(K), K ) = 1.
  95. *> \endverbatim
  96. *>
  97. *> \param[out] RANK
  98. *> \verbatim
  99. *> RANK is INTEGER
  100. *> The rank of A given by the number of steps the algorithm
  101. *> completed.
  102. *> \endverbatim
  103. *>
  104. *> \param[in] TOL
  105. *> \verbatim
  106. *> TOL is REAL
  107. *> User defined tolerance. If TOL < 0, then N*U*MAX( A(K,K) )
  108. *> will be used. The algorithm terminates at the (K-1)st step
  109. *> if the pivot <= TOL.
  110. *> \endverbatim
  111. *>
  112. *> \param[out] WORK
  113. *> \verbatim
  114. *> WORK is REAL array, dimension (2*N)
  115. *> Work space.
  116. *> \endverbatim
  117. *>
  118. *> \param[out] INFO
  119. *> \verbatim
  120. *> INFO is INTEGER
  121. *> < 0: If INFO = -K, the K-th argument had an illegal value,
  122. *> = 0: algorithm completed successfully, and
  123. *> > 0: the matrix A is either rank deficient with computed rank
  124. *> as returned in RANK, or is not positive semidefinite. See
  125. *> Section 7 of LAPACK Working Note #161 for further
  126. *> information.
  127. *> \endverbatim
  128. *
  129. * Authors:
  130. * ========
  131. *
  132. *> \author Univ. of Tennessee
  133. *> \author Univ. of California Berkeley
  134. *> \author Univ. of Colorado Denver
  135. *> \author NAG Ltd.
  136. *
  137. *> \ingroup realOTHERcomputational
  138. *
  139. * =====================================================================
  140. SUBROUTINE SPSTRF( UPLO, N, A, LDA, PIV, RANK, TOL, WORK, INFO )
  141. *
  142. * -- LAPACK computational routine --
  143. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  144. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  145. *
  146. * .. Scalar Arguments ..
  147. REAL TOL
  148. INTEGER INFO, LDA, N, RANK
  149. CHARACTER UPLO
  150. * ..
  151. * .. Array Arguments ..
  152. REAL A( LDA, * ), WORK( 2*N )
  153. INTEGER PIV( N )
  154. * ..
  155. *
  156. * =====================================================================
  157. *
  158. * .. Parameters ..
  159. REAL ONE, ZERO
  160. PARAMETER ( ONE = 1.0E+0, ZERO = 0.0E+0 )
  161. * ..
  162. * .. Local Scalars ..
  163. REAL AJJ, SSTOP, STEMP
  164. INTEGER I, ITEMP, J, JB, K, NB, PVT
  165. LOGICAL UPPER
  166. * ..
  167. * .. External Functions ..
  168. REAL SLAMCH
  169. INTEGER ILAENV
  170. LOGICAL LSAME, SISNAN
  171. EXTERNAL SLAMCH, ILAENV, LSAME, SISNAN
  172. * ..
  173. * .. External Subroutines ..
  174. EXTERNAL SGEMV, SPSTF2, SSCAL, SSWAP, SSYRK, XERBLA
  175. * ..
  176. * .. Intrinsic Functions ..
  177. INTRINSIC MAX, MIN, SQRT, MAXLOC
  178. * ..
  179. * .. Executable Statements ..
  180. *
  181. * Test the input parameters.
  182. *
  183. INFO = 0
  184. UPPER = LSAME( UPLO, 'U' )
  185. IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
  186. INFO = -1
  187. ELSE IF( N.LT.0 ) THEN
  188. INFO = -2
  189. ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
  190. INFO = -4
  191. END IF
  192. IF( INFO.NE.0 ) THEN
  193. CALL XERBLA( 'SPSTRF', -INFO )
  194. RETURN
  195. END IF
  196. *
  197. * Quick return if possible
  198. *
  199. IF( N.EQ.0 )
  200. $ RETURN
  201. *
  202. * Get block size
  203. *
  204. NB = ILAENV( 1, 'SPOTRF', UPLO, N, -1, -1, -1 )
  205. IF( NB.LE.1 .OR. NB.GE.N ) THEN
  206. *
  207. * Use unblocked code
  208. *
  209. CALL SPSTF2( UPLO, N, A( 1, 1 ), LDA, PIV, RANK, TOL, WORK,
  210. $ INFO )
  211. GO TO 200
  212. *
  213. ELSE
  214. *
  215. * Initialize PIV
  216. *
  217. DO 100 I = 1, N
  218. PIV( I ) = I
  219. 100 CONTINUE
  220. *
  221. * Compute stopping value
  222. *
  223. PVT = 1
  224. AJJ = A( PVT, PVT )
  225. DO I = 2, N
  226. IF( A( I, I ).GT.AJJ ) THEN
  227. PVT = I
  228. AJJ = A( PVT, PVT )
  229. END IF
  230. END DO
  231. IF( AJJ.LE.ZERO.OR.SISNAN( AJJ ) ) THEN
  232. RANK = 0
  233. INFO = 1
  234. GO TO 200
  235. END IF
  236. *
  237. * Compute stopping value if not supplied
  238. *
  239. IF( TOL.LT.ZERO ) THEN
  240. SSTOP = N * SLAMCH( 'Epsilon' ) * AJJ
  241. ELSE
  242. SSTOP = TOL
  243. END IF
  244. *
  245. *
  246. IF( UPPER ) THEN
  247. *
  248. * Compute the Cholesky factorization P**T * A * P = U**T * U
  249. *
  250. DO 140 K = 1, N, NB
  251. *
  252. * Account for last block not being NB wide
  253. *
  254. JB = MIN( NB, N-K+1 )
  255. *
  256. * Set relevant part of first half of WORK to zero,
  257. * holds dot products
  258. *
  259. DO 110 I = K, N
  260. WORK( I ) = 0
  261. 110 CONTINUE
  262. *
  263. DO 130 J = K, K + JB - 1
  264. *
  265. * Find pivot, test for exit, else swap rows and columns
  266. * Update dot products, compute possible pivots which are
  267. * stored in the second half of WORK
  268. *
  269. DO 120 I = J, N
  270. *
  271. IF( J.GT.K ) THEN
  272. WORK( I ) = WORK( I ) + A( J-1, I )**2
  273. END IF
  274. WORK( N+I ) = A( I, I ) - WORK( I )
  275. *
  276. 120 CONTINUE
  277. *
  278. IF( J.GT.1 ) THEN
  279. ITEMP = MAXLOC( WORK( (N+J):(2*N) ), 1 )
  280. PVT = ITEMP + J - 1
  281. AJJ = WORK( N+PVT )
  282. IF( AJJ.LE.SSTOP.OR.SISNAN( AJJ ) ) THEN
  283. A( J, J ) = AJJ
  284. GO TO 190
  285. END IF
  286. END IF
  287. *
  288. IF( J.NE.PVT ) THEN
  289. *
  290. * Pivot OK, so can now swap pivot rows and columns
  291. *
  292. A( PVT, PVT ) = A( J, J )
  293. CALL SSWAP( J-1, A( 1, J ), 1, A( 1, PVT ), 1 )
  294. IF( PVT.LT.N )
  295. $ CALL SSWAP( N-PVT, A( J, PVT+1 ), LDA,
  296. $ A( PVT, PVT+1 ), LDA )
  297. CALL SSWAP( PVT-J-1, A( J, J+1 ), LDA,
  298. $ A( J+1, PVT ), 1 )
  299. *
  300. * Swap dot products and PIV
  301. *
  302. STEMP = WORK( J )
  303. WORK( J ) = WORK( PVT )
  304. WORK( PVT ) = STEMP
  305. ITEMP = PIV( PVT )
  306. PIV( PVT ) = PIV( J )
  307. PIV( J ) = ITEMP
  308. END IF
  309. *
  310. AJJ = SQRT( AJJ )
  311. A( J, J ) = AJJ
  312. *
  313. * Compute elements J+1:N of row J.
  314. *
  315. IF( J.LT.N ) THEN
  316. CALL SGEMV( 'Trans', J-K, N-J, -ONE, A( K, J+1 ),
  317. $ LDA, A( K, J ), 1, ONE, A( J, J+1 ),
  318. $ LDA )
  319. CALL SSCAL( N-J, ONE / AJJ, A( J, J+1 ), LDA )
  320. END IF
  321. *
  322. 130 CONTINUE
  323. *
  324. * Update trailing matrix, J already incremented
  325. *
  326. IF( K+JB.LE.N ) THEN
  327. CALL SSYRK( 'Upper', 'Trans', N-J+1, JB, -ONE,
  328. $ A( K, J ), LDA, ONE, A( J, J ), LDA )
  329. END IF
  330. *
  331. 140 CONTINUE
  332. *
  333. ELSE
  334. *
  335. * Compute the Cholesky factorization P**T * A * P = L * L**T
  336. *
  337. DO 180 K = 1, N, NB
  338. *
  339. * Account for last block not being NB wide
  340. *
  341. JB = MIN( NB, N-K+1 )
  342. *
  343. * Set relevant part of first half of WORK to zero,
  344. * holds dot products
  345. *
  346. DO 150 I = K, N
  347. WORK( I ) = 0
  348. 150 CONTINUE
  349. *
  350. DO 170 J = K, K + JB - 1
  351. *
  352. * Find pivot, test for exit, else swap rows and columns
  353. * Update dot products, compute possible pivots which are
  354. * stored in the second half of WORK
  355. *
  356. DO 160 I = J, N
  357. *
  358. IF( J.GT.K ) THEN
  359. WORK( I ) = WORK( I ) + A( I, J-1 )**2
  360. END IF
  361. WORK( N+I ) = A( I, I ) - WORK( I )
  362. *
  363. 160 CONTINUE
  364. *
  365. IF( J.GT.1 ) THEN
  366. ITEMP = MAXLOC( WORK( (N+J):(2*N) ), 1 )
  367. PVT = ITEMP + J - 1
  368. AJJ = WORK( N+PVT )
  369. IF( AJJ.LE.SSTOP.OR.SISNAN( AJJ ) ) THEN
  370. A( J, J ) = AJJ
  371. GO TO 190
  372. END IF
  373. END IF
  374. *
  375. IF( J.NE.PVT ) THEN
  376. *
  377. * Pivot OK, so can now swap pivot rows and columns
  378. *
  379. A( PVT, PVT ) = A( J, J )
  380. CALL SSWAP( J-1, A( J, 1 ), LDA, A( PVT, 1 ), LDA )
  381. IF( PVT.LT.N )
  382. $ CALL SSWAP( N-PVT, A( PVT+1, J ), 1,
  383. $ A( PVT+1, PVT ), 1 )
  384. CALL SSWAP( PVT-J-1, A( J+1, J ), 1, A( PVT, J+1 ),
  385. $ LDA )
  386. *
  387. * Swap dot products and PIV
  388. *
  389. STEMP = WORK( J )
  390. WORK( J ) = WORK( PVT )
  391. WORK( PVT ) = STEMP
  392. ITEMP = PIV( PVT )
  393. PIV( PVT ) = PIV( J )
  394. PIV( J ) = ITEMP
  395. END IF
  396. *
  397. AJJ = SQRT( AJJ )
  398. A( J, J ) = AJJ
  399. *
  400. * Compute elements J+1:N of column J.
  401. *
  402. IF( J.LT.N ) THEN
  403. CALL SGEMV( 'No Trans', N-J, J-K, -ONE,
  404. $ A( J+1, K ), LDA, A( J, K ), LDA, ONE,
  405. $ A( J+1, J ), 1 )
  406. CALL SSCAL( N-J, ONE / AJJ, A( J+1, J ), 1 )
  407. END IF
  408. *
  409. 170 CONTINUE
  410. *
  411. * Update trailing matrix, J already incremented
  412. *
  413. IF( K+JB.LE.N ) THEN
  414. CALL SSYRK( 'Lower', 'No Trans', N-J+1, JB, -ONE,
  415. $ A( J, K ), LDA, ONE, A( J, J ), LDA )
  416. END IF
  417. *
  418. 180 CONTINUE
  419. *
  420. END IF
  421. END IF
  422. *
  423. * Ran to completion, A has full rank
  424. *
  425. RANK = N
  426. *
  427. GO TO 200
  428. 190 CONTINUE
  429. *
  430. * Rank is the number of steps completed. Set INFO = 1 to signal
  431. * that the factorization cannot be used to solve a system.
  432. *
  433. RANK = J - 1
  434. INFO = 1
  435. *
  436. 200 CONTINUE
  437. RETURN
  438. *
  439. * End of SPSTRF
  440. *
  441. END