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.

zgelsx.f 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. *> \brief <b> ZGELSX 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 ZGELSX + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zgelsx.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zgelsx.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zgelsx.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE ZGELSX( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
  22. * WORK, RWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * INTEGER INFO, LDA, LDB, M, N, NRHS, RANK
  26. * DOUBLE PRECISION RCOND
  27. * ..
  28. * .. Array Arguments ..
  29. * INTEGER JPVT( * )
  30. * DOUBLE PRECISION RWORK( * )
  31. * COMPLEX*16 A( LDA, * ), B( LDB, * ), WORK( * )
  32. * ..
  33. *
  34. *
  35. *> \par Purpose:
  36. * =============
  37. *>
  38. *> \verbatim
  39. *>
  40. *> This routine is deprecated and has been replaced by routine ZGELSY.
  41. *>
  42. *> ZGELSX computes the minimum-norm solution to a complex linear least
  43. *> squares problem:
  44. *> minimize || A * X - B ||
  45. *> using a complete orthogonal factorization of A. A is an M-by-N
  46. *> matrix which may be rank-deficient.
  47. *>
  48. *> Several right hand side vectors b and solution vectors x can be
  49. *> handled in a single call; they are stored as the columns of the
  50. *> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
  51. *> matrix X.
  52. *>
  53. *> The routine first computes a QR factorization with column pivoting:
  54. *> A * P = Q * [ R11 R12 ]
  55. *> [ 0 R22 ]
  56. *> with R11 defined as the largest leading submatrix whose estimated
  57. *> condition number is less than 1/RCOND. The order of R11, RANK,
  58. *> is the effective rank of A.
  59. *>
  60. *> Then, R22 is considered to be negligible, and R12 is annihilated
  61. *> by unitary transformations from the right, arriving at the
  62. *> complete orthogonal factorization:
  63. *> A * P = Q * [ T11 0 ] * Z
  64. *> [ 0 0 ]
  65. *> The minimum-norm solution is then
  66. *> X = P * Z**H [ inv(T11)*Q1**H*B ]
  67. *> [ 0 ]
  68. *> where Q1 consists of the first RANK columns of Q.
  69. *> \endverbatim
  70. *
  71. * Arguments:
  72. * ==========
  73. *
  74. *> \param[in] M
  75. *> \verbatim
  76. *> M is INTEGER
  77. *> The number of rows of the matrix A. M >= 0.
  78. *> \endverbatim
  79. *>
  80. *> \param[in] N
  81. *> \verbatim
  82. *> N is INTEGER
  83. *> The number of columns of the matrix A. N >= 0.
  84. *> \endverbatim
  85. *>
  86. *> \param[in] NRHS
  87. *> \verbatim
  88. *> NRHS is INTEGER
  89. *> The number of right hand sides, i.e., the number of
  90. *> columns of matrices B and X. NRHS >= 0.
  91. *> \endverbatim
  92. *>
  93. *> \param[in,out] A
  94. *> \verbatim
  95. *> A is COMPLEX*16 array, dimension (LDA,N)
  96. *> On entry, the M-by-N matrix A.
  97. *> On exit, A has been overwritten by details of its
  98. *> complete orthogonal factorization.
  99. *> \endverbatim
  100. *>
  101. *> \param[in] LDA
  102. *> \verbatim
  103. *> LDA is INTEGER
  104. *> The leading dimension of the array A. LDA >= max(1,M).
  105. *> \endverbatim
  106. *>
  107. *> \param[in,out] B
  108. *> \verbatim
  109. *> B is COMPLEX*16 array, dimension (LDB,NRHS)
  110. *> On entry, the M-by-NRHS right hand side matrix B.
  111. *> On exit, the N-by-NRHS solution matrix X.
  112. *> If m >= n and RANK = n, the residual sum-of-squares for
  113. *> the solution in the i-th column is given by the sum of
  114. *> squares of elements N+1:M in that column.
  115. *> \endverbatim
  116. *>
  117. *> \param[in] LDB
  118. *> \verbatim
  119. *> LDB is INTEGER
  120. *> The leading dimension of the array B. LDB >= max(1,M,N).
  121. *> \endverbatim
  122. *>
  123. *> \param[in,out] JPVT
  124. *> \verbatim
  125. *> JPVT is INTEGER array, dimension (N)
  126. *> On entry, if JPVT(i) .ne. 0, the i-th column of A is an
  127. *> initial column, otherwise it is a free column. Before
  128. *> the QR factorization of A, all initial columns are
  129. *> permuted to the leading positions; only the remaining
  130. *> free columns are moved as a result of column pivoting
  131. *> during the factorization.
  132. *> On exit, if JPVT(i) = k, then the i-th column of A*P
  133. *> was the k-th column of A.
  134. *> \endverbatim
  135. *>
  136. *> \param[in] RCOND
  137. *> \verbatim
  138. *> RCOND is DOUBLE PRECISION
  139. *> RCOND is used to determine the effective rank of A, which
  140. *> is defined as the order of the largest leading triangular
  141. *> submatrix R11 in the QR factorization with pivoting of A,
  142. *> whose estimated condition number < 1/RCOND.
  143. *> \endverbatim
  144. *>
  145. *> \param[out] RANK
  146. *> \verbatim
  147. *> RANK is INTEGER
  148. *> The effective rank of A, i.e., the order of the submatrix
  149. *> R11. This is the same as the order of the submatrix T11
  150. *> in the complete orthogonal factorization of A.
  151. *> \endverbatim
  152. *>
  153. *> \param[out] WORK
  154. *> \verbatim
  155. *> WORK is COMPLEX*16 array, dimension
  156. *> (min(M,N) + max( N, 2*min(M,N)+NRHS )),
  157. *> \endverbatim
  158. *>
  159. *> \param[out] RWORK
  160. *> \verbatim
  161. *> RWORK is DOUBLE PRECISION array, dimension (2*N)
  162. *> \endverbatim
  163. *>
  164. *> \param[out] INFO
  165. *> \verbatim
  166. *> INFO is INTEGER
  167. *> = 0: successful exit
  168. *> < 0: if INFO = -i, the i-th argument had an illegal value
  169. *> \endverbatim
  170. *
  171. * Authors:
  172. * ========
  173. *
  174. *> \author Univ. of Tennessee
  175. *> \author Univ. of California Berkeley
  176. *> \author Univ. of Colorado Denver
  177. *> \author NAG Ltd.
  178. *
  179. *> \date December 2016
  180. *
  181. *> \ingroup complex16GEsolve
  182. *
  183. * =====================================================================
  184. SUBROUTINE ZGELSX( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
  185. $ WORK, RWORK, INFO )
  186. *
  187. * -- LAPACK driver routine (version 3.7.0) --
  188. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  189. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  190. * December 2016
  191. *
  192. * .. Scalar Arguments ..
  193. INTEGER INFO, LDA, LDB, M, N, NRHS, RANK
  194. DOUBLE PRECISION RCOND
  195. * ..
  196. * .. Array Arguments ..
  197. INTEGER JPVT( * )
  198. DOUBLE PRECISION RWORK( * )
  199. COMPLEX*16 A( LDA, * ), B( LDB, * ), WORK( * )
  200. * ..
  201. *
  202. * =====================================================================
  203. *
  204. * .. Parameters ..
  205. INTEGER IMAX, IMIN
  206. PARAMETER ( IMAX = 1, IMIN = 2 )
  207. DOUBLE PRECISION ZERO, ONE, DONE, NTDONE
  208. PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0, DONE = ZERO,
  209. $ NTDONE = ONE )
  210. COMPLEX*16 CZERO, CONE
  211. PARAMETER ( CZERO = ( 0.0D+0, 0.0D+0 ),
  212. $ CONE = ( 1.0D+0, 0.0D+0 ) )
  213. * ..
  214. * .. Local Scalars ..
  215. INTEGER I, IASCL, IBSCL, ISMAX, ISMIN, J, K, MN
  216. DOUBLE PRECISION ANRM, BIGNUM, BNRM, SMAX, SMAXPR, SMIN, SMINPR,
  217. $ SMLNUM
  218. COMPLEX*16 C1, C2, S1, S2, T1, T2
  219. * ..
  220. * .. External Subroutines ..
  221. EXTERNAL XERBLA, ZGEQPF, ZLAIC1, ZLASCL, ZLASET, ZLATZM,
  222. $ ZTRSM, ZTZRQF, ZUNM2R
  223. * ..
  224. * .. External Functions ..
  225. DOUBLE PRECISION DLAMCH, ZLANGE
  226. EXTERNAL DLAMCH, ZLANGE
  227. * ..
  228. * .. Intrinsic Functions ..
  229. INTRINSIC ABS, DCONJG, MAX, MIN
  230. * ..
  231. * .. Executable Statements ..
  232. *
  233. MN = MIN( M, N )
  234. ISMIN = MN + 1
  235. ISMAX = 2*MN + 1
  236. *
  237. * Test the input arguments.
  238. *
  239. INFO = 0
  240. IF( M.LT.0 ) THEN
  241. INFO = -1
  242. ELSE IF( N.LT.0 ) THEN
  243. INFO = -2
  244. ELSE IF( NRHS.LT.0 ) THEN
  245. INFO = -3
  246. ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
  247. INFO = -5
  248. ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
  249. INFO = -7
  250. END IF
  251. *
  252. IF( INFO.NE.0 ) THEN
  253. CALL XERBLA( 'ZGELSX', -INFO )
  254. RETURN
  255. END IF
  256. *
  257. * Quick return if possible
  258. *
  259. IF( MIN( M, N, NRHS ).EQ.0 ) THEN
  260. RANK = 0
  261. RETURN
  262. END IF
  263. *
  264. * Get machine parameters
  265. *
  266. SMLNUM = DLAMCH( 'S' ) / DLAMCH( 'P' )
  267. BIGNUM = ONE / SMLNUM
  268. CALL DLABAD( SMLNUM, BIGNUM )
  269. *
  270. * Scale A, B if max elements outside range [SMLNUM,BIGNUM]
  271. *
  272. ANRM = ZLANGE( 'M', M, N, A, LDA, RWORK )
  273. IASCL = 0
  274. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  275. *
  276. * Scale matrix norm up to SMLNUM
  277. *
  278. CALL ZLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
  279. IASCL = 1
  280. ELSE IF( ANRM.GT.BIGNUM ) THEN
  281. *
  282. * Scale matrix norm down to BIGNUM
  283. *
  284. CALL ZLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
  285. IASCL = 2
  286. ELSE IF( ANRM.EQ.ZERO ) THEN
  287. *
  288. * Matrix all zero. Return zero solution.
  289. *
  290. CALL ZLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  291. RANK = 0
  292. GO TO 100
  293. END IF
  294. *
  295. BNRM = ZLANGE( 'M', M, NRHS, B, LDB, RWORK )
  296. IBSCL = 0
  297. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  298. *
  299. * Scale matrix norm up to SMLNUM
  300. *
  301. CALL ZLASCL( 'G', 0, 0, BNRM, SMLNUM, M, NRHS, B, LDB, INFO )
  302. IBSCL = 1
  303. ELSE IF( BNRM.GT.BIGNUM ) THEN
  304. *
  305. * Scale matrix norm down to BIGNUM
  306. *
  307. CALL ZLASCL( 'G', 0, 0, BNRM, BIGNUM, M, NRHS, B, LDB, INFO )
  308. IBSCL = 2
  309. END IF
  310. *
  311. * Compute QR factorization with column pivoting of A:
  312. * A * P = Q * R
  313. *
  314. CALL ZGEQPF( M, N, A, LDA, JPVT, WORK( 1 ), WORK( MN+1 ), RWORK,
  315. $ INFO )
  316. *
  317. * complex workspace MN+N. Real workspace 2*N. Details of Householder
  318. * rotations stored in WORK(1:MN).
  319. *
  320. * Determine RANK using incremental condition estimation
  321. *
  322. WORK( ISMIN ) = CONE
  323. WORK( ISMAX ) = CONE
  324. SMAX = ABS( A( 1, 1 ) )
  325. SMIN = SMAX
  326. IF( ABS( A( 1, 1 ) ).EQ.ZERO ) THEN
  327. RANK = 0
  328. CALL ZLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  329. GO TO 100
  330. ELSE
  331. RANK = 1
  332. END IF
  333. *
  334. 10 CONTINUE
  335. IF( RANK.LT.MN ) THEN
  336. I = RANK + 1
  337. CALL ZLAIC1( IMIN, RANK, WORK( ISMIN ), SMIN, A( 1, I ),
  338. $ A( I, I ), SMINPR, S1, C1 )
  339. CALL ZLAIC1( IMAX, RANK, WORK( ISMAX ), SMAX, A( 1, I ),
  340. $ A( I, I ), SMAXPR, S2, C2 )
  341. *
  342. IF( SMAXPR*RCOND.LE.SMINPR ) THEN
  343. DO 20 I = 1, RANK
  344. WORK( ISMIN+I-1 ) = S1*WORK( ISMIN+I-1 )
  345. WORK( ISMAX+I-1 ) = S2*WORK( ISMAX+I-1 )
  346. 20 CONTINUE
  347. WORK( ISMIN+RANK ) = C1
  348. WORK( ISMAX+RANK ) = C2
  349. SMIN = SMINPR
  350. SMAX = SMAXPR
  351. RANK = RANK + 1
  352. GO TO 10
  353. END IF
  354. END IF
  355. *
  356. * Logically partition R = [ R11 R12 ]
  357. * [ 0 R22 ]
  358. * where R11 = R(1:RANK,1:RANK)
  359. *
  360. * [R11,R12] = [ T11, 0 ] * Y
  361. *
  362. IF( RANK.LT.N )
  363. $ CALL ZTZRQF( RANK, N, A, LDA, WORK( MN+1 ), INFO )
  364. *
  365. * Details of Householder rotations stored in WORK(MN+1:2*MN)
  366. *
  367. * B(1:M,1:NRHS) := Q**H * B(1:M,1:NRHS)
  368. *
  369. CALL ZUNM2R( 'Left', 'Conjugate transpose', M, NRHS, MN, A, LDA,
  370. $ WORK( 1 ), B, LDB, WORK( 2*MN+1 ), INFO )
  371. *
  372. * workspace NRHS
  373. *
  374. * B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS)
  375. *
  376. CALL ZTRSM( 'Left', 'Upper', 'No transpose', 'Non-unit', RANK,
  377. $ NRHS, CONE, A, LDA, B, LDB )
  378. *
  379. DO 40 I = RANK + 1, N
  380. DO 30 J = 1, NRHS
  381. B( I, J ) = CZERO
  382. 30 CONTINUE
  383. 40 CONTINUE
  384. *
  385. * B(1:N,1:NRHS) := Y**H * B(1:N,1:NRHS)
  386. *
  387. IF( RANK.LT.N ) THEN
  388. DO 50 I = 1, RANK
  389. CALL ZLATZM( 'Left', N-RANK+1, NRHS, A( I, RANK+1 ), LDA,
  390. $ DCONJG( WORK( MN+I ) ), B( I, 1 ),
  391. $ B( RANK+1, 1 ), LDB, WORK( 2*MN+1 ) )
  392. 50 CONTINUE
  393. END IF
  394. *
  395. * workspace NRHS
  396. *
  397. * B(1:N,1:NRHS) := P * B(1:N,1:NRHS)
  398. *
  399. DO 90 J = 1, NRHS
  400. DO 60 I = 1, N
  401. WORK( 2*MN+I ) = NTDONE
  402. 60 CONTINUE
  403. DO 80 I = 1, N
  404. IF( WORK( 2*MN+I ).EQ.NTDONE ) THEN
  405. IF( JPVT( I ).NE.I ) THEN
  406. K = I
  407. T1 = B( K, J )
  408. T2 = B( JPVT( K ), J )
  409. 70 CONTINUE
  410. B( JPVT( K ), J ) = T1
  411. WORK( 2*MN+K ) = DONE
  412. T1 = T2
  413. K = JPVT( K )
  414. T2 = B( JPVT( K ), J )
  415. IF( JPVT( K ).NE.I )
  416. $ GO TO 70
  417. B( I, J ) = T1
  418. WORK( 2*MN+K ) = DONE
  419. END IF
  420. END IF
  421. 80 CONTINUE
  422. 90 CONTINUE
  423. *
  424. * Undo scaling
  425. *
  426. IF( IASCL.EQ.1 ) THEN
  427. CALL ZLASCL( 'G', 0, 0, ANRM, SMLNUM, N, NRHS, B, LDB, INFO )
  428. CALL ZLASCL( 'U', 0, 0, SMLNUM, ANRM, RANK, RANK, A, LDA,
  429. $ INFO )
  430. ELSE IF( IASCL.EQ.2 ) THEN
  431. CALL ZLASCL( 'G', 0, 0, ANRM, BIGNUM, N, NRHS, B, LDB, INFO )
  432. CALL ZLASCL( 'U', 0, 0, BIGNUM, ANRM, RANK, RANK, A, LDA,
  433. $ INFO )
  434. END IF
  435. IF( IBSCL.EQ.1 ) THEN
  436. CALL ZLASCL( 'G', 0, 0, SMLNUM, BNRM, N, NRHS, B, LDB, INFO )
  437. ELSE IF( IBSCL.EQ.2 ) THEN
  438. CALL ZLASCL( 'G', 0, 0, BIGNUM, BNRM, N, NRHS, B, LDB, INFO )
  439. END IF
  440. *
  441. 100 CONTINUE
  442. *
  443. RETURN
  444. *
  445. * End of ZGELSX
  446. *
  447. END