3 /* Table of constant values */
5 static integer c__9 = 9;
6 static integer c__0 = 0;
7 static integer c__6 = 6;
8 static integer c_n1 = -1;
9 static integer c__1 = 1;
10 static real c_b81 = 0.f;
12 /* Subroutine */ int sgelsd_(integer *m, integer *n, integer *nrhs, real *a,
13 integer *lda, real *b, integer *ldb, real *s, real *rcond, integer *
14 rank, real *work, integer *lwork, integer *iwork, integer *info)
16 /* System generated locals */
17 integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
19 /* Builtin functions */
20 double log(doublereal);
25 integer itau, nlvl, iascl, ibscl;
27 integer minmn, maxmn, itaup, itauq, mnthr, nwork;
28 extern /* Subroutine */ int slabad_(real *, real *), sgebrd_(integer *,
29 integer *, real *, integer *, real *, real *, real *, real *,
30 real *, integer *, integer *);
31 extern doublereal slamch_(char *), slange_(char *, integer *,
32 integer *, real *, integer *, real *);
33 extern /* Subroutine */ int xerbla_(char *, integer *);
34 extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
35 integer *, integer *);
37 extern /* Subroutine */ int sgelqf_(integer *, integer *, real *, integer
38 *, real *, real *, integer *, integer *), slalsd_(char *, integer
39 *, integer *, integer *, real *, real *, real *, integer *, real *
40 , integer *, real *, integer *, integer *), slascl_(char *
41 , integer *, integer *, real *, real *, integer *, integer *,
42 real *, integer *, integer *);
44 extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer
45 *, real *, real *, integer *, integer *), slacpy_(char *, integer
46 *, integer *, real *, integer *, real *, integer *),
47 slaset_(char *, integer *, integer *, real *, real *, real *,
50 extern /* Subroutine */ int sormbr_(char *, char *, char *, integer *,
51 integer *, integer *, real *, integer *, real *, real *, integer *
52 , real *, integer *, integer *);
53 integer liwork, minwrk, maxwrk;
55 extern /* Subroutine */ int sormlq_(char *, char *, integer *, integer *,
56 integer *, real *, integer *, real *, real *, integer *, real *,
57 integer *, integer *);
60 extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *,
61 integer *, real *, integer *, real *, real *, integer *, real *,
62 integer *, integer *);
65 /* -- LAPACK driver routine (version 3.1) -- */
66 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
69 /* .. Scalar Arguments .. */
71 /* .. Array Arguments .. */
77 /* SGELSD computes the minimum-norm solution to a real linear least */
78 /* squares problem: */
79 /* minimize 2-norm(| b - A*x |) */
80 /* using the singular value decomposition (SVD) of A. A is an M-by-N */
81 /* matrix which may be rank-deficient. */
83 /* Several right hand side vectors b and solution vectors x can be */
84 /* handled in a single call; they are stored as the columns of the */
85 /* M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
88 /* The problem is solved in three steps: */
89 /* (1) Reduce the coefficient matrix A to bidiagonal form with */
90 /* Householder transformations, reducing the original problem */
91 /* into a "bidiagonal least squares problem" (BLS) */
92 /* (2) Solve the BLS using a divide and conquer approach. */
93 /* (3) Apply back all the Householder tranformations to solve */
94 /* the original least squares problem. */
96 /* The effective rank of A is determined by treating as zero those */
97 /* singular values which are less than RCOND times the largest singular */
100 /* The divide and conquer algorithm makes very mild assumptions about */
101 /* floating point arithmetic. It will work on machines with a guard */
102 /* digit in add/subtract, or on those binary machines without guard */
103 /* digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or */
104 /* Cray-2. It could conceivably fail on hexadecimal or decimal machines */
105 /* without guard digits, but we know of none. */
110 /* M (input) INTEGER */
111 /* The number of rows of A. M >= 0. */
113 /* N (input) INTEGER */
114 /* The number of columns of A. N >= 0. */
116 /* NRHS (input) INTEGER */
117 /* The number of right hand sides, i.e., the number of columns */
118 /* of the matrices B and X. NRHS >= 0. */
120 /* A (input) REAL array, dimension (LDA,N) */
121 /* On entry, the M-by-N matrix A. */
122 /* On exit, A has been destroyed. */
124 /* LDA (input) INTEGER */
125 /* The leading dimension of the array A. LDA >= max(1,M). */
127 /* B (input/output) REAL array, dimension (LDB,NRHS) */
128 /* On entry, the M-by-NRHS right hand side matrix B. */
129 /* On exit, B is overwritten by the N-by-NRHS solution */
130 /* matrix X. If m >= n and RANK = n, the residual */
131 /* sum-of-squares for the solution in the i-th column is given */
132 /* by the sum of squares of elements n+1:m in that column. */
134 /* LDB (input) INTEGER */
135 /* The leading dimension of the array B. LDB >= max(1,max(M,N)). */
137 /* S (output) REAL array, dimension (min(M,N)) */
138 /* The singular values of A in decreasing order. */
139 /* The condition number of A in the 2-norm = S(1)/S(min(m,n)). */
141 /* RCOND (input) REAL */
142 /* RCOND is used to determine the effective rank of A. */
143 /* Singular values S(i) <= RCOND*S(1) are treated as zero. */
144 /* If RCOND < 0, machine precision is used instead. */
146 /* RANK (output) INTEGER */
147 /* The effective rank of A, i.e., the number of singular values */
148 /* which are greater than RCOND*S(1). */
150 /* WORK (workspace/output) REAL array, dimension (MAX(1,LWORK)) */
151 /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
153 /* LWORK (input) INTEGER */
154 /* The dimension of the array WORK. LWORK must be at least 1. */
155 /* The exact minimum amount of workspace needed depends on M, */
156 /* N and NRHS. As long as LWORK is at least */
157 /* 12*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2, */
158 /* if M is greater than or equal to N or */
159 /* 12*M + 2*M*SMLSIZ + 8*M*NLVL + M*NRHS + (SMLSIZ+1)**2, */
160 /* if M is less than N, the code will execute correctly. */
161 /* SMLSIZ is returned by ILAENV and is equal to the maximum */
162 /* size of the subproblems at the bottom of the computation */
163 /* tree (usually about 25), and */
164 /* NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 ) */
165 /* For good performance, LWORK should generally be larger. */
167 /* If LWORK = -1, then a workspace query is assumed; the routine */
168 /* only calculates the optimal size of the array WORK and the */
169 /* minimum size of the array IWORK, and returns these values as */
170 /* the first entries of the WORK and IWORK arrays, and no error */
171 /* message related to LWORK is issued by XERBLA. */
173 /* IWORK (workspace) INTEGER array, dimension (MAX(1,LIWORK)) */
174 /* LIWORK >= max(1, 3*MINMN*NLVL + 11*MINMN), */
175 /* where MINMN = MIN( M,N ). */
176 /* On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK. */
178 /* INFO (output) INTEGER */
179 /* = 0: successful exit */
180 /* < 0: if INFO = -i, the i-th argument had an illegal value. */
181 /* > 0: the algorithm for computing the SVD failed to converge; */
182 /* if INFO = i, i off-diagonal elements of an intermediate */
183 /* bidiagonal form did not converge to zero. */
185 /* Further Details */
186 /* =============== */
188 /* Based on contributions by */
189 /* Ming Gu and Ren-Cang Li, Computer Science Division, University of */
190 /* California at Berkeley, USA */
191 /* Osni Marques, LBNL/NERSC, USA */
193 /* ===================================================================== */
195 /* .. Parameters .. */
197 /* .. Local Scalars .. */
199 /* .. External Subroutines .. */
201 /* .. External Functions .. */
203 /* .. Intrinsic Functions .. */
205 /* .. Executable Statements .. */
207 /* Test the input arguments. */
209 /* Parameter adjustments */
211 a_offset = 1 + a_dim1;
214 b_offset = 1 + b_dim1;
224 lquery = *lwork == -1;
229 } else if (*nrhs < 0) {
231 } else if (*lda < max(1,*m)) {
233 } else if (*ldb < max(1,maxmn)) {
237 /* Compute workspace. */
238 /* (Note: Comments in the code beginning "Workspace:" describe the */
239 /* minimal amount of workspace needed at that point in the code, */
240 /* as well as the preferred amount for good performance. */
241 /* NB refers to the optimal block size for the immediately */
242 /* following subroutine, as returned by ILAENV.) */
249 smlsiz = ilaenv_(&c__9, "SGELSD", " ", &c__0, &c__0, &c__0, &c__0);
250 mnthr = ilaenv_(&c__6, "SGELSD", " ", m, n, nrhs, &c_n1);
252 i__1 = (integer) (log((real) minmn / (real) (smlsiz + 1)) / log(
255 liwork = minmn * 3 * nlvl + minmn * 11;
257 if (*m >= *n && *m >= mnthr) {
259 /* Path 1a - overdetermined, with many more rows than */
264 i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SGEQRF",
265 " ", m, n, &c_n1, &c_n1);
266 maxwrk = max(i__1,i__2);
268 i__1 = maxwrk, i__2 = *n + *nrhs * ilaenv_(&c__1, "SORMQR",
269 "LT", m, nrhs, n, &c_n1);
270 maxwrk = max(i__1,i__2);
274 /* Path 1 - overdetermined or exactly determined. */
277 i__1 = maxwrk, i__2 = *n * 3 + (mm + *n) * ilaenv_(&c__1,
278 "SGEBRD", " ", &mm, n, &c_n1, &c_n1);
279 maxwrk = max(i__1,i__2);
281 i__1 = maxwrk, i__2 = *n * 3 + *nrhs * ilaenv_(&c__1, "SORMBR"
282 , "QLT", &mm, nrhs, n, &c_n1);
283 maxwrk = max(i__1,i__2);
285 i__1 = maxwrk, i__2 = *n * 3 + (*n - 1) * ilaenv_(&c__1,
286 "SORMBR", "PLN", n, nrhs, n, &c_n1);
287 maxwrk = max(i__1,i__2);
288 /* Computing 2nd power */
290 wlalsd = *n * 9 + (*n << 1) * smlsiz + (*n << 3) * nlvl + *n *
293 i__1 = maxwrk, i__2 = *n * 3 + wlalsd;
294 maxwrk = max(i__1,i__2);
296 i__1 = *n * 3 + mm, i__2 = *n * 3 + *nrhs, i__1 = max(i__1,
297 i__2), i__2 = *n * 3 + wlalsd;
298 minwrk = max(i__1,i__2);
301 /* Computing 2nd power */
303 wlalsd = *m * 9 + (*m << 1) * smlsiz + (*m << 3) * nlvl + *m *
307 /* Path 2a - underdetermined, with many more columns */
310 maxwrk = *m + *m * ilaenv_(&c__1, "SGELQF", " ", m, n, &
313 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m << 1) *
314 ilaenv_(&c__1, "SGEBRD", " ", m, m, &c_n1, &c_n1);
315 maxwrk = max(i__1,i__2);
317 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + *nrhs *
318 ilaenv_(&c__1, "SORMBR", "QLT", m, nrhs, m, &c_n1);
319 maxwrk = max(i__1,i__2);
321 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m - 1) *
322 ilaenv_(&c__1, "SORMBR", "PLN", m, nrhs, m, &c_n1);
323 maxwrk = max(i__1,i__2);
326 i__1 = maxwrk, i__2 = *m * *m + *m + *m * *nrhs;
327 maxwrk = max(i__1,i__2);
330 i__1 = maxwrk, i__2 = *m * *m + (*m << 1);
331 maxwrk = max(i__1,i__2);
334 i__1 = maxwrk, i__2 = *m + *nrhs * ilaenv_(&c__1, "SORMLQ"
335 , "LT", n, nrhs, m, &c_n1);
336 maxwrk = max(i__1,i__2);
338 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + wlalsd;
339 maxwrk = max(i__1,i__2);
342 /* Path 2 - remaining underdetermined cases. */
344 maxwrk = *m * 3 + (*n + *m) * ilaenv_(&c__1, "SGEBRD",
345 " ", m, n, &c_n1, &c_n1);
347 i__1 = maxwrk, i__2 = *m * 3 + *nrhs * ilaenv_(&c__1,
348 "SORMBR", "QLT", m, nrhs, n, &c_n1);
349 maxwrk = max(i__1,i__2);
351 i__1 = maxwrk, i__2 = *m * 3 + *m * ilaenv_(&c__1, "SORM"
352 "BR", "PLN", n, nrhs, m, &c_n1);
353 maxwrk = max(i__1,i__2);
355 i__1 = maxwrk, i__2 = *m * 3 + wlalsd;
356 maxwrk = max(i__1,i__2);
359 i__1 = *m * 3 + *nrhs, i__2 = *m * 3 + *m, i__1 = max(i__1,
360 i__2), i__2 = *m * 3 + wlalsd;
361 minwrk = max(i__1,i__2);
364 minwrk = min(minwrk,maxwrk);
365 work[1] = (real) maxwrk;
368 if (*lwork < minwrk && ! lquery) {
375 xerbla_("SGELSD", &i__1);
381 /* Quick return if possible. */
383 if (*m == 0 || *n == 0) {
388 /* Get machine parameters. */
391 sfmin = slamch_("S");
392 smlnum = sfmin / eps;
393 bignum = 1.f / smlnum;
394 slabad_(&smlnum, &bignum);
396 /* Scale A if max entry outside range [SMLNUM,BIGNUM]. */
398 anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
400 if (anrm > 0.f && anrm < smlnum) {
402 /* Scale matrix norm up to SMLNUM. */
404 slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
407 } else if (anrm > bignum) {
409 /* Scale matrix norm down to BIGNUM. */
411 slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
414 } else if (anrm == 0.f) {
416 /* Matrix all zero. Return zero solution. */
419 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[b_offset], ldb);
420 slaset_("F", &minmn, &c__1, &c_b81, &c_b81, &s[1], &c__1);
425 /* Scale B if max entry outside range [SMLNUM,BIGNUM]. */
427 bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
429 if (bnrm > 0.f && bnrm < smlnum) {
431 /* Scale matrix norm up to SMLNUM. */
433 slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
436 } else if (bnrm > bignum) {
438 /* Scale matrix norm down to BIGNUM. */
440 slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
445 /* If M < N make sure certain entries of B are zero. */
449 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1], ldb);
452 /* Overdetermined case. */
456 /* Path 1 - overdetermined or exactly determined. */
461 /* Path 1a - overdetermined, with many more rows than columns. */
468 /* (Workspace: need 2*N, prefer N+N*NB) */
470 i__1 = *lwork - nwork + 1;
471 sgeqrf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
474 /* Multiply B by transpose(Q). */
475 /* (Workspace: need N+NRHS, prefer N+NRHS*NB) */
477 i__1 = *lwork - nwork + 1;
478 sormqr_("L", "T", m, nrhs, n, &a[a_offset], lda, &work[itau], &b[
479 b_offset], ldb, &work[nwork], &i__1, info);
481 /* Zero out below R. */
486 slaset_("L", &i__1, &i__2, &c_b81, &c_b81, &a[a_dim1 + 2],
496 /* Bidiagonalize R in A. */
497 /* (Workspace: need 3*N+MM, prefer 3*N+(MM+N)*NB) */
499 i__1 = *lwork - nwork + 1;
500 sgebrd_(&mm, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
501 work[itaup], &work[nwork], &i__1, info);
503 /* Multiply B by transpose of left bidiagonalizing vectors of R. */
504 /* (Workspace: need 3*N+NRHS, prefer 3*N+NRHS*NB) */
506 i__1 = *lwork - nwork + 1;
507 sormbr_("Q", "L", "T", &mm, nrhs, n, &a[a_offset], lda, &work[itauq],
508 &b[b_offset], ldb, &work[nwork], &i__1, info);
510 /* Solve the bidiagonal least squares problem. */
512 slalsd_("U", &smlsiz, n, nrhs, &s[1], &work[ie], &b[b_offset], ldb,
513 rcond, rank, &work[nwork], &iwork[1], info);
518 /* Multiply B by right bidiagonalizing vectors of R. */
520 i__1 = *lwork - nwork + 1;
521 sormbr_("P", "L", "N", n, nrhs, n, &a[a_offset], lda, &work[itaup], &
522 b[b_offset], ldb, &work[nwork], &i__1, info);
524 } else /* if(complicated condition) */ {
526 i__1 = *m, i__2 = (*m << 1) - 4, i__1 = max(i__1,i__2), i__1 = max(
527 i__1,*nrhs), i__2 = *n - *m * 3, i__1 = max(i__1,i__2);
528 if (*n >= mnthr && *lwork >= (*m << 2) + *m * *m + max(i__1,wlalsd)) {
530 /* Path 2a - underdetermined, with many more columns than rows */
531 /* and sufficient workspace for an efficient algorithm. */
536 i__3 = *m, i__4 = (*m << 1) - 4, i__3 = max(i__3,i__4), i__3 =
537 max(i__3,*nrhs), i__4 = *n - *m * 3;
538 i__1 = (*m << 2) + *m * *lda + max(i__3,i__4), i__2 = *m * *lda +
539 *m + *m * *nrhs, i__1 = max(i__1,i__2), i__2 = (*m << 2)
540 + *m * *lda + wlalsd;
541 if (*lwork >= max(i__1,i__2)) {
548 /* (Workspace: need 2*M, prefer M+M*NB) */
550 i__1 = *lwork - nwork + 1;
551 sgelqf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
555 /* Copy L to WORK(IL), zeroing out above its diagonal. */
557 slacpy_("L", m, m, &a[a_offset], lda, &work[il], &ldwork);
560 slaset_("U", &i__1, &i__2, &c_b81, &c_b81, &work[il + ldwork], &
562 ie = il + ldwork * *m;
567 /* Bidiagonalize L in WORK(IL). */
568 /* (Workspace: need M*M+5*M, prefer M*M+4*M+2*M*NB) */
570 i__1 = *lwork - nwork + 1;
571 sgebrd_(m, m, &work[il], &ldwork, &s[1], &work[ie], &work[itauq],
572 &work[itaup], &work[nwork], &i__1, info);
574 /* Multiply B by transpose of left bidiagonalizing vectors of L. */
575 /* (Workspace: need M*M+4*M+NRHS, prefer M*M+4*M+NRHS*NB) */
577 i__1 = *lwork - nwork + 1;
578 sormbr_("Q", "L", "T", m, nrhs, m, &work[il], &ldwork, &work[
579 itauq], &b[b_offset], ldb, &work[nwork], &i__1, info);
581 /* Solve the bidiagonal least squares problem. */
583 slalsd_("U", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
584 ldb, rcond, rank, &work[nwork], &iwork[1], info);
589 /* Multiply B by right bidiagonalizing vectors of L. */
591 i__1 = *lwork - nwork + 1;
592 sormbr_("P", "L", "N", m, nrhs, m, &work[il], &ldwork, &work[
593 itaup], &b[b_offset], ldb, &work[nwork], &i__1, info);
595 /* Zero out below first M rows of B. */
598 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1],
602 /* Multiply transpose(Q) by B. */
603 /* (Workspace: need M+NRHS, prefer M+NRHS*NB) */
605 i__1 = *lwork - nwork + 1;
606 sormlq_("L", "T", n, nrhs, m, &a[a_offset], lda, &work[itau], &b[
607 b_offset], ldb, &work[nwork], &i__1, info);
611 /* Path 2 - remaining underdetermined cases. */
618 /* Bidiagonalize A. */
619 /* (Workspace: need 3*M+N, prefer 3*M+(M+N)*NB) */
621 i__1 = *lwork - nwork + 1;
622 sgebrd_(m, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
623 work[itaup], &work[nwork], &i__1, info);
625 /* Multiply B by transpose of left bidiagonalizing vectors. */
626 /* (Workspace: need 3*M+NRHS, prefer 3*M+NRHS*NB) */
628 i__1 = *lwork - nwork + 1;
629 sormbr_("Q", "L", "T", m, nrhs, n, &a[a_offset], lda, &work[itauq]
630 , &b[b_offset], ldb, &work[nwork], &i__1, info);
632 /* Solve the bidiagonal least squares problem. */
634 slalsd_("L", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
635 ldb, rcond, rank, &work[nwork], &iwork[1], info);
640 /* Multiply B by right bidiagonalizing vectors of A. */
642 i__1 = *lwork - nwork + 1;
643 sormbr_("P", "L", "N", n, nrhs, m, &a[a_offset], lda, &work[itaup]
644 , &b[b_offset], ldb, &work[nwork], &i__1, info);
652 slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
654 slascl_("G", &c__0, &c__0, &smlnum, &anrm, &minmn, &c__1, &s[1], &
656 } else if (iascl == 2) {
657 slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
659 slascl_("G", &c__0, &c__0, &bignum, &anrm, &minmn, &c__1, &s[1], &
663 slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
665 } else if (ibscl == 2) {
666 slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
671 work[1] = (real) maxwrk;