1 *> \brief <b> CGELSY solves overdetermined or underdetermined systems for GE matrices</b>
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download CGELSY + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgelsy.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgelsy.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgelsy.f">
21 * SUBROUTINE CGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
22 * WORK, LWORK, RWORK, INFO )
24 * .. Scalar Arguments ..
25 * INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
28 * .. Array Arguments ..
31 * COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
40 *> CGELSY computes the minimum-norm solution to a complex linear least
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.
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
51 *> The routine first computes a QR factorization with column pivoting:
52 *> A * P = Q * [ R11 R12 ]
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.
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
63 *> The minimum-norm solution is then
64 *> X = P * Z**H [ inv(T11)*Q1**H*B ]
66 *> where Q1 consists of the first RANK columns of Q.
68 *> This routine is basically identical to the original xGELSX except
70 *> o The permutation of matrix B (the right hand side) is faster and
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.
84 *> The number of rows of the matrix A. M >= 0.
90 *> The number of columns of the matrix A. N >= 0.
96 *> The number of right hand sides, i.e., the number of
97 *> columns of matrices B and X. NRHS >= 0.
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.
111 *> The leading dimension of the array A. LDA >= max(1,M).
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.
124 *> The leading dimension of the array B. LDB >= max(1,M,N).
127 *> \param[in,out] JPVT
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 A*P
133 *> was the k-th column of A.
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.
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.
155 *> WORK is COMPLEX array, dimension (MAX(1,LWORK))
156 *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
162 *> The dimension of the array WORK.
163 *> The unblocked strategy requires that:
164 *> LWORK >= MN + MAX( 2*MN, N+1, MN+NRHS )
165 *> where MN = min(M,N).
166 *> The block algorithm requires that:
167 *> LWORK >= MN + MAX( 2*MN, NB*(N+1), MN+MN*NB, MN+NB*NRHS )
168 *> where NB is an upper bound on the blocksize returned
169 *> by ILAENV for the routines CGEQP3, CTZRZF, CTZRQF, CUNMQR,
172 *> If LWORK = -1, then a workspace query is assumed; the routine
173 *> only calculates the optimal size of the WORK array, returns
174 *> this value as the first entry of the WORK array, and no error
175 *> message related to LWORK is issued by XERBLA.
180 *> RWORK is REAL array, dimension (2*N)
186 *> = 0: successful exit
187 *> < 0: if INFO = -i, the i-th argument had an illegal value
193 *> \author Univ. of Tennessee
194 *> \author Univ. of California Berkeley
195 *> \author Univ. of Colorado Denver
198 *> \date November 2011
200 *> \ingroup complexGEsolve
202 *> \par Contributors:
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
209 * =====================================================================
210 SUBROUTINE CGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
211 $ WORK, LWORK, RWORK, INFO )
213 * -- LAPACK driver routine (version 3.4.0) --
214 * -- LAPACK is a software package provided by Univ. of Tennessee, --
215 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
218 * .. Scalar Arguments ..
219 INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
222 * .. Array Arguments ..
225 COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
228 * =====================================================================
232 PARAMETER ( IMAX = 1, IMIN = 2 )
234 PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
236 PARAMETER ( CZERO = ( 0.0E+0, 0.0E+0 ),
237 $ CONE = ( 1.0E+0, 0.0E+0 ) )
239 * .. Local Scalars ..
241 INTEGER I, IASCL, IBSCL, ISMAX, ISMIN, J, LWKOPT, MN,
242 $ NB, NB1, NB2, NB3, NB4
243 REAL ANRM, BIGNUM, BNRM, SMAX, SMAXPR, SMIN, SMINPR,
245 COMPLEX C1, C2, S1, S2
247 * .. External Subroutines ..
248 EXTERNAL CCOPY, CGEQP3, CLAIC1, CLASCL, CLASET, CTRSM,
249 $ CTZRZF, CUNMQR, CUNMRZ, SLABAD, XERBLA
251 * .. External Functions ..
254 EXTERNAL CLANGE, ILAENV, SLAMCH
256 * .. Intrinsic Functions ..
257 INTRINSIC ABS, MAX, MIN, REAL, CMPLX
259 * .. Executable Statements ..
265 * Test the input arguments.
268 NB1 = ILAENV( 1, 'CGEQRF', ' ', M, N, -1, -1 )
269 NB2 = ILAENV( 1, 'CGERQF', ' ', M, N, -1, -1 )
270 NB3 = ILAENV( 1, 'CUNMQR', ' ', M, N, NRHS, -1 )
271 NB4 = ILAENV( 1, 'CUNMRQ', ' ', M, N, NRHS, -1 )
272 NB = MAX( NB1, NB2, NB3, NB4 )
273 LWKOPT = MAX( 1, MN+2*N+NB*(N+1), 2*MN+NB*NRHS )
274 WORK( 1 ) = CMPLX( LWKOPT )
275 LQUERY = ( LWORK.EQ.-1 )
278 ELSE IF( N.LT.0 ) THEN
280 ELSE IF( NRHS.LT.0 ) THEN
282 ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
284 ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
286 ELSE IF( LWORK.LT.( MN+MAX( 2*MN, N+1, MN+NRHS ) ) .AND.
292 CALL XERBLA( 'CGELSY', -INFO )
294 ELSE IF( LQUERY ) THEN
298 * Quick return if possible
300 IF( MIN( M, N, NRHS ).EQ.0 ) THEN
305 * Get machine parameters
307 SMLNUM = SLAMCH( 'S' ) / SLAMCH( 'P' )
308 BIGNUM = ONE / SMLNUM
309 CALL SLABAD( SMLNUM, BIGNUM )
311 * Scale A, B if max entries outside range [SMLNUM,BIGNUM]
313 ANRM = CLANGE( 'M', M, N, A, LDA, RWORK )
315 IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
317 * Scale matrix norm up to SMLNUM
319 CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
321 ELSE IF( ANRM.GT.BIGNUM ) THEN
323 * Scale matrix norm down to BIGNUM
325 CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
327 ELSE IF( ANRM.EQ.ZERO ) THEN
329 * Matrix all zero. Return zero solution.
331 CALL CLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
336 BNRM = CLANGE( 'M', M, NRHS, B, LDB, RWORK )
338 IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
340 * Scale matrix norm up to SMLNUM
342 CALL CLASCL( 'G', 0, 0, BNRM, SMLNUM, M, NRHS, B, LDB, INFO )
344 ELSE IF( BNRM.GT.BIGNUM ) THEN
346 * Scale matrix norm down to BIGNUM
348 CALL CLASCL( 'G', 0, 0, BNRM, BIGNUM, M, NRHS, B, LDB, INFO )
352 * Compute QR factorization with column pivoting of A:
355 CALL CGEQP3( M, N, A, LDA, JPVT, WORK( 1 ), WORK( MN+1 ),
356 $ LWORK-MN, RWORK, INFO )
357 WSIZE = MN + REAL( WORK( MN+1 ) )
359 * complex workspace: MN+NB*(N+1). real workspace 2*N.
360 * Details of Householder rotations stored in WORK(1:MN).
362 * Determine RANK using incremental condition estimation
366 SMAX = ABS( A( 1, 1 ) )
368 IF( ABS( A( 1, 1 ) ).EQ.ZERO ) THEN
370 CALL CLASET( 'F', MAX( M, N ), NRHS, CZERO, CZERO, B, LDB )
377 IF( RANK.LT.MN ) THEN
379 CALL CLAIC1( IMIN, RANK, WORK( ISMIN ), SMIN, A( 1, I ),
380 $ A( I, I ), SMINPR, S1, C1 )
381 CALL CLAIC1( IMAX, RANK, WORK( ISMAX ), SMAX, A( 1, I ),
382 $ A( I, I ), SMAXPR, S2, C2 )
384 IF( SMAXPR*RCOND.LE.SMINPR ) THEN
386 WORK( ISMIN+I-1 ) = S1*WORK( ISMIN+I-1 )
387 WORK( ISMAX+I-1 ) = S2*WORK( ISMAX+I-1 )
389 WORK( ISMIN+RANK ) = C1
390 WORK( ISMAX+RANK ) = C2
398 * complex workspace: 3*MN.
400 * Logically partition R = [ R11 R12 ]
402 * where R11 = R(1:RANK,1:RANK)
404 * [R11,R12] = [ T11, 0 ] * Y
407 $ CALL CTZRZF( RANK, N, A, LDA, WORK( MN+1 ), WORK( 2*MN+1 ),
410 * complex workspace: 2*MN.
411 * Details of Householder rotations stored in WORK(MN+1:2*MN)
413 * B(1:M,1:NRHS) := Q**H * B(1:M,1:NRHS)
415 CALL CUNMQR( 'Left', 'Conjugate transpose', M, NRHS, MN, A, LDA,
416 $ WORK( 1 ), B, LDB, WORK( 2*MN+1 ), LWORK-2*MN, INFO )
417 WSIZE = MAX( WSIZE, 2*MN+REAL( WORK( 2*MN+1 ) ) )
419 * complex workspace: 2*MN+NB*NRHS.
421 * B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS)
423 CALL CTRSM( 'Left', 'Upper', 'No transpose', 'Non-unit', RANK,
424 $ NRHS, CONE, A, LDA, B, LDB )
427 DO 30 I = RANK + 1, N
432 * B(1:N,1:NRHS) := Y**H * B(1:N,1:NRHS)
435 CALL CUNMRZ( 'Left', 'Conjugate transpose', N, NRHS, RANK,
436 $ N-RANK, A, LDA, WORK( MN+1 ), B, LDB,
437 $ WORK( 2*MN+1 ), LWORK-2*MN, INFO )
440 * complex workspace: 2*MN+NRHS.
442 * B(1:N,1:NRHS) := P * B(1:N,1:NRHS)
446 WORK( JPVT( I ) ) = B( I, J )
448 CALL CCOPY( N, WORK( 1 ), 1, B( 1, J ), 1 )
451 * complex workspace: N.
455 IF( IASCL.EQ.1 ) THEN
456 CALL CLASCL( 'G', 0, 0, ANRM, SMLNUM, N, NRHS, B, LDB, INFO )
457 CALL CLASCL( 'U', 0, 0, SMLNUM, ANRM, RANK, RANK, A, LDA,
459 ELSE IF( IASCL.EQ.2 ) THEN
460 CALL CLASCL( 'G', 0, 0, ANRM, BIGNUM, N, NRHS, B, LDB, INFO )
461 CALL CLASCL( 'U', 0, 0, BIGNUM, ANRM, RANK, RANK, A, LDA,
464 IF( IBSCL.EQ.1 ) THEN
465 CALL CLASCL( 'G', 0, 0, SMLNUM, BNRM, N, NRHS, B, LDB, INFO )
466 ELSE IF( IBSCL.EQ.2 ) THEN
467 CALL CLASCL( 'G', 0, 0, BIGNUM, BNRM, N, NRHS, B, LDB, INFO )
471 WORK( 1 ) = CMPLX( LWKOPT )