14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
22 typedef BLASLONG blasint;
24 #define blasabs(x) llabs(x)
26 #define blasabs(x) labs(x)
30 #define blasabs(x) abs(x)
33 typedef blasint integer;
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
63 /* Extern is for use with -E */
74 /*external read, write*/
83 /*internal read, write*/
113 /*rewind, backspace, endfile*/
125 ftnint *inex; /*parameters in standard's order*/
151 union Multitype { /* for multiple entry points */
162 typedef union Multitype Multitype;
164 struct Vardesc { /* for Namelist */
170 typedef struct Vardesc Vardesc;
177 typedef struct Namelist Namelist;
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b) ((a) >> (b) & 1)
186 #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
261 /* procedure parameter types for -A and -C++ */
263 #define F2C_proc_par_types 1
265 typedef logical (*L_fp)(...);
267 typedef logical (*L_fp)();
270 static float spow_ui(float x, integer n) {
271 float pow=1.0; unsigned long int u;
273 if(n < 0) n = -n, x = 1/x;
282 static double dpow_ui(double x, integer n) {
283 double pow=1.0; unsigned long int u;
285 if(n < 0) n = -n, x = 1/x;
295 static _Fcomplex cpow_ui(complex x, integer n) {
296 complex pow={1.0,0.0}; unsigned long int u;
298 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
300 if(u & 01) pow.r *= x.r, pow.i *= x.i;
301 if(u >>= 1) x.r *= x.r, x.i *= x.i;
305 _Fcomplex p={pow.r, pow.i};
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310 _Complex float pow=1.0; unsigned long int u;
312 if(n < 0) n = -n, x = 1/x;
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324 _Dcomplex pow={1.0,0.0}; unsigned long int u;
326 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
328 if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329 if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
333 _Dcomplex p = {pow._Val[0], pow._Val[1]};
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338 _Complex double pow=1.0; unsigned long int u;
340 if(n < 0) n = -n, x = 1/x;
350 static integer pow_ii(integer x, integer n) {
351 integer pow; unsigned long int u;
353 if (n == 0 || x == 1) pow = 1;
354 else if (x != -1) pow = x == 0 ? 1/x : 0;
357 if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
369 double m; integer i, mi;
370 for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371 if (w[i-1]>m) mi=i ,m=w[i-1];
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
376 float m; integer i, mi;
377 for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378 if (w[i-1]>m) mi=i ,m=w[i-1];
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382 integer n = *n_, incx = *incx_, incy = *incy_, i;
384 _Fcomplex zdotc = {0.0, 0.0};
385 if (incx == 1 && incy == 1) {
386 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387 zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388 zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
391 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392 zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393 zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
399 _Complex float zdotc = 0.0;
400 if (incx == 1 && incy == 1) {
401 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402 zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
405 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406 zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413 integer n = *n_, incx = *incx_, incy = *incy_, i;
415 _Dcomplex zdotc = {0.0, 0.0};
416 if (incx == 1 && incy == 1) {
417 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418 zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419 zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
422 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423 zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424 zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
430 _Complex double zdotc = 0.0;
431 if (incx == 1 && incy == 1) {
432 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433 zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
436 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437 zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444 integer n = *n_, incx = *incx_, incy = *incy_, i;
446 _Fcomplex zdotc = {0.0, 0.0};
447 if (incx == 1 && incy == 1) {
448 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449 zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450 zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
453 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454 zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455 zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
461 _Complex float zdotc = 0.0;
462 if (incx == 1 && incy == 1) {
463 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464 zdotc += Cf(&x[i]) * Cf(&y[i]);
467 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468 zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475 integer n = *n_, incx = *incx_, incy = *incy_, i;
477 _Dcomplex zdotc = {0.0, 0.0};
478 if (incx == 1 && incy == 1) {
479 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480 zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481 zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
484 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485 zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486 zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
492 _Complex double zdotc = 0.0;
493 if (incx == 1 && incy == 1) {
494 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495 zdotc += Cd(&x[i]) * Cd(&y[i]);
498 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499 zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
505 /* -- translated by f2c (version 20000121).
506 You must link the resulting object file with the libraries:
507 -lf2c -lm (in that order)
513 /* Table of constant values */
515 static integer c__9 = 9;
516 static integer c__0 = 0;
517 static integer c__6 = 6;
518 static integer c_n1 = -1;
519 static integer c__1 = 1;
520 static real c_b81 = 0.f;
522 /* > \brief <b> SGELSD computes the minimum-norm solution to a linear least squares problem for GE matrices</b
525 /* =========== DOCUMENTATION =========== */
527 /* Online html documentation available at */
528 /* http://www.netlib.org/lapack/explore-html/ */
531 /* > Download SGELSD + dependencies */
532 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgelsd.
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgelsd.
538 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgelsd.
546 /* SUBROUTINE SGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, */
547 /* RANK, WORK, LWORK, IWORK, INFO ) */
549 /* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK */
551 /* INTEGER IWORK( * ) */
552 /* REAL A( LDA, * ), B( LDB, * ), S( * ), WORK( * ) */
555 /* > \par Purpose: */
560 /* > SGELSD computes the minimum-norm solution to a real linear least */
561 /* > squares problem: */
562 /* > minimize 2-norm(| b - A*x |) */
563 /* > using the singular value decomposition (SVD) of A. A is an M-by-N */
564 /* > matrix which may be rank-deficient. */
566 /* > Several right hand side vectors b and solution vectors x can be */
567 /* > handled in a single call; they are stored as the columns of the */
568 /* > M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
571 /* > The problem is solved in three steps: */
572 /* > (1) Reduce the coefficient matrix A to bidiagonal form with */
573 /* > Householder transformations, reducing the original problem */
574 /* > into a "bidiagonal least squares problem" (BLS) */
575 /* > (2) Solve the BLS using a divide and conquer approach. */
576 /* > (3) Apply back all the Householder transformations to solve */
577 /* > the original least squares problem. */
579 /* > The effective rank of A is determined by treating as zero those */
580 /* > singular values which are less than RCOND times the largest singular */
583 /* > The divide and conquer algorithm makes very mild assumptions about */
584 /* > floating point arithmetic. It will work on machines with a guard */
585 /* > digit in add/subtract, or on those binary machines without guard */
586 /* > digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or */
587 /* > Cray-2. It could conceivably fail on hexadecimal or decimal machines */
588 /* > without guard digits, but we know of none. */
597 /* > The number of rows of A. M >= 0. */
603 /* > The number of columns of A. N >= 0. */
606 /* > \param[in] NRHS */
608 /* > NRHS is INTEGER */
609 /* > The number of right hand sides, i.e., the number of columns */
610 /* > of the matrices B and X. NRHS >= 0. */
613 /* > \param[in,out] A */
615 /* > A is REAL array, dimension (LDA,N) */
616 /* > On entry, the M-by-N matrix A. */
617 /* > On exit, A has been destroyed. */
620 /* > \param[in] LDA */
622 /* > LDA is INTEGER */
623 /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
626 /* > \param[in,out] B */
628 /* > B is REAL array, dimension (LDB,NRHS) */
629 /* > On entry, the M-by-NRHS right hand side matrix B. */
630 /* > On exit, B is overwritten by the N-by-NRHS solution */
631 /* > matrix X. If m >= n and RANK = n, the residual */
632 /* > sum-of-squares for the solution in the i-th column is given */
633 /* > by the sum of squares of elements n+1:m in that column. */
636 /* > \param[in] LDB */
638 /* > LDB is INTEGER */
639 /* > The leading dimension of the array B. LDB >= f2cmax(1,f2cmax(M,N)). */
642 /* > \param[out] S */
644 /* > S is REAL array, dimension (f2cmin(M,N)) */
645 /* > The singular values of A in decreasing order. */
646 /* > The condition number of A in the 2-norm = S(1)/S(f2cmin(m,n)). */
649 /* > \param[in] RCOND */
651 /* > RCOND is REAL */
652 /* > RCOND is used to determine the effective rank of A. */
653 /* > Singular values S(i) <= RCOND*S(1) are treated as zero. */
654 /* > If RCOND < 0, machine precision is used instead. */
657 /* > \param[out] RANK */
659 /* > RANK is INTEGER */
660 /* > The effective rank of A, i.e., the number of singular values */
661 /* > which are greater than RCOND*S(1). */
664 /* > \param[out] WORK */
666 /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
667 /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
670 /* > \param[in] LWORK */
672 /* > LWORK is INTEGER */
673 /* > The dimension of the array WORK. LWORK must be at least 1. */
674 /* > The exact minimum amount of workspace needed depends on M, */
675 /* > N and NRHS. As long as LWORK is at least */
676 /* > 12*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2, */
677 /* > if M is greater than or equal to N or */
678 /* > 12*M + 2*M*SMLSIZ + 8*M*NLVL + M*NRHS + (SMLSIZ+1)**2, */
679 /* > if M is less than N, the code will execute correctly. */
680 /* > SMLSIZ is returned by ILAENV and is equal to the maximum */
681 /* > size of the subproblems at the bottom of the computation */
682 /* > tree (usually about 25), and */
683 /* > NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 ) */
684 /* > For good performance, LWORK should generally be larger. */
686 /* > If LWORK = -1, then a workspace query is assumed; the routine */
687 /* > only calculates the optimal size of the array WORK and the */
688 /* > minimum size of the array IWORK, and returns these values as */
689 /* > the first entries of the WORK and IWORK arrays, and no error */
690 /* > message related to LWORK is issued by XERBLA. */
693 /* > \param[out] IWORK */
695 /* > IWORK is INTEGER array, dimension (MAX(1,LIWORK)) */
696 /* > LIWORK >= f2cmax(1, 3*MINMN*NLVL + 11*MINMN), */
697 /* > where MINMN = MIN( M,N ). */
698 /* > On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK. */
701 /* > \param[out] INFO */
703 /* > INFO is INTEGER */
704 /* > = 0: successful exit */
705 /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
706 /* > > 0: the algorithm for computing the SVD failed to converge; */
707 /* > if INFO = i, i off-diagonal elements of an intermediate */
708 /* > bidiagonal form did not converge to zero. */
714 /* > \author Univ. of Tennessee */
715 /* > \author Univ. of California Berkeley */
716 /* > \author Univ. of Colorado Denver */
717 /* > \author NAG Ltd. */
719 /* > \date June 2017 */
721 /* > \ingroup realGEsolve */
723 /* > \par Contributors: */
724 /* ================== */
726 /* > Ming Gu and Ren-Cang Li, Computer Science Division, University of */
727 /* > California at Berkeley, USA \n */
728 /* > Osni Marques, LBNL/NERSC, USA \n */
730 /* ===================================================================== */
731 /* Subroutine */ int sgelsd_(integer *m, integer *n, integer *nrhs, real *a,
732 integer *lda, real *b, integer *ldb, real *s, real *rcond, integer *
733 rank, real *work, integer *lwork, integer *iwork, integer *info)
735 /* System generated locals */
736 integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
738 /* Local variables */
740 integer itau, nlvl, iascl, ibscl;
742 integer minmn, maxmn, itaup, itauq, mnthr, nwork, ie, il;
743 extern /* Subroutine */ int slabad_(real *, real *);
745 extern /* Subroutine */ int sgebrd_(integer *, integer *, real *, integer
746 *, real *, real *, real *, real *, real *, integer *, integer *);
747 extern real slamch_(char *), slange_(char *, integer *, integer *,
748 real *, integer *, real *);
749 extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
750 extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
751 integer *, integer *, ftnlen, ftnlen);
753 extern /* Subroutine */ int sgelqf_(integer *, integer *, real *, integer
754 *, real *, real *, integer *, integer *), slalsd_(char *, integer
755 *, integer *, integer *, real *, real *, real *, integer *, real *
756 , integer *, real *, integer *, integer *), slascl_(char *
757 , integer *, integer *, real *, real *, integer *, integer *,
758 real *, integer *, integer *);
760 extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer
761 *, real *, real *, integer *, integer *), slacpy_(char *, integer
762 *, integer *, real *, integer *, real *, integer *),
763 slaset_(char *, integer *, integer *, real *, real *, real *,
766 extern /* Subroutine */ int sormbr_(char *, char *, char *, integer *,
767 integer *, integer *, real *, integer *, real *, real *, integer *
768 , real *, integer *, integer *);
769 integer liwork, minwrk, maxwrk;
771 extern /* Subroutine */ int sormlq_(char *, char *, integer *, integer *,
772 integer *, real *, integer *, real *, real *, integer *, real *,
773 integer *, integer *);
776 extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *,
777 integer *, real *, integer *, real *, real *, integer *, real *,
778 integer *, integer *);
782 /* -- LAPACK driver routine (version 3.7.1) -- */
783 /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
784 /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
788 /* ===================================================================== */
791 /* Test the input arguments. */
793 /* Parameter adjustments */
795 a_offset = 1 + a_dim1;
798 b_offset = 1 + b_dim1;
803 fprintf(stdout,"start of SGELSD\n");
806 minmn = f2cmin(*m,*n);
807 maxmn = f2cmax(*m,*n);
808 lquery = *lwork == -1;
813 } else if (*nrhs < 0) {
815 } else if (*lda < f2cmax(1,*m)) {
817 } else if (*ldb < f2cmax(1,maxmn)) {
821 /* Compute workspace. */
822 /* (Note: Comments in the code beginning "Workspace:" describe the */
823 /* minimal amount of workspace needed at that point in the code, */
824 /* as well as the preferred amount for good performance. */
825 /* NB refers to the optimal block size for the immediately */
826 /* following subroutine, as returned by ILAENV.) */
833 smlsiz = ilaenv_(&c__9, "SGELSD", " ", &c__0, &c__0, &c__0, &c__0,
834 (ftnlen)6, (ftnlen)1);
835 mnthr = ilaenv_(&c__6, "SGELSD", " ", m, n, nrhs, &c_n1, (ftnlen)
838 i__1 = (integer) (logf((real) minmn / (real) (smlsiz + 1)) / logf(
840 nlvl = f2cmax(i__1,0);
841 liwork = minmn * 3 * nlvl + minmn * 11;
843 if (*m >= *n && *m >= mnthr) {
845 /* Path 1a - overdetermined, with many more rows than */
850 i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SGEQRF",
851 " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
852 maxwrk = f2cmax(i__1,i__2);
854 i__1 = maxwrk, i__2 = *n + *nrhs * ilaenv_(&c__1, "SORMQR",
855 "LT", m, nrhs, n, &c_n1, (ftnlen)6, (ftnlen)2);
856 maxwrk = f2cmax(i__1,i__2);
860 /* Path 1 - overdetermined or exactly determined. */
863 i__1 = maxwrk, i__2 = *n * 3 + (mm + *n) * ilaenv_(&c__1,
864 "SGEBRD", " ", &mm, n, &c_n1, &c_n1, (ftnlen)6, (
866 maxwrk = f2cmax(i__1,i__2);
868 i__1 = maxwrk, i__2 = *n * 3 + *nrhs * ilaenv_(&c__1, "SORMBR"
869 , "QLT", &mm, nrhs, n, &c_n1, (ftnlen)6, (ftnlen)3);
870 maxwrk = f2cmax(i__1,i__2);
872 i__1 = maxwrk, i__2 = *n * 3 + (*n - 1) * ilaenv_(&c__1,
873 "SORMBR", "PLN", n, nrhs, n, &c_n1, (ftnlen)6, (
875 maxwrk = f2cmax(i__1,i__2);
876 /* Computing 2nd power */
878 wlalsd = *n * 9 + (*n << 1) * smlsiz + (*n << 3) * nlvl + *n *
881 i__1 = maxwrk, i__2 = *n * 3 + wlalsd;
882 maxwrk = f2cmax(i__1,i__2);
884 i__1 = *n * 3 + mm, i__2 = *n * 3 + *nrhs, i__1 = f2cmax(i__1,
885 i__2), i__2 = *n * 3 + wlalsd;
886 minwrk = f2cmax(i__1,i__2);
889 /* Computing 2nd power */
891 wlalsd = *m * 9 + (*m << 1) * smlsiz + (*m << 3) * nlvl + *m *
895 /* Path 2a - underdetermined, with many more columns */
898 maxwrk = *m + *m * ilaenv_(&c__1, "SGELQF", " ", m, n, &
899 c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
901 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m << 1) *
902 ilaenv_(&c__1, "SGEBRD", " ", m, m, &c_n1, &c_n1,
903 (ftnlen)6, (ftnlen)1);
904 maxwrk = f2cmax(i__1,i__2);
906 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + *nrhs *
907 ilaenv_(&c__1, "SORMBR", "QLT", m, nrhs, m, &c_n1,
908 (ftnlen)6, (ftnlen)3);
909 maxwrk = f2cmax(i__1,i__2);
911 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m - 1) *
912 ilaenv_(&c__1, "SORMBR", "PLN", m, nrhs, m, &c_n1,
913 (ftnlen)6, (ftnlen)3);
914 maxwrk = f2cmax(i__1,i__2);
917 i__1 = maxwrk, i__2 = *m * *m + *m + *m * *nrhs;
918 maxwrk = f2cmax(i__1,i__2);
921 i__1 = maxwrk, i__2 = *m * *m + (*m << 1);
922 maxwrk = f2cmax(i__1,i__2);
925 i__1 = maxwrk, i__2 = *m + *nrhs * ilaenv_(&c__1, "SORMLQ"
926 , "LT", n, nrhs, m, &c_n1, (ftnlen)6, (ftnlen)2);
927 maxwrk = f2cmax(i__1,i__2);
929 i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + wlalsd;
930 maxwrk = f2cmax(i__1,i__2);
931 /* XXX: Ensure the Path 2a case below is triggered. The workspace */
932 /* calculation should use queries for all routines eventually. */
935 i__3 = *m, i__4 = (*m << 1) - 4, i__3 = f2cmax(i__3,i__4),
936 i__3 = f2cmax(i__3,*nrhs), i__4 = *n - *m * 3;
937 i__1 = maxwrk, i__2 = (*m << 2) + *m * *m + f2cmax(i__3,i__4)
939 maxwrk = f2cmax(i__1,i__2);
942 /* Path 2 - remaining underdetermined cases. */
944 maxwrk = *m * 3 + (*n + *m) * ilaenv_(&c__1, "SGEBRD",
945 " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
947 i__1 = maxwrk, i__2 = *m * 3 + *nrhs * ilaenv_(&c__1,
948 "SORMBR", "QLT", m, nrhs, n, &c_n1, (ftnlen)6, (
950 maxwrk = f2cmax(i__1,i__2);
952 i__1 = maxwrk, i__2 = *m * 3 + *m * ilaenv_(&c__1, "SORM"
953 "BR", "PLN", n, nrhs, m, &c_n1, (ftnlen)6, (ftnlen)
955 maxwrk = f2cmax(i__1,i__2);
957 i__1 = maxwrk, i__2 = *m * 3 + wlalsd;
958 maxwrk = f2cmax(i__1,i__2);
961 i__1 = *m * 3 + *nrhs, i__2 = *m * 3 + *m, i__1 = f2cmax(i__1,
962 i__2), i__2 = *m * 3 + wlalsd;
963 minwrk = f2cmax(i__1,i__2);
966 minwrk = f2cmin(minwrk,maxwrk);
967 work[1] = (real) maxwrk;
970 if (*lwork < minwrk && ! lquery) {
977 xerbla_("SGELSD", &i__1, (ftnlen)6);
983 /* Quick return if possible. */
985 if (*m == 0 || *n == 0) {
986 fprintf(stdout,"SGELSD quickreturn rank=0\n");
991 /* Get machine parameters. */
994 sfmin = slamch_("S");
995 smlnum = sfmin / eps;
996 bignum = 1.f / smlnum;
997 // FILE *bla=fopen("/tmp/bla","w");
998 //fprintf(bla,"SGELSD eps=%g sfmin=%g smlnum=%g bignum=%g\n",eps,sfmin,smlnum,bignum);
1000 slabad_(&smlnum, &bignum);
1002 /* Scale A if f2cmax entry outside range [SMLNUM,BIGNUM]. */
1004 anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
1006 if (anrm > 0.f && anrm < smlnum) {
1008 /* Scale matrix norm up to SMLNUM. */
1009 fprintf(stdout,"scaling A up to SML\n");
1010 slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
1013 } else if (anrm > bignum) {
1015 /* Scale matrix norm down to BIGNUM. */
1017 fprintf(stdout,"scaling A down to BIG\n");
1018 slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
1021 } else if (anrm == 0.f) {
1023 /* Matrix all zero. Return zero solution. */
1025 fprintf(stdout,"A is zero soln\n");
1026 i__1 = f2cmax(*m,*n);
1027 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[b_offset], ldb);
1028 slaset_("F", &minmn, &c__1, &c_b81, &c_b81, &s[1], &c__1);
1033 /* Scale B if f2cmax entry outside range [SMLNUM,BIGNUM]. */
1035 bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
1037 if (bnrm > 0.f && bnrm < smlnum) {
1039 /* Scale matrix norm up to SMLNUM. */
1040 fprintf(stdout,"scaling B up to SML\n");
1042 slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
1045 } else if (bnrm > bignum) {
1047 /* Scale matrix norm down to BIGNUM. */
1048 fprintf(stdout,"scaling B down to BIG\n");
1050 slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
1055 /* If M < N make sure certain entries of B are zero. */
1059 fprintf(stdout,"zeroing parts of B \n");
1060 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1], ldb);
1063 /* Overdetermined case. */
1066 fprintf(stdout,"overdetermined, path 1 \n");
1068 /* Path 1 - overdetermined or exactly determined. */
1073 /* Path 1a - overdetermined, with many more rows than columns. */
1074 fprintf(stdout,"overdetermined, path 1a \n");
1080 /* Compute A=Q*R. */
1081 /* (Workspace: need 2*N, prefer N+N*NB) */
1083 i__1 = *lwork - nwork + 1;
1084 sgeqrf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
1087 /* Multiply B by transpose(Q). */
1088 /* (Workspace: need N+NRHS, prefer N+NRHS*NB) */
1090 i__1 = *lwork - nwork + 1;
1091 sormqr_("L", "T", m, nrhs, n, &a[a_offset], lda, &work[itau], &b[
1092 b_offset], ldb, &work[nwork], &i__1, info);
1094 /* Zero out below R. */
1099 slaset_("L", &i__1, &i__2, &c_b81, &c_b81, &a[a_dim1 + 2],
1109 /* Bidiagonalize R in A. */
1110 /* (Workspace: need 3*N+MM, prefer 3*N+(MM+N)*NB) */
1112 i__1 = *lwork - nwork + 1;
1113 sgebrd_(&mm, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
1114 work[itaup], &work[nwork], &i__1, info);
1116 /* Multiply B by transpose of left bidiagonalizing vectors of R. */
1117 /* (Workspace: need 3*N+NRHS, prefer 3*N+NRHS*NB) */
1119 i__1 = *lwork - nwork + 1;
1120 sormbr_("Q", "L", "T", &mm, nrhs, n, &a[a_offset], lda, &work[itauq],
1121 &b[b_offset], ldb, &work[nwork], &i__1, info);
1123 /* Solve the bidiagonal least squares problem. */
1125 slalsd_("U", &smlsiz, n, nrhs, &s[1], &work[ie], &b[b_offset], ldb,
1126 rcond, rank, &work[nwork], &iwork[1], info);
1128 fprintf(stdout,"info !=0 nach slalsd\n");
1132 /* Multiply B by right bidiagonalizing vectors of R. */
1134 i__1 = *lwork - nwork + 1;
1135 sormbr_("P", "L", "N", n, nrhs, n, &a[a_offset], lda, &work[itaup], &
1136 b[b_offset], ldb, &work[nwork], &i__1, info);
1138 } else /* if(complicated condition) */ {
1139 fprintf(stdout,"not overdetermined \n");
1141 i__1 = *m, i__2 = (*m << 1) - 4, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
1142 i__1,*nrhs), i__2 = *n - *m * 3, i__1 = f2cmax(i__1,i__2);
1143 if (*n >= mnthr && *lwork >= (*m << 2) + *m * *m + f2cmax(i__1,wlalsd)) {
1145 /* Path 2a - underdetermined, with many more columns than rows */
1146 /* and sufficient workspace for an efficient algorithm. */
1148 fprintf(stdout,"not overdetermined, path 2a\n");
1153 i__3 = *m, i__4 = (*m << 1) - 4, i__3 = f2cmax(i__3,i__4), i__3 =
1154 f2cmax(i__3,*nrhs), i__4 = *n - *m * 3;
1155 i__1 = (*m << 2) + *m * *lda + f2cmax(i__3,i__4), i__2 = *m * *lda +
1156 *m + *m * *nrhs, i__1 = f2cmax(i__1,i__2), i__2 = (*m << 2)
1157 + *m * *lda + wlalsd;
1158 if (*lwork >= f2cmax(i__1,i__2)) {
1164 /* Compute A=L*Q. */
1165 /* (Workspace: need 2*M, prefer M+M*NB) */
1167 i__1 = *lwork - nwork + 1;
1168 sgelqf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
1172 /* Copy L to WORK(IL), zeroing out above its diagonal. */
1174 slacpy_("L", m, m, &a[a_offset], lda, &work[il], &ldwork);
1177 slaset_("U", &i__1, &i__2, &c_b81, &c_b81, &work[il + ldwork], &
1179 ie = il + ldwork * *m;
1184 /* Bidiagonalize L in WORK(IL). */
1185 /* (Workspace: need M*M+5*M, prefer M*M+4*M+2*M*NB) */
1187 i__1 = *lwork - nwork + 1;
1188 sgebrd_(m, m, &work[il], &ldwork, &s[1], &work[ie], &work[itauq],
1189 &work[itaup], &work[nwork], &i__1, info);
1191 /* Multiply B by transpose of left bidiagonalizing vectors of L. */
1192 /* (Workspace: need M*M+4*M+NRHS, prefer M*M+4*M+NRHS*NB) */
1194 i__1 = *lwork - nwork + 1;
1195 sormbr_("Q", "L", "T", m, nrhs, m, &work[il], &ldwork, &work[
1196 itauq], &b[b_offset], ldb, &work[nwork], &i__1, info);
1198 /* Solve the bidiagonal least squares problem. */
1200 slalsd_("U", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
1201 ldb, rcond, rank, &work[nwork], &iwork[1], info);
1206 /* Multiply B by right bidiagonalizing vectors of L. */
1208 i__1 = *lwork - nwork + 1;
1209 sormbr_("P", "L", "N", m, nrhs, m, &work[il], &ldwork, &work[
1210 itaup], &b[b_offset], ldb, &work[nwork], &i__1, info);
1212 /* Zero out below first M rows of B. */
1215 slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1],
1219 /* Multiply transpose(Q) by B. */
1220 /* (Workspace: need M+NRHS, prefer M+NRHS*NB) */
1222 i__1 = *lwork - nwork + 1;
1223 sormlq_("L", "T", n, nrhs, m, &a[a_offset], lda, &work[itau], &b[
1224 b_offset], ldb, &work[nwork], &i__1, info);
1228 /* Path 2 - remaining underdetermined cases. */
1229 fprintf(stdout,"other underdetermined, path 2");
1236 /* Bidiagonalize A. */
1237 /* (Workspace: need 3*M+N, prefer 3*M+(M+N)*NB) */
1239 i__1 = *lwork - nwork + 1;
1240 sgebrd_(m, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
1241 work[itaup], &work[nwork], &i__1, info);
1243 /* Multiply B by transpose of left bidiagonalizing vectors. */
1244 /* (Workspace: need 3*M+NRHS, prefer 3*M+NRHS*NB) */
1246 i__1 = *lwork - nwork + 1;
1247 sormbr_("Q", "L", "T", m, nrhs, n, &a[a_offset], lda, &work[itauq]
1248 , &b[b_offset], ldb, &work[nwork], &i__1, info);
1250 /* Solve the bidiagonal least squares problem. */
1252 slalsd_("L", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
1253 ldb, rcond, rank, &work[nwork], &iwork[1], info);
1258 /* Multiply B by right bidiagonalizing vectors of A. */
1260 i__1 = *lwork - nwork + 1;
1261 sormbr_("P", "L", "N", n, nrhs, m, &a[a_offset], lda, &work[itaup]
1262 , &b[b_offset], ldb, &work[nwork], &i__1, info);
1270 fprintf(stdout," unscaling a1\n");
1271 slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
1273 slascl_("G", &c__0, &c__0, &smlnum, &anrm, &minmn, &c__1, &s[1], &
1275 } else if (iascl == 2) {
1276 fprintf(stdout," unscaling a2\n");
1277 slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
1279 slascl_("G", &c__0, &c__0, &bignum, &anrm, &minmn, &c__1, &s[1], &
1283 fprintf(stdout," unscaling b1\n");
1284 slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
1286 } else if (ibscl == 2) {
1287 fprintf(stdout," unscaling b2\n");
1288 slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
1293 work[1] = (real) maxwrk;
1295 fprintf(stdout, "end of SGELSD\n");