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.

cgelsy.f 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. *> \brief <b> CGELSY solves overdetermined or underdetermined systems 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 CGELSY + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgelsy.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgelsy.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgelsy.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
  22. * WORK, LWORK, RWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
  26. * REAL RCOND
  27. * ..
  28. * .. Array Arguments ..
  29. * INTEGER JPVT( * )
  30. * REAL RWORK( * )
  31. * COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  32. * ..
  33. *
  34. *
  35. *> \par Purpose:
  36. * =============
  37. *>
  38. *> \verbatim
  39. *>
  40. *> CGELSY computes the minimum-norm solution to a complex linear least
  41. *> squares problem:
  42. *> minimize || A * X - B ||
  43. *> using a complete orthogonal factorization 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 routine first computes a QR factorization with column pivoting:
  52. *> A * P = Q * [ R11 R12 ]
  53. *> [ 0 R22 ]
  54. *> with R11 defined as the largest leading submatrix whose estimated
  55. *> condition number is less than 1/RCOND. The order of R11, RANK,
  56. *> is the effective rank of A.
  57. *>
  58. *> Then, R22 is considered to be negligible, and R12 is annihilated
  59. *> by unitary transformations from the right, arriving at the
  60. *> complete orthogonal factorization:
  61. *> A * P = Q * [ T11 0 ] * Z
  62. *> [ 0 0 ]
  63. *> The minimum-norm solution is then
  64. *> X = P * Z**H [ inv(T11)*Q1**H*B ]
  65. *> [ 0 ]
  66. *> where Q1 consists of the first RANK columns of Q.
  67. *>
  68. *> This routine is basically identical to the original xGELSX except
  69. *> three differences:
  70. *> o The permutation of matrix B (the right hand side) is faster and
  71. *> more simple.
  72. *> o The call to the subroutine xGEQPF has been substituted by the
  73. *> the call to the subroutine xGEQP3. This subroutine is a Blas-3
  74. *> version of the QR factorization with column pivoting.
  75. *> o Matrix B (the right hand side) is updated with Blas-3.
  76. *> \endverbatim
  77. *
  78. * Arguments:
  79. * ==========
  80. *
  81. *> \param[in] M
  82. *> \verbatim
  83. *> M is INTEGER
  84. *> The number of rows of the matrix A. M >= 0.
  85. *> \endverbatim
  86. *>
  87. *> \param[in] N
  88. *> \verbatim
  89. *> N is INTEGER
  90. *> The number of columns of the matrix A. N >= 0.
  91. *> \endverbatim
  92. *>
  93. *> \param[in] NRHS
  94. *> \verbatim
  95. *> NRHS is INTEGER
  96. *> The number of right hand sides, i.e., the number of
  97. *> columns of matrices B and X. NRHS >= 0.
  98. *> \endverbatim
  99. *>
  100. *> \param[in,out] A
  101. *> \verbatim
  102. *> A is COMPLEX array, dimension (LDA,N)
  103. *> On entry, the M-by-N matrix A.
  104. *> On exit, A has been overwritten by details of its
  105. *> complete orthogonal factorization.
  106. *> \endverbatim
  107. *>
  108. *> \param[in] LDA
  109. *> \verbatim
  110. *> LDA is INTEGER
  111. *> The leading dimension of the array A. LDA >= max(1,M).
  112. *> \endverbatim
  113. *>
  114. *> \param[in,out] B
  115. *> \verbatim
  116. *> B is COMPLEX array, dimension (LDB,NRHS)
  117. *> On entry, the M-by-NRHS right hand side matrix B.
  118. *> On exit, the N-by-NRHS solution matrix X.
  119. *> If M = 0 or N = 0, B is not referenced.
  120. *> \endverbatim
  121. *>
  122. *> \param[in] LDB
  123. *> \verbatim
  124. *> LDB is INTEGER
  125. *> The leading dimension of the array B. LDB >= max(1,M,N).
  126. *> \endverbatim
  127. *>
  128. *> \param[in,out] JPVT
  129. *> \verbatim
  130. *> JPVT is INTEGER array, dimension (N)
  131. *> On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted
  132. *> to the front of AP, otherwise column i is a free column.
  133. *> On exit, if JPVT(i) = k, then the i-th column of A*P
  134. *> was the k-th column of A.
  135. *> \endverbatim
  136. *>
  137. *> \param[in] RCOND
  138. *> \verbatim
  139. *> RCOND is REAL
  140. *> RCOND is used to determine the effective rank of A, which
  141. *> is defined as the order of the largest leading triangular
  142. *> submatrix R11 in the QR factorization with pivoting of A,
  143. *> whose estimated condition number < 1/RCOND.
  144. *> \endverbatim
  145. *>
  146. *> \param[out] RANK
  147. *> \verbatim
  148. *> RANK is INTEGER
  149. *> The effective rank of A, i.e., the order of the submatrix
  150. *> R11. This is the same as the order of the submatrix T11
  151. *> in the complete orthogonal factorization of A.
  152. *> If NRHS = 0, RANK = 0 on output.
  153. *> \endverbatim
  154. *>
  155. *> \param[out] WORK
  156. *> \verbatim
  157. *> WORK is COMPLEX array, dimension (MAX(1,LWORK))
  158. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  159. *> \endverbatim
  160. *>
  161. *> \param[in] LWORK
  162. *> \verbatim
  163. *> LWORK is INTEGER
  164. *> The dimension of the array WORK.
  165. *> The unblocked strategy requires that:
  166. *> LWORK >= MN + MAX( 2*MN, N+1, MN+NRHS )
  167. *> where MN = min(M,N).
  168. *> The block algorithm requires that:
  169. *> LWORK >= MN + MAX( 2*MN, NB*(N+1), MN+MN*NB, MN+NB*NRHS )
  170. *> where NB is an upper bound on the blocksize returned
  171. *> by ILAENV for the routines CGEQP3, CTZRZF, CTZRQF, CUNMQR,
  172. *> and CUNMRZ.
  173. *>
  174. *> If LWORK = -1, then a workspace query is assumed; the routine
  175. *> only calculates the optimal size of the WORK array, returns
  176. *> this value as the first entry of the WORK array, and no error
  177. *> message related to LWORK is issued by XERBLA.
  178. *> \endverbatim
  179. *>
  180. *> \param[out] RWORK
  181. *> \verbatim
  182. *> RWORK is REAL array, dimension (2*N)
  183. *> \endverbatim
  184. *>
  185. *> \param[out] INFO
  186. *> \verbatim
  187. *> INFO is INTEGER
  188. *> = 0: successful exit
  189. *> < 0: if INFO = -i, the i-th argument had an illegal value
  190. *> \endverbatim
  191. *
  192. * Authors:
  193. * ========
  194. *
  195. *> \author Univ. of Tennessee
  196. *> \author Univ. of California Berkeley
  197. *> \author Univ. of Colorado Denver
  198. *> \author NAG Ltd.
  199. *
  200. *> \ingroup complexGEsolve
  201. *
  202. *> \par Contributors:
  203. * ==================
  204. *>
  205. *> A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA \n
  206. *> E. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n
  207. *> G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n
  208. *>
  209. * =====================================================================
  210. SUBROUTINE CGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
  211. $ WORK, LWORK, RWORK, INFO )
  212. *
  213. * -- LAPACK driver routine --
  214. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  215. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  216. *
  217. * .. Scalar Arguments ..
  218. INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
  219. REAL RCOND
  220. * ..
  221. * .. Array Arguments ..
  222. INTEGER JPVT( * )
  223. REAL RWORK( * )
  224. COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  225. * ..
  226. *
  227. * =====================================================================
  228. *
  229. * .. Parameters ..
  230. INTEGER IMAX, IMIN
  231. PARAMETER ( IMAX = 1, IMIN = 2 )
  232. REAL ZERO, ONE
  233. PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
  234. COMPLEX CZERO, CONE
  235. PARAMETER ( CZERO = ( 0.0E+0, 0.0E+0 ),
  236. $ CONE = ( 1.0E+0, 0.0E+0 ) )
  237. * ..
  238. * .. Local Scalars ..
  239. LOGICAL LQUERY
  240. INTEGER I, IASCL, IBSCL, ISMAX, ISMIN, J, LWKOPT, MN,
  241. $ NB, NB1, NB2, NB3, NB4
  242. REAL ANRM, BIGNUM, BNRM, SMAX, SMAXPR, SMIN, SMINPR,
  243. $ SMLNUM, WSIZE
  244. COMPLEX C1, C2, S1, S2
  245. * ..
  246. * .. External Subroutines ..
  247. EXTERNAL CCOPY, CGEQP3, CLAIC1, CLASCL, CLASET, CTRSM,
  248. $ CTZRZF, CUNMQR, CUNMRZ, SLABAD, XERBLA
  249. * ..
  250. * .. External Functions ..
  251. INTEGER ILAENV
  252. REAL CLANGE, SLAMCH
  253. EXTERNAL CLANGE, ILAENV, SLAMCH
  254. * ..
  255. * .. Intrinsic Functions ..
  256. INTRINSIC ABS, MAX, MIN, REAL, CMPLX
  257. * ..
  258. * .. Executable Statements ..
  259. *
  260. MN = MIN( M, N )
  261. ISMIN = MN + 1
  262. ISMAX = 2*MN + 1
  263. *
  264. * Test the input arguments.
  265. *
  266. INFO = 0
  267. NB1 = ILAENV( 1, 'CGEQRF', ' ', M, N, -1, -1 )
  268. NB2 = ILAENV( 1, 'CGERQF', ' ', M, N, -1, -1 )
  269. NB3 = ILAENV( 1, 'CUNMQR', ' ', M, N, NRHS, -1 )
  270. NB4 = ILAENV( 1, 'CUNMRQ', ' ', M, N, NRHS, -1 )
  271. NB = MAX( NB1, NB2, NB3, NB4 )
  272. LWKOPT = MAX( 1, MN+2*N+NB*(N+1), 2*MN+NB*NRHS )
  273. WORK( 1 ) = CMPLX( LWKOPT )
  274. LQUERY = ( LWORK.EQ.-1 )
  275. IF( M.LT.0 ) THEN
  276. INFO = -1
  277. ELSE IF( N.LT.0 ) THEN
  278. INFO = -2
  279. ELSE IF( NRHS.LT.0 ) THEN
  280. INFO = -3
  281. ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
  282. INFO = -5
  283. ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
  284. INFO = -7
  285. ELSE IF( LWORK.LT.( MN+MAX( 2*MN, N+1, MN+NRHS ) ) .AND.
  286. $ .NOT.LQUERY ) THEN
  287. INFO = -12
  288. END IF
  289. *
  290. IF( INFO.NE.0 ) THEN
  291. CALL XERBLA( 'CGELSY', -INFO )
  292. RETURN
  293. ELSE IF( LQUERY ) THEN
  294. RETURN
  295. END IF
  296. *
  297. * Quick return if possible
  298. *
  299. IF( MIN( M, N, NRHS ).EQ.0 ) THEN
  300. RANK = 0
  301. RETURN
  302. END IF
  303. *
  304. * Get machine parameters
  305. *
  306. SMLNUM = SLAMCH( 'S' ) / SLAMCH( 'P' )
  307. BIGNUM = ONE / SMLNUM
  308. CALL SLABAD( SMLNUM, BIGNUM )
  309. *
  310. * Scale A, B if max entries outside range [SMLNUM,BIGNUM]
  311. *
  312. ANRM = CLANGE( 'M', M, N, A, LDA, RWORK )
  313. IASCL = 0
  314. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  315. *
  316. * Scale matrix norm up to SMLNUM
  317. *
  318. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
  319. IASCL = 1
  320. ELSE IF( ANRM.GT.BIGNUM ) THEN
  321. *
  322. * Scale matrix norm down to BIGNUM
  323. *
  324. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
  325. IASCL = 2
  326. ELSE IF( ANRM.EQ.ZERO ) THEN
  327. *
  328. * Matrix all zero. Return zero solution.
  329. *
  330. CALL CLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  331. RANK = 0
  332. GO TO 70
  333. END IF
  334. *
  335. BNRM = CLANGE( 'M', M, NRHS, B, LDB, RWORK )
  336. IBSCL = 0
  337. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  338. *
  339. * Scale matrix norm up to SMLNUM
  340. *
  341. CALL CLASCL( 'G', 0, 0, BNRM, SMLNUM, M, NRHS, B, LDB, INFO )
  342. IBSCL = 1
  343. ELSE IF( BNRM.GT.BIGNUM ) THEN
  344. *
  345. * Scale matrix norm down to BIGNUM
  346. *
  347. CALL CLASCL( 'G', 0, 0, BNRM, BIGNUM, M, NRHS, B, LDB, INFO )
  348. IBSCL = 2
  349. END IF
  350. *
  351. * Compute QR factorization with column pivoting of A:
  352. * A * P = Q * R
  353. *
  354. CALL CGEQP3( M, N, A, LDA, JPVT, WORK( 1 ), WORK( MN+1 ),
  355. $ LWORK-MN, RWORK, INFO )
  356. WSIZE = MN + REAL( WORK( MN+1 ) )
  357. *
  358. * complex workspace: MN+NB*(N+1). real workspace 2*N.
  359. * Details of Householder rotations stored in WORK(1:MN).
  360. *
  361. * Determine RANK using incremental condition estimation
  362. *
  363. WORK( ISMIN ) = CONE
  364. WORK( ISMAX ) = CONE
  365. SMAX = ABS( A( 1, 1 ) )
  366. SMIN = SMAX
  367. IF( ABS( A( 1, 1 ) ).EQ.ZERO ) THEN
  368. RANK = 0
  369. CALL CLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  370. GO TO 70
  371. ELSE
  372. RANK = 1
  373. END IF
  374. *
  375. 10 CONTINUE
  376. IF( RANK.LT.MN ) THEN
  377. I = RANK + 1
  378. CALL CLAIC1( IMIN, RANK, WORK( ISMIN ), SMIN, A( 1, I ),
  379. $ A( I, I ), SMINPR, S1, C1 )
  380. CALL CLAIC1( IMAX, RANK, WORK( ISMAX ), SMAX, A( 1, I ),
  381. $ A( I, I ), SMAXPR, S2, C2 )
  382. *
  383. IF( SMAXPR*RCOND.LE.SMINPR ) THEN
  384. DO 20 I = 1, RANK
  385. WORK( ISMIN+I-1 ) = S1*WORK( ISMIN+I-1 )
  386. WORK( ISMAX+I-1 ) = S2*WORK( ISMAX+I-1 )
  387. 20 CONTINUE
  388. WORK( ISMIN+RANK ) = C1
  389. WORK( ISMAX+RANK ) = C2
  390. SMIN = SMINPR
  391. SMAX = SMAXPR
  392. RANK = RANK + 1
  393. GO TO 10
  394. END IF
  395. END IF
  396. *
  397. * complex workspace: 3*MN.
  398. *
  399. * Logically partition R = [ R11 R12 ]
  400. * [ 0 R22 ]
  401. * where R11 = R(1:RANK,1:RANK)
  402. *
  403. * [R11,R12] = [ T11, 0 ] * Y
  404. *
  405. IF( RANK.LT.N )
  406. $ CALL CTZRZF( RANK, N, A, LDA, WORK( MN+1 ), WORK( 2*MN+1 ),
  407. $ LWORK-2*MN, INFO )
  408. *
  409. * complex workspace: 2*MN.
  410. * Details of Householder rotations stored in WORK(MN+1:2*MN)
  411. *
  412. * B(1:M,1:NRHS) := Q**H * B(1:M,1:NRHS)
  413. *
  414. CALL CUNMQR( 'Left', 'Conjugate transpose', M, NRHS, MN, A, LDA,
  415. $ WORK( 1 ), B, LDB, WORK( 2*MN+1 ), LWORK-2*MN, INFO )
  416. WSIZE = MAX( WSIZE, 2*MN+REAL( WORK( 2*MN+1 ) ) )
  417. *
  418. * complex workspace: 2*MN+NB*NRHS.
  419. *
  420. * B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS)
  421. *
  422. CALL CTRSM( 'Left', 'Upper', 'No transpose', 'Non-unit', RANK,
  423. $ NRHS, CONE, A, LDA, B, LDB )
  424. *
  425. DO 40 J = 1, NRHS
  426. DO 30 I = RANK + 1, N
  427. B( I, J ) = CZERO
  428. 30 CONTINUE
  429. 40 CONTINUE
  430. *
  431. * B(1:N,1:NRHS) := Y**H * B(1:N,1:NRHS)
  432. *
  433. IF( RANK.LT.N ) THEN
  434. CALL CUNMRZ( 'Left', 'Conjugate transpose', N, NRHS, RANK,
  435. $ N-RANK, A, LDA, WORK( MN+1 ), B, LDB,
  436. $ WORK( 2*MN+1 ), LWORK-2*MN, INFO )
  437. END IF
  438. *
  439. * complex workspace: 2*MN+NRHS.
  440. *
  441. * B(1:N,1:NRHS) := P * B(1:N,1:NRHS)
  442. *
  443. DO 60 J = 1, NRHS
  444. DO 50 I = 1, N
  445. WORK( JPVT( I ) ) = B( I, J )
  446. 50 CONTINUE
  447. CALL CCOPY( N, WORK( 1 ), 1, B( 1, J ), 1 )
  448. 60 CONTINUE
  449. *
  450. * complex workspace: N.
  451. *
  452. * Undo scaling
  453. *
  454. IF( IASCL.EQ.1 ) THEN
  455. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, N, NRHS, B, LDB, INFO )
  456. CALL CLASCL( 'U', 0, 0, SMLNUM, ANRM, RANK, RANK, A, LDA,
  457. $ INFO )
  458. ELSE IF( IASCL.EQ.2 ) THEN
  459. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, N, NRHS, B, LDB, INFO )
  460. CALL CLASCL( 'U', 0, 0, BIGNUM, ANRM, RANK, RANK, A, LDA,
  461. $ INFO )
  462. END IF
  463. IF( IBSCL.EQ.1 ) THEN
  464. CALL CLASCL( 'G', 0, 0, SMLNUM, BNRM, N, NRHS, B, LDB, INFO )
  465. ELSE IF( IBSCL.EQ.2 ) THEN
  466. CALL CLASCL( 'G', 0, 0, BIGNUM, BNRM, N, NRHS, B, LDB, INFO )
  467. END IF
  468. *
  469. 70 CONTINUE
  470. WORK( 1 ) = CMPLX( LWKOPT )
  471. *
  472. RETURN
  473. *
  474. * End of CGELSY
  475. *
  476. END