LAPACK 3.12.1
LAPACK: Linear Algebra PACKage
Loading...
Searching...
No Matches
cgelsy.f
Go to the documentation of this file.
1*> \brief <b> CGELSY 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*> Download CGELSY + dependencies
9*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgelsy.f">
10*> [TGZ]</a>
11*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgelsy.f">
12*> [ZIP]</a>
13*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgelsy.f">
14*> [TXT]</a>
15*
16* Definition:
17* ===========
18*
19* SUBROUTINE CGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
20* WORK, LWORK, RWORK, INFO )
21*
22* .. Scalar Arguments ..
23* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
24* REAL RCOND
25* ..
26* .. Array Arguments ..
27* INTEGER JPVT( * )
28* REAL RWORK( * )
29* COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
30* ..
31*
32*
33*> \par Purpose:
34* =============
35*>
36*> \verbatim
37*>
38*> CGELSY computes the minimum-norm solution to a complex linear least
39*> squares problem:
40*> minimize || A * X - B ||
41*> using a complete orthogonal factorization of A. A is an M-by-N
42*> matrix which may be rank-deficient.
43*>
44*> Several right hand side vectors b and solution vectors x can be
45*> handled in a single call; they are stored as the columns of the
46*> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
47*> matrix X.
48*>
49*> The routine first computes a QR factorization with column pivoting:
50*> A * P = Q * [ R11 R12 ]
51*> [ 0 R22 ]
52*> with R11 defined as the largest leading submatrix whose estimated
53*> condition number is less than 1/RCOND. The order of R11, RANK,
54*> is the effective rank of A.
55*>
56*> Then, R22 is considered to be negligible, and R12 is annihilated
57*> by unitary transformations from the right, arriving at the
58*> complete orthogonal factorization:
59*> A * P = Q * [ T11 0 ] * Z
60*> [ 0 0 ]
61*> The minimum-norm solution is then
62*> X = P * Z**H [ inv(T11)*Q1**H*B ]
63*> [ 0 ]
64*> where Q1 consists of the first RANK columns of Q.
65*>
66*> This routine is basically identical to the original xGELSX except
67*> three differences:
68*> o The permutation of matrix B (the right hand side) is faster and
69*> more simple.
70*> o The call to the subroutine xGEQPF has been substituted by the
71*> the call to the subroutine xGEQP3. This subroutine is a Blas-3
72*> version of the QR factorization with column pivoting.
73*> o Matrix B (the right hand side) is updated with Blas-3.
74*> \endverbatim
75*
76* Arguments:
77* ==========
78*
79*> \param[in] M
80*> \verbatim
81*> M is INTEGER
82*> The number of rows of the matrix A. M >= 0.
83*> \endverbatim
84*>
85*> \param[in] N
86*> \verbatim
87*> N is INTEGER
88*> The number of columns of the matrix A. N >= 0.
89*> \endverbatim
90*>
91*> \param[in] NRHS
92*> \verbatim
93*> NRHS is INTEGER
94*> The number of right hand sides, i.e., the number of
95*> columns of matrices B and X. NRHS >= 0.
96*> \endverbatim
97*>
98*> \param[in,out] A
99*> \verbatim
100*> A is COMPLEX array, dimension (LDA,N)
101*> On entry, the M-by-N matrix A.
102*> On exit, A has been overwritten by details of its
103*> complete orthogonal factorization.
104*> \endverbatim
105*>
106*> \param[in] LDA
107*> \verbatim
108*> LDA is INTEGER
109*> The leading dimension of the array A. LDA >= max(1,M).
110*> \endverbatim
111*>
112*> \param[in,out] B
113*> \verbatim
114*> B is COMPLEX array, dimension (LDB,NRHS)
115*> On entry, the M-by-NRHS right hand side matrix B.
116*> On exit, the N-by-NRHS solution matrix X.
117*> If M = 0 or N = 0, B is not referenced.
118*> \endverbatim
119*>
120*> \param[in] LDB
121*> \verbatim
122*> LDB is INTEGER
123*> The leading dimension of the array B. LDB >= max(1,M,N).
124*> \endverbatim
125*>
126*> \param[in,out] JPVT
127*> \verbatim
128*> JPVT is INTEGER array, dimension (N)
129*> On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted
130*> to the front of AP, otherwise column i is a free column.
131*> On exit, if JPVT(i) = k, then the i-th column of A*P
132*> was the k-th column of A.
133*> \endverbatim
134*>
135*> \param[in] RCOND
136*> \verbatim
137*> RCOND is REAL
138*> RCOND is used to determine the effective rank of A, which
139*> is defined as the order of the largest leading triangular
140*> submatrix R11 in the QR factorization with pivoting of A,
141*> whose estimated condition number < 1/RCOND.
142*> \endverbatim
143*>
144*> \param[out] RANK
145*> \verbatim
146*> RANK is INTEGER
147*> The effective rank of A, i.e., the order of the submatrix
148*> R11. This is the same as the order of the submatrix T11
149*> in the complete orthogonal factorization of A.
150*> If NRHS = 0, RANK = 0 on output.
151*> \endverbatim
152*>
153*> \param[out] WORK
154*> \verbatim
155*> WORK is COMPLEX array, dimension (MAX(1,LWORK))
156*> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
157*> \endverbatim
158*>
159*> \param[in] LWORK
160*> \verbatim
161*> LWORK is INTEGER
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,
170*> and CUNMRZ.
171*>
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.
176*> \endverbatim
177*>
178*> \param[out] RWORK
179*> \verbatim
180*> RWORK is REAL array, dimension (2*N)
181*> \endverbatim
182*>
183*> \param[out] INFO
184*> \verbatim
185*> INFO is INTEGER
186*> = 0: successful exit
187*> < 0: if INFO = -i, the i-th argument had an illegal value
188*> \endverbatim
189*
190* Authors:
191* ========
192*
193*> \author Univ. of Tennessee
194*> \author Univ. of California Berkeley
195*> \author Univ. of Colorado Denver
196*> \author NAG Ltd.
197*
198*> \ingroup gelsy
199*
200*> \par Contributors:
201* ==================
202*>
203*> A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA \n
204*> E. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n
205*> G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n
206*>
207* =====================================================================
208 SUBROUTINE cgelsy( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND,
209 $ RANK,
210 $ WORK, LWORK, RWORK, INFO )
211*
212* -- LAPACK driver routine --
213* -- LAPACK is a software package provided by Univ. of Tennessee, --
214* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
215*
216* .. Scalar Arguments ..
217 INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
218 REAL RCOND
219* ..
220* .. Array Arguments ..
221 INTEGER JPVT( * )
222 REAL RWORK( * )
223 COMPLEX A( LDA, * ), B( LDB, * ), WORK( * )
224* ..
225*
226* =====================================================================
227*
228* .. Parameters ..
229 INTEGER IMAX, IMIN
230 PARAMETER ( IMAX = 1, imin = 2 )
231 REAL ZERO, ONE
232 parameter( zero = 0.0e+0, one = 1.0e+0 )
233 COMPLEX CZERO, CONE
234 parameter( czero = ( 0.0e+0, 0.0e+0 ),
235 $ cone = ( 1.0e+0, 0.0e+0 ) )
236* ..
237* .. Local Scalars ..
238 LOGICAL LQUERY
239 INTEGER I, IASCL, IBSCL, ISMAX, ISMIN, J, LWKOPT, MN,
240 $ nb, nb1, nb2, nb3, nb4
241 REAL ANRM, BIGNUM, BNRM, SMAX, SMAXPR, SMIN, SMINPR,
242 $ SMLNUM, WSIZE
243 COMPLEX C1, C2, S1, S2
244* ..
245* .. External Subroutines ..
246 EXTERNAL ccopy, cgeqp3, claic1, clascl, claset,
247 $ ctrsm,
249* ..
250* .. External Functions ..
251 INTEGER ILAENV
252 REAL CLANGE, SLAMCH
253 EXTERNAL clange, ilaenv, slamch
254* ..
255* .. Intrinsic Functions ..
256 INTRINSIC abs, max, min, real, cmplx
257* ..
258* .. Executable Statements ..
259*
260 mn = min( m, n )
261 ismin = mn + 1
262 ismax = 2*mn + 1
263*
264* Test the input arguments.
265*
266 info = 0
267 nb1 = ilaenv( 1, 'CGEQRF', ' ', m, n, -1, -1 )
268 nb2 = ilaenv( 1, 'CGERQF', ' ', m, n, -1, -1 )
269 nb3 = ilaenv( 1, 'CUNMQR', ' ', m, n, nrhs, -1 )
270 nb4 = ilaenv( 1, 'CUNMRQ', ' ', m, n, nrhs, -1 )
271 nb = max( nb1, nb2, nb3, nb4 )
272 lwkopt = max( 1, mn+2*n+nb*(n+1), 2*mn+nb*nrhs )
273 work( 1 ) = cmplx( lwkopt )
274 lquery = ( lwork.EQ.-1 )
275 IF( m.LT.0 ) THEN
276 info = -1
277 ELSE IF( n.LT.0 ) THEN
278 info = -2
279 ELSE IF( nrhs.LT.0 ) THEN
280 info = -3
281 ELSE IF( lda.LT.max( 1, m ) ) THEN
282 info = -5
283 ELSE IF( ldb.LT.max( 1, m, n ) ) THEN
284 info = -7
285 ELSE IF( lwork.LT.( mn+max( 2*mn, n+1, mn+nrhs ) ) .AND.
286 $ .NOT.lquery ) THEN
287 info = -12
288 END IF
289*
290 IF( info.NE.0 ) THEN
291 CALL xerbla( 'CGELSY', -info )
292 RETURN
293 ELSE IF( lquery ) THEN
294 RETURN
295 END IF
296*
297* Quick return if possible
298*
299 IF( min( m, n, nrhs ).EQ.0 ) THEN
300 rank = 0
301 RETURN
302 END IF
303*
304* Get machine parameters
305*
306 smlnum = slamch( 'S' ) / slamch( 'P' )
307 bignum = one / smlnum
308*
309* Scale A, B if max entries outside range [SMLNUM,BIGNUM]
310*
311 anrm = clange( 'M', m, n, a, lda, rwork )
312 iascl = 0
313 IF( anrm.GT.zero .AND. anrm.LT.smlnum ) THEN
314*
315* Scale matrix norm up to SMLNUM
316*
317 CALL clascl( 'G', 0, 0, anrm, smlnum, m, n, a, lda, info )
318 iascl = 1
319 ELSE IF( anrm.GT.bignum ) THEN
320*
321* Scale matrix norm down to BIGNUM
322*
323 CALL clascl( 'G', 0, 0, anrm, bignum, m, n, a, lda, info )
324 iascl = 2
325 ELSE IF( anrm.EQ.zero ) THEN
326*
327* Matrix all zero. Return zero solution.
328*
329 CALL claset( 'F', max( m, n ), nrhs, czero, czero, b, ldb )
330 rank = 0
331 GO TO 70
332 END IF
333*
334 bnrm = clange( 'M', m, nrhs, b, ldb, rwork )
335 ibscl = 0
336 IF( bnrm.GT.zero .AND. bnrm.LT.smlnum ) THEN
337*
338* Scale matrix norm up to SMLNUM
339*
340 CALL clascl( 'G', 0, 0, bnrm, smlnum, m, nrhs, b, ldb,
341 $ info )
342 ibscl = 1
343 ELSE IF( bnrm.GT.bignum ) THEN
344*
345* Scale matrix norm down to BIGNUM
346*
347 CALL clascl( 'G', 0, 0, bnrm, bignum, m, nrhs, b, ldb,
348 $ info )
349 ibscl = 2
350 END IF
351*
352* Compute QR factorization with column pivoting of A:
353* A * P = Q * R
354*
355 CALL cgeqp3( m, n, a, lda, jpvt, work( 1 ), work( mn+1 ),
356 $ lwork-mn, rwork, info )
357 wsize = real( mn ) + real( work( mn+1 ) )
358*
359* complex workspace: MN+NB*(N+1). real workspace 2*N.
360* Details of Householder rotations stored in WORK(1:MN).
361*
362* Determine RANK using incremental condition estimation
363*
364 work( ismin ) = cone
365 work( ismax ) = cone
366 smax = abs( a( 1, 1 ) )
367 smin = smax
368 IF( abs( a( 1, 1 ) ).EQ.zero ) THEN
369 rank = 0
370 CALL claset( 'F', max( m, n ), nrhs, czero, czero, b, ldb )
371 GO TO 70
372 ELSE
373 rank = 1
374 END IF
375*
376 10 CONTINUE
377 IF( rank.LT.mn ) THEN
378 i = rank + 1
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 )
383*
384 IF( smaxpr*rcond.LE.sminpr ) THEN
385 DO 20 i = 1, rank
386 work( ismin+i-1 ) = s1*work( ismin+i-1 )
387 work( ismax+i-1 ) = s2*work( ismax+i-1 )
388 20 CONTINUE
389 work( ismin+rank ) = c1
390 work( ismax+rank ) = c2
391 smin = sminpr
392 smax = smaxpr
393 rank = rank + 1
394 GO TO 10
395 END IF
396 END IF
397*
398* complex workspace: 3*MN.
399*
400* Logically partition R = [ R11 R12 ]
401* [ 0 R22 ]
402* where R11 = R(1:RANK,1:RANK)
403*
404* [R11,R12] = [ T11, 0 ] * Y
405*
406 IF( rank.LT.n )
407 $ CALL ctzrzf( rank, n, a, lda, work( mn+1 ), work( 2*mn+1 ),
408 $ lwork-2*mn, info )
409*
410* complex workspace: 2*MN.
411* Details of Householder rotations stored in WORK(MN+1:2*MN)
412*
413* B(1:M,1:NRHS) := Q**H * B(1:M,1:NRHS)
414*
415 CALL cunmqr( 'Left', 'Conjugate transpose', m, nrhs, mn, a,
416 $ lda,
417 $ work( 1 ), b, ldb, work( 2*mn+1 ), lwork-2*mn, info )
418 wsize = max( wsize, real( 2*mn )+real( work( 2*mn+1 ) ) )
419*
420* complex workspace: 2*MN+NB*NRHS.
421*
422* B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS)
423*
424 CALL ctrsm( 'Left', 'Upper', 'No transpose', 'Non-unit', rank,
425 $ nrhs, cone, a, lda, b, ldb )
426*
427 DO 40 j = 1, nrhs
428 DO 30 i = rank + 1, n
429 b( i, j ) = czero
430 30 CONTINUE
431 40 CONTINUE
432*
433* B(1:N,1:NRHS) := Y**H * B(1:N,1:NRHS)
434*
435 IF( rank.LT.n ) THEN
436 CALL cunmrz( 'Left', 'Conjugate transpose', n, nrhs, rank,
437 $ n-rank, a, lda, work( mn+1 ), b, ldb,
438 $ work( 2*mn+1 ), lwork-2*mn, info )
439 END IF
440*
441* complex 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 ccopy( n, work( 1 ), 1, b( 1, j ), 1 )
450 60 CONTINUE
451*
452* complex workspace: N.
453*
454* Undo scaling
455*
456 IF( iascl.EQ.1 ) THEN
457 CALL clascl( 'G', 0, 0, anrm, smlnum, n, nrhs, b, ldb,
458 $ info )
459 CALL clascl( 'U', 0, 0, smlnum, anrm, rank, rank, a, lda,
460 $ info )
461 ELSE IF( iascl.EQ.2 ) THEN
462 CALL clascl( 'G', 0, 0, anrm, bignum, n, nrhs, b, ldb,
463 $ info )
464 CALL clascl( 'U', 0, 0, bignum, anrm, rank, rank, a, lda,
465 $ info )
466 END IF
467 IF( ibscl.EQ.1 ) THEN
468 CALL clascl( 'G', 0, 0, smlnum, bnrm, n, nrhs, b, ldb,
469 $ info )
470 ELSE IF( ibscl.EQ.2 ) THEN
471 CALL clascl( 'G', 0, 0, bignum, bnrm, n, nrhs, b, ldb,
472 $ info )
473 END IF
474*
475 70 CONTINUE
476 work( 1 ) = cmplx( lwkopt )
477*
478 RETURN
479*
480* End of CGELSY
481*
482 END
subroutine xerbla(srname, info)
Definition cblat2.f:3285
subroutine ccopy(n, cx, incx, cy, incy)
CCOPY
Definition ccopy.f:81
subroutine cgelsy(m, n, nrhs, a, lda, b, ldb, jpvt, rcond, rank, work, lwork, rwork, info)
CGELSY solves overdetermined or underdetermined systems for GE matrices
Definition cgelsy.f:211
subroutine cgeqp3(m, n, a, lda, jpvt, tau, work, lwork, rwork, info)
CGEQP3
Definition cgeqp3.f:157
subroutine claic1(job, j, x, sest, w, gamma, sestpr, s, c)
CLAIC1 applies one step of incremental condition estimation.
Definition claic1.f:133
subroutine clascl(type, kl, ku, cfrom, cto, m, n, a, lda, info)
CLASCL multiplies a general rectangular matrix by a real scalar defined as cto/cfrom.
Definition clascl.f:142
subroutine claset(uplo, m, n, alpha, beta, a, lda)
CLASET initializes the off-diagonal elements and the diagonal elements of a matrix to given values.
Definition claset.f:104
subroutine ctrsm(side, uplo, transa, diag, m, n, alpha, a, lda, b, ldb)
CTRSM
Definition ctrsm.f:180
subroutine ctzrzf(m, n, a, lda, tau, work, lwork, info)
CTZRZF
Definition ctzrzf.f:149
subroutine cunmqr(side, trans, m, n, k, a, lda, tau, c, ldc, work, lwork, info)
CUNMQR
Definition cunmqr.f:166
subroutine cunmrz(side, trans, m, n, k, l, a, lda, tau, c, ldc, work, lwork, info)
CUNMRZ
Definition cunmrz.f:186