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.

cgesvdx.f 29 kB

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