OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
zgetsls.f
Go to the documentation of this file.
1*> \brief \b ZGETSLS
2*
3* Definition:
4* ===========
5*
6* SUBROUTINE ZGETSLS( TRANS, M, N, NRHS, A, LDA, B, LDB,
7* $ WORK, LWORK, INFO )
8*
9* .. Scalar Arguments ..
10* CHARACTER TRANS
11* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
12* ..
13* .. Array Arguments ..
14* COMPLEX*16 A( LDA, * ), B( LDB, * ), WORK( * )
15* ..
16*
17*
18*> \par Purpose:
19* =============
20*>
21*> \verbatim
22*>
23*> ZGETSLS solves overdetermined or underdetermined complex linear systems
24*> involving an M-by-N matrix A, using a tall skinny QR or short wide LQ
25*> factorization of A. It is assumed that A has full rank.
26*>
27*>
28*>
29*> The following options are provided:
30*>
31*> 1. If TRANS = 'N' and m >= n: find the least squares solution of
32*> an overdetermined system, i.e., solve the least squares problem
33*> minimize || B - A*X ||.
34*>
35*> 2. If TRANS = 'N' and m < n: find the minimum norm solution of
36*> an underdetermined system A * X = B.
37*>
38*> 3. If TRANS = 'C' and m >= n: find the minimum norm solution of
39*> an undetermined system A**T * X = B.
40*>
41*> 4. If TRANS = 'C' and m < n: find the least squares solution of
42*> an overdetermined system, i.e., solve the least squares problem
43*> minimize || B - A**T * X ||.
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*> \endverbatim
50*
51* Arguments:
52* ==========
53*
54*> \param[in] TRANS
55*> \verbatim
56*> TRANS is CHARACTER*1
57*> = 'N': the linear system involves A;
58*> = 'C': the linear system involves A**H.
59*> \endverbatim
60*>
61*> \param[in] M
62*> \verbatim
63*> M is INTEGER
64*> The number of rows of the matrix A. M >= 0.
65*> \endverbatim
66*>
67*> \param[in] N
68*> \verbatim
69*> N is INTEGER
70*> The number of columns of the matrix A. N >= 0.
71*> \endverbatim
72*>
73*> \param[in] NRHS
74*> \verbatim
75*> NRHS is INTEGER
76*> The number of right hand sides, i.e., the number of
77*> columns of the matrices B and X. NRHS >=0.
78*> \endverbatim
79*>
80*> \param[in,out] A
81*> \verbatim
82*> A is COMPLEX*16 array, dimension (LDA,N)
83*> On entry, the M-by-N matrix A.
84*> On exit,
85*> A is overwritten by details of its QR or LQ
86*> factorization as returned by ZGEQR or ZGELQ.
87*> \endverbatim
88*>
89*> \param[in] LDA
90*> \verbatim
91*> LDA is INTEGER
92*> The leading dimension of the array A. LDA >= max(1,M).
93*> \endverbatim
94*>
95*> \param[in,out] B
96*> \verbatim
97*> B is COMPLEX*16 array, dimension (LDB,NRHS)
98*> On entry, the matrix B of right hand side vectors, stored
99*> columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS
100*> if TRANS = 'C'.
101*> On exit, if INFO = 0, B is overwritten by the solution
102*> vectors, stored columnwise:
103*> if TRANS = 'N' and m >= n, rows 1 to n of B contain the least
104*> squares solution vectors.
105*> if TRANS = 'N' and m < n, rows 1 to N of B contain the
106*> minimum norm solution vectors;
107*> if TRANS = 'C' and m >= n, rows 1 to M of B contain the
108*> minimum norm solution vectors;
109*> if TRANS = 'C' and m < n, rows 1 to M of B contain the
110*> least squares solution vectors.
111*> \endverbatim
112*>
113*> \param[in] LDB
114*> \verbatim
115*> LDB is INTEGER
116*> The leading dimension of the array B. LDB >= MAX(1,M,N).
117*> \endverbatim
118*>
119*> \param[out] WORK
120*> \verbatim
121*> (workspace) COMPLEX*16 array, dimension (MAX(1,LWORK))
122*> On exit, if INFO = 0, WORK(1) contains optimal (or either minimal
123*> or optimal, if query was assumed) LWORK.
124*> See LWORK for details.
125*> \endverbatim
126*>
127*> \param[in] LWORK
128*> \verbatim
129*> LWORK is INTEGER
130*> The dimension of the array WORK.
131*> If LWORK = -1 or -2, then a workspace query is assumed.
132*> If LWORK = -1, the routine calculates optimal size of WORK for the
133*> optimal performance and returns this value in WORK(1).
134*> If LWORK = -2, the routine calculates minimal size of WORK and
135*> returns this value in WORK(1).
136*> \endverbatim
137*>
138*> \param[out] INFO
139*> \verbatim
140*> INFO is INTEGER
141*> = 0: successful exit
142*> < 0: if INFO = -i, the i-th argument had an illegal value
143*> > 0: if INFO = i, the i-th diagonal element of the
144*> triangular factor of A is zero, so that A does not have
145*> full rank; the least squares solution could not be
146*> computed.
147*> \endverbatim
148*
149* Authors:
150* ========
151*
152*> \author Univ. of Tennessee
153*> \author Univ. of California Berkeley
154*> \author Univ. of Colorado Denver
155*> \author NAG Ltd.
156*
157*> \ingroup complex16GEsolve
158*
159* =====================================================================
160 SUBROUTINE zgetsls( TRANS, M, N, NRHS, A, LDA, B, LDB,
161 $ WORK, LWORK, INFO )
162*
163* -- LAPACK driver routine --
164* -- LAPACK is a software package provided by Univ. of Tennessee, --
165* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
166*
167* .. Scalar Arguments ..
168 CHARACTER TRANS
169 INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
170* ..
171* .. Array Arguments ..
172 COMPLEX*16 A( LDA, * ), B( LDB, * ), WORK( * )
173*
174* ..
175*
176* =====================================================================
177*
178* .. Parameters ..
179 DOUBLE PRECISION ZERO, ONE
180 parameter( zero = 0.0d0, one = 1.0d0 )
181 COMPLEX*16 CZERO
182 parameter( czero = ( 0.0d+0, 0.0d+0 ) )
183* ..
184* .. Local Scalars ..
185 LOGICAL LQUERY, TRAN
186 INTEGER I, IASCL, IBSCL, J, MAXMN, BROW,
187 $ scllen, tszo, tszm, lwo, lwm, lw1, lw2,
188 $ wsizeo, wsizem, info2
189 DOUBLE PRECISION ANRM, BIGNUM, BNRM, SMLNUM, DUM( 1 )
190 COMPLEX*16 TQ( 5 ), WORKQ( 1 )
191* ..
192* .. External Functions ..
193 LOGICAL LSAME
194 DOUBLE PRECISION DLAMCH, ZLANGE
195 EXTERNAL lsame, dlabad, dlamch, zlange
196* ..
197* .. External Subroutines ..
198 EXTERNAL zgeqr, zgemqr, zlascl, zlaset,
200* ..
201* .. Intrinsic Functions ..
202 INTRINSIC dble, max, min, int
203* ..
204* .. Executable Statements ..
205*
206* Test the input arguments.
207*
208 info = 0
209 maxmn = max( m, n )
210 tran = lsame( trans, 'C' )
211*
212 lquery = ( lwork.EQ.-1 .OR. lwork.EQ.-2 )
213 IF( .NOT.( lsame( trans, 'N' ) .OR.
214 $ lsame( trans, 'C' ) ) ) THEN
215 info = -1
216 ELSE IF( m.LT.0 ) THEN
217 info = -2
218 ELSE IF( n.LT.0 ) THEN
219 info = -3
220 ELSE IF( nrhs.LT.0 ) THEN
221 info = -4
222 ELSE IF( lda.LT.max( 1, m ) ) THEN
223 info = -6
224 ELSE IF( ldb.LT.max( 1, m, n ) ) THEN
225 info = -8
226 END IF
227*
228 IF( info.EQ.0 ) THEN
229*
230* Determine the optimum and minimum LWORK
231*
232 IF( m.GE.n ) THEN
233 CALL zgeqr( m, n, a, lda, tq, -1, workq, -1, info2 )
234 tszo = int( tq( 1 ) )
235 lwo = int( workq( 1 ) )
236 CALL zgemqr( 'L', trans, m, nrhs, n, a, lda, tq,
237 $ tszo, b, ldb, workq, -1, info2 )
238 lwo = max( lwo, int( workq( 1 ) ) )
239 CALL zgeqr( m, n, a, lda, tq, -2, workq, -2, info2 )
240 tszm = int( tq( 1 ) )
241 lwm = int( workq( 1 ) )
242 CALL zgemqr( 'L', trans, m, nrhs, n, a, lda, tq,
243 $ tszm, b, ldb, workq, -1, info2 )
244 lwm = max( lwm, int( workq( 1 ) ) )
245 wsizeo = tszo + lwo
246 wsizem = tszm + lwm
247 ELSE
248 CALL zgelq( m, n, a, lda, tq, -1, workq, -1, info2 )
249 tszo = int( tq( 1 ) )
250 lwo = int( workq( 1 ) )
251 CALL zgemlq( 'L', trans, n, nrhs, m, a, lda, tq,
252 $ tszo, b, ldb, workq, -1, info2 )
253 lwo = max( lwo, int( workq( 1 ) ) )
254 CALL zgelq( m, n, a, lda, tq, -2, workq, -2, info2 )
255 tszm = int( tq( 1 ) )
256 lwm = int( workq( 1 ) )
257 CALL zgemlq( 'l', TRANS, N, NRHS, M, A, LDA, TQ,
258 $ TSZM, B, LDB, WORKQ, -1, INFO2 )
259 LWM = MAX( LWM, INT( WORKQ( 1 ) ) )
260 WSIZEO = TSZO + LWO
261 WSIZEM = TSZM + LWM
262 END IF
263*
264.LT..AND..NOT. IF( ( LWORKWSIZEM )( LQUERY ) ) THEN
265 INFO = -10
266 END IF
267*
268 WORK( 1 ) = DBLE( WSIZEO )
269*
270 END IF
271*
272.NE. IF( INFO0 ) THEN
273 CALL XERBLA( 'zgetsls', -INFO )
274 RETURN
275 END IF
276 IF( LQUERY ) THEN
277.EQ. IF( LWORK-2 ) WORK( 1 ) = DBLE( WSIZEM )
278 RETURN
279 END IF
280.LT. IF( LWORKWSIZEO ) THEN
281 LW1 = TSZM
282 LW2 = LWM
283 ELSE
284 LW1 = TSZO
285 LW2 = LWO
286 END IF
287*
288* Quick return if possible
289*
290.EQ. IF( MIN( M, N, NRHS )0 ) THEN
291 CALL ZLASET( 'full', MAX( M, N ), NRHS, CZERO, CZERO,
292 $ B, LDB )
293 RETURN
294 END IF
295*
296* Get machine parameters
297*
298 SMLNUM = DLAMCH( 's' ) / dlamch( 'P' )
299 bignum = one / smlnum
300 CALL dlabad( smlnum, bignum )
301*
302* Scale A, B if max element outside range [SMLNUM,BIGNUM]
303*
304 anrm = zlange( 'M', m, n, a, lda, dum )
305 iascl = 0
306 IF( anrm.GT.zero .AND. anrm.LT.smlnum ) THEN
307*
308* Scale matrix norm up to SMLNUM
309*
310 CALL zlascl( 'G', 0, 0, anrm, smlnum, m, n, a, lda, info )
311 iascl = 1
312 ELSE IF( anrm.GT.bignum ) THEN
313*
314* Scale matrix norm down to BIGNUM
315*
316 CALL zlascl( 'G', 0, 0, anrm, bignum, m, n, a, lda, info )
317 iascl = 2
318 ELSE IF( anrm.EQ.zero ) THEN
319*
320* Matrix all zero. Return zero solution.
321*
322 CALL zlaset( 'F', maxmn, nrhs, czero, czero, b, ldb )
323 GO TO 50
324 END IF
325*
326 brow = m
327 IF ( tran ) THEN
328 brow = n
329 END IF
330 bnrm = zlange( 'M', brow, nrhs, b, ldb, dum )
331 ibscl = 0
332 IF( bnrm.GT.zero .AND. bnrm.LT.smlnum ) THEN
333*
334* Scale matrix norm up to SMLNUM
335*
336 CALL zlascl( 'g', 0, 0, BNRM, SMLNUM, BROW, NRHS, B, LDB,
337 $ INFO )
338 IBSCL = 1
339.GT. ELSE IF( BNRMBIGNUM ) THEN
340*
341* Scale matrix norm down to BIGNUM
342*
343 CALL ZLASCL( 'g', 0, 0, BNRM, BIGNUM, BROW, NRHS, B, LDB,
344 $ INFO )
345 IBSCL = 2
346 END IF
347*
348.GE. IF ( MN ) THEN
349*
350* compute QR factorization of A
351*
352 CALL ZGEQR( M, N, A, LDA, WORK( LW2+1 ), LW1,
353 $ WORK( 1 ), LW2, INFO )
354.NOT. IF ( TRAN ) THEN
355*
356* Least-Squares Problem min || A * X - B ||
357*
358* B(1:M,1:NRHS) := Q**T * B(1:M,1:NRHS)
359*
360 CALL ZGEMQR( 'l' , 'c', M, NRHS, N, A, LDA,
361 $ WORK( LW2+1 ), LW1, B, LDB, WORK( 1 ), LW2,
362 $ INFO )
363*
364* B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS)
365*
366 CALL ZTRTRS( 'u', 'n', 'n', n, nrhs,
367 $ a, lda, b, ldb, info )
368 IF( info.GT.0 ) THEN
369 RETURN
370 END IF
371 scllen = n
372 ELSE
373*
374* Overdetermined system of equations A**T * X = B
375*
376* B(1:N,1:NRHS) := inv(R**T) * B(1:N,1:NRHS)
377*
378 CALL ztrtrs( 'U', 'C', 'N', n, nrhs,
379 $ a, lda, b, ldb, info )
380*
381 IF( info.GT.0 ) THEN
382 RETURN
383 END IF
384*
385* B(N+1:M,1:NRHS) = CZERO
386*
387 DO 20 j = 1, nrhs
388 DO 10 i = n + 1, m
389 b( i, j ) = czero
390 10 CONTINUE
391 20 CONTINUE
392*
393* B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS)
394*
395 CALL zgemqr( 'L', 'N', m, nrhs, n, a, lda,
396 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
397 $ info )
398*
399 scllen = m
400*
401 END IF
402*
403 ELSE
404*
405* Compute LQ factorization of A
406*
407 CALL zgelq( m, n, a, lda, work( lw2+1 ), lw1,
408 $ work( 1 ), lw2, info )
409*
410* workspace at least M, optimally M*NB.
411*
412 IF( .NOT.tran ) THEN
413*
414* underdetermined system of equations A * X = B
415*
416* B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS)
417*
418 CALL ztrtrs( 'L', 'N', 'N', m, nrhs,
419 $ a, lda, b, ldb, info )
420*
421 IF( info.GT.0 ) THEN
422 RETURN
423 END IF
424*
425* B(M+1:N,1:NRHS) = 0
426*
427 DO 40 j = 1, nrhs
428 DO 30 i = m + 1, n
429 b( i, j ) = czero
430 30 CONTINUE
431 40 CONTINUE
432*
433* B(1:N,1:NRHS) := Q(1:N,:)**T * B(1:M,1:NRHS)
434*
435 CALL zgemlq( 'L', 'C', n, nrhs, m, a, lda,
436 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
437 $ info )
438*
439* workspace at least NRHS, optimally NRHS*NB
440*
441 scllen = n
442*
443 ELSE
444*
445* overdetermined system min || A**T * X - B ||
446*
447* B(1:N,1:NRHS) := Q * B(1:N,1:NRHS)
448*
449 CALL zgemlq( 'L', 'N', n, nrhs, m, a, lda,
450 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
451 $ info )
452*
453* workspace at least NRHS, optimally NRHS*NB
454*
455* B(1:M,1:NRHS) := inv(L**T) * B(1:M,1:NRHS)
456*
457 CALL ztrtrs( 'L', 'C', 'N', m, nrhs,
458 $ a, lda, b, ldb, info )
459*
460 IF( info.GT.0 ) THEN
461 RETURN
462 END IF
463*
464 scllen = m
465*
466 END IF
467*
468 END IF
469*
470* Undo scaling
471*
472 IF( iascl.EQ.1 ) THEN
473 CALL zlascl( 'G', 0, 0, anrm, smlnum, scllen, nrhs, b, ldb,
474 $ info )
475 ELSE IF( iascl.EQ.2 ) THEN
476 CALL zlascl( 'G', 0, 0, anrm, bignum, scllen, nrhs, b, ldb,
477 $ info )
478 END IF
479 IF( ibscl.EQ.1 ) THEN
480 CALL zlascl( 'G', 0, 0, smlnum, bnrm, scllen, nrhs, b, ldb,
481 $ info )
482 ELSE IF( ibscl.EQ.2 ) THEN
483 CALL zlascl( 'G', 0, 0, bignum, bnrm, scllen, nrhs, b, ldb,
484 $ info )
485 END IF
486*
487 50 CONTINUE
488 work( 1 ) = dble( tszo + lwo )
489 RETURN
490*
491* End of ZGETSLS
492*
493 END
subroutine dlabad(small, large)
DLABAD
Definition dlabad.f:74
subroutine xerbla(srname, info)
XERBLA
Definition xerbla.f:60
subroutine zgetsls(trans, m, n, nrhs, a, lda, b, ldb, work, lwork, info)
ZGETSLS
Definition zgetsls.f:162
subroutine zlascl(type, kl, ku, cfrom, cto, m, n, a, lda, info)
ZLASCL multiplies a general rectangular matrix by a real scalar defined as cto/cfrom.
Definition zlascl.f:143
subroutine zlaset(uplo, m, n, alpha, beta, a, lda)
ZLASET initializes the off-diagonal elements and the diagonal elements of a matrix to given values.
Definition zlaset.f:106
subroutine ztrtrs(uplo, trans, diag, n, nrhs, a, lda, b, ldb, info)
ZTRTRS
Definition ztrtrs.f:140
#define min(a, b)
Definition macros.h:20
#define max(a, b)
Definition macros.h:21
subroutine zgelq(m, n, a, lda, t, tsize, work, lwork, info)
ZGELQ
Definition zgelq.f:172
subroutine zgemlq(side, trans, m, n, k, a, lda, t, tsize, c, ldc, work, lwork, info)
ZGEMLQ
Definition zgemlq.f:169
subroutine zgemqr(side, trans, m, n, k, a, lda, t, tsize, c, ldc, work, lwork, info)
ZGEMQR
Definition zgemqr.f:172
subroutine zgeqr(m, n, a, lda, t, tsize, work, lwork, info)
ZGEQR
Definition zgeqr.f:174