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.

cgelst.f 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. *> \brief <b> CGELST solves overdetermined or underdetermined systems for GE matrices using QR or LQ factorization with compact WY representation of Q.</b>
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download CGELST + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgelst.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgelst.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgelst.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGELST( TRANS, M, N, NRHS, A, LDA, B, LDB, WORK, LWORK,
  22. * INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * CHARACTER TRANS
  26. * INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
  27. * ..
  28. * .. Array Arguments ..
  29. * COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  30. * ..
  31. *
  32. *
  33. *> \par Purpose:
  34. * =============
  35. *>
  36. *> \verbatim
  37. *>
  38. *> CGELST solves overdetermined or underdetermined real linear systems
  39. *> involving an M-by-N matrix A, or its conjugate-transpose, using a QR
  40. *> or LQ factorization of A with compact WY representation of Q.
  41. *> It is assumed that A has full rank.
  42. *>
  43. *> The following options are provided:
  44. *>
  45. *> 1. If TRANS = 'N' and m >= n: find the least squares solution of
  46. *> an overdetermined system, i.e., solve the least squares problem
  47. *> minimize || B - A*X ||.
  48. *>
  49. *> 2. If TRANS = 'N' and m < n: find the minimum norm solution of
  50. *> an underdetermined system A * X = B.
  51. *>
  52. *> 3. If TRANS = 'C' and m >= n: find the minimum norm solution of
  53. *> an underdetermined system A**T * X = B.
  54. *>
  55. *> 4. If TRANS = 'C' and m < n: find the least squares solution of
  56. *> an overdetermined system, i.e., solve the least squares problem
  57. *> minimize || B - A**T * X ||.
  58. *>
  59. *> Several right hand side vectors b and solution vectors x can be
  60. *> handled in a single call; they are stored as the columns of the
  61. *> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
  62. *> matrix X.
  63. *> \endverbatim
  64. *
  65. * Arguments:
  66. * ==========
  67. *
  68. *> \param[in] TRANS
  69. *> \verbatim
  70. *> TRANS is CHARACTER*1
  71. *> = 'N': the linear system involves A;
  72. *> = 'C': the linear system involves A**H.
  73. *> \endverbatim
  74. *>
  75. *> \param[in] M
  76. *> \verbatim
  77. *> M is INTEGER
  78. *> The number of rows of the matrix A. M >= 0.
  79. *> \endverbatim
  80. *>
  81. *> \param[in] N
  82. *> \verbatim
  83. *> N is INTEGER
  84. *> The number of columns of the matrix A. N >= 0.
  85. *> \endverbatim
  86. *>
  87. *> \param[in] NRHS
  88. *> \verbatim
  89. *> NRHS is INTEGER
  90. *> The number of right hand sides, i.e., the number of
  91. *> columns of the matrices B and X. NRHS >=0.
  92. *> \endverbatim
  93. *>
  94. *> \param[in,out] A
  95. *> \verbatim
  96. *> A is COMPLEX array, dimension (LDA,N)
  97. *> On entry, the M-by-N matrix A.
  98. *> On exit,
  99. *> if M >= N, A is overwritten by details of its QR
  100. *> factorization as returned by CGEQRT;
  101. *> if M < N, A is overwritten by details of its LQ
  102. *> factorization as returned by CGELQT.
  103. *> \endverbatim
  104. *>
  105. *> \param[in] LDA
  106. *> \verbatim
  107. *> LDA is INTEGER
  108. *> The leading dimension of the array A. LDA >= max(1,M).
  109. *> \endverbatim
  110. *>
  111. *> \param[in,out] B
  112. *> \verbatim
  113. *> B is COMPLEX array, dimension (LDB,NRHS)
  114. *> On entry, the matrix B of right hand side vectors, stored
  115. *> columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS
  116. *> if TRANS = 'C'.
  117. *> On exit, if INFO = 0, B is overwritten by the solution
  118. *> vectors, stored columnwise:
  119. *> if TRANS = 'N' and m >= n, rows 1 to n of B contain the least
  120. *> squares solution vectors; the residual sum of squares for the
  121. *> solution in each column is given by the sum of squares of
  122. *> modulus of elements N+1 to M in that column;
  123. *> if TRANS = 'N' and m < n, rows 1 to N of B contain the
  124. *> minimum norm solution vectors;
  125. *> if TRANS = 'C' and m >= n, rows 1 to M of B contain the
  126. *> minimum norm solution vectors;
  127. *> if TRANS = 'C' and m < n, rows 1 to M of B contain the
  128. *> least squares solution vectors; the residual sum of squares
  129. *> for the solution in each column is given by the sum of
  130. *> squares of the modulus of elements M+1 to N in that column.
  131. *> \endverbatim
  132. *>
  133. *> \param[in] LDB
  134. *> \verbatim
  135. *> LDB is INTEGER
  136. *> The leading dimension of the array B. LDB >= MAX(1,M,N).
  137. *> \endverbatim
  138. *>
  139. *> \param[out] WORK
  140. *> \verbatim
  141. *> WORK is COMPLEX array, dimension (MAX(1,LWORK))
  142. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  143. *> \endverbatim
  144. *>
  145. *> \param[in] LWORK
  146. *> \verbatim
  147. *> LWORK is INTEGER
  148. *> The dimension of the array WORK.
  149. *> LWORK >= max( 1, MN + max( MN, NRHS ) ).
  150. *> For optimal performance,
  151. *> LWORK >= max( 1, (MN + max( MN, NRHS ))*NB ).
  152. *> where MN = min(M,N) and NB is the optimum block size.
  153. *>
  154. *> If LWORK = -1, then a workspace query is assumed; the routine
  155. *> only calculates the optimal size of the WORK array, returns
  156. *> this value as the first entry of the WORK array, and no error
  157. *> message related to LWORK is issued by XERBLA.
  158. *> \endverbatim
  159. *>
  160. *> \param[out] INFO
  161. *> \verbatim
  162. *> INFO is INTEGER
  163. *> = 0: successful exit
  164. *> < 0: if INFO = -i, the i-th argument had an illegal value
  165. *> > 0: if INFO = i, the i-th diagonal element of the
  166. *> triangular factor of A is zero, so that A does not have
  167. *> full rank; the least squares solution could not be
  168. *> computed.
  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. *> \ingroup gelst
  180. *
  181. *> \par Contributors:
  182. * ==================
  183. *>
  184. *> \verbatim
  185. *>
  186. *> November 2022, Igor Kozachenko,
  187. *> Computer Science Division,
  188. *> University of California, Berkeley
  189. *> \endverbatim
  190. *
  191. * =====================================================================
  192. SUBROUTINE CGELST( TRANS, M, N, NRHS, A, LDA, B, LDB, WORK, LWORK,
  193. $ INFO )
  194. *
  195. * -- LAPACK driver routine --
  196. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  197. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  198. *
  199. * .. Scalar Arguments ..
  200. CHARACTER TRANS
  201. INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
  202. * ..
  203. * .. Array Arguments ..
  204. COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
  205. * ..
  206. *
  207. * =====================================================================
  208. *
  209. * .. Parameters ..
  210. REAL ZERO, ONE
  211. PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
  212. COMPLEX CZERO
  213. PARAMETER ( CZERO = ( 0.0E+0, 0.0E+0 ) )
  214. * ..
  215. * .. Local Scalars ..
  216. LOGICAL LQUERY, TPSD
  217. INTEGER BROW, I, IASCL, IBSCL, J, LWOPT, MN, MNNRHS,
  218. $ NB, NBMIN, SCLLEN
  219. REAL ANRM, BIGNUM, BNRM, SMLNUM
  220. * ..
  221. * .. Local Arrays ..
  222. REAL RWORK( 1 )
  223. * ..
  224. * .. External Functions ..
  225. LOGICAL LSAME
  226. INTEGER ILAENV
  227. REAL SLAMCH, CLANGE, SROUNDUP_LWORK
  228. EXTERNAL LSAME, ILAENV, SLAMCH, CLANGE, SROUNDUP_LWORK
  229. * ..
  230. * .. External Subroutines ..
  231. EXTERNAL CGELQT, CGEQRT, CGEMLQT, CGEMQRT,
  232. $ CLASCL, CLASET, CTRTRS, XERBLA
  233. * ..
  234. * .. Intrinsic Functions ..
  235. INTRINSIC MAX, MIN
  236. * ..
  237. * .. Executable Statements ..
  238. *
  239. * Test the input arguments.
  240. *
  241. INFO = 0
  242. MN = MIN( M, N )
  243. LQUERY = ( LWORK.EQ.-1 )
  244. IF( .NOT.( LSAME( TRANS, 'N' ) .OR. LSAME( TRANS, 'C' ) ) ) THEN
  245. INFO = -1
  246. ELSE IF( M.LT.0 ) THEN
  247. INFO = -2
  248. ELSE IF( N.LT.0 ) THEN
  249. INFO = -3
  250. ELSE IF( NRHS.LT.0 ) THEN
  251. INFO = -4
  252. ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
  253. INFO = -6
  254. ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
  255. INFO = -8
  256. ELSE IF( LWORK.LT.MAX( 1, MN+MAX( MN, NRHS ) ) .AND. .NOT.LQUERY )
  257. $ THEN
  258. INFO = -10
  259. END IF
  260. *
  261. * Figure out optimal block size and optimal workspace size
  262. *
  263. IF( INFO.EQ.0 .OR. INFO.EQ.-10 ) THEN
  264. *
  265. TPSD = .TRUE.
  266. IF( LSAME( TRANS, 'N' ) )
  267. $ TPSD = .FALSE.
  268. *
  269. NB = ILAENV( 1, 'CGELST', ' ', M, N, -1, -1 )
  270. *
  271. MNNRHS = MAX( MN, NRHS )
  272. LWOPT = MAX( 1, (MN+MNNRHS)*NB )
  273. WORK( 1 ) = SROUNDUP_LWORK( LWOPT )
  274. *
  275. END IF
  276. *
  277. IF( INFO.NE.0 ) THEN
  278. CALL XERBLA( 'CGELST ', -INFO )
  279. RETURN
  280. ELSE IF( LQUERY ) THEN
  281. RETURN
  282. END IF
  283. *
  284. * Quick return if possible
  285. *
  286. IF( MIN( M, N, NRHS ).EQ.0 ) THEN
  287. CALL CLASET( 'Full', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  288. WORK( 1 ) = SROUNDUP_LWORK( LWOPT )
  289. RETURN
  290. END IF
  291. *
  292. * *GEQRT and *GELQT routines cannot accept NB larger than min(M,N)
  293. *
  294. IF( NB.GT.MN ) NB = MN
  295. *
  296. * Determine the block size from the supplied LWORK
  297. * ( at this stage we know that LWORK >= (minimum required workspace,
  298. * but it may be less than optimal)
  299. *
  300. NB = MIN( NB, LWORK/( MN + MNNRHS ) )
  301. *
  302. * The minimum value of NB, when blocked code is used
  303. *
  304. NBMIN = MAX( 2, ILAENV( 2, 'CGELST', ' ', M, N, -1, -1 ) )
  305. *
  306. IF( NB.LT.NBMIN ) THEN
  307. NB = 1
  308. END IF
  309. *
  310. * Get machine parameters
  311. *
  312. SMLNUM = SLAMCH( 'S' ) / SLAMCH( 'P' )
  313. BIGNUM = ONE / SMLNUM
  314. *
  315. * Scale A, B if max element outside range [SMLNUM,BIGNUM]
  316. *
  317. ANRM = CLANGE( 'M', M, N, A, LDA, RWORK )
  318. IASCL = 0
  319. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  320. *
  321. * Scale matrix norm up to SMLNUM
  322. *
  323. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
  324. IASCL = 1
  325. ELSE IF( ANRM.GT.BIGNUM ) THEN
  326. *
  327. * Scale matrix norm down to BIGNUM
  328. *
  329. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
  330. IASCL = 2
  331. ELSE IF( ANRM.EQ.ZERO ) THEN
  332. *
  333. * Matrix all zero. Return zero solution.
  334. *
  335. CALL CLASET( 'Full', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
  336. WORK( 1 ) = SROUNDUP_LWORK( LWOPT )
  337. RETURN
  338. END IF
  339. *
  340. BROW = M
  341. IF( TPSD )
  342. $ BROW = N
  343. BNRM = CLANGE( 'M', BROW, NRHS, B, LDB, RWORK )
  344. IBSCL = 0
  345. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  346. *
  347. * Scale matrix norm up to SMLNUM
  348. *
  349. CALL CLASCL( 'G', 0, 0, BNRM, SMLNUM, BROW, NRHS, B, LDB,
  350. $ INFO )
  351. IBSCL = 1
  352. ELSE IF( BNRM.GT.BIGNUM ) THEN
  353. *
  354. * Scale matrix norm down to BIGNUM
  355. *
  356. CALL CLASCL( 'G', 0, 0, BNRM, BIGNUM, BROW, NRHS, B, LDB,
  357. $ INFO )
  358. IBSCL = 2
  359. END IF
  360. *
  361. IF( M.GE.N ) THEN
  362. *
  363. * M > N:
  364. * Compute the blocked QR factorization of A,
  365. * using the compact WY representation of Q,
  366. * workspace at least N, optimally N*NB.
  367. *
  368. CALL CGEQRT( M, N, NB, A, LDA, WORK( 1 ), NB,
  369. $ WORK( MN*NB+1 ), INFO )
  370. *
  371. IF( .NOT.TPSD ) THEN
  372. *
  373. * M > N, A is not transposed:
  374. * Overdetermined system of equations,
  375. * least-squares problem, min || A * X - B ||.
  376. *
  377. * Compute B(1:M,1:NRHS) := Q**T * B(1:M,1:NRHS),
  378. * using the compact WY representation of Q,
  379. * workspace at least NRHS, optimally NRHS*NB.
  380. *
  381. CALL CGEMQRT( 'Left', 'Conjugate transpose', M, NRHS, N, NB,
  382. $ A, LDA, WORK( 1 ), NB, B, LDB,
  383. $ WORK( MN*NB+1 ), INFO )
  384. *
  385. * Compute B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS)
  386. *
  387. CALL CTRTRS( 'Upper', 'No transpose', 'Non-unit', N, NRHS,
  388. $ A, LDA, B, LDB, INFO )
  389. *
  390. IF( INFO.GT.0 ) THEN
  391. RETURN
  392. END IF
  393. *
  394. SCLLEN = N
  395. *
  396. ELSE
  397. *
  398. * M > N, A is transposed:
  399. * Underdetermined system of equations,
  400. * minimum norm solution of A**T * X = B.
  401. *
  402. * Compute B := inv(R**T) * B in two row blocks of B.
  403. *
  404. * Block 1: B(1:N,1:NRHS) := inv(R**T) * B(1:N,1:NRHS)
  405. *
  406. CALL CTRTRS( 'Upper', 'Conjugate transpose', 'Non-unit',
  407. $ N, NRHS, A, LDA, B, LDB, INFO )
  408. *
  409. IF( INFO.GT.0 ) THEN
  410. RETURN
  411. END IF
  412. *
  413. * Block 2: Zero out all rows below the N-th row in B:
  414. * B(N+1:M,1:NRHS) = ZERO
  415. *
  416. DO J = 1, NRHS
  417. DO I = N + 1, M
  418. B( I, J ) = ZERO
  419. END DO
  420. END DO
  421. *
  422. * Compute B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS),
  423. * using the compact WY representation of Q,
  424. * workspace at least NRHS, optimally NRHS*NB.
  425. *
  426. CALL CGEMQRT( 'Left', 'No transpose', M, NRHS, N, NB,
  427. $ A, LDA, WORK( 1 ), NB, B, LDB,
  428. $ WORK( MN*NB+1 ), INFO )
  429. *
  430. SCLLEN = M
  431. *
  432. END IF
  433. *
  434. ELSE
  435. *
  436. * M < N:
  437. * Compute the blocked LQ factorization of A,
  438. * using the compact WY representation of Q,
  439. * workspace at least M, optimally M*NB.
  440. *
  441. CALL CGELQT( M, N, NB, A, LDA, WORK( 1 ), NB,
  442. $ WORK( MN*NB+1 ), INFO )
  443. *
  444. IF( .NOT.TPSD ) THEN
  445. *
  446. * M < N, A is not transposed:
  447. * Underdetermined system of equations,
  448. * minimum norm solution of A * X = B.
  449. *
  450. * Compute B := inv(L) * B in two row blocks of B.
  451. *
  452. * Block 1: B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS)
  453. *
  454. CALL CTRTRS( 'Lower', 'No transpose', 'Non-unit', M, NRHS,
  455. $ A, LDA, B, LDB, INFO )
  456. *
  457. IF( INFO.GT.0 ) THEN
  458. RETURN
  459. END IF
  460. *
  461. * Block 2: Zero out all rows below the M-th row in B:
  462. * B(M+1:N,1:NRHS) = ZERO
  463. *
  464. DO J = 1, NRHS
  465. DO I = M + 1, N
  466. B( I, J ) = ZERO
  467. END DO
  468. END DO
  469. *
  470. * Compute B(1:N,1:NRHS) := Q(1:N,:)**T * B(1:M,1:NRHS),
  471. * using the compact WY representation of Q,
  472. * workspace at least NRHS, optimally NRHS*NB.
  473. *
  474. CALL CGEMLQT( 'Left', 'Conjugate transpose', N, NRHS, M, NB,
  475. $ A, LDA, WORK( 1 ), NB, B, LDB,
  476. $ WORK( MN*NB+1 ), INFO )
  477. *
  478. SCLLEN = N
  479. *
  480. ELSE
  481. *
  482. * M < N, A is transposed:
  483. * Overdetermined system of equations,
  484. * least-squares problem, min || A**T * X - B ||.
  485. *
  486. * Compute B(1:N,1:NRHS) := Q * B(1:N,1:NRHS),
  487. * using the compact WY representation of Q,
  488. * workspace at least NRHS, optimally NRHS*NB.
  489. *
  490. CALL CGEMLQT( 'Left', 'No transpose', N, NRHS, M, NB,
  491. $ A, LDA, WORK( 1 ), NB, B, LDB,
  492. $ WORK( MN*NB+1), INFO )
  493. *
  494. * Compute B(1:M,1:NRHS) := inv(L**T) * B(1:M,1:NRHS)
  495. *
  496. CALL CTRTRS( 'Lower', 'Conjugate transpose', 'Non-unit',
  497. $ M, NRHS, A, LDA, B, LDB, INFO )
  498. *
  499. IF( INFO.GT.0 ) THEN
  500. RETURN
  501. END IF
  502. *
  503. SCLLEN = M
  504. *
  505. END IF
  506. *
  507. END IF
  508. *
  509. * Undo scaling
  510. *
  511. IF( IASCL.EQ.1 ) THEN
  512. CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, SCLLEN, NRHS, B, LDB,
  513. $ INFO )
  514. ELSE IF( IASCL.EQ.2 ) THEN
  515. CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, SCLLEN, NRHS, B, LDB,
  516. $ INFO )
  517. END IF
  518. IF( IBSCL.EQ.1 ) THEN
  519. CALL CLASCL( 'G', 0, 0, SMLNUM, BNRM, SCLLEN, NRHS, B, LDB,
  520. $ INFO )
  521. ELSE IF( IBSCL.EQ.2 ) THEN
  522. CALL CLASCL( 'G', 0, 0, BIGNUM, BNRM, SCLLEN, NRHS, B, LDB,
  523. $ INFO )
  524. END IF
  525. *
  526. WORK( 1 ) = SROUNDUP_LWORK( LWOPT )
  527. *
  528. RETURN
  529. *
  530. * End of CGELST
  531. *
  532. END