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.

dlasd2.c 32 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /* f2c.h -- Standard Fortran to C header file */
  2. /** barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed."
  3. - From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */
  4. #ifndef F2C_INCLUDE
  5. #define F2C_INCLUDE
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <complex.h>
  11. #ifdef complex
  12. #undef complex
  13. #endif
  14. #ifdef I
  15. #undef I
  16. #endif
  17. typedef int integer;
  18. typedef unsigned int uinteger;
  19. typedef char *address;
  20. typedef short int shortint;
  21. typedef float real;
  22. typedef double doublereal;
  23. typedef struct { real r, i; } complex;
  24. typedef struct { doublereal r, i; } doublecomplex;
  25. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  26. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  27. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  28. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  29. #define pCf(z) (*_pCf(z))
  30. #define pCd(z) (*_pCd(z))
  31. typedef int logical;
  32. typedef short int shortlogical;
  33. typedef char logical1;
  34. typedef char integer1;
  35. #define TRUE_ (1)
  36. #define FALSE_ (0)
  37. /* Extern is for use with -E */
  38. #ifndef Extern
  39. #define Extern extern
  40. #endif
  41. /* I/O stuff */
  42. typedef int flag;
  43. typedef int ftnlen;
  44. typedef int ftnint;
  45. /*external read, write*/
  46. typedef struct
  47. { flag cierr;
  48. ftnint ciunit;
  49. flag ciend;
  50. char *cifmt;
  51. ftnint cirec;
  52. } cilist;
  53. /*internal read, write*/
  54. typedef struct
  55. { flag icierr;
  56. char *iciunit;
  57. flag iciend;
  58. char *icifmt;
  59. ftnint icirlen;
  60. ftnint icirnum;
  61. } icilist;
  62. /*open*/
  63. typedef struct
  64. { flag oerr;
  65. ftnint ounit;
  66. char *ofnm;
  67. ftnlen ofnmlen;
  68. char *osta;
  69. char *oacc;
  70. char *ofm;
  71. ftnint orl;
  72. char *oblnk;
  73. } olist;
  74. /*close*/
  75. typedef struct
  76. { flag cerr;
  77. ftnint cunit;
  78. char *csta;
  79. } cllist;
  80. /*rewind, backspace, endfile*/
  81. typedef struct
  82. { flag aerr;
  83. ftnint aunit;
  84. } alist;
  85. /* inquire */
  86. typedef struct
  87. { flag inerr;
  88. ftnint inunit;
  89. char *infile;
  90. ftnlen infilen;
  91. ftnint *inex; /*parameters in standard's order*/
  92. ftnint *inopen;
  93. ftnint *innum;
  94. ftnint *innamed;
  95. char *inname;
  96. ftnlen innamlen;
  97. char *inacc;
  98. ftnlen inacclen;
  99. char *inseq;
  100. ftnlen inseqlen;
  101. char *indir;
  102. ftnlen indirlen;
  103. char *infmt;
  104. ftnlen infmtlen;
  105. char *inform;
  106. ftnint informlen;
  107. char *inunf;
  108. ftnlen inunflen;
  109. ftnint *inrecl;
  110. ftnint *innrec;
  111. char *inblank;
  112. ftnlen inblanklen;
  113. } inlist;
  114. #define VOID void
  115. union Multitype { /* for multiple entry points */
  116. integer1 g;
  117. shortint h;
  118. integer i;
  119. /* longint j; */
  120. real r;
  121. doublereal d;
  122. complex c;
  123. doublecomplex z;
  124. };
  125. typedef union Multitype Multitype;
  126. struct Vardesc { /* for Namelist */
  127. char *name;
  128. char *addr;
  129. ftnlen *dims;
  130. int type;
  131. };
  132. typedef struct Vardesc Vardesc;
  133. struct Namelist {
  134. char *name;
  135. Vardesc **vars;
  136. int nvars;
  137. };
  138. typedef struct Namelist Namelist;
  139. #define abs(x) ((x) >= 0 ? (x) : -(x))
  140. #define dabs(x) (fabs(x))
  141. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  142. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  143. #define dmin(a,b) (f2cmin(a,b))
  144. #define dmax(a,b) (f2cmax(a,b))
  145. #define bit_test(a,b) ((a) >> (b) & 1)
  146. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  147. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  148. #define abort_() { sig_die("Fortran abort routine called", 1); }
  149. #define c_abs(z) (cabsf(Cf(z)))
  150. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  151. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  152. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  153. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  154. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  155. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  156. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  157. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  158. #define d_abs(x) (fabs(*(x)))
  159. #define d_acos(x) (acos(*(x)))
  160. #define d_asin(x) (asin(*(x)))
  161. #define d_atan(x) (atan(*(x)))
  162. #define d_atn2(x, y) (atan2(*(x),*(y)))
  163. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  164. #define r_cnjg(R, Z) { pCf(R) = conj(Cf(Z)); }
  165. #define d_cos(x) (cos(*(x)))
  166. #define d_cosh(x) (cosh(*(x)))
  167. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  168. #define d_exp(x) (exp(*(x)))
  169. #define d_imag(z) (cimag(Cd(z)))
  170. #define r_imag(z) (cimag(Cf(z)))
  171. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  172. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  173. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  174. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  175. #define d_log(x) (log(*(x)))
  176. #define d_mod(x, y) (fmod(*(x), *(y)))
  177. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  178. #define d_nint(x) u_nint(*(x))
  179. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  180. #define d_sign(a,b) u_sign(*(a),*(b))
  181. #define r_sign(a,b) u_sign(*(a),*(b))
  182. #define d_sin(x) (sin(*(x)))
  183. #define d_sinh(x) (sinh(*(x)))
  184. #define d_sqrt(x) (sqrt(*(x)))
  185. #define d_tan(x) (tan(*(x)))
  186. #define d_tanh(x) (tanh(*(x)))
  187. #define i_abs(x) abs(*(x))
  188. #define i_dnnt(x) ((integer)u_nint(*(x)))
  189. #define i_len(s, n) (n)
  190. #define i_nint(x) ((integer)u_nint(*(x)))
  191. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  192. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  193. #define pow_si(B,E) spow_ui(*(B),*(E))
  194. #define pow_ri(B,E) spow_ui(*(B),*(E))
  195. #define pow_di(B,E) dpow_ui(*(B),*(E))
  196. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  197. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  198. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  199. #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++ = ' '; }
  200. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  201. #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]; }
  202. #define sig_die(s, kill) { exit(1); }
  203. #define s_stop(s, n) {exit(0);}
  204. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  205. #define z_abs(z) (cabs(Cd(z)))
  206. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  207. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  208. #define myexit_() break;
  209. #define mycycle() continue;
  210. #define myceiling(w) {ceil(w)}
  211. #define myhuge(w) {HUGE_VAL}
  212. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  213. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  214. /* procedure parameter types for -A and -C++ */
  215. #define F2C_proc_par_types 1
  216. #ifdef __cplusplus
  217. typedef logical (*L_fp)(...);
  218. #else
  219. typedef logical (*L_fp)();
  220. #endif
  221. static float spow_ui(float x, integer n) {
  222. float pow=1.0; unsigned long int u;
  223. if(n != 0) {
  224. if(n < 0) n = -n, x = 1/x;
  225. for(u = n; ; ) {
  226. if(u & 01) pow *= x;
  227. if(u >>= 1) x *= x;
  228. else break;
  229. }
  230. }
  231. return pow;
  232. }
  233. static double dpow_ui(double x, integer n) {
  234. double pow=1.0; unsigned long int u;
  235. if(n != 0) {
  236. if(n < 0) n = -n, x = 1/x;
  237. for(u = n; ; ) {
  238. if(u & 01) pow *= x;
  239. if(u >>= 1) x *= x;
  240. else break;
  241. }
  242. }
  243. return pow;
  244. }
  245. static _Complex float cpow_ui(_Complex float x, integer n) {
  246. _Complex float pow=1.0; unsigned long int u;
  247. if(n != 0) {
  248. if(n < 0) n = -n, x = 1/x;
  249. for(u = n; ; ) {
  250. if(u & 01) pow *= x;
  251. if(u >>= 1) x *= x;
  252. else break;
  253. }
  254. }
  255. return pow;
  256. }
  257. static _Complex double zpow_ui(_Complex double x, integer n) {
  258. _Complex double pow=1.0; unsigned long int u;
  259. if(n != 0) {
  260. if(n < 0) n = -n, x = 1/x;
  261. for(u = n; ; ) {
  262. if(u & 01) pow *= x;
  263. if(u >>= 1) x *= x;
  264. else break;
  265. }
  266. }
  267. return pow;
  268. }
  269. static integer pow_ii(integer x, integer n) {
  270. integer pow; unsigned long int u;
  271. if (n <= 0) {
  272. if (n == 0 || x == 1) pow = 1;
  273. else if (x != -1) pow = x == 0 ? 1/x : 0;
  274. else n = -n;
  275. }
  276. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  277. u = n;
  278. for(pow = 1; ; ) {
  279. if(u & 01) pow *= x;
  280. if(u >>= 1) x *= x;
  281. else break;
  282. }
  283. }
  284. return pow;
  285. }
  286. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  287. {
  288. double m; integer i, mi;
  289. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  290. if (w[i-1]>m) mi=i ,m=w[i-1];
  291. return mi-s+1;
  292. }
  293. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  294. {
  295. float m; integer i, mi;
  296. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  297. if (w[i-1]>m) mi=i ,m=w[i-1];
  298. return mi-s+1;
  299. }
  300. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  301. integer n = *n_, incx = *incx_, incy = *incy_, i;
  302. _Complex float zdotc = 0.0;
  303. if (incx == 1 && incy == 1) {
  304. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  305. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  306. }
  307. } else {
  308. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  309. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  310. }
  311. }
  312. pCf(z) = zdotc;
  313. }
  314. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  315. integer n = *n_, incx = *incx_, incy = *incy_, i;
  316. _Complex double zdotc = 0.0;
  317. if (incx == 1 && incy == 1) {
  318. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  319. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  320. }
  321. } else {
  322. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  323. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  324. }
  325. }
  326. pCd(z) = zdotc;
  327. }
  328. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  329. integer n = *n_, incx = *incx_, incy = *incy_, i;
  330. _Complex float zdotc = 0.0;
  331. if (incx == 1 && incy == 1) {
  332. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  333. zdotc += Cf(&x[i]) * Cf(&y[i]);
  334. }
  335. } else {
  336. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  337. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  338. }
  339. }
  340. pCf(z) = zdotc;
  341. }
  342. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  343. integer n = *n_, incx = *incx_, incy = *incy_, i;
  344. _Complex double zdotc = 0.0;
  345. if (incx == 1 && incy == 1) {
  346. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  347. zdotc += Cd(&x[i]) * Cd(&y[i]);
  348. }
  349. } else {
  350. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  351. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  352. }
  353. }
  354. pCd(z) = zdotc;
  355. }
  356. #endif
  357. /* -- translated by f2c (version 20000121).
  358. You must link the resulting object file with the libraries:
  359. -lf2c -lm (in that order)
  360. */
  361. /* Table of constant values */
  362. static integer c__1 = 1;
  363. static doublereal c_b30 = 0.;
  364. /* > \brief \b DLASD2 merges the two sets of singular values together into a single sorted set. Used by sbdsdc
  365. . */
  366. /* =========== DOCUMENTATION =========== */
  367. /* Online html documentation available at */
  368. /* http://www.netlib.org/lapack/explore-html/ */
  369. /* > \htmlonly */
  370. /* > Download DLASD2 + dependencies */
  371. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dlasd2.
  372. f"> */
  373. /* > [TGZ]</a> */
  374. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dlasd2.
  375. f"> */
  376. /* > [ZIP]</a> */
  377. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dlasd2.
  378. f"> */
  379. /* > [TXT]</a> */
  380. /* > \endhtmlonly */
  381. /* Definition: */
  382. /* =========== */
  383. /* SUBROUTINE DLASD2( NL, NR, SQRE, K, D, Z, ALPHA, BETA, U, LDU, VT, */
  384. /* LDVT, DSIGMA, U2, LDU2, VT2, LDVT2, IDXP, IDX, */
  385. /* IDXC, IDXQ, COLTYP, INFO ) */
  386. /* INTEGER INFO, K, LDU, LDU2, LDVT, LDVT2, NL, NR, SQRE */
  387. /* DOUBLE PRECISION ALPHA, BETA */
  388. /* INTEGER COLTYP( * ), IDX( * ), IDXC( * ), IDXP( * ), */
  389. /* $ IDXQ( * ) */
  390. /* DOUBLE PRECISION D( * ), DSIGMA( * ), U( LDU, * ), */
  391. /* $ U2( LDU2, * ), VT( LDVT, * ), VT2( LDVT2, * ), */
  392. /* $ Z( * ) */
  393. /* > \par Purpose: */
  394. /* ============= */
  395. /* > */
  396. /* > \verbatim */
  397. /* > */
  398. /* > DLASD2 merges the two sets of singular values together into a single */
  399. /* > sorted set. Then it tries to deflate the size of the problem. */
  400. /* > There are two ways in which deflation can occur: when two or more */
  401. /* > singular values are close together or if there is a tiny entry in the */
  402. /* > Z vector. For each such occurrence the order of the related secular */
  403. /* > equation problem is reduced by one. */
  404. /* > */
  405. /* > DLASD2 is called from DLASD1. */
  406. /* > \endverbatim */
  407. /* Arguments: */
  408. /* ========== */
  409. /* > \param[in] NL */
  410. /* > \verbatim */
  411. /* > NL is INTEGER */
  412. /* > The row dimension of the upper block. NL >= 1. */
  413. /* > \endverbatim */
  414. /* > */
  415. /* > \param[in] NR */
  416. /* > \verbatim */
  417. /* > NR is INTEGER */
  418. /* > The row dimension of the lower block. NR >= 1. */
  419. /* > \endverbatim */
  420. /* > */
  421. /* > \param[in] SQRE */
  422. /* > \verbatim */
  423. /* > SQRE is INTEGER */
  424. /* > = 0: the lower block is an NR-by-NR square matrix. */
  425. /* > = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
  426. /* > */
  427. /* > The bidiagonal matrix has N = NL + NR + 1 rows and */
  428. /* > M = N + SQRE >= N columns. */
  429. /* > \endverbatim */
  430. /* > */
  431. /* > \param[out] K */
  432. /* > \verbatim */
  433. /* > K is INTEGER */
  434. /* > Contains the dimension of the non-deflated matrix, */
  435. /* > This is the order of the related secular equation. 1 <= K <=N. */
  436. /* > \endverbatim */
  437. /* > */
  438. /* > \param[in,out] D */
  439. /* > \verbatim */
  440. /* > D is DOUBLE PRECISION array, dimension(N) */
  441. /* > On entry D contains the singular values of the two submatrices */
  442. /* > to be combined. On exit D contains the trailing (N-K) updated */
  443. /* > singular values (those which were deflated) sorted into */
  444. /* > increasing order. */
  445. /* > \endverbatim */
  446. /* > */
  447. /* > \param[out] Z */
  448. /* > \verbatim */
  449. /* > Z is DOUBLE PRECISION array, dimension(N) */
  450. /* > On exit Z contains the updating row vector in the secular */
  451. /* > equation. */
  452. /* > \endverbatim */
  453. /* > */
  454. /* > \param[in] ALPHA */
  455. /* > \verbatim */
  456. /* > ALPHA is DOUBLE PRECISION */
  457. /* > Contains the diagonal element associated with the added row. */
  458. /* > \endverbatim */
  459. /* > */
  460. /* > \param[in] BETA */
  461. /* > \verbatim */
  462. /* > BETA is DOUBLE PRECISION */
  463. /* > Contains the off-diagonal element associated with the added */
  464. /* > row. */
  465. /* > \endverbatim */
  466. /* > */
  467. /* > \param[in,out] U */
  468. /* > \verbatim */
  469. /* > U is DOUBLE PRECISION array, dimension(LDU,N) */
  470. /* > On entry U contains the left singular vectors of two */
  471. /* > submatrices in the two square blocks with corners at (1,1), */
  472. /* > (NL, NL), and (NL+2, NL+2), (N,N). */
  473. /* > On exit U contains the trailing (N-K) updated left singular */
  474. /* > vectors (those which were deflated) in its last N-K columns. */
  475. /* > \endverbatim */
  476. /* > */
  477. /* > \param[in] LDU */
  478. /* > \verbatim */
  479. /* > LDU is INTEGER */
  480. /* > The leading dimension of the array U. LDU >= N. */
  481. /* > \endverbatim */
  482. /* > */
  483. /* > \param[in,out] VT */
  484. /* > \verbatim */
  485. /* > VT is DOUBLE PRECISION array, dimension(LDVT,M) */
  486. /* > On entry VT**T contains the right singular vectors of two */
  487. /* > submatrices in the two square blocks with corners at (1,1), */
  488. /* > (NL+1, NL+1), and (NL+2, NL+2), (M,M). */
  489. /* > On exit VT**T contains the trailing (N-K) updated right singular */
  490. /* > vectors (those which were deflated) in its last N-K columns. */
  491. /* > In case SQRE =1, the last row of VT spans the right null */
  492. /* > space. */
  493. /* > \endverbatim */
  494. /* > */
  495. /* > \param[in] LDVT */
  496. /* > \verbatim */
  497. /* > LDVT is INTEGER */
  498. /* > The leading dimension of the array VT. LDVT >= M. */
  499. /* > \endverbatim */
  500. /* > */
  501. /* > \param[out] DSIGMA */
  502. /* > \verbatim */
  503. /* > DSIGMA is DOUBLE PRECISION array, dimension (N) */
  504. /* > Contains a copy of the diagonal elements (K-1 singular values */
  505. /* > and one zero) in the secular equation. */
  506. /* > \endverbatim */
  507. /* > */
  508. /* > \param[out] U2 */
  509. /* > \verbatim */
  510. /* > U2 is DOUBLE PRECISION array, dimension(LDU2,N) */
  511. /* > Contains a copy of the first K-1 left singular vectors which */
  512. /* > will be used by DLASD3 in a matrix multiply (DGEMM) to solve */
  513. /* > for the new left singular vectors. U2 is arranged into four */
  514. /* > blocks. The first block contains a column with 1 at NL+1 and */
  515. /* > zero everywhere else; the second block contains non-zero */
  516. /* > entries only at and above NL; the third contains non-zero */
  517. /* > entries only below NL+1; and the fourth is dense. */
  518. /* > \endverbatim */
  519. /* > */
  520. /* > \param[in] LDU2 */
  521. /* > \verbatim */
  522. /* > LDU2 is INTEGER */
  523. /* > The leading dimension of the array U2. LDU2 >= N. */
  524. /* > \endverbatim */
  525. /* > */
  526. /* > \param[out] VT2 */
  527. /* > \verbatim */
  528. /* > VT2 is DOUBLE PRECISION array, dimension(LDVT2,N) */
  529. /* > VT2**T contains a copy of the first K right singular vectors */
  530. /* > which will be used by DLASD3 in a matrix multiply (DGEMM) to */
  531. /* > solve for the new right singular vectors. VT2 is arranged into */
  532. /* > three blocks. The first block contains a row that corresponds */
  533. /* > to the special 0 diagonal element in SIGMA; the second block */
  534. /* > contains non-zeros only at and before NL +1; the third block */
  535. /* > contains non-zeros only at and after NL +2. */
  536. /* > \endverbatim */
  537. /* > */
  538. /* > \param[in] LDVT2 */
  539. /* > \verbatim */
  540. /* > LDVT2 is INTEGER */
  541. /* > The leading dimension of the array VT2. LDVT2 >= M. */
  542. /* > \endverbatim */
  543. /* > */
  544. /* > \param[out] IDXP */
  545. /* > \verbatim */
  546. /* > IDXP is INTEGER array, dimension(N) */
  547. /* > This will contain the permutation used to place deflated */
  548. /* > values of D at the end of the array. On output IDXP(2:K) */
  549. /* > points to the nondeflated D-values and IDXP(K+1:N) */
  550. /* > points to the deflated singular values. */
  551. /* > \endverbatim */
  552. /* > */
  553. /* > \param[out] IDX */
  554. /* > \verbatim */
  555. /* > IDX is INTEGER array, dimension(N) */
  556. /* > This will contain the permutation used to sort the contents of */
  557. /* > D into ascending order. */
  558. /* > \endverbatim */
  559. /* > */
  560. /* > \param[out] IDXC */
  561. /* > \verbatim */
  562. /* > IDXC is INTEGER array, dimension(N) */
  563. /* > This will contain the permutation used to arrange the columns */
  564. /* > of the deflated U matrix into three groups: the first group */
  565. /* > contains non-zero entries only at and above NL, the second */
  566. /* > contains non-zero entries only below NL+2, and the third is */
  567. /* > dense. */
  568. /* > \endverbatim */
  569. /* > */
  570. /* > \param[in,out] IDXQ */
  571. /* > \verbatim */
  572. /* > IDXQ is INTEGER array, dimension(N) */
  573. /* > This contains the permutation which separately sorts the two */
  574. /* > sub-problems in D into ascending order. Note that entries in */
  575. /* > the first hlaf of this permutation must first be moved one */
  576. /* > position backward; and entries in the second half */
  577. /* > must first have NL+1 added to their values. */
  578. /* > \endverbatim */
  579. /* > */
  580. /* > \param[out] COLTYP */
  581. /* > \verbatim */
  582. /* > COLTYP is INTEGER array, dimension(N) */
  583. /* > As workspace, this will contain a label which will indicate */
  584. /* > which of the following types a column in the U2 matrix or a */
  585. /* > row in the VT2 matrix is: */
  586. /* > 1 : non-zero in the upper half only */
  587. /* > 2 : non-zero in the lower half only */
  588. /* > 3 : dense */
  589. /* > 4 : deflated */
  590. /* > */
  591. /* > On exit, it is an array of dimension 4, with COLTYP(I) being */
  592. /* > the dimension of the I-th type columns. */
  593. /* > \endverbatim */
  594. /* > */
  595. /* > \param[out] INFO */
  596. /* > \verbatim */
  597. /* > INFO is INTEGER */
  598. /* > = 0: successful exit. */
  599. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  600. /* > \endverbatim */
  601. /* Authors: */
  602. /* ======== */
  603. /* > \author Univ. of Tennessee */
  604. /* > \author Univ. of California Berkeley */
  605. /* > \author Univ. of Colorado Denver */
  606. /* > \author NAG Ltd. */
  607. /* > \date June 2017 */
  608. /* > \ingroup OTHERauxiliary */
  609. /* > \par Contributors: */
  610. /* ================== */
  611. /* > */
  612. /* > Ming Gu and Huan Ren, Computer Science Division, University of */
  613. /* > California at Berkeley, USA */
  614. /* > */
  615. /* ===================================================================== */
  616. /* Subroutine */ int dlasd2_(integer *nl, integer *nr, integer *sqre, integer
  617. *k, doublereal *d__, doublereal *z__, doublereal *alpha, doublereal *
  618. beta, doublereal *u, integer *ldu, doublereal *vt, integer *ldvt,
  619. doublereal *dsigma, doublereal *u2, integer *ldu2, doublereal *vt2,
  620. integer *ldvt2, integer *idxp, integer *idx, integer *idxc, integer *
  621. idxq, integer *coltyp, integer *info)
  622. {
  623. /* System generated locals */
  624. integer u_dim1, u_offset, u2_dim1, u2_offset, vt_dim1, vt_offset,
  625. vt2_dim1, vt2_offset, i__1;
  626. doublereal d__1, d__2;
  627. /* Local variables */
  628. integer idxi, idxj;
  629. extern /* Subroutine */ int drot_(integer *, doublereal *, integer *,
  630. doublereal *, integer *, doublereal *, doublereal *);
  631. integer ctot[4];
  632. doublereal c__;
  633. integer i__, j, m, n;
  634. doublereal s;
  635. integer idxjp;
  636. extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *,
  637. doublereal *, integer *);
  638. integer jprev, k2;
  639. doublereal z1;
  640. extern doublereal dlapy2_(doublereal *, doublereal *);
  641. integer ct;
  642. extern doublereal dlamch_(char *);
  643. integer jp;
  644. extern /* Subroutine */ int dlamrg_(integer *, integer *, doublereal *,
  645. integer *, integer *, integer *), dlacpy_(char *, integer *,
  646. integer *, doublereal *, integer *, doublereal *, integer *), dlaset_(char *, integer *, integer *, doublereal *,
  647. doublereal *, doublereal *, integer *), xerbla_(char *,
  648. integer *, ftnlen);
  649. doublereal hlftol, eps, tau, tol;
  650. integer psm[4], nlp1, nlp2;
  651. /* -- LAPACK auxiliary routine (version 3.7.1) -- */
  652. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  653. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  654. /* June 2017 */
  655. /* ===================================================================== */
  656. /* Test the input parameters. */
  657. /* Parameter adjustments */
  658. --d__;
  659. --z__;
  660. u_dim1 = *ldu;
  661. u_offset = 1 + u_dim1 * 1;
  662. u -= u_offset;
  663. vt_dim1 = *ldvt;
  664. vt_offset = 1 + vt_dim1 * 1;
  665. vt -= vt_offset;
  666. --dsigma;
  667. u2_dim1 = *ldu2;
  668. u2_offset = 1 + u2_dim1 * 1;
  669. u2 -= u2_offset;
  670. vt2_dim1 = *ldvt2;
  671. vt2_offset = 1 + vt2_dim1 * 1;
  672. vt2 -= vt2_offset;
  673. --idxp;
  674. --idx;
  675. --idxc;
  676. --idxq;
  677. --coltyp;
  678. /* Function Body */
  679. *info = 0;
  680. if (*nl < 1) {
  681. *info = -1;
  682. } else if (*nr < 1) {
  683. *info = -2;
  684. } else if (*sqre != 1 && *sqre != 0) {
  685. *info = -3;
  686. }
  687. n = *nl + *nr + 1;
  688. m = n + *sqre;
  689. if (*ldu < n) {
  690. *info = -10;
  691. } else if (*ldvt < m) {
  692. *info = -12;
  693. } else if (*ldu2 < n) {
  694. *info = -15;
  695. } else if (*ldvt2 < m) {
  696. *info = -17;
  697. }
  698. if (*info != 0) {
  699. i__1 = -(*info);
  700. xerbla_("DLASD2", &i__1, (ftnlen)6);
  701. return 0;
  702. }
  703. nlp1 = *nl + 1;
  704. nlp2 = *nl + 2;
  705. /* Generate the first part of the vector Z; and move the singular */
  706. /* values in the first part of D one position backward. */
  707. z1 = *alpha * vt[nlp1 + nlp1 * vt_dim1];
  708. z__[1] = z1;
  709. for (i__ = *nl; i__ >= 1; --i__) {
  710. z__[i__ + 1] = *alpha * vt[i__ + nlp1 * vt_dim1];
  711. d__[i__ + 1] = d__[i__];
  712. idxq[i__ + 1] = idxq[i__] + 1;
  713. /* L10: */
  714. }
  715. /* Generate the second part of the vector Z. */
  716. i__1 = m;
  717. for (i__ = nlp2; i__ <= i__1; ++i__) {
  718. z__[i__] = *beta * vt[i__ + nlp2 * vt_dim1];
  719. /* L20: */
  720. }
  721. /* Initialize some reference arrays. */
  722. i__1 = nlp1;
  723. for (i__ = 2; i__ <= i__1; ++i__) {
  724. coltyp[i__] = 1;
  725. /* L30: */
  726. }
  727. i__1 = n;
  728. for (i__ = nlp2; i__ <= i__1; ++i__) {
  729. coltyp[i__] = 2;
  730. /* L40: */
  731. }
  732. /* Sort the singular values into increasing order */
  733. i__1 = n;
  734. for (i__ = nlp2; i__ <= i__1; ++i__) {
  735. idxq[i__] += nlp1;
  736. /* L50: */
  737. }
  738. /* DSIGMA, IDXC, IDXC, and the first column of U2 */
  739. /* are used as storage space. */
  740. i__1 = n;
  741. for (i__ = 2; i__ <= i__1; ++i__) {
  742. dsigma[i__] = d__[idxq[i__]];
  743. u2[i__ + u2_dim1] = z__[idxq[i__]];
  744. idxc[i__] = coltyp[idxq[i__]];
  745. /* L60: */
  746. }
  747. dlamrg_(nl, nr, &dsigma[2], &c__1, &c__1, &idx[2]);
  748. i__1 = n;
  749. for (i__ = 2; i__ <= i__1; ++i__) {
  750. idxi = idx[i__] + 1;
  751. d__[i__] = dsigma[idxi];
  752. z__[i__] = u2[idxi + u2_dim1];
  753. coltyp[i__] = idxc[idxi];
  754. /* L70: */
  755. }
  756. /* Calculate the allowable deflation tolerance */
  757. eps = dlamch_("Epsilon");
  758. /* Computing MAX */
  759. d__1 = abs(*alpha), d__2 = abs(*beta);
  760. tol = f2cmax(d__1,d__2);
  761. /* Computing MAX */
  762. d__2 = (d__1 = d__[n], abs(d__1));
  763. tol = eps * 8. * f2cmax(d__2,tol);
  764. /* There are 2 kinds of deflation -- first a value in the z-vector */
  765. /* is small, second two (or more) singular values are very close */
  766. /* together (their difference is small). */
  767. /* If the value in the z-vector is small, we simply permute the */
  768. /* array so that the corresponding singular value is moved to the */
  769. /* end. */
  770. /* If two values in the D-vector are close, we perform a two-sided */
  771. /* rotation designed to make one of the corresponding z-vector */
  772. /* entries zero, and then permute the array so that the deflated */
  773. /* singular value is moved to the end. */
  774. /* If there are multiple singular values then the problem deflates. */
  775. /* Here the number of equal singular values are found. As each equal */
  776. /* singular value is found, an elementary reflector is computed to */
  777. /* rotate the corresponding singular subspace so that the */
  778. /* corresponding components of Z are zero in this new basis. */
  779. *k = 1;
  780. k2 = n + 1;
  781. i__1 = n;
  782. for (j = 2; j <= i__1; ++j) {
  783. if ((d__1 = z__[j], abs(d__1)) <= tol) {
  784. /* Deflate due to small z component. */
  785. --k2;
  786. idxp[k2] = j;
  787. coltyp[j] = 4;
  788. if (j == n) {
  789. goto L120;
  790. }
  791. } else {
  792. jprev = j;
  793. goto L90;
  794. }
  795. /* L80: */
  796. }
  797. L90:
  798. j = jprev;
  799. L100:
  800. ++j;
  801. if (j > n) {
  802. goto L110;
  803. }
  804. if ((d__1 = z__[j], abs(d__1)) <= tol) {
  805. /* Deflate due to small z component. */
  806. --k2;
  807. idxp[k2] = j;
  808. coltyp[j] = 4;
  809. } else {
  810. /* Check if singular values are close enough to allow deflation. */
  811. if ((d__1 = d__[j] - d__[jprev], abs(d__1)) <= tol) {
  812. /* Deflation is possible. */
  813. s = z__[jprev];
  814. c__ = z__[j];
  815. /* Find sqrt(a**2+b**2) without overflow or */
  816. /* destructive underflow. */
  817. tau = dlapy2_(&c__, &s);
  818. c__ /= tau;
  819. s = -s / tau;
  820. z__[j] = tau;
  821. z__[jprev] = 0.;
  822. /* Apply back the Givens rotation to the left and right */
  823. /* singular vector matrices. */
  824. idxjp = idxq[idx[jprev] + 1];
  825. idxj = idxq[idx[j] + 1];
  826. if (idxjp <= nlp1) {
  827. --idxjp;
  828. }
  829. if (idxj <= nlp1) {
  830. --idxj;
  831. }
  832. drot_(&n, &u[idxjp * u_dim1 + 1], &c__1, &u[idxj * u_dim1 + 1], &
  833. c__1, &c__, &s);
  834. drot_(&m, &vt[idxjp + vt_dim1], ldvt, &vt[idxj + vt_dim1], ldvt, &
  835. c__, &s);
  836. if (coltyp[j] != coltyp[jprev]) {
  837. coltyp[j] = 3;
  838. }
  839. coltyp[jprev] = 4;
  840. --k2;
  841. idxp[k2] = jprev;
  842. jprev = j;
  843. } else {
  844. ++(*k);
  845. u2[*k + u2_dim1] = z__[jprev];
  846. dsigma[*k] = d__[jprev];
  847. idxp[*k] = jprev;
  848. jprev = j;
  849. }
  850. }
  851. goto L100;
  852. L110:
  853. /* Record the last singular value. */
  854. ++(*k);
  855. u2[*k + u2_dim1] = z__[jprev];
  856. dsigma[*k] = d__[jprev];
  857. idxp[*k] = jprev;
  858. L120:
  859. /* Count up the total number of the various types of columns, then */
  860. /* form a permutation which positions the four column types into */
  861. /* four groups of uniform structure (although one or more of these */
  862. /* groups may be empty). */
  863. for (j = 1; j <= 4; ++j) {
  864. ctot[j - 1] = 0;
  865. /* L130: */
  866. }
  867. i__1 = n;
  868. for (j = 2; j <= i__1; ++j) {
  869. ct = coltyp[j];
  870. ++ctot[ct - 1];
  871. /* L140: */
  872. }
  873. /* PSM(*) = Position in SubMatrix (of types 1 through 4) */
  874. psm[0] = 2;
  875. psm[1] = ctot[0] + 2;
  876. psm[2] = psm[1] + ctot[1];
  877. psm[3] = psm[2] + ctot[2];
  878. /* Fill out the IDXC array so that the permutation which it induces */
  879. /* will place all type-1 columns first, all type-2 columns next, */
  880. /* then all type-3's, and finally all type-4's, starting from the */
  881. /* second column. This applies similarly to the rows of VT. */
  882. i__1 = n;
  883. for (j = 2; j <= i__1; ++j) {
  884. jp = idxp[j];
  885. ct = coltyp[jp];
  886. idxc[psm[ct - 1]] = j;
  887. ++psm[ct - 1];
  888. /* L150: */
  889. }
  890. /* Sort the singular values and corresponding singular vectors into */
  891. /* DSIGMA, U2, and VT2 respectively. The singular values/vectors */
  892. /* which were not deflated go into the first K slots of DSIGMA, U2, */
  893. /* and VT2 respectively, while those which were deflated go into the */
  894. /* last N - K slots, except that the first column/row will be treated */
  895. /* separately. */
  896. i__1 = n;
  897. for (j = 2; j <= i__1; ++j) {
  898. jp = idxp[j];
  899. dsigma[j] = d__[jp];
  900. idxj = idxq[idx[idxp[idxc[j]]] + 1];
  901. if (idxj <= nlp1) {
  902. --idxj;
  903. }
  904. dcopy_(&n, &u[idxj * u_dim1 + 1], &c__1, &u2[j * u2_dim1 + 1], &c__1);
  905. dcopy_(&m, &vt[idxj + vt_dim1], ldvt, &vt2[j + vt2_dim1], ldvt2);
  906. /* L160: */
  907. }
  908. /* Determine DSIGMA(1), DSIGMA(2) and Z(1) */
  909. dsigma[1] = 0.;
  910. hlftol = tol / 2.;
  911. if (abs(dsigma[2]) <= hlftol) {
  912. dsigma[2] = hlftol;
  913. }
  914. if (m > n) {
  915. z__[1] = dlapy2_(&z1, &z__[m]);
  916. if (z__[1] <= tol) {
  917. c__ = 1.;
  918. s = 0.;
  919. z__[1] = tol;
  920. } else {
  921. c__ = z1 / z__[1];
  922. s = z__[m] / z__[1];
  923. }
  924. } else {
  925. if (abs(z1) <= tol) {
  926. z__[1] = tol;
  927. } else {
  928. z__[1] = z1;
  929. }
  930. }
  931. /* Move the rest of the updating row to Z. */
  932. i__1 = *k - 1;
  933. dcopy_(&i__1, &u2[u2_dim1 + 2], &c__1, &z__[2], &c__1);
  934. /* Determine the first column of U2, the first row of VT2 and the */
  935. /* last row of VT. */
  936. dlaset_("A", &n, &c__1, &c_b30, &c_b30, &u2[u2_offset], ldu2);
  937. u2[nlp1 + u2_dim1] = 1.;
  938. if (m > n) {
  939. i__1 = nlp1;
  940. for (i__ = 1; i__ <= i__1; ++i__) {
  941. vt[m + i__ * vt_dim1] = -s * vt[nlp1 + i__ * vt_dim1];
  942. vt2[i__ * vt2_dim1 + 1] = c__ * vt[nlp1 + i__ * vt_dim1];
  943. /* L170: */
  944. }
  945. i__1 = m;
  946. for (i__ = nlp2; i__ <= i__1; ++i__) {
  947. vt2[i__ * vt2_dim1 + 1] = s * vt[m + i__ * vt_dim1];
  948. vt[m + i__ * vt_dim1] = c__ * vt[m + i__ * vt_dim1];
  949. /* L180: */
  950. }
  951. } else {
  952. dcopy_(&m, &vt[nlp1 + vt_dim1], ldvt, &vt2[vt2_dim1 + 1], ldvt2);
  953. }
  954. if (m > n) {
  955. dcopy_(&m, &vt[m + vt_dim1], ldvt, &vt2[m + vt2_dim1], ldvt2);
  956. }
  957. /* The deflated singular values and their corresponding vectors go */
  958. /* into the back of D, U, and V respectively. */
  959. if (n > *k) {
  960. i__1 = n - *k;
  961. dcopy_(&i__1, &dsigma[*k + 1], &c__1, &d__[*k + 1], &c__1);
  962. i__1 = n - *k;
  963. dlacpy_("A", &n, &i__1, &u2[(*k + 1) * u2_dim1 + 1], ldu2, &u[(*k + 1)
  964. * u_dim1 + 1], ldu);
  965. i__1 = n - *k;
  966. dlacpy_("A", &i__1, &m, &vt2[*k + 1 + vt2_dim1], ldvt2, &vt[*k + 1 +
  967. vt_dim1], ldvt);
  968. }
  969. /* Copy CTOT into COLTYP for referencing in DLASD3. */
  970. for (j = 1; j <= 4; ++j) {
  971. coltyp[j] = ctot[j - 1];
  972. /* L190: */
  973. }
  974. return 0;
  975. /* End of DLASD2 */
  976. } /* dlasd2_ */