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.

cgelsd.f 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. *> \brief <b> CGELSD computes the minimum-norm solution to a linear least squares problem 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 CGELSD + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgelsd.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgelsd.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgelsd.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK,
  22. * WORK, LWORK, RWORK, IWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
  26. * REAL RCOND
  27. * ..
  28. * .. Array Arguments ..
  29. * INTEGER IWORK( * )
  30. * REAL RWORK( * ), S( * )
  31. * COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  32. * ..
  33. *
  34. *
  35. *> \par Purpose:
  36. * =============
  37. *>
  38. *> \verbatim
  39. *>
  40. *> CGELSD computes the minimum-norm solution to a real linear least
  41. *> squares problem:
  42. *> minimize 2-norm(| b - A*x |)
  43. *> using the singular value decomposition (SVD) of A. A is an M-by-N
  44. *> matrix which may be rank-deficient.
  45. *>
  46. *> Several right hand side vectors b and solution vectors x can be
  47. *> handled in a single call; they are stored as the columns of the
  48. *> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
  49. *> matrix X.
  50. *>
  51. *> The problem is solved in three steps:
  52. *> (1) Reduce the coefficient matrix A to bidiagonal form with
  53. *> Householder transformations, reducing the original problem
  54. *> into a "bidiagonal least squares problem" (BLS)
  55. *> (2) Solve the BLS using a divide and conquer approach.
  56. *> (3) Apply back all the Householder transformations to solve
  57. *> the original least squares problem.
  58. *>
  59. *> The effective rank of A is determined by treating as zero those
  60. *> singular values which are less than RCOND times the largest singular
  61. *> value.
  62. *>
  63. *> \endverbatim
  64. *
  65. * Arguments:
  66. * ==========
  67. *
  68. *> \param[in] M
  69. *> \verbatim
  70. *> M is INTEGER
  71. *> The number of rows of the matrix A. M >= 0.
  72. *> \endverbatim
  73. *>
  74. *> \param[in] N
  75. *> \verbatim
  76. *> N is INTEGER
  77. *> The number of columns of the matrix A. N >= 0.
  78. *> \endverbatim
  79. *>
  80. *> \param[in] NRHS
  81. *> \verbatim
  82. *> NRHS is INTEGER
  83. *> The number of right hand sides, i.e., the number of columns
  84. *> of the matrices B and X. NRHS >= 0.
  85. *> \endverbatim
  86. *>
  87. *> \param[in,out] A
  88. *> \verbatim
  89. *> A is COMPLEX array, dimension (LDA,N)
  90. *> On entry, the M-by-N matrix A.
  91. *> On exit, A has been destroyed.
  92. *> \endverbatim
  93. *>
  94. *> \param[in] LDA
  95. *> \verbatim
  96. *> LDA is INTEGER
  97. *> The leading dimension of the array A. LDA >= max(1,M).
  98. *> \endverbatim
  99. *>
  100. *> \param[in,out] B
  101. *> \verbatim
  102. *> B is COMPLEX array, dimension (LDB,NRHS)
  103. *> On entry, the M-by-NRHS right hand side matrix B.
  104. *> On exit, B is overwritten by the N-by-NRHS solution matrix X.
  105. *> If m >= n and RANK = n, the residual sum-of-squares for
  106. *> the solution in the i-th column is given by the sum of
  107. *> squares of the modulus of elements n+1:m in that column.
  108. *> \endverbatim
  109. *>
  110. *> \param[in] LDB
  111. *> \verbatim
  112. *> LDB is INTEGER
  113. *> The leading dimension of the array B. LDB >= max(1,M,N).
  114. *> \endverbatim
  115. *>
  116. *> \param[out] S
  117. *> \verbatim
  118. *> S is REAL array, dimension (min(M,N))
  119. *> The singular values of A in decreasing order.
  120. *> The condition number of A in the 2-norm = S(1)/S(min(m,n)).
  121. *> \endverbatim
  122. *>
  123. *> \param[in] RCOND
  124. *> \verbatim
  125. *> RCOND is REAL
  126. *> RCOND is used to determine the effective rank of A.
  127. *> Singular values S(i) <= RCOND*S(1) are treated as zero.
  128. *> If RCOND < 0, machine precision is used instead.
  129. *> \endverbatim
  130. *>
  131. *> \param[out] RANK
  132. *> \verbatim
  133. *> RANK is INTEGER
  134. *> The effective rank of A, i.e., the number of singular values
  135. *> which are greater than RCOND*S(1).
  136. *> \endverbatim
  137. *>
  138. *> \param[out] WORK
  139. *> \verbatim
  140. *> WORK is COMPLEX array, dimension (MAX(1,LWORK))
  141. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  142. *> \endverbatim
  143. *>
  144. *> \param[in] LWORK
  145. *> \verbatim
  146. *> LWORK is INTEGER
  147. *> The dimension of the array WORK. LWORK must be at least 1.
  148. *> The exact minimum amount of workspace needed depends on M,
  149. *> N and NRHS. As long as LWORK is at least
  150. *> 2 * N + N * NRHS
  151. *> if M is greater than or equal to N or
  152. *> 2 * M + M * NRHS
  153. *> if M is less than N, the code will execute correctly.
  154. *> For good performance, LWORK should generally be larger.
  155. *>
  156. *> If LWORK = -1, then a workspace query is assumed; the routine
  157. *> only calculates the optimal size of the array WORK and the
  158. *> minimum sizes of the arrays RWORK and IWORK, and returns
  159. *> these values as the first entries of the WORK, RWORK and
  160. *> IWORK arrays, and no error message related to LWORK is issued
  161. *> by XERBLA.
  162. *> \endverbatim
  163. *>
  164. *> \param[out] RWORK
  165. *> \verbatim
  166. *> RWORK is REAL array, dimension (MAX(1,LRWORK))
  167. *> LRWORK >=
  168. *> 10*N + 2*N*SMLSIZ + 8*N*NLVL + 3*SMLSIZ*NRHS +
  169. *> MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS )
  170. *> if M is greater than or equal to N or
  171. *> 10*M + 2*M*SMLSIZ + 8*M*NLVL + 3*SMLSIZ*NRHS +
  172. *> MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS )
  173. *> if M is less than N, the code will execute correctly.
  174. *> SMLSIZ is returned by ILAENV and is equal to the maximum
  175. *> size of the subproblems at the bottom of the computation
  176. *> tree (usually about 25), and
  177. *> NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 )
  178. *> On exit, if INFO = 0, RWORK(1) returns the minimum LRWORK.
  179. *> \endverbatim
  180. *>
  181. *> \param[out] IWORK
  182. *> \verbatim
  183. *> IWORK is INTEGER array, dimension (MAX(1,LIWORK))
  184. *> LIWORK >= max(1, 3*MINMN*NLVL + 11*MINMN),
  185. *> where MINMN = MIN( M,N ).
  186. *> On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK.
  187. *> \endverbatim
  188. *>
  189. *> \param[out] INFO
  190. *> \verbatim
  191. *> INFO is INTEGER
  192. *> = 0: successful exit
  193. *> < 0: if INFO = -i, the i-th argument had an illegal value.
  194. *> > 0: the algorithm for computing the SVD failed to converge;
  195. *> if INFO = i, i off-diagonal elements of an intermediate
  196. *> bidiagonal form did not converge to zero.
  197. *> \endverbatim
  198. *
  199. * Authors:
  200. * ========
  201. *
  202. *> \author Univ. of Tennessee
  203. *> \author Univ. of California Berkeley
  204. *> \author Univ. of Colorado Denver
  205. *> \author NAG Ltd.
  206. *
  207. *> \ingroup gelsd
  208. *
  209. *> \par Contributors:
  210. * ==================
  211. *>
  212. *> Ming Gu and Ren-Cang Li, Computer Science Division, University of
  213. *> California at Berkeley, USA \n
  214. *> Osni Marques, LBNL/NERSC, USA \n
  215. *
  216. * =====================================================================
  217. SUBROUTINE CGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK,
  218. $ WORK, LWORK, RWORK, IWORK, INFO )
  219. *
  220. * -- LAPACK driver routine --
  221. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  222. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  223. *
  224. * .. Scalar Arguments ..
  225. INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
  226. REAL RCOND
  227. * ..
  228. * .. Array Arguments ..
  229. INTEGER IWORK( * )
  230. REAL RWORK( * ), S( * )
  231. COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  232. * ..
  233. *
  234. * =====================================================================
  235. *
  236. * .. Parameters ..
  237. REAL ZERO, ONE, TWO
  238. PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0, TWO = 2.0E+0 )
  239. COMPLEX CZERO
  240. PARAMETER ( CZERO = ( 0.0E+0, 0.0E+0 ) )
  241. * ..
  242. * .. Local Scalars ..
  243. LOGICAL LQUERY
  244. INTEGER IASCL, IBSCL, IE, IL, ITAU, ITAUP, ITAUQ,
  245. $ LDWORK, LIWORK, LRWORK, MAXMN, MAXWRK, MINMN,
  246. $ MINWRK, MM, MNTHR, NLVL, NRWORK, NWORK, SMLSIZ
  247. REAL ANRM, BIGNUM, BNRM, EPS, SFMIN, SMLNUM
  248. * ..
  249. * .. External Subroutines ..
  250. EXTERNAL CGEBRD, CGELQF, CGEQRF, CLACPY,
  251. $ CLALSD, CLASCL, CLASET, CUNMBR,
  252. $ CUNMLQ, CUNMQR, SLASCL,
  253. $ SLASET, XERBLA
  254. * ..
  255. * .. External Functions ..
  256. INTEGER ILAENV
  257. REAL CLANGE, SLAMCH, SROUNDUP_LWORK
  258. EXTERNAL CLANGE, SLAMCH, ILAENV, SROUNDUP_LWORK
  259. * ..
  260. * .. Intrinsic Functions ..
  261. INTRINSIC INT, LOG, MAX, MIN, REAL
  262. * ..
  263. * .. Executable Statements ..
  264. *
  265. * Test the input arguments.
  266. *
  267. INFO = 0
  268. MINMN = MIN( M, N )
  269. MAXMN = MAX( M, N )
  270. LQUERY = ( LWORK.EQ.-1 )
  271. IF( M.LT.0 ) THEN
  272. INFO = -1
  273. ELSE IF( N.LT.0 ) THEN
  274. INFO = -2
  275. ELSE IF( NRHS.LT.0 ) THEN
  276. INFO = -3
  277. ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
  278. INFO = -5
  279. ELSE IF( LDB.LT.MAX( 1, MAXMN ) ) THEN
  280. INFO = -7
  281. END IF
  282. *
  283. * Compute workspace.
  284. * (Note: Comments in the code beginning "Workspace:" describe the
  285. * minimal amount of workspace needed at that point in the code,
  286. * as well as the preferred amount for good performance.
  287. * NB refers to the optimal block size for the immediately
  288. * following subroutine, as returned by ILAENV.)
  289. *
  290. IF( INFO.EQ.0 ) THEN
  291. MINWRK = 1
  292. MAXWRK = 1
  293. LIWORK = 1
  294. LRWORK = 1
  295. IF( MINMN.GT.0 ) THEN
  296. SMLSIZ = ILAENV( 9, 'CGELSD', ' ', 0, 0, 0, 0 )
  297. MNTHR = ILAENV( 6, 'CGELSD', ' ', M, N, NRHS, -1 )
  298. NLVL = MAX( INT( LOG( REAL( MINMN ) / REAL( SMLSIZ + 1 ) ) /
  299. $ LOG( TWO ) ) + 1, 0 )
  300. LIWORK = 3*MINMN*NLVL + 11*MINMN
  301. MM = M
  302. IF( M.GE.N .AND. M.GE.MNTHR ) THEN
  303. *
  304. * Path 1a - overdetermined, with many more rows than
  305. * columns.
  306. *
  307. MM = N
  308. MAXWRK = MAX( MAXWRK, N*ILAENV( 1, 'CGEQRF', ' ', M, N,
  309. $ -1, -1 ) )
  310. MAXWRK = MAX( MAXWRK, NRHS*ILAENV( 1, 'CUNMQR', 'LC', M,
  311. $ NRHS, N, -1 ) )
  312. END IF
  313. IF( M.GE.N ) THEN
  314. *
  315. * Path 1 - overdetermined or exactly determined.
  316. *
  317. LRWORK = 10*N + 2*N*SMLSIZ + 8*N*NLVL + 3*SMLSIZ*NRHS +
  318. $ MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS )
  319. MAXWRK = MAX( MAXWRK, 2*N + ( MM + N )*ILAENV( 1,
  320. $ 'CGEBRD', ' ', MM, N, -1, -1 ) )
  321. MAXWRK = MAX( MAXWRK, 2*N + NRHS*ILAENV( 1, 'CUNMBR',
  322. $ 'QLC', MM, NRHS, N, -1 ) )
  323. MAXWRK = MAX( MAXWRK, 2*N + ( N - 1 )*ILAENV( 1,
  324. $ 'CUNMBR', 'PLN', N, NRHS, N, -1 ) )
  325. MAXWRK = MAX( MAXWRK, 2*N + N*NRHS )
  326. MINWRK = MAX( 2*N + MM, 2*N + N*NRHS )
  327. END IF
  328. IF( N.GT.M ) THEN
  329. LRWORK = 10*M + 2*M*SMLSIZ + 8*M*NLVL + 3*SMLSIZ*NRHS +
  330. $ MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS )
  331. IF( N.GE.MNTHR ) THEN
  332. *
  333. * Path 2a - underdetermined, with many more columns
  334. * than rows.
  335. *
  336. MAXWRK = M + M*ILAENV( 1, 'CGELQF', ' ', M, N, -1,
  337. $ -1 )
  338. MAXWRK = MAX( MAXWRK, M*M + 4*M + 2*M*ILAENV( 1,
  339. $ 'CGEBRD', ' ', M, M, -1, -1 ) )
  340. MAXWRK = MAX( MAXWRK, M*M + 4*M + NRHS*ILAENV( 1,
  341. $ 'CUNMBR', 'QLC', M, NRHS, M, -1 ) )
  342. MAXWRK = MAX( MAXWRK, M*M + 4*M + ( M - 1 )*ILAENV( 1,
  343. $ 'CUNMLQ', 'LC', N, NRHS, M, -1 ) )
  344. IF( NRHS.GT.1 ) THEN
  345. MAXWRK = MAX( MAXWRK, M*M + M + M*NRHS )
  346. ELSE
  347. MAXWRK = MAX( MAXWRK, M*M + 2*M )
  348. END IF
  349. MAXWRK = MAX( MAXWRK, M*M + 4*M + M*NRHS )
  350. ! XXX: Ensure the Path 2a case below is triggered. The workspace
  351. ! calculation should use queries for all routines eventually.
  352. MAXWRK = MAX( MAXWRK,
  353. $ 4*M+M*M+MAX( M, 2*M-4, NRHS, N-3*M ) )
  354. ELSE
  355. *
  356. * Path 2 - underdetermined.
  357. *
  358. MAXWRK = 2*M + ( N + M )*ILAENV( 1, 'CGEBRD', ' ', M,
  359. $ N, -1, -1 )
  360. MAXWRK = MAX( MAXWRK, 2*M + NRHS*ILAENV( 1, 'CUNMBR',
  361. $ 'QLC', M, NRHS, M, -1 ) )
  362. MAXWRK = MAX( MAXWRK, 2*M + M*ILAENV( 1, 'CUNMBR',
  363. $ 'PLN', N, NRHS, M, -1 ) )
  364. MAXWRK = MAX( MAXWRK, 2*M + M*NRHS )
  365. END IF
  366. MINWRK = MAX( 2*M + N, 2*M + M*NRHS )
  367. END IF
  368. END IF
  369. MINWRK = MIN( MINWRK, MAXWRK )
  370. WORK( 1 ) = SROUNDUP_LWORK(MAXWRK)
  371. IWORK( 1 ) = LIWORK
  372. RWORK( 1 ) = LRWORK
  373. *
  374. IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
  375. INFO = -12
  376. END IF
  377. END IF
  378. *
  379. IF( INFO.NE.0 ) THEN
  380. CALL XERBLA( 'CGELSD', -INFO )
  381. RETURN
  382. ELSE IF( LQUERY ) THEN
  383. RETURN
  384. END IF
  385. *
  386. * Quick return if possible.
  387. *
  388. IF( M.EQ.0 .OR. N.EQ.0 ) THEN
  389. RANK = 0
  390. RETURN
  391. END IF
  392. *
  393. * Get machine parameters.
  394. *
  395. EPS = SLAMCH( 'P' )
  396. SFMIN = SLAMCH( 'S' )
  397. SMLNUM = SFMIN / EPS
  398. BIGNUM = ONE / SMLNUM
  399. *
  400. * Scale A if max entry outside range [SMLNUM,BIGNUM].
  401. *
  402. ANRM = CLANGE( 'M', M, N, A, LDA, RWORK )
  403. IASCL = 0
  404. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  405. *
  406. * Scale matrix norm up to SMLNUM
  407. *
  408. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
  409. IASCL = 1
  410. ELSE IF( ANRM.GT.BIGNUM ) THEN
  411. *
  412. * Scale matrix norm down to BIGNUM.
  413. *
  414. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
  415. IASCL = 2
  416. ELSE IF( ANRM.EQ.ZERO ) THEN
  417. *
  418. * Matrix all zero. Return zero solution.
  419. *
  420. CALL CLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  421. CALL SLASET( 'F', MINMN, 1, ZERO, ZERO, S, 1 )
  422. RANK = 0
  423. GO TO 10
  424. END IF
  425. *
  426. * Scale B if max entry outside range [SMLNUM,BIGNUM].
  427. *
  428. BNRM = CLANGE( 'M', M, NRHS, B, LDB, RWORK )
  429. IBSCL = 0
  430. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  431. *
  432. * Scale matrix norm up to SMLNUM.
  433. *
  434. CALL CLASCL( 'G', 0, 0, BNRM, SMLNUM, M, NRHS, B, LDB, INFO )
  435. IBSCL = 1
  436. ELSE IF( BNRM.GT.BIGNUM ) THEN
  437. *
  438. * Scale matrix norm down to BIGNUM.
  439. *
  440. CALL CLASCL( 'G', 0, 0, BNRM, BIGNUM, M, NRHS, B, LDB, INFO )
  441. IBSCL = 2
  442. END IF
  443. *
  444. * If M < N make sure B(M+1:N,:) = 0
  445. *
  446. IF( M.LT.N )
  447. $ CALL CLASET( 'F', N-M, NRHS, CZERO, CZERO, B( M+1, 1 ), LDB )
  448. *
  449. * Overdetermined case.
  450. *
  451. IF( M.GE.N ) THEN
  452. *
  453. * Path 1 - overdetermined or exactly determined.
  454. *
  455. MM = M
  456. IF( M.GE.MNTHR ) THEN
  457. *
  458. * Path 1a - overdetermined, with many more rows than columns
  459. *
  460. MM = N
  461. ITAU = 1
  462. NWORK = ITAU + N
  463. *
  464. * Compute A=Q*R.
  465. * (RWorkspace: need N)
  466. * (CWorkspace: need N, prefer N*NB)
  467. *
  468. CALL CGEQRF( M, N, A, LDA, WORK( ITAU ), WORK( NWORK ),
  469. $ LWORK-NWORK+1, INFO )
  470. *
  471. * Multiply B by transpose(Q).
  472. * (RWorkspace: need N)
  473. * (CWorkspace: need NRHS, prefer NRHS*NB)
  474. *
  475. CALL CUNMQR( 'L', 'C', M, NRHS, N, A, LDA, WORK( ITAU ), B,
  476. $ LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  477. *
  478. * Zero out below R.
  479. *
  480. IF( N.GT.1 ) THEN
  481. CALL CLASET( 'L', N-1, N-1, CZERO, CZERO, A( 2, 1 ),
  482. $ LDA )
  483. END IF
  484. END IF
  485. *
  486. ITAUQ = 1
  487. ITAUP = ITAUQ + N
  488. NWORK = ITAUP + N
  489. IE = 1
  490. NRWORK = IE + N
  491. *
  492. * Bidiagonalize R in A.
  493. * (RWorkspace: need N)
  494. * (CWorkspace: need 2*N+MM, prefer 2*N+(MM+N)*NB)
  495. *
  496. CALL CGEBRD( MM, N, A, LDA, S, RWORK( IE ), WORK( ITAUQ ),
  497. $ WORK( ITAUP ), WORK( NWORK ), LWORK-NWORK+1,
  498. $ INFO )
  499. *
  500. * Multiply B by transpose of left bidiagonalizing vectors of R.
  501. * (CWorkspace: need 2*N+NRHS, prefer 2*N+NRHS*NB)
  502. *
  503. CALL CUNMBR( 'Q', 'L', 'C', MM, NRHS, N, A, LDA, WORK( ITAUQ ),
  504. $ B, LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  505. *
  506. * Solve the bidiagonal least squares problem.
  507. *
  508. CALL CLALSD( 'U', SMLSIZ, N, NRHS, S, RWORK( IE ), B, LDB,
  509. $ RCOND, RANK, WORK( NWORK ), RWORK( NRWORK ),
  510. $ IWORK, INFO )
  511. IF( INFO.NE.0 ) THEN
  512. GO TO 10
  513. END IF
  514. *
  515. * Multiply B by right bidiagonalizing vectors of R.
  516. *
  517. CALL CUNMBR( 'P', 'L', 'N', N, NRHS, N, A, LDA, WORK( ITAUP ),
  518. $ B, LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  519. *
  520. ELSE IF( N.GE.MNTHR .AND. LWORK.GE.4*M+M*M+
  521. $ MAX( M, 2*M-4, NRHS, N-3*M ) ) THEN
  522. *
  523. * Path 2a - underdetermined, with many more columns than rows
  524. * and sufficient workspace for an efficient algorithm.
  525. *
  526. LDWORK = M
  527. IF( LWORK.GE.MAX( 4*M+M*LDA+MAX( M, 2*M-4, NRHS, N-3*M ),
  528. $ M*LDA+M+M*NRHS ) )LDWORK = LDA
  529. ITAU = 1
  530. NWORK = M + 1
  531. *
  532. * Compute A=L*Q.
  533. * (CWorkspace: need 2*M, prefer M+M*NB)
  534. *
  535. CALL CGELQF( M, N, A, LDA, WORK( ITAU ), WORK( NWORK ),
  536. $ LWORK-NWORK+1, INFO )
  537. IL = NWORK
  538. *
  539. * Copy L to WORK(IL), zeroing out above its diagonal.
  540. *
  541. CALL CLACPY( 'L', M, M, A, LDA, WORK( IL ), LDWORK )
  542. CALL CLASET( 'U', M-1, M-1, CZERO, CZERO, WORK( IL+LDWORK ),
  543. $ LDWORK )
  544. ITAUQ = IL + LDWORK*M
  545. ITAUP = ITAUQ + M
  546. NWORK = ITAUP + M
  547. IE = 1
  548. NRWORK = IE + M
  549. *
  550. * Bidiagonalize L in WORK(IL).
  551. * (RWorkspace: need M)
  552. * (CWorkspace: need M*M+4*M, prefer M*M+4*M+2*M*NB)
  553. *
  554. CALL CGEBRD( M, M, WORK( IL ), LDWORK, S, RWORK( IE ),
  555. $ WORK( ITAUQ ), WORK( ITAUP ), WORK( NWORK ),
  556. $ LWORK-NWORK+1, INFO )
  557. *
  558. * Multiply B by transpose of left bidiagonalizing vectors of L.
  559. * (CWorkspace: need M*M+4*M+NRHS, prefer M*M+4*M+NRHS*NB)
  560. *
  561. CALL CUNMBR( 'Q', 'L', 'C', M, NRHS, M, WORK( IL ), LDWORK,
  562. $ WORK( ITAUQ ), B, LDB, WORK( NWORK ),
  563. $ LWORK-NWORK+1, INFO )
  564. *
  565. * Solve the bidiagonal least squares problem.
  566. *
  567. CALL CLALSD( 'U', SMLSIZ, M, NRHS, S, RWORK( IE ), B, LDB,
  568. $ RCOND, RANK, WORK( NWORK ), RWORK( NRWORK ),
  569. $ IWORK, INFO )
  570. IF( INFO.NE.0 ) THEN
  571. GO TO 10
  572. END IF
  573. *
  574. * Multiply B by right bidiagonalizing vectors of L.
  575. *
  576. CALL CUNMBR( 'P', 'L', 'N', M, NRHS, M, WORK( IL ), LDWORK,
  577. $ WORK( ITAUP ), B, LDB, WORK( NWORK ),
  578. $ LWORK-NWORK+1, INFO )
  579. *
  580. * Zero out below first M rows of B.
  581. *
  582. CALL CLASET( 'F', N-M, NRHS, CZERO, CZERO, B( M+1, 1 ), LDB )
  583. NWORK = ITAU + M
  584. *
  585. * Multiply transpose(Q) by B.
  586. * (CWorkspace: need NRHS, prefer NRHS*NB)
  587. *
  588. CALL CUNMLQ( 'L', 'C', N, NRHS, M, A, LDA, WORK( ITAU ), B,
  589. $ LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  590. *
  591. ELSE
  592. *
  593. * Path 2 - remaining underdetermined cases.
  594. *
  595. ITAUQ = 1
  596. ITAUP = ITAUQ + M
  597. NWORK = ITAUP + M
  598. IE = 1
  599. NRWORK = IE + M
  600. *
  601. * Bidiagonalize A.
  602. * (RWorkspace: need M)
  603. * (CWorkspace: need 2*M+N, prefer 2*M+(M+N)*NB)
  604. *
  605. CALL CGEBRD( M, N, A, LDA, S, RWORK( IE ), WORK( ITAUQ ),
  606. $ WORK( ITAUP ), WORK( NWORK ), LWORK-NWORK+1,
  607. $ INFO )
  608. *
  609. * Multiply B by transpose of left bidiagonalizing vectors.
  610. * (CWorkspace: need 2*M+NRHS, prefer 2*M+NRHS*NB)
  611. *
  612. CALL CUNMBR( 'Q', 'L', 'C', M, NRHS, N, A, LDA, WORK( ITAUQ ),
  613. $ B, LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  614. *
  615. * Solve the bidiagonal least squares problem.
  616. *
  617. CALL CLALSD( 'L', SMLSIZ, M, NRHS, S, RWORK( IE ), B, LDB,
  618. $ RCOND, RANK, WORK( NWORK ), RWORK( NRWORK ),
  619. $ IWORK, INFO )
  620. IF( INFO.NE.0 ) THEN
  621. GO TO 10
  622. END IF
  623. *
  624. * Multiply B by right bidiagonalizing vectors of A.
  625. *
  626. CALL CUNMBR( 'P', 'L', 'N', N, NRHS, M, A, LDA, WORK( ITAUP ),
  627. $ B, LDB, WORK( NWORK ), LWORK-NWORK+1, INFO )
  628. *
  629. END IF
  630. *
  631. * Undo scaling.
  632. *
  633. IF( IASCL.EQ.1 ) THEN
  634. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, N, NRHS, B, LDB, INFO )
  635. CALL SLASCL( 'G', 0, 0, SMLNUM, ANRM, MINMN, 1, S, MINMN,
  636. $ INFO )
  637. ELSE IF( IASCL.EQ.2 ) THEN
  638. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, N, NRHS, B, LDB, INFO )
  639. CALL SLASCL( 'G', 0, 0, BIGNUM, ANRM, MINMN, 1, S, MINMN,
  640. $ INFO )
  641. END IF
  642. IF( IBSCL.EQ.1 ) THEN
  643. CALL CLASCL( 'G', 0, 0, SMLNUM, BNRM, N, NRHS, B, LDB, INFO )
  644. ELSE IF( IBSCL.EQ.2 ) THEN
  645. CALL CLASCL( 'G', 0, 0, BIGNUM, BNRM, N, NRHS, B, LDB, INFO )
  646. END IF
  647. *
  648. 10 CONTINUE
  649. WORK( 1 ) = SROUNDUP_LWORK(MAXWRK)
  650. IWORK( 1 ) = LIWORK
  651. RWORK( 1 ) = LRWORK
  652. RETURN
  653. *
  654. * End of CGELSD
  655. *
  656. END