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.

dgelsy.f 15 kB

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