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.

dgeqr.f 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. *> \brief \b DGEQR
  2. *
  3. * Definition:
  4. * ===========
  5. *
  6. * SUBROUTINE DGEQR( M, N, A, LDA, T, TSIZE, WORK, LWORK,
  7. * INFO )
  8. *
  9. * .. Scalar Arguments ..
  10. * INTEGER INFO, LDA, M, N, TSIZE, LWORK
  11. * ..
  12. * .. Array Arguments ..
  13. * DOUBLE PRECISION A( LDA, * ), T( * ), WORK( * )
  14. * ..
  15. *
  16. *
  17. *> \par Purpose:
  18. * =============
  19. *>
  20. *> \verbatim
  21. *>
  22. *> DGEQR computes a QR factorization of a real M-by-N matrix A:
  23. *>
  24. *> A = Q * ( R ),
  25. *> ( 0 )
  26. *>
  27. *> where:
  28. *>
  29. *> Q is a M-by-M orthogonal matrix;
  30. *> R is an upper-triangular N-by-N matrix;
  31. *> 0 is a (M-N)-by-N zero matrix, if M > N.
  32. *>
  33. *> \endverbatim
  34. *
  35. * Arguments:
  36. * ==========
  37. *
  38. *> \param[in] M
  39. *> \verbatim
  40. *> M is INTEGER
  41. *> The number of rows of the matrix A. M >= 0.
  42. *> \endverbatim
  43. *>
  44. *> \param[in] N
  45. *> \verbatim
  46. *> N is INTEGER
  47. *> The number of columns of the matrix A. N >= 0.
  48. *> \endverbatim
  49. *>
  50. *> \param[in,out] A
  51. *> \verbatim
  52. *> A is DOUBLE PRECISION array, dimension (LDA,N)
  53. *> On entry, the M-by-N matrix A.
  54. *> On exit, the elements on and above the diagonal of the array
  55. *> contain the min(M,N)-by-N upper trapezoidal matrix R
  56. *> (R is upper triangular if M >= N);
  57. *> the elements below the diagonal are used to store part of the
  58. *> data structure to represent Q.
  59. *> \endverbatim
  60. *>
  61. *> \param[in] LDA
  62. *> \verbatim
  63. *> LDA is INTEGER
  64. *> The leading dimension of the array A. LDA >= max(1,M).
  65. *> \endverbatim
  66. *>
  67. *> \param[out] T
  68. *> \verbatim
  69. *> T is DOUBLE PRECISION array, dimension (MAX(5,TSIZE))
  70. *> On exit, if INFO = 0, T(1) returns optimal (or either minimal
  71. *> or optimal, if query is assumed) TSIZE. See TSIZE for details.
  72. *> Remaining T contains part of the data structure used to represent Q.
  73. *> If one wants to apply or construct Q, then one needs to keep T
  74. *> (in addition to A) and pass it to further subroutines.
  75. *> \endverbatim
  76. *>
  77. *> \param[in] TSIZE
  78. *> \verbatim
  79. *> TSIZE is INTEGER
  80. *> If TSIZE >= 5, the dimension of the array T.
  81. *> If TSIZE = -1 or -2, then a workspace query is assumed. The routine
  82. *> only calculates the sizes of the T and WORK arrays, returns these
  83. *> values as the first entries of the T and WORK arrays, and no error
  84. *> message related to T or WORK is issued by XERBLA.
  85. *> If TSIZE = -1, the routine calculates optimal size of T for the
  86. *> optimum performance and returns this value in T(1).
  87. *> If TSIZE = -2, the routine calculates minimal size of T and
  88. *> returns this value in T(1).
  89. *> \endverbatim
  90. *>
  91. *> \param[out] WORK
  92. *> \verbatim
  93. *> (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
  94. *> On exit, if INFO = 0, WORK(1) contains optimal (or either minimal
  95. *> or optimal, if query was assumed) LWORK.
  96. *> See LWORK for details.
  97. *> \endverbatim
  98. *>
  99. *> \param[in] LWORK
  100. *> \verbatim
  101. *> LWORK is INTEGER
  102. *> The dimension of the array WORK. LWORK >= 1.
  103. *> If LWORK = -1 or -2, then a workspace query is assumed. The routine
  104. *> only calculates the sizes of the T and WORK arrays, returns these
  105. *> values as the first entries of the T and WORK arrays, and no error
  106. *> message related to T or WORK is issued by XERBLA.
  107. *> If LWORK = -1, the routine calculates optimal size of WORK for the
  108. *> optimal performance and returns this value in WORK(1).
  109. *> If LWORK = -2, the routine calculates minimal size of WORK and
  110. *> returns this value in WORK(1).
  111. *> \endverbatim
  112. *>
  113. *> \param[out] INFO
  114. *> \verbatim
  115. *> INFO is INTEGER
  116. *> = 0: successful exit
  117. *> < 0: if INFO = -i, the i-th argument had an illegal value
  118. *> \endverbatim
  119. *
  120. * Authors:
  121. * ========
  122. *
  123. *> \author Univ. of Tennessee
  124. *> \author Univ. of California Berkeley
  125. *> \author Univ. of Colorado Denver
  126. *> \author NAG Ltd.
  127. *
  128. *> \par Further Details
  129. * ====================
  130. *>
  131. *> \verbatim
  132. *>
  133. *> The goal of the interface is to give maximum freedom to the developers for
  134. *> creating any QR factorization algorithm they wish. The triangular
  135. *> (trapezoidal) R has to be stored in the upper part of A. The lower part of A
  136. *> and the array T can be used to store any relevant information for applying or
  137. *> constructing the Q factor. The WORK array can safely be discarded after exit.
  138. *>
  139. *> Caution: One should not expect the sizes of T and WORK to be the same from one
  140. *> LAPACK implementation to the other, or even from one execution to the other.
  141. *> A workspace query (for T and WORK) is needed at each execution. However,
  142. *> for a given execution, the size of T and WORK are fixed and will not change
  143. *> from one query to the next.
  144. *>
  145. *> \endverbatim
  146. *>
  147. *> \par Further Details particular to this LAPACK implementation:
  148. * ==============================================================
  149. *>
  150. *> \verbatim
  151. *>
  152. *> These details are particular for this LAPACK implementation. Users should not
  153. *> take them for granted. These details may change in the future, and are not likely
  154. *> true for another LAPACK implementation. These details are relevant if one wants
  155. *> to try to understand the code. They are not part of the interface.
  156. *>
  157. *> In this version,
  158. *>
  159. *> T(2): row block size (MB)
  160. *> T(3): column block size (NB)
  161. *> T(6:TSIZE): data structure needed for Q, computed by
  162. *> DLATSQR or DGEQRT
  163. *>
  164. *> Depending on the matrix dimensions M and N, and row and column
  165. *> block sizes MB and NB returned by ILAENV, DGEQR will use either
  166. *> DLATSQR (if the matrix is tall-and-skinny) or DGEQRT to compute
  167. *> the QR factorization.
  168. *>
  169. *> \endverbatim
  170. *>
  171. *> \ingroup geqr
  172. *>
  173. * =====================================================================
  174. SUBROUTINE DGEQR( M, N, A, LDA, T, TSIZE, WORK, LWORK,
  175. $ INFO )
  176. *
  177. * -- LAPACK computational routine --
  178. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  179. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd. --
  180. *
  181. * .. Scalar Arguments ..
  182. INTEGER INFO, LDA, M, N, TSIZE, LWORK
  183. * ..
  184. * .. Array Arguments ..
  185. DOUBLE PRECISION A( LDA, * ), T( * ), WORK( * )
  186. * ..
  187. *
  188. * =====================================================================
  189. *
  190. * ..
  191. * .. Local Scalars ..
  192. LOGICAL LQUERY, LMINWS, MINT, MINW
  193. INTEGER MB, NB, MINTSZ, NBLCKS, LWMIN, LWREQ
  194. * ..
  195. * .. External Functions ..
  196. LOGICAL LSAME
  197. EXTERNAL LSAME
  198. * ..
  199. * .. External Subroutines ..
  200. EXTERNAL DLATSQR, DGEQRT, XERBLA
  201. * ..
  202. * .. Intrinsic Functions ..
  203. INTRINSIC MAX, MIN, MOD
  204. * ..
  205. * .. External Functions ..
  206. INTEGER ILAENV
  207. EXTERNAL ILAENV
  208. * ..
  209. * .. Executable Statements ..
  210. *
  211. * Test the input arguments
  212. *
  213. INFO = 0
  214. *
  215. LQUERY = ( TSIZE.EQ.-1 .OR. TSIZE.EQ.-2 .OR.
  216. $ LWORK.EQ.-1 .OR. LWORK.EQ.-2 )
  217. *
  218. MINT = .FALSE.
  219. MINW = .FALSE.
  220. IF( TSIZE.EQ.-2 .OR. LWORK.EQ.-2 ) THEN
  221. IF( TSIZE.NE.-1 ) MINT = .TRUE.
  222. IF( LWORK.NE.-1 ) MINW = .TRUE.
  223. END IF
  224. *
  225. * Determine the block size
  226. *
  227. IF( MIN( M, N ).GT.0 ) THEN
  228. MB = ILAENV( 1, 'DGEQR ', ' ', M, N, 1, -1 )
  229. NB = ILAENV( 1, 'DGEQR ', ' ', M, N, 2, -1 )
  230. ELSE
  231. MB = M
  232. NB = 1
  233. END IF
  234. IF( MB.GT.M .OR. MB.LE.N ) MB = M
  235. IF( NB.GT.MIN( M, N ) .OR. NB.LT.1 ) NB = 1
  236. MINTSZ = N + 5
  237. IF( MB.GT.N .AND. M.GT.N ) THEN
  238. IF( MOD( M - N, MB - N ).EQ.0 ) THEN
  239. NBLCKS = ( M - N ) / ( MB - N )
  240. ELSE
  241. NBLCKS = ( M - N ) / ( MB - N ) + 1
  242. END IF
  243. ELSE
  244. NBLCKS = 1
  245. END IF
  246. *
  247. * Determine if the workspace size satisfies minimal size
  248. *
  249. LWMIN = MAX( 1, N )
  250. LWREQ = MAX( 1, N*NB )
  251. LMINWS = .FALSE.
  252. IF( ( TSIZE.LT.MAX( 1, NB*N*NBLCKS + 5 ) .OR. LWORK.LT.LWREQ )
  253. $ .AND. ( LWORK.GE.N ) .AND. ( TSIZE.GE.MINTSZ )
  254. $ .AND. ( .NOT.LQUERY ) ) THEN
  255. IF( TSIZE.LT.MAX( 1, NB*N*NBLCKS + 5 ) ) THEN
  256. LMINWS = .TRUE.
  257. NB = 1
  258. MB = M
  259. END IF
  260. IF( LWORK.LT.LWREQ ) THEN
  261. LMINWS = .TRUE.
  262. NB = 1
  263. END IF
  264. END IF
  265. *
  266. IF( M.LT.0 ) THEN
  267. INFO = -1
  268. ELSE IF( N.LT.0 ) THEN
  269. INFO = -2
  270. ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
  271. INFO = -4
  272. ELSE IF( TSIZE.LT.MAX( 1, NB*N*NBLCKS + 5 )
  273. $ .AND. ( .NOT.LQUERY ) .AND. ( .NOT.LMINWS ) ) THEN
  274. INFO = -6
  275. ELSE IF( ( LWORK.LT.LWREQ ) .AND. ( .NOT.LQUERY )
  276. $ .AND. ( .NOT.LMINWS ) ) THEN
  277. INFO = -8
  278. END IF
  279. *
  280. IF( INFO.EQ.0 ) THEN
  281. IF( MINT ) THEN
  282. T( 1 ) = MINTSZ
  283. ELSE
  284. T( 1 ) = NB*N*NBLCKS + 5
  285. END IF
  286. T( 2 ) = MB
  287. T( 3 ) = NB
  288. IF( MINW ) THEN
  289. WORK( 1 ) = LWMIN
  290. ELSE
  291. WORK( 1 ) = LWREQ
  292. END IF
  293. END IF
  294. IF( INFO.NE.0 ) THEN
  295. CALL XERBLA( 'DGEQR', -INFO )
  296. RETURN
  297. ELSE IF( LQUERY ) THEN
  298. RETURN
  299. END IF
  300. *
  301. * Quick return if possible
  302. *
  303. IF( MIN( M, N ).EQ.0 ) THEN
  304. RETURN
  305. END IF
  306. *
  307. * The QR Decomposition
  308. *
  309. IF( ( M.LE.N ) .OR. ( MB.LE.N ) .OR. ( MB.GE.M ) ) THEN
  310. CALL DGEQRT( M, N, NB, A, LDA, T( 6 ), NB, WORK, INFO )
  311. ELSE
  312. CALL DLATSQR( M, N, MB, NB, A, LDA, T( 6 ), NB, WORK,
  313. $ LWORK, INFO )
  314. END IF
  315. *
  316. WORK( 1 ) = LWREQ
  317. *
  318. RETURN
  319. *
  320. * End of DGEQR
  321. *
  322. END