dlasd7.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* dlasd7.f -- translated by f2c (version 20061008).
  2. You must link the resulting object file with libf2c:
  3. on Microsoft Windows system, link with libf2c.lib;
  4. on Linux or Unix systems, link with .../path/to/libf2c.a -lm
  5. or, if you install libf2c.a in a standard place, with -lf2c -lm
  6. -- in that order, at the end of the command line, as in
  7. cc *.o -lf2c -lm
  8. Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
  9. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Table of constant values */
  14. static integer c__1 = 1;
  15. /* Subroutine */ int _starpu_dlasd7_(integer *icompq, integer *nl, integer *nr,
  16. integer *sqre, integer *k, doublereal *d__, doublereal *z__,
  17. doublereal *zw, doublereal *vf, doublereal *vfw, doublereal *vl,
  18. doublereal *vlw, doublereal *alpha, doublereal *beta, doublereal *
  19. dsigma, integer *idx, integer *idxp, integer *idxq, integer *perm,
  20. integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum,
  21. integer *ldgnum, doublereal *c__, doublereal *s, integer *info)
  22. {
  23. /* System generated locals */
  24. integer givcol_dim1, givcol_offset, givnum_dim1, givnum_offset, i__1;
  25. doublereal d__1, d__2;
  26. /* Local variables */
  27. integer i__, j, m, n, k2;
  28. doublereal z1;
  29. integer jp;
  30. doublereal eps, tau, tol;
  31. integer nlp1, nlp2, idxi, idxj;
  32. extern /* Subroutine */ int _starpu_drot_(integer *, doublereal *, integer *,
  33. doublereal *, integer *, doublereal *, doublereal *);
  34. integer idxjp;
  35. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  36. doublereal *, integer *);
  37. integer jprev;
  38. extern doublereal _starpu_dlapy2_(doublereal *, doublereal *), _starpu_dlamch_(char *);
  39. extern /* Subroutine */ int _starpu_dlamrg_(integer *, integer *, doublereal *,
  40. integer *, integer *, integer *), _starpu_xerbla_(char *, integer *);
  41. doublereal hlftol;
  42. /* -- LAPACK auxiliary routine (version 3.2) -- */
  43. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  44. /* November 2006 */
  45. /* .. Scalar Arguments .. */
  46. /* .. */
  47. /* .. Array Arguments .. */
  48. /* .. */
  49. /* Purpose */
  50. /* ======= */
  51. /* DLASD7 merges the two sets of singular values together into a single */
  52. /* sorted set. Then it tries to deflate the size of the problem. There */
  53. /* are two ways in which deflation can occur: when two or more singular */
  54. /* values are close together or if there is a tiny entry in the Z */
  55. /* vector. For each such occurrence the order of the related */
  56. /* secular equation problem is reduced by one. */
  57. /* DLASD7 is called from DLASD6. */
  58. /* Arguments */
  59. /* ========= */
  60. /* ICOMPQ (input) INTEGER */
  61. /* Specifies whether singular vectors are to be computed */
  62. /* in compact form, as follows: */
  63. /* = 0: Compute singular values only. */
  64. /* = 1: Compute singular vectors of upper */
  65. /* bidiagonal matrix in compact form. */
  66. /* NL (input) INTEGER */
  67. /* The row dimension of the upper block. NL >= 1. */
  68. /* NR (input) INTEGER */
  69. /* The row dimension of the lower block. NR >= 1. */
  70. /* SQRE (input) INTEGER */
  71. /* = 0: the lower block is an NR-by-NR square matrix. */
  72. /* = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
  73. /* The bidiagonal matrix has */
  74. /* N = NL + NR + 1 rows and */
  75. /* M = N + SQRE >= N columns. */
  76. /* K (output) INTEGER */
  77. /* Contains the dimension of the non-deflated matrix, this is */
  78. /* the order of the related secular equation. 1 <= K <=N. */
  79. /* D (input/output) DOUBLE PRECISION array, dimension ( N ) */
  80. /* On entry D contains the singular values of the two submatrices */
  81. /* to be combined. On exit D contains the trailing (N-K) updated */
  82. /* singular values (those which were deflated) sorted into */
  83. /* increasing order. */
  84. /* Z (output) DOUBLE PRECISION array, dimension ( M ) */
  85. /* On exit Z contains the updating row vector in the secular */
  86. /* equation. */
  87. /* ZW (workspace) DOUBLE PRECISION array, dimension ( M ) */
  88. /* Workspace for Z. */
  89. /* VF (input/output) DOUBLE PRECISION array, dimension ( M ) */
  90. /* On entry, VF(1:NL+1) contains the first components of all */
  91. /* right singular vectors of the upper block; and VF(NL+2:M) */
  92. /* contains the first components of all right singular vectors */
  93. /* of the lower block. On exit, VF contains the first components */
  94. /* of all right singular vectors of the bidiagonal matrix. */
  95. /* VFW (workspace) DOUBLE PRECISION array, dimension ( M ) */
  96. /* Workspace for VF. */
  97. /* VL (input/output) DOUBLE PRECISION array, dimension ( M ) */
  98. /* On entry, VL(1:NL+1) contains the last components of all */
  99. /* right singular vectors of the upper block; and VL(NL+2:M) */
  100. /* contains the last components of all right singular vectors */
  101. /* of the lower block. On exit, VL contains the last components */
  102. /* of all right singular vectors of the bidiagonal matrix. */
  103. /* VLW (workspace) DOUBLE PRECISION array, dimension ( M ) */
  104. /* Workspace for VL. */
  105. /* ALPHA (input) DOUBLE PRECISION */
  106. /* Contains the diagonal element associated with the added row. */
  107. /* BETA (input) DOUBLE PRECISION */
  108. /* Contains the off-diagonal element associated with the added */
  109. /* row. */
  110. /* DSIGMA (output) DOUBLE PRECISION array, dimension ( N ) */
  111. /* Contains a copy of the diagonal elements (K-1 singular values */
  112. /* and one zero) in the secular equation. */
  113. /* IDX (workspace) INTEGER array, dimension ( N ) */
  114. /* This will contain the permutation used to sort the contents of */
  115. /* D into ascending order. */
  116. /* IDXP (workspace) INTEGER array, dimension ( N ) */
  117. /* This will contain the permutation used to place deflated */
  118. /* values of D at the end of the array. On output IDXP(2:K) */
  119. /* points to the nondeflated D-values and IDXP(K+1:N) */
  120. /* points to the deflated singular values. */
  121. /* IDXQ (input) INTEGER array, dimension ( N ) */
  122. /* This contains the permutation which separately sorts the two */
  123. /* sub-problems in D into ascending order. Note that entries in */
  124. /* the first half of this permutation must first be moved one */
  125. /* position backward; and entries in the second half */
  126. /* must first have NL+1 added to their values. */
  127. /* PERM (output) INTEGER array, dimension ( N ) */
  128. /* The permutations (from deflation and sorting) to be applied */
  129. /* to each singular block. Not referenced if ICOMPQ = 0. */
  130. /* GIVPTR (output) INTEGER */
  131. /* The number of Givens rotations which took place in this */
  132. /* subproblem. Not referenced if ICOMPQ = 0. */
  133. /* GIVCOL (output) INTEGER array, dimension ( LDGCOL, 2 ) */
  134. /* Each pair of numbers indicates a pair of columns to take place */
  135. /* in a Givens rotation. Not referenced if ICOMPQ = 0. */
  136. /* LDGCOL (input) INTEGER */
  137. /* The leading dimension of GIVCOL, must be at least N. */
  138. /* GIVNUM (output) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
  139. /* Each number indicates the C or S value to be used in the */
  140. /* corresponding Givens rotation. Not referenced if ICOMPQ = 0. */
  141. /* LDGNUM (input) INTEGER */
  142. /* The leading dimension of GIVNUM, must be at least N. */
  143. /* C (output) DOUBLE PRECISION */
  144. /* C contains garbage if SQRE =0 and the C-value of a Givens */
  145. /* rotation related to the right null space if SQRE = 1. */
  146. /* S (output) DOUBLE PRECISION */
  147. /* S contains garbage if SQRE =0 and the S-value of a Givens */
  148. /* rotation related to the right null space if SQRE = 1. */
  149. /* INFO (output) INTEGER */
  150. /* = 0: successful exit. */
  151. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  152. /* Further Details */
  153. /* =============== */
  154. /* Based on contributions by */
  155. /* Ming Gu and Huan Ren, Computer Science Division, University of */
  156. /* California at Berkeley, USA */
  157. /* ===================================================================== */
  158. /* .. Parameters .. */
  159. /* .. */
  160. /* .. Local Scalars .. */
  161. /* .. */
  162. /* .. External Subroutines .. */
  163. /* .. */
  164. /* .. External Functions .. */
  165. /* .. */
  166. /* .. Intrinsic Functions .. */
  167. /* .. */
  168. /* .. Executable Statements .. */
  169. /* Test the input parameters. */
  170. /* Parameter adjustments */
  171. --d__;
  172. --z__;
  173. --zw;
  174. --vf;
  175. --vfw;
  176. --vl;
  177. --vlw;
  178. --dsigma;
  179. --idx;
  180. --idxp;
  181. --idxq;
  182. --perm;
  183. givcol_dim1 = *ldgcol;
  184. givcol_offset = 1 + givcol_dim1;
  185. givcol -= givcol_offset;
  186. givnum_dim1 = *ldgnum;
  187. givnum_offset = 1 + givnum_dim1;
  188. givnum -= givnum_offset;
  189. /* Function Body */
  190. *info = 0;
  191. n = *nl + *nr + 1;
  192. m = n + *sqre;
  193. if (*icompq < 0 || *icompq > 1) {
  194. *info = -1;
  195. } else if (*nl < 1) {
  196. *info = -2;
  197. } else if (*nr < 1) {
  198. *info = -3;
  199. } else if (*sqre < 0 || *sqre > 1) {
  200. *info = -4;
  201. } else if (*ldgcol < n) {
  202. *info = -22;
  203. } else if (*ldgnum < n) {
  204. *info = -24;
  205. }
  206. if (*info != 0) {
  207. i__1 = -(*info);
  208. _starpu_xerbla_("DLASD7", &i__1);
  209. return 0;
  210. }
  211. nlp1 = *nl + 1;
  212. nlp2 = *nl + 2;
  213. if (*icompq == 1) {
  214. *givptr = 0;
  215. }
  216. /* Generate the first part of the vector Z and move the singular */
  217. /* values in the first part of D one position backward. */
  218. z1 = *alpha * vl[nlp1];
  219. vl[nlp1] = 0.;
  220. tau = vf[nlp1];
  221. for (i__ = *nl; i__ >= 1; --i__) {
  222. z__[i__ + 1] = *alpha * vl[i__];
  223. vl[i__] = 0.;
  224. vf[i__ + 1] = vf[i__];
  225. d__[i__ + 1] = d__[i__];
  226. idxq[i__ + 1] = idxq[i__] + 1;
  227. /* L10: */
  228. }
  229. vf[1] = tau;
  230. /* Generate the second part of the vector Z. */
  231. i__1 = m;
  232. for (i__ = nlp2; i__ <= i__1; ++i__) {
  233. z__[i__] = *beta * vf[i__];
  234. vf[i__] = 0.;
  235. /* L20: */
  236. }
  237. /* Sort the singular values into increasing order */
  238. i__1 = n;
  239. for (i__ = nlp2; i__ <= i__1; ++i__) {
  240. idxq[i__] += nlp1;
  241. /* L30: */
  242. }
  243. /* DSIGMA, IDXC, IDXC, and ZW are used as storage space. */
  244. i__1 = n;
  245. for (i__ = 2; i__ <= i__1; ++i__) {
  246. dsigma[i__] = d__[idxq[i__]];
  247. zw[i__] = z__[idxq[i__]];
  248. vfw[i__] = vf[idxq[i__]];
  249. vlw[i__] = vl[idxq[i__]];
  250. /* L40: */
  251. }
  252. _starpu_dlamrg_(nl, nr, &dsigma[2], &c__1, &c__1, &idx[2]);
  253. i__1 = n;
  254. for (i__ = 2; i__ <= i__1; ++i__) {
  255. idxi = idx[i__] + 1;
  256. d__[i__] = dsigma[idxi];
  257. z__[i__] = zw[idxi];
  258. vf[i__] = vfw[idxi];
  259. vl[i__] = vlw[idxi];
  260. /* L50: */
  261. }
  262. /* Calculate the allowable deflation tolerence */
  263. eps = _starpu_dlamch_("Epsilon");
  264. /* Computing MAX */
  265. d__1 = abs(*alpha), d__2 = abs(*beta);
  266. tol = max(d__1,d__2);
  267. /* Computing MAX */
  268. d__2 = (d__1 = d__[n], abs(d__1));
  269. tol = eps * 64. * max(d__2,tol);
  270. /* There are 2 kinds of deflation -- first a value in the z-vector */
  271. /* is small, second two (or more) singular values are very close */
  272. /* together (their difference is small). */
  273. /* If the value in the z-vector is small, we simply permute the */
  274. /* array so that the corresponding singular value is moved to the */
  275. /* end. */
  276. /* If two values in the D-vector are close, we perform a two-sided */
  277. /* rotation designed to make one of the corresponding z-vector */
  278. /* entries zero, and then permute the array so that the deflated */
  279. /* singular value is moved to the end. */
  280. /* If there are multiple singular values then the problem deflates. */
  281. /* Here the number of equal singular values are found. As each equal */
  282. /* singular value is found, an elementary reflector is computed to */
  283. /* rotate the corresponding singular subspace so that the */
  284. /* corresponding components of Z are zero in this new basis. */
  285. *k = 1;
  286. k2 = n + 1;
  287. i__1 = n;
  288. for (j = 2; j <= i__1; ++j) {
  289. if ((d__1 = z__[j], abs(d__1)) <= tol) {
  290. /* Deflate due to small z component. */
  291. --k2;
  292. idxp[k2] = j;
  293. if (j == n) {
  294. goto L100;
  295. }
  296. } else {
  297. jprev = j;
  298. goto L70;
  299. }
  300. /* L60: */
  301. }
  302. L70:
  303. j = jprev;
  304. L80:
  305. ++j;
  306. if (j > n) {
  307. goto L90;
  308. }
  309. if ((d__1 = z__[j], abs(d__1)) <= tol) {
  310. /* Deflate due to small z component. */
  311. --k2;
  312. idxp[k2] = j;
  313. } else {
  314. /* Check if singular values are close enough to allow deflation. */
  315. if ((d__1 = d__[j] - d__[jprev], abs(d__1)) <= tol) {
  316. /* Deflation is possible. */
  317. *s = z__[jprev];
  318. *c__ = z__[j];
  319. /* Find sqrt(a**2+b**2) without overflow or */
  320. /* destructive underflow. */
  321. tau = _starpu_dlapy2_(c__, s);
  322. z__[j] = tau;
  323. z__[jprev] = 0.;
  324. *c__ /= tau;
  325. *s = -(*s) / tau;
  326. /* Record the appropriate Givens rotation */
  327. if (*icompq == 1) {
  328. ++(*givptr);
  329. idxjp = idxq[idx[jprev] + 1];
  330. idxj = idxq[idx[j] + 1];
  331. if (idxjp <= nlp1) {
  332. --idxjp;
  333. }
  334. if (idxj <= nlp1) {
  335. --idxj;
  336. }
  337. givcol[*givptr + (givcol_dim1 << 1)] = idxjp;
  338. givcol[*givptr + givcol_dim1] = idxj;
  339. givnum[*givptr + (givnum_dim1 << 1)] = *c__;
  340. givnum[*givptr + givnum_dim1] = *s;
  341. }
  342. _starpu_drot_(&c__1, &vf[jprev], &c__1, &vf[j], &c__1, c__, s);
  343. _starpu_drot_(&c__1, &vl[jprev], &c__1, &vl[j], &c__1, c__, s);
  344. --k2;
  345. idxp[k2] = jprev;
  346. jprev = j;
  347. } else {
  348. ++(*k);
  349. zw[*k] = z__[jprev];
  350. dsigma[*k] = d__[jprev];
  351. idxp[*k] = jprev;
  352. jprev = j;
  353. }
  354. }
  355. goto L80;
  356. L90:
  357. /* Record the last singular value. */
  358. ++(*k);
  359. zw[*k] = z__[jprev];
  360. dsigma[*k] = d__[jprev];
  361. idxp[*k] = jprev;
  362. L100:
  363. /* Sort the singular values into DSIGMA. The singular values which */
  364. /* were not deflated go into the first K slots of DSIGMA, except */
  365. /* that DSIGMA(1) is treated separately. */
  366. i__1 = n;
  367. for (j = 2; j <= i__1; ++j) {
  368. jp = idxp[j];
  369. dsigma[j] = d__[jp];
  370. vfw[j] = vf[jp];
  371. vlw[j] = vl[jp];
  372. /* L110: */
  373. }
  374. if (*icompq == 1) {
  375. i__1 = n;
  376. for (j = 2; j <= i__1; ++j) {
  377. jp = idxp[j];
  378. perm[j] = idxq[idx[jp] + 1];
  379. if (perm[j] <= nlp1) {
  380. --perm[j];
  381. }
  382. /* L120: */
  383. }
  384. }
  385. /* The deflated singular values go back into the last N - K slots of */
  386. /* D. */
  387. i__1 = n - *k;
  388. _starpu_dcopy_(&i__1, &dsigma[*k + 1], &c__1, &d__[*k + 1], &c__1);
  389. /* Determine DSIGMA(1), DSIGMA(2), Z(1), VF(1), VL(1), VF(M), and */
  390. /* VL(M). */
  391. dsigma[1] = 0.;
  392. hlftol = tol / 2.;
  393. if (abs(dsigma[2]) <= hlftol) {
  394. dsigma[2] = hlftol;
  395. }
  396. if (m > n) {
  397. z__[1] = _starpu_dlapy2_(&z1, &z__[m]);
  398. if (z__[1] <= tol) {
  399. *c__ = 1.;
  400. *s = 0.;
  401. z__[1] = tol;
  402. } else {
  403. *c__ = z1 / z__[1];
  404. *s = -z__[m] / z__[1];
  405. }
  406. _starpu_drot_(&c__1, &vf[m], &c__1, &vf[1], &c__1, c__, s);
  407. _starpu_drot_(&c__1, &vl[m], &c__1, &vl[1], &c__1, c__, s);
  408. } else {
  409. if (abs(z1) <= tol) {
  410. z__[1] = tol;
  411. } else {
  412. z__[1] = z1;
  413. }
  414. }
  415. /* Restore Z, VF, and VL. */
  416. i__1 = *k - 1;
  417. _starpu_dcopy_(&i__1, &zw[2], &c__1, &z__[2], &c__1);
  418. i__1 = n - 1;
  419. _starpu_dcopy_(&i__1, &vfw[2], &c__1, &vf[2], &c__1);
  420. i__1 = n - 1;
  421. _starpu_dcopy_(&i__1, &vlw[2], &c__1, &vl[2], &c__1);
  422. return 0;
  423. /* End of DLASD7 */
  424. } /* _starpu_dlasd7_ */