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.

sgesvdx.f 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. *> \brief <b> SGESVDX computes the singular value decomposition (SVD) for GE matrices</b>
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download SGESVDX + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvdx.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvdx.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvdx.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU,
  22. * $ IL, IU, NS, S, U, LDU, VT, LDVT, WORK,
  23. * $ LWORK, IWORK, INFO )
  24. *
  25. *
  26. * .. Scalar Arguments ..
  27. * CHARACTER JOBU, JOBVT, RANGE
  28. * INTEGER IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS
  29. * REAL VL, VU
  30. * ..
  31. * .. Array Arguments ..
  32. * INTEGER IWORK( * )
  33. * REAL A( LDA, * ), S( * ), U( LDU, * ),
  34. * $ VT( LDVT, * ), WORK( * )
  35. * ..
  36. *
  37. *
  38. *> \par Purpose:
  39. * =============
  40. *>
  41. *> \verbatim
  42. *>
  43. *> SGESVDX computes the singular value decomposition (SVD) of a real
  44. *> M-by-N matrix A, optionally computing the left and/or right singular
  45. *> vectors. The SVD is written
  46. *>
  47. *> A = U * SIGMA * transpose(V)
  48. *>
  49. *> where SIGMA is an M-by-N matrix which is zero except for its
  50. *> min(m,n) diagonal elements, U is an M-by-M orthogonal matrix, and
  51. *> V is an N-by-N orthogonal matrix. The diagonal elements of SIGMA
  52. *> are the singular values of A; they are real and non-negative, and
  53. *> are returned in descending order. The first min(m,n) columns of
  54. *> U and V are the left and right singular vectors of A.
  55. *>
  56. *> SGESVDX uses an eigenvalue problem for obtaining the SVD, which
  57. *> allows for the computation of a subset of singular values and
  58. *> vectors. See SBDSVDX for details.
  59. *>
  60. *> Note that the routine returns V**T, not V.
  61. *> \endverbatim
  62. *
  63. * Arguments:
  64. * ==========
  65. *
  66. *> \param[in] JOBU
  67. *> \verbatim
  68. *> JOBU is CHARACTER*1
  69. *> Specifies options for computing all or part of the matrix U:
  70. *> = 'V': the first min(m,n) columns of U (the left singular
  71. *> vectors) or as specified by RANGE are returned in
  72. *> the array U;
  73. *> = 'N': no columns of U (no left singular vectors) are
  74. *> computed.
  75. *> \endverbatim
  76. *>
  77. *> \param[in] JOBVT
  78. *> \verbatim
  79. *> JOBVT is CHARACTER*1
  80. *> Specifies options for computing all or part of the matrix
  81. *> V**T:
  82. *> = 'V': the first min(m,n) rows of V**T (the right singular
  83. *> vectors) or as specified by RANGE are returned in
  84. *> the array VT;
  85. *> = 'N': no rows of V**T (no right singular vectors) are
  86. *> computed.
  87. *> \endverbatim
  88. *>
  89. *> \param[in] RANGE
  90. *> \verbatim
  91. *> RANGE is CHARACTER*1
  92. *> = 'A': all singular values will be found.
  93. *> = 'V': all singular values in the half-open interval (VL,VU]
  94. *> will be found.
  95. *> = 'I': the IL-th through IU-th singular values will be found.
  96. *> \endverbatim
  97. *>
  98. *> \param[in] M
  99. *> \verbatim
  100. *> M is INTEGER
  101. *> The number of rows of the input matrix A. M >= 0.
  102. *> \endverbatim
  103. *>
  104. *> \param[in] N
  105. *> \verbatim
  106. *> N is INTEGER
  107. *> The number of columns of the input matrix A. N >= 0.
  108. *> \endverbatim
  109. *>
  110. *> \param[in,out] A
  111. *> \verbatim
  112. *> A is REAL array, dimension (LDA,N)
  113. *> On entry, the M-by-N matrix A.
  114. *> On exit, the contents of A are destroyed.
  115. *> \endverbatim
  116. *>
  117. *> \param[in] LDA
  118. *> \verbatim
  119. *> LDA is INTEGER
  120. *> The leading dimension of the array A. LDA >= max(1,M).
  121. *> \endverbatim
  122. *>
  123. *> \param[in] VL
  124. *> \verbatim
  125. *> VL is REAL
  126. *> If RANGE='V', the lower bound of the interval to
  127. *> be searched for singular values. VU > VL.
  128. *> Not referenced if RANGE = 'A' or 'I'.
  129. *> \endverbatim
  130. *>
  131. *> \param[in] VU
  132. *> \verbatim
  133. *> VU is REAL
  134. *> If RANGE='V', the upper bound of the interval to
  135. *> be searched for singular values. VU > VL.
  136. *> Not referenced if RANGE = 'A' or 'I'.
  137. *> \endverbatim
  138. *>
  139. *> \param[in] IL
  140. *> \verbatim
  141. *> IL is INTEGER
  142. *> If RANGE='I', the index of the
  143. *> smallest singular value to be returned.
  144. *> 1 <= IL <= IU <= min(M,N), if min(M,N) > 0.
  145. *> Not referenced if RANGE = 'A' or 'V'.
  146. *> \endverbatim
  147. *>
  148. *> \param[in] IU
  149. *> \verbatim
  150. *> IU is INTEGER
  151. *> If RANGE='I', the index of the
  152. *> largest singular value to be returned.
  153. *> 1 <= IL <= IU <= min(M,N), if min(M,N) > 0.
  154. *> Not referenced if RANGE = 'A' or 'V'.
  155. *> \endverbatim
  156. *>
  157. *> \param[out] NS
  158. *> \verbatim
  159. *> NS is INTEGER
  160. *> The total number of singular values found,
  161. *> 0 <= NS <= min(M,N).
  162. *> If RANGE = 'A', NS = min(M,N); if RANGE = 'I', NS = IU-IL+1.
  163. *> \endverbatim
  164. *>
  165. *> \param[out] S
  166. *> \verbatim
  167. *> S is REAL array, dimension (min(M,N))
  168. *> The singular values of A, sorted so that S(i) >= S(i+1).
  169. *> \endverbatim
  170. *>
  171. *> \param[out] U
  172. *> \verbatim
  173. *> U is REAL array, dimension (LDU,UCOL)
  174. *> If JOBU = 'V', U contains columns of U (the left singular
  175. *> vectors, stored columnwise) as specified by RANGE; if
  176. *> JOBU = 'N', U is not referenced.
  177. *> Note: The user must ensure that UCOL >= NS; if RANGE = 'V',
  178. *> the exact value of NS is not known in advance and an upper
  179. *> bound must be used.
  180. *> \endverbatim
  181. *>
  182. *> \param[in] LDU
  183. *> \verbatim
  184. *> LDU is INTEGER
  185. *> The leading dimension of the array U. LDU >= 1; if
  186. *> JOBU = 'V', LDU >= M.
  187. *> \endverbatim
  188. *>
  189. *> \param[out] VT
  190. *> \verbatim
  191. *> VT is REAL array, dimension (LDVT,N)
  192. *> If JOBVT = 'V', VT contains the rows of V**T (the right singular
  193. *> vectors, stored rowwise) as specified by RANGE; if JOBVT = 'N',
  194. *> VT is not referenced.
  195. *> Note: The user must ensure that LDVT >= NS; if RANGE = 'V',
  196. *> the exact value of NS is not known in advance and an upper
  197. *> bound must be used.
  198. *> \endverbatim
  199. *>
  200. *> \param[in] LDVT
  201. *> \verbatim
  202. *> LDVT is INTEGER
  203. *> The leading dimension of the array VT. LDVT >= 1; if
  204. *> JOBVT = 'V', LDVT >= NS (see above).
  205. *> \endverbatim
  206. *>
  207. *> \param[out] WORK
  208. *> \verbatim
  209. *> WORK is REAL array, dimension (MAX(1,LWORK))
  210. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK;
  211. *> \endverbatim
  212. *>
  213. *> \param[in] LWORK
  214. *> \verbatim
  215. *> LWORK is INTEGER
  216. *> The dimension of the array WORK.
  217. *> LWORK >= MAX(1,MIN(M,N)*(MIN(M,N)+4)) for the paths (see
  218. *> comments inside the code):
  219. *> - PATH 1 (M much larger than N)
  220. *> - PATH 1t (N much larger than M)
  221. *> LWORK >= MAX(1,MIN(M,N)*2+MAX(M,N)) for the other paths.
  222. *> For good performance, LWORK should generally be larger.
  223. *>
  224. *> If LWORK = -1, then a workspace query is assumed; the routine
  225. *> only calculates the optimal size of the WORK array, returns
  226. *> this value as the first entry of the WORK array, and no error
  227. *> message related to LWORK is issued by XERBLA.
  228. *> \endverbatim
  229. *>
  230. *> \param[out] IWORK
  231. *> \verbatim
  232. *> IWORK is INTEGER array, dimension (12*MIN(M,N))
  233. *> If INFO = 0, the first NS elements of IWORK are zero. If INFO > 0,
  234. *> then IWORK contains the indices of the eigenvectors that failed
  235. *> to converge in SBDSVDX/SSTEVX.
  236. *> \endverbatim
  237. *>
  238. *> \param[out] INFO
  239. *> \verbatim
  240. *> INFO is INTEGER
  241. *> = 0: successful exit
  242. *> < 0: if INFO = -i, the i-th argument had an illegal value
  243. *> > 0: if INFO = i, then i eigenvectors failed to converge
  244. *> in SBDSVDX/SSTEVX.
  245. *> if INFO = N*2 + 1, an internal error occurred in
  246. *> SBDSVDX
  247. *> \endverbatim
  248. *
  249. * Authors:
  250. * ========
  251. *
  252. *> \author Univ. of Tennessee
  253. *> \author Univ. of California Berkeley
  254. *> \author Univ. of Colorado Denver
  255. *> \author NAG Ltd.
  256. *
  257. *> \ingroup gesvdx
  258. *
  259. * =====================================================================
  260. SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU,
  261. $ IL, IU, NS, S, U, LDU, VT, LDVT, WORK,
  262. $ LWORK, IWORK, INFO )
  263. *
  264. * -- LAPACK driver routine --
  265. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  266. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  267. *
  268. * .. Scalar Arguments ..
  269. CHARACTER JOBU, JOBVT, RANGE
  270. INTEGER IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS
  271. REAL VL, VU
  272. * ..
  273. * .. Array Arguments ..
  274. INTEGER IWORK( * )
  275. REAL A( LDA, * ), S( * ), U( LDU, * ),
  276. $ VT( LDVT, * ), WORK( * )
  277. * ..
  278. *
  279. * =====================================================================
  280. *
  281. * .. Parameters ..
  282. REAL ZERO, ONE
  283. PARAMETER ( ZERO = 0.0E0, ONE = 1.0E0 )
  284. * ..
  285. * .. Local Scalars ..
  286. CHARACTER JOBZ, RNGTGK
  287. LOGICAL ALLS, INDS, LQUERY, VALS, WANTU, WANTVT
  288. INTEGER I, ID, IE, IERR, ILQF, ILTGK, IQRF, ISCL,
  289. $ ITAU, ITAUP, ITAUQ, ITEMP, ITGKZ, IUTGK,
  290. $ J, MAXWRK, MINMN, MINWRK, MNTHR
  291. REAL ABSTOL, ANRM, BIGNUM, EPS, SMLNUM
  292. * ..
  293. * .. Local Arrays ..
  294. REAL DUM( 1 )
  295. * ..
  296. * .. External Subroutines ..
  297. EXTERNAL SBDSVDX, SGEBRD, SGELQF, SGEQRF, SLACPY,
  298. $ SLASCL, SLASET, SORMBR, SORMLQ, SORMQR,
  299. $ SCOPY, XERBLA
  300. * ..
  301. * .. External Functions ..
  302. LOGICAL LSAME
  303. INTEGER ILAENV
  304. REAL SLAMCH, SLANGE, SROUNDUP_LWORK
  305. EXTERNAL LSAME, ILAENV, SLAMCH, SLANGE, SROUNDUP_LWORK
  306. * ..
  307. * .. Intrinsic Functions ..
  308. INTRINSIC MAX, MIN, SQRT
  309. * ..
  310. * .. Executable Statements ..
  311. *
  312. * Test the input arguments.
  313. *
  314. NS = 0
  315. INFO = 0
  316. ABSTOL = 2*SLAMCH('S')
  317. LQUERY = ( LWORK.EQ.-1 )
  318. MINMN = MIN( M, N )
  319. WANTU = LSAME( JOBU, 'V' )
  320. WANTVT = LSAME( JOBVT, 'V' )
  321. IF( WANTU .OR. WANTVT ) THEN
  322. JOBZ = 'V'
  323. ELSE
  324. JOBZ = 'N'
  325. END IF
  326. ALLS = LSAME( RANGE, 'A' )
  327. VALS = LSAME( RANGE, 'V' )
  328. INDS = LSAME( RANGE, 'I' )
  329. *
  330. INFO = 0
  331. IF( .NOT.LSAME( JOBU, 'V' ) .AND.
  332. $ .NOT.LSAME( JOBU, 'N' ) ) THEN
  333. INFO = -1
  334. ELSE IF( .NOT.LSAME( JOBVT, 'V' ) .AND.
  335. $ .NOT.LSAME( JOBVT, 'N' ) ) THEN
  336. INFO = -2
  337. ELSE IF( .NOT.( ALLS .OR. VALS .OR. INDS ) ) THEN
  338. INFO = -3
  339. ELSE IF( M.LT.0 ) THEN
  340. INFO = -4
  341. ELSE IF( N.LT.0 ) THEN
  342. INFO = -5
  343. ELSE IF( M.GT.LDA ) THEN
  344. INFO = -7
  345. ELSE IF( MINMN.GT.0 ) THEN
  346. IF( VALS ) THEN
  347. IF( VL.LT.ZERO ) THEN
  348. INFO = -8
  349. ELSE IF( VU.LE.VL ) THEN
  350. INFO = -9
  351. END IF
  352. ELSE IF( INDS ) THEN
  353. IF( IL.LT.1 .OR. IL.GT.MAX( 1, MINMN ) ) THEN
  354. INFO = -10
  355. ELSE IF( IU.LT.MIN( MINMN, IL ) .OR. IU.GT.MINMN ) THEN
  356. INFO = -11
  357. END IF
  358. END IF
  359. IF( INFO.EQ.0 ) THEN
  360. IF( WANTU .AND. LDU.LT.M ) THEN
  361. INFO = -15
  362. ELSE IF( WANTVT ) THEN
  363. IF( INDS ) THEN
  364. IF( LDVT.LT.IU-IL+1 ) THEN
  365. INFO = -17
  366. END IF
  367. ELSE IF( LDVT.LT.MINMN ) THEN
  368. INFO = -17
  369. END IF
  370. END IF
  371. END IF
  372. END IF
  373. *
  374. * Compute workspace
  375. * (Note: Comments in the code beginning "Workspace:" describe the
  376. * minimal amount of workspace needed at that point in the code,
  377. * as well as the preferred amount for good performance.
  378. * NB refers to the optimal block size for the immediately
  379. * following subroutine, as returned by ILAENV.)
  380. *
  381. IF( INFO.EQ.0 ) THEN
  382. MINWRK = 1
  383. MAXWRK = 1
  384. IF( MINMN.GT.0 ) THEN
  385. IF( M.GE.N ) THEN
  386. MNTHR = ILAENV( 6, 'SGESVD', JOBU // JOBVT, M, N, 0, 0 )
  387. IF( M.GE.MNTHR ) THEN
  388. *
  389. * Path 1 (M much larger than N)
  390. *
  391. MAXWRK = N +
  392. $ N*ILAENV( 1, 'SGEQRF', ' ', M, N, -1, -1 )
  393. MAXWRK = MAX( MAXWRK, N*(N+5) + 2*N*
  394. $ ILAENV( 1, 'SGEBRD', ' ', N, N, -1, -1 ) )
  395. IF (WANTU) THEN
  396. MAXWRK = MAX(MAXWRK,N*(N*3+6)+N*
  397. $ ILAENV( 1, 'SORMQR', ' ', N, N, -1, -1 ) )
  398. END IF
  399. IF (WANTVT) THEN
  400. MAXWRK = MAX(MAXWRK,N*(N*3+6)+N*
  401. $ ILAENV( 1, 'SORMLQ', ' ', N, N, -1, -1 ) )
  402. END IF
  403. MINWRK = N*(N*3+20)
  404. ELSE
  405. *
  406. * Path 2 (M at least N, but not much larger)
  407. *
  408. MAXWRK = 4*N + ( M+N )*
  409. $ ILAENV( 1, 'SGEBRD', ' ', M, N, -1, -1 )
  410. IF (WANTU) THEN
  411. MAXWRK = MAX(MAXWRK,N*(N*2+5)+N*
  412. $ ILAENV( 1, 'SORMQR', ' ', N, N, -1, -1 ) )
  413. END IF
  414. IF (WANTVT) THEN
  415. MAXWRK = MAX(MAXWRK,N*(N*2+5)+N*
  416. $ ILAENV( 1, 'SORMLQ', ' ', N, N, -1, -1 ) )
  417. END IF
  418. MINWRK = MAX(N*(N*2+19),4*N+M)
  419. END IF
  420. ELSE
  421. MNTHR = ILAENV( 6, 'SGESVD', JOBU // JOBVT, M, N, 0, 0 )
  422. IF( N.GE.MNTHR ) THEN
  423. *
  424. * Path 1t (N much larger than M)
  425. *
  426. MAXWRK = M +
  427. $ M*ILAENV( 1, 'SGELQF', ' ', M, N, -1, -1 )
  428. MAXWRK = MAX( MAXWRK, M*(M+5) + 2*M*
  429. $ ILAENV( 1, 'SGEBRD', ' ', M, M, -1, -1 ) )
  430. IF (WANTU) THEN
  431. MAXWRK = MAX(MAXWRK,M*(M*3+6)+M*
  432. $ ILAENV( 1, 'SORMQR', ' ', M, M, -1, -1 ) )
  433. END IF
  434. IF (WANTVT) THEN
  435. MAXWRK = MAX(MAXWRK,M*(M*3+6)+M*
  436. $ ILAENV( 1, 'SORMLQ', ' ', M, M, -1, -1 ) )
  437. END IF
  438. MINWRK = M*(M*3+20)
  439. ELSE
  440. *
  441. * Path 2t (N at least M, but not much larger)
  442. *
  443. MAXWRK = 4*M + ( M+N )*
  444. $ ILAENV( 1, 'SGEBRD', ' ', M, N, -1, -1 )
  445. IF (WANTU) THEN
  446. MAXWRK = MAX(MAXWRK,M*(M*2+5)+M*
  447. $ ILAENV( 1, 'SORMQR', ' ', M, M, -1, -1 ) )
  448. END IF
  449. IF (WANTVT) THEN
  450. MAXWRK = MAX(MAXWRK,M*(M*2+5)+M*
  451. $ ILAENV( 1, 'SORMLQ', ' ', M, M, -1, -1 ) )
  452. END IF
  453. MINWRK = MAX(M*(M*2+19),4*M+N)
  454. END IF
  455. END IF
  456. END IF
  457. MAXWRK = MAX( MAXWRK, MINWRK )
  458. WORK( 1 ) = SROUNDUP_LWORK( MAXWRK )
  459. *
  460. IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
  461. INFO = -19
  462. END IF
  463. END IF
  464. *
  465. IF( INFO.NE.0 ) THEN
  466. CALL XERBLA( 'SGESVDX', -INFO )
  467. RETURN
  468. ELSE IF( LQUERY ) THEN
  469. RETURN
  470. END IF
  471. *
  472. * Quick return if possible
  473. *
  474. IF( M.EQ.0 .OR. N.EQ.0 ) THEN
  475. RETURN
  476. END IF
  477. *
  478. * Set singular values indices accord to RANGE.
  479. *
  480. IF( ALLS ) THEN
  481. RNGTGK = 'I'
  482. ILTGK = 1
  483. IUTGK = MIN( M, N )
  484. ELSE IF( INDS ) THEN
  485. RNGTGK = 'I'
  486. ILTGK = IL
  487. IUTGK = IU
  488. ELSE
  489. RNGTGK = 'V'
  490. ILTGK = 0
  491. IUTGK = 0
  492. END IF
  493. *
  494. * Get machine constants
  495. *
  496. EPS = SLAMCH( 'P' )
  497. SMLNUM = SQRT( SLAMCH( 'S' ) ) / EPS
  498. BIGNUM = ONE / SMLNUM
  499. *
  500. * Scale A if max element outside range [SMLNUM,BIGNUM]
  501. *
  502. ANRM = SLANGE( 'M', M, N, A, LDA, DUM )
  503. ISCL = 0
  504. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  505. ISCL = 1
  506. CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
  507. ELSE IF( ANRM.GT.BIGNUM ) THEN
  508. ISCL = 1
  509. CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
  510. END IF
  511. *
  512. IF( M.GE.N ) THEN
  513. *
  514. * A has at least as many rows as columns. If A has sufficiently
  515. * more rows than columns, first reduce A using the QR
  516. * decomposition.
  517. *
  518. IF( M.GE.MNTHR ) THEN
  519. *
  520. * Path 1 (M much larger than N):
  521. * A = Q * R = Q * ( QB * B * PB**T )
  522. * = Q * ( QB * ( UB * S * VB**T ) * PB**T )
  523. * U = Q * QB * UB; V**T = VB**T * PB**T
  524. *
  525. * Compute A=Q*R
  526. * (Workspace: need 2*N, prefer N+N*NB)
  527. *
  528. ITAU = 1
  529. ITEMP = ITAU + N
  530. CALL SGEQRF( M, N, A, LDA, WORK( ITAU ), WORK( ITEMP ),
  531. $ LWORK-ITEMP+1, INFO )
  532. *
  533. * Copy R into WORK and bidiagonalize it:
  534. * (Workspace: need N*N+5*N, prefer N*N+4*N+2*N*NB)
  535. *
  536. IQRF = ITEMP
  537. ID = IQRF + N*N
  538. IE = ID + N
  539. ITAUQ = IE + N
  540. ITAUP = ITAUQ + N
  541. ITEMP = ITAUP + N
  542. CALL SLACPY( 'U', N, N, A, LDA, WORK( IQRF ), N )
  543. CALL SLASET( 'L', N-1, N-1, ZERO, ZERO, WORK( IQRF+1 ), N )
  544. CALL SGEBRD( N, N, WORK( IQRF ), N, WORK( ID ), WORK( IE ),
  545. $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
  546. $ LWORK-ITEMP+1, INFO )
  547. *
  548. * Solve eigenvalue problem TGK*Z=Z*S.
  549. * (Workspace: need 14*N + 2*N*(N+1))
  550. *
  551. ITGKZ = ITEMP
  552. ITEMP = ITGKZ + N*(N*2+1)
  553. CALL SBDSVDX( 'U', JOBZ, RNGTGK, N, WORK( ID ), WORK( IE ),
  554. $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
  555. $ N*2, WORK( ITEMP ), IWORK, INFO)
  556. *
  557. * If needed, compute left singular vectors.
  558. *
  559. IF( WANTU ) THEN
  560. J = ITGKZ
  561. DO I = 1, NS
  562. CALL SCOPY( N, WORK( J ), 1, U( 1,I ), 1 )
  563. J = J + N*2
  564. END DO
  565. CALL SLASET( 'A', M-N, NS, ZERO, ZERO, U( N+1,1 ), LDU )
  566. *
  567. * Call SORMBR to compute QB*UB.
  568. * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
  569. *
  570. CALL SORMBR( 'Q', 'L', 'N', N, NS, N, WORK( IQRF ), N,
  571. $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
  572. $ LWORK-ITEMP+1, INFO )
  573. *
  574. * Call SORMQR to compute Q*(QB*UB).
  575. * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
  576. *
  577. CALL SORMQR( 'L', 'N', M, NS, N, A, LDA,
  578. $ WORK( ITAU ), U, LDU, WORK( ITEMP ),
  579. $ LWORK-ITEMP+1, INFO )
  580. END IF
  581. *
  582. * If needed, compute right singular vectors.
  583. *
  584. IF( WANTVT) THEN
  585. J = ITGKZ + N
  586. DO I = 1, NS
  587. CALL SCOPY( N, WORK( J ), 1, VT( I,1 ), LDVT )
  588. J = J + N*2
  589. END DO
  590. *
  591. * Call SORMBR to compute VB**T * PB**T
  592. * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
  593. *
  594. CALL SORMBR( 'P', 'R', 'T', NS, N, N, WORK( IQRF ), N,
  595. $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
  596. $ LWORK-ITEMP+1, INFO )
  597. END IF
  598. ELSE
  599. *
  600. * Path 2 (M at least N, but not much larger)
  601. * Reduce A to bidiagonal form without QR decomposition
  602. * A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T
  603. * U = QB * UB; V**T = VB**T * PB**T
  604. *
  605. * Bidiagonalize A
  606. * (Workspace: need 4*N+M, prefer 4*N+(M+N)*NB)
  607. *
  608. ID = 1
  609. IE = ID + N
  610. ITAUQ = IE + N
  611. ITAUP = ITAUQ + N
  612. ITEMP = ITAUP + N
  613. CALL SGEBRD( M, N, A, LDA, WORK( ID ), WORK( IE ),
  614. $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
  615. $ LWORK-ITEMP+1, INFO )
  616. *
  617. * Solve eigenvalue problem TGK*Z=Z*S.
  618. * (Workspace: need 14*N + 2*N*(N+1))
  619. *
  620. ITGKZ = ITEMP
  621. ITEMP = ITGKZ + N*(N*2+1)
  622. CALL SBDSVDX( 'U', JOBZ, RNGTGK, N, WORK( ID ), WORK( IE ),
  623. $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
  624. $ N*2, WORK( ITEMP ), IWORK, INFO)
  625. *
  626. * If needed, compute left singular vectors.
  627. *
  628. IF( WANTU ) THEN
  629. J = ITGKZ
  630. DO I = 1, NS
  631. CALL SCOPY( N, WORK( J ), 1, U( 1,I ), 1 )
  632. J = J + N*2
  633. END DO
  634. CALL SLASET( 'A', M-N, NS, ZERO, ZERO, U( N+1,1 ), LDU )
  635. *
  636. * Call SORMBR to compute QB*UB.
  637. * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
  638. *
  639. CALL SORMBR( 'Q', 'L', 'N', M, NS, N, A, LDA,
  640. $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
  641. $ LWORK-ITEMP+1, IERR )
  642. END IF
  643. *
  644. * If needed, compute right singular vectors.
  645. *
  646. IF( WANTVT) THEN
  647. J = ITGKZ + N
  648. DO I = 1, NS
  649. CALL SCOPY( N, WORK( J ), 1, VT( I,1 ), LDVT )
  650. J = J + N*2
  651. END DO
  652. *
  653. * Call SORMBR to compute VB**T * PB**T
  654. * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
  655. *
  656. CALL SORMBR( 'P', 'R', 'T', NS, N, N, A, LDA,
  657. $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
  658. $ LWORK-ITEMP+1, IERR )
  659. END IF
  660. END IF
  661. ELSE
  662. *
  663. * A has more columns than rows. If A has sufficiently more
  664. * columns than rows, first reduce A using the LQ decomposition.
  665. *
  666. IF( N.GE.MNTHR ) THEN
  667. *
  668. * Path 1t (N much larger than M):
  669. * A = L * Q = ( QB * B * PB**T ) * Q
  670. * = ( QB * ( UB * S * VB**T ) * PB**T ) * Q
  671. * U = QB * UB ; V**T = VB**T * PB**T * Q
  672. *
  673. * Compute A=L*Q
  674. * (Workspace: need 2*M, prefer M+M*NB)
  675. *
  676. ITAU = 1
  677. ITEMP = ITAU + M
  678. CALL SGELQF( M, N, A, LDA, WORK( ITAU ), WORK( ITEMP ),
  679. $ LWORK-ITEMP+1, INFO )
  680. * Copy L into WORK and bidiagonalize it:
  681. * (Workspace in WORK( ITEMP ): need M*M+5*N, prefer M*M+4*M+2*M*NB)
  682. *
  683. ILQF = ITEMP
  684. ID = ILQF + M*M
  685. IE = ID + M
  686. ITAUQ = IE + M
  687. ITAUP = ITAUQ + M
  688. ITEMP = ITAUP + M
  689. CALL SLACPY( 'L', M, M, A, LDA, WORK( ILQF ), M )
  690. CALL SLASET( 'U', M-1, M-1, ZERO, ZERO, WORK( ILQF+M ), M )
  691. CALL SGEBRD( M, M, WORK( ILQF ), M, WORK( ID ), WORK( IE ),
  692. $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
  693. $ LWORK-ITEMP+1, INFO )
  694. *
  695. * Solve eigenvalue problem TGK*Z=Z*S.
  696. * (Workspace: need 2*M*M+14*M)
  697. *
  698. ITGKZ = ITEMP
  699. ITEMP = ITGKZ + M*(M*2+1)
  700. CALL SBDSVDX( 'U', JOBZ, RNGTGK, M, WORK( ID ), WORK( IE ),
  701. $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
  702. $ M*2, WORK( ITEMP ), IWORK, INFO)
  703. *
  704. * If needed, compute left singular vectors.
  705. *
  706. IF( WANTU ) THEN
  707. J = ITGKZ
  708. DO I = 1, NS
  709. CALL SCOPY( M, WORK( J ), 1, U( 1,I ), 1 )
  710. J = J + M*2
  711. END DO
  712. *
  713. * Call SORMBR to compute QB*UB.
  714. * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
  715. *
  716. CALL SORMBR( 'Q', 'L', 'N', M, NS, M, WORK( ILQF ), M,
  717. $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
  718. $ LWORK-ITEMP+1, INFO )
  719. END IF
  720. *
  721. * If needed, compute right singular vectors.
  722. *
  723. IF( WANTVT) THEN
  724. J = ITGKZ + M
  725. DO I = 1, NS
  726. CALL SCOPY( M, WORK( J ), 1, VT( I,1 ), LDVT )
  727. J = J + M*2
  728. END DO
  729. CALL SLASET( 'A', NS, N-M, ZERO, ZERO, VT( 1,M+1 ), LDVT)
  730. *
  731. * Call SORMBR to compute (VB**T)*(PB**T)
  732. * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
  733. *
  734. CALL SORMBR( 'P', 'R', 'T', NS, M, M, WORK( ILQF ), M,
  735. $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
  736. $ LWORK-ITEMP+1, INFO )
  737. *
  738. * Call SORMLQ to compute ((VB**T)*(PB**T))*Q.
  739. * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
  740. *
  741. CALL SORMLQ( 'R', 'N', NS, N, M, A, LDA,
  742. $ WORK( ITAU ), VT, LDVT, WORK( ITEMP ),
  743. $ LWORK-ITEMP+1, INFO )
  744. END IF
  745. ELSE
  746. *
  747. * Path 2t (N greater than M, but not much larger)
  748. * Reduce to bidiagonal form without LQ decomposition
  749. * A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T
  750. * U = QB * UB; V**T = VB**T * PB**T
  751. *
  752. * Bidiagonalize A
  753. * (Workspace: need 4*M+N, prefer 4*M+(M+N)*NB)
  754. *
  755. ID = 1
  756. IE = ID + M
  757. ITAUQ = IE + M
  758. ITAUP = ITAUQ + M
  759. ITEMP = ITAUP + M
  760. CALL SGEBRD( M, N, A, LDA, WORK( ID ), WORK( IE ),
  761. $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
  762. $ LWORK-ITEMP+1, INFO )
  763. *
  764. * Solve eigenvalue problem TGK*Z=Z*S.
  765. * (Workspace: need 2*M*M+14*M)
  766. *
  767. ITGKZ = ITEMP
  768. ITEMP = ITGKZ + M*(M*2+1)
  769. CALL SBDSVDX( 'L', JOBZ, RNGTGK, M, WORK( ID ), WORK( IE ),
  770. $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
  771. $ M*2, WORK( ITEMP ), IWORK, INFO)
  772. *
  773. * If needed, compute left singular vectors.
  774. *
  775. IF( WANTU ) THEN
  776. J = ITGKZ
  777. DO I = 1, NS
  778. CALL SCOPY( M, WORK( J ), 1, U( 1,I ), 1 )
  779. J = J + M*2
  780. END DO
  781. *
  782. * Call SORMBR to compute QB*UB.
  783. * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
  784. *
  785. CALL SORMBR( 'Q', 'L', 'N', M, NS, N, A, LDA,
  786. $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
  787. $ LWORK-ITEMP+1, INFO )
  788. END IF
  789. *
  790. * If needed, compute right singular vectors.
  791. *
  792. IF( WANTVT) THEN
  793. J = ITGKZ + M
  794. DO I = 1, NS
  795. CALL SCOPY( M, WORK( J ), 1, VT( I,1 ), LDVT )
  796. J = J + M*2
  797. END DO
  798. CALL SLASET( 'A', NS, N-M, ZERO, ZERO, VT( 1,M+1 ), LDVT)
  799. *
  800. * Call SORMBR to compute VB**T * PB**T
  801. * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
  802. *
  803. CALL SORMBR( 'P', 'R', 'T', NS, N, M, A, LDA,
  804. $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
  805. $ LWORK-ITEMP+1, INFO )
  806. END IF
  807. END IF
  808. END IF
  809. *
  810. * Undo scaling if necessary
  811. *
  812. IF( ISCL.EQ.1 ) THEN
  813. IF( ANRM.GT.BIGNUM )
  814. $ CALL SLASCL( 'G', 0, 0, BIGNUM, ANRM, MINMN, 1,
  815. $ S, MINMN, INFO )
  816. IF( ANRM.LT.SMLNUM )
  817. $ CALL SLASCL( 'G', 0, 0, SMLNUM, ANRM, MINMN, 1,
  818. $ S, MINMN, INFO )
  819. END IF
  820. *
  821. * Return optimal workspace in WORK(1)
  822. *
  823. WORK( 1 ) = SROUNDUP_LWORK( MAXWRK )
  824. *
  825. RETURN
  826. *
  827. * End of SGESVDX
  828. *
  829. END