You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgesvdx.c 37 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef blasint logical;
  52. typedef char logical1;
  53. typedef char integer1;
  54. #define TRUE_ (1)
  55. #define FALSE_ (0)
  56. /* Extern is for use with -E */
  57. #ifndef Extern
  58. #define Extern extern
  59. #endif
  60. /* I/O stuff */
  61. typedef int flag;
  62. typedef int ftnlen;
  63. typedef int ftnint;
  64. /*external read, write*/
  65. typedef struct
  66. { flag cierr;
  67. ftnint ciunit;
  68. flag ciend;
  69. char *cifmt;
  70. ftnint cirec;
  71. } cilist;
  72. /*internal read, write*/
  73. typedef struct
  74. { flag icierr;
  75. char *iciunit;
  76. flag iciend;
  77. char *icifmt;
  78. ftnint icirlen;
  79. ftnint icirnum;
  80. } icilist;
  81. /*open*/
  82. typedef struct
  83. { flag oerr;
  84. ftnint ounit;
  85. char *ofnm;
  86. ftnlen ofnmlen;
  87. char *osta;
  88. char *oacc;
  89. char *ofm;
  90. ftnint orl;
  91. char *oblnk;
  92. } olist;
  93. /*close*/
  94. typedef struct
  95. { flag cerr;
  96. ftnint cunit;
  97. char *csta;
  98. } cllist;
  99. /*rewind, backspace, endfile*/
  100. typedef struct
  101. { flag aerr;
  102. ftnint aunit;
  103. } alist;
  104. /* inquire */
  105. typedef struct
  106. { flag inerr;
  107. ftnint inunit;
  108. char *infile;
  109. ftnlen infilen;
  110. ftnint *inex; /*parameters in standard's order*/
  111. ftnint *inopen;
  112. ftnint *innum;
  113. ftnint *innamed;
  114. char *inname;
  115. ftnlen innamlen;
  116. char *inacc;
  117. ftnlen inacclen;
  118. char *inseq;
  119. ftnlen inseqlen;
  120. char *indir;
  121. ftnlen indirlen;
  122. char *infmt;
  123. ftnlen infmtlen;
  124. char *inform;
  125. ftnint informlen;
  126. char *inunf;
  127. ftnlen inunflen;
  128. ftnint *inrecl;
  129. ftnint *innrec;
  130. char *inblank;
  131. ftnlen inblanklen;
  132. } inlist;
  133. #define VOID void
  134. union Multitype { /* for multiple entry points */
  135. integer1 g;
  136. shortint h;
  137. integer i;
  138. /* longint j; */
  139. real r;
  140. doublereal d;
  141. complex c;
  142. doublecomplex z;
  143. };
  144. typedef union Multitype Multitype;
  145. struct Vardesc { /* for Namelist */
  146. char *name;
  147. char *addr;
  148. ftnlen *dims;
  149. int type;
  150. };
  151. typedef struct Vardesc Vardesc;
  152. struct Namelist {
  153. char *name;
  154. Vardesc **vars;
  155. int nvars;
  156. };
  157. typedef struct Namelist Namelist;
  158. #define abs(x) ((x) >= 0 ? (x) : -(x))
  159. #define dabs(x) (fabs(x))
  160. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  161. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  162. #define dmin(a,b) (f2cmin(a,b))
  163. #define dmax(a,b) (f2cmax(a,b))
  164. #define bit_test(a,b) ((a) >> (b) & 1)
  165. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  166. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  167. #define abort_() { sig_die("Fortran abort routine called", 1); }
  168. #define c_abs(z) (cabsf(Cf(z)))
  169. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  170. #ifdef _MSC_VER
  171. #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]);}
  172. #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]);}
  173. #else
  174. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  175. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  176. #endif
  177. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  178. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  179. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  180. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  181. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  182. #define d_abs(x) (fabs(*(x)))
  183. #define d_acos(x) (acos(*(x)))
  184. #define d_asin(x) (asin(*(x)))
  185. #define d_atan(x) (atan(*(x)))
  186. #define d_atn2(x, y) (atan2(*(x),*(y)))
  187. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  188. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  189. #define d_cos(x) (cos(*(x)))
  190. #define d_cosh(x) (cosh(*(x)))
  191. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  192. #define d_exp(x) (exp(*(x)))
  193. #define d_imag(z) (cimag(Cd(z)))
  194. #define r_imag(z) (cimagf(Cf(z)))
  195. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  196. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  198. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define d_log(x) (log(*(x)))
  200. #define d_mod(x, y) (fmod(*(x), *(y)))
  201. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  202. #define d_nint(x) u_nint(*(x))
  203. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  204. #define d_sign(a,b) u_sign(*(a),*(b))
  205. #define r_sign(a,b) u_sign(*(a),*(b))
  206. #define d_sin(x) (sin(*(x)))
  207. #define d_sinh(x) (sinh(*(x)))
  208. #define d_sqrt(x) (sqrt(*(x)))
  209. #define d_tan(x) (tan(*(x)))
  210. #define d_tanh(x) (tanh(*(x)))
  211. #define i_abs(x) abs(*(x))
  212. #define i_dnnt(x) ((integer)u_nint(*(x)))
  213. #define i_len(s, n) (n)
  214. #define i_nint(x) ((integer)u_nint(*(x)))
  215. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  216. #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++ = ' '; }
  217. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  218. #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]; }
  219. #define sig_die(s, kill) { exit(1); }
  220. #define s_stop(s, n) {exit(0);}
  221. #define z_abs(z) (cabs(Cd(z)))
  222. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  223. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  224. #define myexit_() break;
  225. #define mycycle() continue;
  226. #define myceiling(w) {ceil(w)}
  227. #define myhuge(w) {HUGE_VAL}
  228. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  229. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  230. /* -- translated by f2c (version 20000121).
  231. You must link the resulting object file with the libraries:
  232. -lf2c -lm (in that order)
  233. */
  234. /* Table of constant values */
  235. static integer c__6 = 6;
  236. static integer c__0 = 0;
  237. static integer c__2 = 2;
  238. static integer c__1 = 1;
  239. static integer c_n1 = -1;
  240. static real c_b109 = 0.f;
  241. /* > \brief <b> SGESVDX computes the singular value decomposition (SVD) for GE matrices</b> */
  242. /* =========== DOCUMENTATION =========== */
  243. /* Online html documentation available at */
  244. /* http://www.netlib.org/lapack/explore-html/ */
  245. /* > \htmlonly */
  246. /* > Download SGESVDX + dependencies */
  247. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvdx
  248. .f"> */
  249. /* > [TGZ]</a> */
  250. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvdx
  251. .f"> */
  252. /* > [ZIP]</a> */
  253. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvdx
  254. .f"> */
  255. /* > [TXT]</a> */
  256. /* > \endhtmlonly */
  257. /* Definition: */
  258. /* =========== */
  259. /* SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, */
  260. /* $ IL, IU, NS, S, U, LDU, VT, LDVT, WORK, */
  261. /* $ LWORK, IWORK, INFO ) */
  262. /* CHARACTER JOBU, JOBVT, RANGE */
  263. /* INTEGER IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS */
  264. /* REAL VL, VU */
  265. /* INTEGER IWORK( * ) */
  266. /* REAL A( LDA, * ), S( * ), U( LDU, * ), */
  267. /* $ VT( LDVT, * ), WORK( * ) */
  268. /* > \par Purpose: */
  269. /* ============= */
  270. /* > */
  271. /* > \verbatim */
  272. /* > */
  273. /* > SGESVDX computes the singular value decomposition (SVD) of a real */
  274. /* > M-by-N matrix A, optionally computing the left and/or right singular */
  275. /* > vectors. The SVD is written */
  276. /* > */
  277. /* > A = U * SIGMA * transpose(V) */
  278. /* > */
  279. /* > where SIGMA is an M-by-N matrix which is zero except for its */
  280. /* > f2cmin(m,n) diagonal elements, U is an M-by-M orthogonal matrix, and */
  281. /* > V is an N-by-N orthogonal matrix. The diagonal elements of SIGMA */
  282. /* > are the singular values of A; they are real and non-negative, and */
  283. /* > are returned in descending order. The first f2cmin(m,n) columns of */
  284. /* > U and V are the left and right singular vectors of A. */
  285. /* > */
  286. /* > SGESVDX uses an eigenvalue problem for obtaining the SVD, which */
  287. /* > allows for the computation of a subset of singular values and */
  288. /* > vectors. See SBDSVDX for details. */
  289. /* > */
  290. /* > Note that the routine returns V**T, not V. */
  291. /* > \endverbatim */
  292. /* Arguments: */
  293. /* ========== */
  294. /* > \param[in] JOBU */
  295. /* > \verbatim */
  296. /* > JOBU is CHARACTER*1 */
  297. /* > Specifies options for computing all or part of the matrix U: */
  298. /* > = 'V': the first f2cmin(m,n) columns of U (the left singular */
  299. /* > vectors) or as specified by RANGE are returned in */
  300. /* > the array U; */
  301. /* > = 'N': no columns of U (no left singular vectors) are */
  302. /* > computed. */
  303. /* > \endverbatim */
  304. /* > */
  305. /* > \param[in] JOBVT */
  306. /* > \verbatim */
  307. /* > JOBVT is CHARACTER*1 */
  308. /* > Specifies options for computing all or part of the matrix */
  309. /* > V**T: */
  310. /* > = 'V': the first f2cmin(m,n) rows of V**T (the right singular */
  311. /* > vectors) or as specified by RANGE are returned in */
  312. /* > the array VT; */
  313. /* > = 'N': no rows of V**T (no right singular vectors) are */
  314. /* > computed. */
  315. /* > \endverbatim */
  316. /* > */
  317. /* > \param[in] RANGE */
  318. /* > \verbatim */
  319. /* > RANGE is CHARACTER*1 */
  320. /* > = 'A': all singular values will be found. */
  321. /* > = 'V': all singular values in the half-open interval (VL,VU] */
  322. /* > will be found. */
  323. /* > = 'I': the IL-th through IU-th singular values will be found. */
  324. /* > \endverbatim */
  325. /* > */
  326. /* > \param[in] M */
  327. /* > \verbatim */
  328. /* > M is INTEGER */
  329. /* > The number of rows of the input matrix A. M >= 0. */
  330. /* > \endverbatim */
  331. /* > */
  332. /* > \param[in] N */
  333. /* > \verbatim */
  334. /* > N is INTEGER */
  335. /* > The number of columns of the input matrix A. N >= 0. */
  336. /* > \endverbatim */
  337. /* > */
  338. /* > \param[in,out] A */
  339. /* > \verbatim */
  340. /* > A is REAL array, dimension (LDA,N) */
  341. /* > On entry, the M-by-N matrix A. */
  342. /* > On exit, the contents of A are destroyed. */
  343. /* > \endverbatim */
  344. /* > */
  345. /* > \param[in] LDA */
  346. /* > \verbatim */
  347. /* > LDA is INTEGER */
  348. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  349. /* > \endverbatim */
  350. /* > */
  351. /* > \param[in] VL */
  352. /* > \verbatim */
  353. /* > VL is REAL */
  354. /* > If RANGE='V', the lower bound of the interval to */
  355. /* > be searched for singular values. VU > VL. */
  356. /* > Not referenced if RANGE = 'A' or 'I'. */
  357. /* > \endverbatim */
  358. /* > */
  359. /* > \param[in] VU */
  360. /* > \verbatim */
  361. /* > VU is REAL */
  362. /* > If RANGE='V', the upper bound of the interval to */
  363. /* > be searched for singular values. VU > VL. */
  364. /* > Not referenced if RANGE = 'A' or 'I'. */
  365. /* > \endverbatim */
  366. /* > */
  367. /* > \param[in] IL */
  368. /* > \verbatim */
  369. /* > IL is INTEGER */
  370. /* > If RANGE='I', the index of the */
  371. /* > smallest singular value to be returned. */
  372. /* > 1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
  373. /* > Not referenced if RANGE = 'A' or 'V'. */
  374. /* > \endverbatim */
  375. /* > */
  376. /* > \param[in] IU */
  377. /* > \verbatim */
  378. /* > IU is INTEGER */
  379. /* > If RANGE='I', the index of the */
  380. /* > largest singular value to be returned. */
  381. /* > 1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
  382. /* > Not referenced if RANGE = 'A' or 'V'. */
  383. /* > \endverbatim */
  384. /* > */
  385. /* > \param[out] NS */
  386. /* > \verbatim */
  387. /* > NS is INTEGER */
  388. /* > The total number of singular values found, */
  389. /* > 0 <= NS <= f2cmin(M,N). */
  390. /* > If RANGE = 'A', NS = f2cmin(M,N); if RANGE = 'I', NS = IU-IL+1. */
  391. /* > \endverbatim */
  392. /* > */
  393. /* > \param[out] S */
  394. /* > \verbatim */
  395. /* > S is REAL array, dimension (f2cmin(M,N)) */
  396. /* > The singular values of A, sorted so that S(i) >= S(i+1). */
  397. /* > \endverbatim */
  398. /* > */
  399. /* > \param[out] U */
  400. /* > \verbatim */
  401. /* > U is REAL array, dimension (LDU,UCOL) */
  402. /* > If JOBU = 'V', U contains columns of U (the left singular */
  403. /* > vectors, stored columnwise) as specified by RANGE; if */
  404. /* > JOBU = 'N', U is not referenced. */
  405. /* > Note: The user must ensure that UCOL >= NS; if RANGE = 'V', */
  406. /* > the exact value of NS is not known in advance and an upper */
  407. /* > bound must be used. */
  408. /* > \endverbatim */
  409. /* > */
  410. /* > \param[in] LDU */
  411. /* > \verbatim */
  412. /* > LDU is INTEGER */
  413. /* > The leading dimension of the array U. LDU >= 1; if */
  414. /* > JOBU = 'V', LDU >= M. */
  415. /* > \endverbatim */
  416. /* > */
  417. /* > \param[out] VT */
  418. /* > \verbatim */
  419. /* > VT is REAL array, dimension (LDVT,N) */
  420. /* > If JOBVT = 'V', VT contains the rows of V**T (the right singular */
  421. /* > vectors, stored rowwise) as specified by RANGE; if JOBVT = 'N', */
  422. /* > VT is not referenced. */
  423. /* > Note: The user must ensure that LDVT >= NS; if RANGE = 'V', */
  424. /* > the exact value of NS is not known in advance and an upper */
  425. /* > bound must be used. */
  426. /* > \endverbatim */
  427. /* > */
  428. /* > \param[in] LDVT */
  429. /* > \verbatim */
  430. /* > LDVT is INTEGER */
  431. /* > The leading dimension of the array VT. LDVT >= 1; if */
  432. /* > JOBVT = 'V', LDVT >= NS (see above). */
  433. /* > \endverbatim */
  434. /* > */
  435. /* > \param[out] WORK */
  436. /* > \verbatim */
  437. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  438. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK; */
  439. /* > \endverbatim */
  440. /* > */
  441. /* > \param[in] LWORK */
  442. /* > \verbatim */
  443. /* > LWORK is INTEGER */
  444. /* > The dimension of the array WORK. */
  445. /* > LWORK >= MAX(1,MIN(M,N)*(MIN(M,N)+4)) for the paths (see */
  446. /* > comments inside the code): */
  447. /* > - PATH 1 (M much larger than N) */
  448. /* > - PATH 1t (N much larger than M) */
  449. /* > LWORK >= MAX(1,MIN(M,N)*2+MAX(M,N)) for the other paths. */
  450. /* > For good performance, LWORK should generally be larger. */
  451. /* > */
  452. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  453. /* > only calculates the optimal size of the WORK array, returns */
  454. /* > this value as the first entry of the WORK array, and no error */
  455. /* > message related to LWORK is issued by XERBLA. */
  456. /* > \endverbatim */
  457. /* > */
  458. /* > \param[out] IWORK */
  459. /* > \verbatim */
  460. /* > IWORK is INTEGER array, dimension (12*MIN(M,N)) */
  461. /* > If INFO = 0, the first NS elements of IWORK are zero. If INFO > 0, */
  462. /* > then IWORK contains the indices of the eigenvectors that failed */
  463. /* > to converge in SBDSVDX/SSTEVX. */
  464. /* > \endverbatim */
  465. /* > */
  466. /* > \param[out] INFO */
  467. /* > \verbatim */
  468. /* > INFO is INTEGER */
  469. /* > = 0: successful exit */
  470. /* > < 0: if INFO = -i, the i-th argument had an illegal value */
  471. /* > > 0: if INFO = i, then i eigenvectors failed to converge */
  472. /* > in SBDSVDX/SSTEVX. */
  473. /* > if INFO = N*2 + 1, an internal error occurred in */
  474. /* > SBDSVDX */
  475. /* > \endverbatim */
  476. /* Authors: */
  477. /* ======== */
  478. /* > \author Univ. of Tennessee */
  479. /* > \author Univ. of California Berkeley */
  480. /* > \author Univ. of Colorado Denver */
  481. /* > \author NAG Ltd. */
  482. /* > \date June 2016 */
  483. /* > \ingroup realGEsing */
  484. /* ===================================================================== */
  485. /* Subroutine */ void sgesvdx_(char *jobu, char *jobvt, char *range, integer *
  486. m, integer *n, real *a, integer *lda, real *vl, real *vu, integer *il,
  487. integer *iu, integer *ns, real *s, real *u, integer *ldu, real *vt,
  488. integer *ldvt, real *work, integer *lwork, integer *iwork, integer *
  489. info)
  490. {
  491. /* System generated locals */
  492. address a__1[2];
  493. integer a_dim1, a_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1[2],
  494. i__2, i__3;
  495. char ch__1[2];
  496. /* Local variables */
  497. integer iscl;
  498. logical alls, inds;
  499. integer ilqf;
  500. real anrm;
  501. integer ierr, iqrf, itau;
  502. char jobz[1];
  503. logical vals;
  504. integer i__, j;
  505. extern logical lsame_(char *, char *);
  506. integer iltgk, itemp, minmn, itaup, itauq, iutgk, itgkz, mnthr;
  507. extern /* Subroutine */ void scopy_(integer *, real *, integer *, real *,
  508. integer *);
  509. logical wantu;
  510. integer id, ie;
  511. extern /* Subroutine */ void sgebrd_(integer *, integer *, real *, integer
  512. *, real *, real *, real *, real *, real *, integer *, integer *);
  513. extern real slamch_(char *), slange_(char *, integer *, integer *,
  514. real *, integer *, real *);
  515. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  516. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  517. integer *, integer *, ftnlen, ftnlen);
  518. real bignum;
  519. extern /* Subroutine */ void sgelqf_(integer *, integer *, real *, integer
  520. *, real *, real *, integer *, integer *), slascl_(char *, integer
  521. *, integer *, real *, real *, integer *, integer *, real *,
  522. integer *, integer *);
  523. real abstol;
  524. extern /* Subroutine */ void sgeqrf_(integer *, integer *, real *, integer
  525. *, real *, real *, integer *, integer *), slacpy_(char *, integer
  526. *, integer *, real *, integer *, real *, integer *);
  527. char rngtgk[1];
  528. extern /* Subroutine */ void slaset_(char *, integer *, integer *, real *,
  529. real *, real *, integer *), sormbr_(char *, char *, char *
  530. , integer *, integer *, integer *, real *, integer *, real *,
  531. real *, integer *, real *, integer *, integer *);
  532. integer minwrk, maxwrk;
  533. real smlnum;
  534. extern /* Subroutine */ void sormlq_(char *, char *, integer *, integer *,
  535. integer *, real *, integer *, real *, real *, integer *, real *,
  536. integer *, integer *);
  537. logical lquery, wantvt;
  538. extern /* Subroutine */ void sormqr_(char *, char *, integer *, integer *,
  539. integer *, real *, integer *, real *, real *, integer *, real *,
  540. integer *, integer *);
  541. real dum[1], eps;
  542. extern /* Subroutine */ void sbdsvdx_(char *, char *, char *, integer *,
  543. real *, real *, real *, real *, integer *, integer *, integer *,
  544. real *, real *, integer *, real *, integer *, integer *);
  545. /* -- LAPACK driver routine (version 3.8.0) -- */
  546. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  547. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  548. /* June 2016 */
  549. /* ===================================================================== */
  550. /* Test the input arguments. */
  551. /* Parameter adjustments */
  552. a_dim1 = *lda;
  553. a_offset = 1 + a_dim1 * 1;
  554. a -= a_offset;
  555. --s;
  556. u_dim1 = *ldu;
  557. u_offset = 1 + u_dim1 * 1;
  558. u -= u_offset;
  559. vt_dim1 = *ldvt;
  560. vt_offset = 1 + vt_dim1 * 1;
  561. vt -= vt_offset;
  562. --work;
  563. --iwork;
  564. /* Function Body */
  565. *ns = 0;
  566. *info = 0;
  567. abstol = slamch_("S") * 2;
  568. lquery = *lwork == -1;
  569. minmn = f2cmin(*m,*n);
  570. wantu = lsame_(jobu, "V");
  571. wantvt = lsame_(jobvt, "V");
  572. if (wantu || wantvt) {
  573. *(unsigned char *)jobz = 'V';
  574. } else {
  575. *(unsigned char *)jobz = 'N';
  576. }
  577. alls = lsame_(range, "A");
  578. vals = lsame_(range, "V");
  579. inds = lsame_(range, "I");
  580. *info = 0;
  581. if (! lsame_(jobu, "V") && ! lsame_(jobu, "N")) {
  582. *info = -1;
  583. } else if (! lsame_(jobvt, "V") && ! lsame_(jobvt,
  584. "N")) {
  585. *info = -2;
  586. } else if (! (alls || vals || inds)) {
  587. *info = -3;
  588. } else if (*m < 0) {
  589. *info = -4;
  590. } else if (*n < 0) {
  591. *info = -5;
  592. } else if (*m > *lda) {
  593. *info = -7;
  594. } else if (minmn > 0) {
  595. if (vals) {
  596. if (*vl < 0.f) {
  597. *info = -8;
  598. } else if (*vu <= *vl) {
  599. *info = -9;
  600. }
  601. } else if (inds) {
  602. if (*il < 1 || *il > f2cmax(1,minmn)) {
  603. *info = -10;
  604. } else if (*iu < f2cmin(minmn,*il) || *iu > minmn) {
  605. *info = -11;
  606. }
  607. }
  608. if (*info == 0) {
  609. if (wantu && *ldu < *m) {
  610. *info = -15;
  611. } else if (wantvt) {
  612. if (inds) {
  613. if (*ldvt < *iu - *il + 1) {
  614. *info = -17;
  615. }
  616. } else if (*ldvt < minmn) {
  617. *info = -17;
  618. }
  619. }
  620. }
  621. }
  622. /* Compute workspace */
  623. /* (Note: Comments in the code beginning "Workspace:" describe the */
  624. /* minimal amount of workspace needed at that point in the code, */
  625. /* as well as the preferred amount for good performance. */
  626. /* NB refers to the optimal block size for the immediately */
  627. /* following subroutine, as returned by ILAENV.) */
  628. if (*info == 0) {
  629. minwrk = 1;
  630. maxwrk = 1;
  631. if (minmn > 0) {
  632. if (*m >= *n) {
  633. /* Writing concatenation */
  634. i__1[0] = 1, a__1[0] = jobu;
  635. i__1[1] = 1, a__1[1] = jobvt;
  636. s_cat(ch__1, a__1, i__1, &c__2, (ftnlen)2);
  637. mnthr = ilaenv_(&c__6, "SGESVD", ch__1, m, n, &c__0, &c__0, (
  638. ftnlen)6, (ftnlen)2);
  639. if (*m >= mnthr) {
  640. /* Path 1 (M much larger than N) */
  641. maxwrk = *n + *n * ilaenv_(&c__1, "SGEQRF", " ", m, n, &
  642. c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  643. /* Computing MAX */
  644. i__2 = maxwrk, i__3 = *n * (*n + 5) + (*n << 1) * ilaenv_(
  645. &c__1, "SGEBRD", " ", n, n, &c_n1, &c_n1, (ftnlen)
  646. 6, (ftnlen)1);
  647. maxwrk = f2cmax(i__2,i__3);
  648. if (wantu) {
  649. /* Computing MAX */
  650. i__2 = maxwrk, i__3 = *n * (*n * 3 + 6) + *n *
  651. ilaenv_(&c__1, "SORMQR", " ", n, n, &c_n1, &
  652. c_n1, (ftnlen)6, (ftnlen)1);
  653. maxwrk = f2cmax(i__2,i__3);
  654. }
  655. if (wantvt) {
  656. /* Computing MAX */
  657. i__2 = maxwrk, i__3 = *n * (*n * 3 + 6) + *n *
  658. ilaenv_(&c__1, "SORMLQ", " ", n, n, &c_n1, &
  659. c_n1, (ftnlen)6, (ftnlen)1);
  660. maxwrk = f2cmax(i__2,i__3);
  661. }
  662. minwrk = *n * (*n * 3 + 20);
  663. } else {
  664. /* Path 2 (M at least N, but not much larger) */
  665. maxwrk = (*n << 2) + (*m + *n) * ilaenv_(&c__1, "SGEBRD",
  666. " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  667. if (wantu) {
  668. /* Computing MAX */
  669. i__2 = maxwrk, i__3 = *n * ((*n << 1) + 5) + *n *
  670. ilaenv_(&c__1, "SORMQR", " ", n, n, &c_n1, &
  671. c_n1, (ftnlen)6, (ftnlen)1);
  672. maxwrk = f2cmax(i__2,i__3);
  673. }
  674. if (wantvt) {
  675. /* Computing MAX */
  676. i__2 = maxwrk, i__3 = *n * ((*n << 1) + 5) + *n *
  677. ilaenv_(&c__1, "SORMLQ", " ", n, n, &c_n1, &
  678. c_n1, (ftnlen)6, (ftnlen)1);
  679. maxwrk = f2cmax(i__2,i__3);
  680. }
  681. /* Computing MAX */
  682. i__2 = *n * ((*n << 1) + 19), i__3 = (*n << 2) + *m;
  683. minwrk = f2cmax(i__2,i__3);
  684. }
  685. } else {
  686. /* Writing concatenation */
  687. i__1[0] = 1, a__1[0] = jobu;
  688. i__1[1] = 1, a__1[1] = jobvt;
  689. s_cat(ch__1, a__1, i__1, &c__2, (ftnlen)2);
  690. mnthr = ilaenv_(&c__6, "SGESVD", ch__1, m, n, &c__0, &c__0, (
  691. ftnlen)6, (ftnlen)2);
  692. if (*n >= mnthr) {
  693. /* Path 1t (N much larger than M) */
  694. maxwrk = *m + *m * ilaenv_(&c__1, "SGELQF", " ", m, n, &
  695. c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  696. /* Computing MAX */
  697. i__2 = maxwrk, i__3 = *m * (*m + 5) + (*m << 1) * ilaenv_(
  698. &c__1, "SGEBRD", " ", m, m, &c_n1, &c_n1, (ftnlen)
  699. 6, (ftnlen)1);
  700. maxwrk = f2cmax(i__2,i__3);
  701. if (wantu) {
  702. /* Computing MAX */
  703. i__2 = maxwrk, i__3 = *m * (*m * 3 + 6) + *m *
  704. ilaenv_(&c__1, "SORMQR", " ", m, m, &c_n1, &
  705. c_n1, (ftnlen)6, (ftnlen)1);
  706. maxwrk = f2cmax(i__2,i__3);
  707. }
  708. if (wantvt) {
  709. /* Computing MAX */
  710. i__2 = maxwrk, i__3 = *m * (*m * 3 + 6) + *m *
  711. ilaenv_(&c__1, "SORMLQ", " ", m, m, &c_n1, &
  712. c_n1, (ftnlen)6, (ftnlen)1);
  713. maxwrk = f2cmax(i__2,i__3);
  714. }
  715. minwrk = *m * (*m * 3 + 20);
  716. } else {
  717. /* Path 2t (N at least M, but not much larger) */
  718. maxwrk = (*m << 2) + (*m + *n) * ilaenv_(&c__1, "SGEBRD",
  719. " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  720. if (wantu) {
  721. /* Computing MAX */
  722. i__2 = maxwrk, i__3 = *m * ((*m << 1) + 5) + *m *
  723. ilaenv_(&c__1, "SORMQR", " ", m, m, &c_n1, &
  724. c_n1, (ftnlen)6, (ftnlen)1);
  725. maxwrk = f2cmax(i__2,i__3);
  726. }
  727. if (wantvt) {
  728. /* Computing MAX */
  729. i__2 = maxwrk, i__3 = *m * ((*m << 1) + 5) + *m *
  730. ilaenv_(&c__1, "SORMLQ", " ", m, m, &c_n1, &
  731. c_n1, (ftnlen)6, (ftnlen)1);
  732. maxwrk = f2cmax(i__2,i__3);
  733. }
  734. /* Computing MAX */
  735. i__2 = *m * ((*m << 1) + 19), i__3 = (*m << 2) + *n;
  736. minwrk = f2cmax(i__2,i__3);
  737. }
  738. }
  739. }
  740. maxwrk = f2cmax(maxwrk,minwrk);
  741. work[1] = (real) maxwrk;
  742. if (*lwork < minwrk && ! lquery) {
  743. *info = -19;
  744. }
  745. }
  746. if (*info != 0) {
  747. i__2 = -(*info);
  748. xerbla_("SGESVDX", &i__2, (ftnlen)7);
  749. return;
  750. } else if (lquery) {
  751. return;
  752. }
  753. /* Quick return if possible */
  754. if (*m == 0 || *n == 0) {
  755. return;
  756. }
  757. /* Set singular values indices accord to RANGE. */
  758. if (alls) {
  759. *(unsigned char *)rngtgk = 'I';
  760. iltgk = 1;
  761. iutgk = f2cmin(*m,*n);
  762. } else if (inds) {
  763. *(unsigned char *)rngtgk = 'I';
  764. iltgk = *il;
  765. iutgk = *iu;
  766. } else {
  767. *(unsigned char *)rngtgk = 'V';
  768. iltgk = 0;
  769. iutgk = 0;
  770. }
  771. /* Get machine constants */
  772. eps = slamch_("P");
  773. smlnum = sqrt(slamch_("S")) / eps;
  774. bignum = 1.f / smlnum;
  775. /* Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
  776. anrm = slange_("M", m, n, &a[a_offset], lda, dum);
  777. iscl = 0;
  778. if (anrm > 0.f && anrm < smlnum) {
  779. iscl = 1;
  780. slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  781. info);
  782. } else if (anrm > bignum) {
  783. iscl = 1;
  784. slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  785. info);
  786. }
  787. if (*m >= *n) {
  788. /* A has at least as many rows as columns. If A has sufficiently */
  789. /* more rows than columns, first reduce A using the QR */
  790. /* decomposition. */
  791. if (*m >= mnthr) {
  792. /* Path 1 (M much larger than N): */
  793. /* A = Q * R = Q * ( QB * B * PB**T ) */
  794. /* = Q * ( QB * ( UB * S * VB**T ) * PB**T ) */
  795. /* U = Q * QB * UB; V**T = VB**T * PB**T */
  796. /* Compute A=Q*R */
  797. /* (Workspace: need 2*N, prefer N+N*NB) */
  798. itau = 1;
  799. itemp = itau + *n;
  800. i__2 = *lwork - itemp + 1;
  801. sgeqrf_(m, n, &a[a_offset], lda, &work[itau], &work[itemp], &i__2,
  802. info);
  803. /* Copy R into WORK and bidiagonalize it: */
  804. /* (Workspace: need N*N+5*N, prefer N*N+4*N+2*N*NB) */
  805. iqrf = itemp;
  806. id = iqrf + *n * *n;
  807. ie = id + *n;
  808. itauq = ie + *n;
  809. itaup = itauq + *n;
  810. itemp = itaup + *n;
  811. slacpy_("U", n, n, &a[a_offset], lda, &work[iqrf], n);
  812. i__2 = *n - 1;
  813. i__3 = *n - 1;
  814. slaset_("L", &i__2, &i__3, &c_b109, &c_b109, &work[iqrf + 1], n);
  815. i__2 = *lwork - itemp + 1;
  816. sgebrd_(n, n, &work[iqrf], n, &work[id], &work[ie], &work[itauq],
  817. &work[itaup], &work[itemp], &i__2, info);
  818. /* Solve eigenvalue problem TGK*Z=Z*S. */
  819. /* (Workspace: need 14*N + 2*N*(N+1)) */
  820. itgkz = itemp;
  821. itemp = itgkz + *n * ((*n << 1) + 1);
  822. i__2 = *n << 1;
  823. sbdsvdx_("U", jobz, rngtgk, n, &work[id], &work[ie], vl, vu, &
  824. iltgk, &iutgk, ns, &s[1], &work[itgkz], &i__2, &work[
  825. itemp], &iwork[1], info);
  826. /* If needed, compute left singular vectors. */
  827. if (wantu) {
  828. j = itgkz;
  829. i__2 = *ns;
  830. for (i__ = 1; i__ <= i__2; ++i__) {
  831. scopy_(n, &work[j], &c__1, &u[i__ * u_dim1 + 1], &c__1);
  832. j += *n << 1;
  833. }
  834. i__2 = *m - *n;
  835. slaset_("A", &i__2, ns, &c_b109, &c_b109, &u[*n + 1 + u_dim1],
  836. ldu);
  837. /* Call SORMBR to compute QB*UB. */
  838. /* (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
  839. i__2 = *lwork - itemp + 1;
  840. sormbr_("Q", "L", "N", n, ns, n, &work[iqrf], n, &work[itauq],
  841. &u[u_offset], ldu, &work[itemp], &i__2, info);
  842. /* Call SORMQR to compute Q*(QB*UB). */
  843. /* (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
  844. i__2 = *lwork - itemp + 1;
  845. sormqr_("L", "N", m, ns, n, &a[a_offset], lda, &work[itau], &
  846. u[u_offset], ldu, &work[itemp], &i__2, info);
  847. }
  848. /* If needed, compute right singular vectors. */
  849. if (wantvt) {
  850. j = itgkz + *n;
  851. i__2 = *ns;
  852. for (i__ = 1; i__ <= i__2; ++i__) {
  853. scopy_(n, &work[j], &c__1, &vt[i__ + vt_dim1], ldvt);
  854. j += *n << 1;
  855. }
  856. /* Call SORMBR to compute VB**T * PB**T */
  857. /* (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
  858. i__2 = *lwork - itemp + 1;
  859. sormbr_("P", "R", "T", ns, n, n, &work[iqrf], n, &work[itaup],
  860. &vt[vt_offset], ldvt, &work[itemp], &i__2, info);
  861. }
  862. } else {
  863. /* Path 2 (M at least N, but not much larger) */
  864. /* Reduce A to bidiagonal form without QR decomposition */
  865. /* A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T */
  866. /* U = QB * UB; V**T = VB**T * PB**T */
  867. /* Bidiagonalize A */
  868. /* (Workspace: need 4*N+M, prefer 4*N+(M+N)*NB) */
  869. id = 1;
  870. ie = id + *n;
  871. itauq = ie + *n;
  872. itaup = itauq + *n;
  873. itemp = itaup + *n;
  874. i__2 = *lwork - itemp + 1;
  875. sgebrd_(m, n, &a[a_offset], lda, &work[id], &work[ie], &work[
  876. itauq], &work[itaup], &work[itemp], &i__2, info);
  877. /* Solve eigenvalue problem TGK*Z=Z*S. */
  878. /* (Workspace: need 14*N + 2*N*(N+1)) */
  879. itgkz = itemp;
  880. itemp = itgkz + *n * ((*n << 1) + 1);
  881. i__2 = *n << 1;
  882. sbdsvdx_("U", jobz, rngtgk, n, &work[id], &work[ie], vl, vu, &
  883. iltgk, &iutgk, ns, &s[1], &work[itgkz], &i__2, &work[
  884. itemp], &iwork[1], info);
  885. /* If needed, compute left singular vectors. */
  886. if (wantu) {
  887. j = itgkz;
  888. i__2 = *ns;
  889. for (i__ = 1; i__ <= i__2; ++i__) {
  890. scopy_(n, &work[j], &c__1, &u[i__ * u_dim1 + 1], &c__1);
  891. j += *n << 1;
  892. }
  893. i__2 = *m - *n;
  894. slaset_("A", &i__2, ns, &c_b109, &c_b109, &u[*n + 1 + u_dim1],
  895. ldu);
  896. /* Call SORMBR to compute QB*UB. */
  897. /* (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
  898. i__2 = *lwork - itemp + 1;
  899. sormbr_("Q", "L", "N", m, ns, n, &a[a_offset], lda, &work[
  900. itauq], &u[u_offset], ldu, &work[itemp], &i__2, &ierr);
  901. }
  902. /* If needed, compute right singular vectors. */
  903. if (wantvt) {
  904. j = itgkz + *n;
  905. i__2 = *ns;
  906. for (i__ = 1; i__ <= i__2; ++i__) {
  907. scopy_(n, &work[j], &c__1, &vt[i__ + vt_dim1], ldvt);
  908. j += *n << 1;
  909. }
  910. /* Call SORMBR to compute VB**T * PB**T */
  911. /* (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
  912. i__2 = *lwork - itemp + 1;
  913. sormbr_("P", "R", "T", ns, n, n, &a[a_offset], lda, &work[
  914. itaup], &vt[vt_offset], ldvt, &work[itemp], &i__2, &
  915. ierr);
  916. }
  917. }
  918. } else {
  919. /* A has more columns than rows. If A has sufficiently more */
  920. /* columns than rows, first reduce A using the LQ decomposition. */
  921. if (*n >= mnthr) {
  922. /* Path 1t (N much larger than M): */
  923. /* A = L * Q = ( QB * B * PB**T ) * Q */
  924. /* = ( QB * ( UB * S * VB**T ) * PB**T ) * Q */
  925. /* U = QB * UB ; V**T = VB**T * PB**T * Q */
  926. /* Compute A=L*Q */
  927. /* (Workspace: need 2*M, prefer M+M*NB) */
  928. itau = 1;
  929. itemp = itau + *m;
  930. i__2 = *lwork - itemp + 1;
  931. sgelqf_(m, n, &a[a_offset], lda, &work[itau], &work[itemp], &i__2,
  932. info);
  933. /* Copy L into WORK and bidiagonalize it: */
  934. /* (Workspace in WORK( ITEMP ): need M*M+5*N, prefer M*M+4*M+2*M*NB) */
  935. ilqf = itemp;
  936. id = ilqf + *m * *m;
  937. ie = id + *m;
  938. itauq = ie + *m;
  939. itaup = itauq + *m;
  940. itemp = itaup + *m;
  941. slacpy_("L", m, m, &a[a_offset], lda, &work[ilqf], m);
  942. i__2 = *m - 1;
  943. i__3 = *m - 1;
  944. slaset_("U", &i__2, &i__3, &c_b109, &c_b109, &work[ilqf + *m], m);
  945. i__2 = *lwork - itemp + 1;
  946. sgebrd_(m, m, &work[ilqf], m, &work[id], &work[ie], &work[itauq],
  947. &work[itaup], &work[itemp], &i__2, info);
  948. /* Solve eigenvalue problem TGK*Z=Z*S. */
  949. /* (Workspace: need 2*M*M+14*M) */
  950. itgkz = itemp;
  951. itemp = itgkz + *m * ((*m << 1) + 1);
  952. i__2 = *m << 1;
  953. sbdsvdx_("U", jobz, rngtgk, m, &work[id], &work[ie], vl, vu, &
  954. iltgk, &iutgk, ns, &s[1], &work[itgkz], &i__2, &work[
  955. itemp], &iwork[1], info);
  956. /* If needed, compute left singular vectors. */
  957. if (wantu) {
  958. j = itgkz;
  959. i__2 = *ns;
  960. for (i__ = 1; i__ <= i__2; ++i__) {
  961. scopy_(m, &work[j], &c__1, &u[i__ * u_dim1 + 1], &c__1);
  962. j += *m << 1;
  963. }
  964. /* Call SORMBR to compute QB*UB. */
  965. /* (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
  966. i__2 = *lwork - itemp + 1;
  967. sormbr_("Q", "L", "N", m, ns, m, &work[ilqf], m, &work[itauq],
  968. &u[u_offset], ldu, &work[itemp], &i__2, info);
  969. }
  970. /* If needed, compute right singular vectors. */
  971. if (wantvt) {
  972. j = itgkz + *m;
  973. i__2 = *ns;
  974. for (i__ = 1; i__ <= i__2; ++i__) {
  975. scopy_(m, &work[j], &c__1, &vt[i__ + vt_dim1], ldvt);
  976. j += *m << 1;
  977. }
  978. i__2 = *n - *m;
  979. slaset_("A", ns, &i__2, &c_b109, &c_b109, &vt[(*m + 1) *
  980. vt_dim1 + 1], ldvt);
  981. /* Call SORMBR to compute (VB**T)*(PB**T) */
  982. /* (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
  983. i__2 = *lwork - itemp + 1;
  984. sormbr_("P", "R", "T", ns, m, m, &work[ilqf], m, &work[itaup],
  985. &vt[vt_offset], ldvt, &work[itemp], &i__2, info);
  986. /* Call SORMLQ to compute ((VB**T)*(PB**T))*Q. */
  987. /* (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
  988. i__2 = *lwork - itemp + 1;
  989. sormlq_("R", "N", ns, n, m, &a[a_offset], lda, &work[itau], &
  990. vt[vt_offset], ldvt, &work[itemp], &i__2, info);
  991. }
  992. } else {
  993. /* Path 2t (N greater than M, but not much larger) */
  994. /* Reduce to bidiagonal form without LQ decomposition */
  995. /* A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T */
  996. /* U = QB * UB; V**T = VB**T * PB**T */
  997. /* Bidiagonalize A */
  998. /* (Workspace: need 4*M+N, prefer 4*M+(M+N)*NB) */
  999. id = 1;
  1000. ie = id + *m;
  1001. itauq = ie + *m;
  1002. itaup = itauq + *m;
  1003. itemp = itaup + *m;
  1004. i__2 = *lwork - itemp + 1;
  1005. sgebrd_(m, n, &a[a_offset], lda, &work[id], &work[ie], &work[
  1006. itauq], &work[itaup], &work[itemp], &i__2, info);
  1007. /* Solve eigenvalue problem TGK*Z=Z*S. */
  1008. /* (Workspace: need 2*M*M+14*M) */
  1009. itgkz = itemp;
  1010. itemp = itgkz + *m * ((*m << 1) + 1);
  1011. i__2 = *m << 1;
  1012. sbdsvdx_("L", jobz, rngtgk, m, &work[id], &work[ie], vl, vu, &
  1013. iltgk, &iutgk, ns, &s[1], &work[itgkz], &i__2, &work[
  1014. itemp], &iwork[1], info);
  1015. /* If needed, compute left singular vectors. */
  1016. if (wantu) {
  1017. j = itgkz;
  1018. i__2 = *ns;
  1019. for (i__ = 1; i__ <= i__2; ++i__) {
  1020. scopy_(m, &work[j], &c__1, &u[i__ * u_dim1 + 1], &c__1);
  1021. j += *m << 1;
  1022. }
  1023. /* Call SORMBR to compute QB*UB. */
  1024. /* (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
  1025. i__2 = *lwork - itemp + 1;
  1026. sormbr_("Q", "L", "N", m, ns, n, &a[a_offset], lda, &work[
  1027. itauq], &u[u_offset], ldu, &work[itemp], &i__2, info);
  1028. }
  1029. /* If needed, compute right singular vectors. */
  1030. if (wantvt) {
  1031. j = itgkz + *m;
  1032. i__2 = *ns;
  1033. for (i__ = 1; i__ <= i__2; ++i__) {
  1034. scopy_(m, &work[j], &c__1, &vt[i__ + vt_dim1], ldvt);
  1035. j += *m << 1;
  1036. }
  1037. i__2 = *n - *m;
  1038. slaset_("A", ns, &i__2, &c_b109, &c_b109, &vt[(*m + 1) *
  1039. vt_dim1 + 1], ldvt);
  1040. /* Call SORMBR to compute VB**T * PB**T */
  1041. /* (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
  1042. i__2 = *lwork - itemp + 1;
  1043. sormbr_("P", "R", "T", ns, n, m, &a[a_offset], lda, &work[
  1044. itaup], &vt[vt_offset], ldvt, &work[itemp], &i__2,
  1045. info);
  1046. }
  1047. }
  1048. }
  1049. /* Undo scaling if necessary */
  1050. if (iscl == 1) {
  1051. if (anrm > bignum) {
  1052. slascl_("G", &c__0, &c__0, &bignum, &anrm, &minmn, &c__1, &s[1], &
  1053. minmn, info);
  1054. }
  1055. if (anrm < smlnum) {
  1056. slascl_("G", &c__0, &c__0, &smlnum, &anrm, &minmn, &c__1, &s[1], &
  1057. minmn, info);
  1058. }
  1059. }
  1060. /* Return optimal workspace in WORK(1) */
  1061. work[1] = (real) maxwrk;
  1062. return;
  1063. /* End of SGESVDX */
  1064. } /* sgesvdx_ */