dlasd3.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* dlasd3.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. static integer c__0 = 0;
  16. static doublereal c_b13 = 1.;
  17. static doublereal c_b26 = 0.;
  18. /* Subroutine */ int _starpu_dlasd3_(integer *nl, integer *nr, integer *sqre, integer
  19. *k, doublereal *d__, doublereal *q, integer *ldq, doublereal *dsigma,
  20. doublereal *u, integer *ldu, doublereal *u2, integer *ldu2,
  21. doublereal *vt, integer *ldvt, doublereal *vt2, integer *ldvt2,
  22. integer *idxc, integer *ctot, doublereal *z__, integer *info)
  23. {
  24. /* System generated locals */
  25. integer q_dim1, q_offset, u_dim1, u_offset, u2_dim1, u2_offset, vt_dim1,
  26. vt_offset, vt2_dim1, vt2_offset, i__1, i__2;
  27. doublereal d__1, d__2;
  28. /* Builtin functions */
  29. double sqrt(doublereal), d_sign(doublereal *, doublereal *);
  30. /* Local variables */
  31. integer i__, j, m, n, jc;
  32. doublereal rho;
  33. integer nlp1, nlp2, nrp1;
  34. doublereal temp;
  35. extern doublereal _starpu_dnrm2_(integer *, doublereal *, integer *);
  36. extern /* Subroutine */ int _starpu_dgemm_(char *, char *, integer *, integer *,
  37. integer *, doublereal *, doublereal *, integer *, doublereal *,
  38. integer *, doublereal *, doublereal *, integer *);
  39. integer ctemp;
  40. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  41. doublereal *, integer *);
  42. integer ktemp;
  43. extern doublereal _starpu_dlamc3_(doublereal *, doublereal *);
  44. extern /* Subroutine */ int _starpu_dlasd4_(integer *, integer *, doublereal *,
  45. doublereal *, doublereal *, doublereal *, doublereal *,
  46. doublereal *, integer *), _starpu_dlascl_(char *, integer *, integer *,
  47. doublereal *, doublereal *, integer *, integer *, doublereal *,
  48. integer *, integer *), _starpu_dlacpy_(char *, integer *, integer
  49. *, doublereal *, integer *, doublereal *, integer *),
  50. _starpu_xerbla_(char *, integer *);
  51. /* -- LAPACK auxiliary routine (version 3.2) -- */
  52. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  53. /* November 2006 */
  54. /* .. Scalar Arguments .. */
  55. /* .. */
  56. /* .. Array Arguments .. */
  57. /* .. */
  58. /* Purpose */
  59. /* ======= */
  60. /* DLASD3 finds all the square roots of the roots of the secular */
  61. /* equation, as defined by the values in D and Z. It makes the */
  62. /* appropriate calls to DLASD4 and then updates the singular */
  63. /* vectors by matrix multiplication. */
  64. /* This code makes very mild assumptions about floating point */
  65. /* arithmetic. It will work on machines with a guard digit in */
  66. /* add/subtract, or on those binary machines without guard digits */
  67. /* which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
  68. /* It could conceivably fail on hexadecimal or decimal machines */
  69. /* without guard digits, but we know of none. */
  70. /* DLASD3 is called from DLASD1. */
  71. /* Arguments */
  72. /* ========= */
  73. /* NL (input) INTEGER */
  74. /* The row dimension of the upper block. NL >= 1. */
  75. /* NR (input) INTEGER */
  76. /* The row dimension of the lower block. NR >= 1. */
  77. /* SQRE (input) INTEGER */
  78. /* = 0: the lower block is an NR-by-NR square matrix. */
  79. /* = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
  80. /* The bidiagonal matrix has N = NL + NR + 1 rows and */
  81. /* M = N + SQRE >= N columns. */
  82. /* K (input) INTEGER */
  83. /* The size of the secular equation, 1 =< K = < N. */
  84. /* D (output) DOUBLE PRECISION array, dimension(K) */
  85. /* On exit the square roots of the roots of the secular equation, */
  86. /* in ascending order. */
  87. /* Q (workspace) DOUBLE PRECISION array, */
  88. /* dimension at least (LDQ,K). */
  89. /* LDQ (input) INTEGER */
  90. /* The leading dimension of the array Q. LDQ >= K. */
  91. /* DSIGMA (input) DOUBLE PRECISION array, dimension(K) */
  92. /* The first K elements of this array contain the old roots */
  93. /* of the deflated updating problem. These are the poles */
  94. /* of the secular equation. */
  95. /* U (output) DOUBLE PRECISION array, dimension (LDU, N) */
  96. /* The last N - K columns of this matrix contain the deflated */
  97. /* left singular vectors. */
  98. /* LDU (input) INTEGER */
  99. /* The leading dimension of the array U. LDU >= N. */
  100. /* U2 (input/output) DOUBLE PRECISION array, dimension (LDU2, N) */
  101. /* The first K columns of this matrix contain the non-deflated */
  102. /* left singular vectors for the split problem. */
  103. /* LDU2 (input) INTEGER */
  104. /* The leading dimension of the array U2. LDU2 >= N. */
  105. /* VT (output) DOUBLE PRECISION array, dimension (LDVT, M) */
  106. /* The last M - K columns of VT' contain the deflated */
  107. /* right singular vectors. */
  108. /* LDVT (input) INTEGER */
  109. /* The leading dimension of the array VT. LDVT >= N. */
  110. /* VT2 (input/output) DOUBLE PRECISION array, dimension (LDVT2, N) */
  111. /* The first K columns of VT2' contain the non-deflated */
  112. /* right singular vectors for the split problem. */
  113. /* LDVT2 (input) INTEGER */
  114. /* The leading dimension of the array VT2. LDVT2 >= N. */
  115. /* IDXC (input) INTEGER array, dimension ( N ) */
  116. /* The permutation used to arrange the columns of U (and rows of */
  117. /* VT) into three groups: the first group contains non-zero */
  118. /* entries only at and above (or before) NL +1; the second */
  119. /* contains non-zero entries only at and below (or after) NL+2; */
  120. /* and the third is dense. The first column of U and the row of */
  121. /* VT are treated separately, however. */
  122. /* The rows of the singular vectors found by DLASD4 */
  123. /* must be likewise permuted before the matrix multiplies can */
  124. /* take place. */
  125. /* CTOT (input) INTEGER array, dimension ( 4 ) */
  126. /* A count of the total number of the various types of columns */
  127. /* in U (or rows in VT), as described in IDXC. The fourth column */
  128. /* type is any column which has been deflated. */
  129. /* Z (input) DOUBLE PRECISION array, dimension (K) */
  130. /* The first K elements of this array contain the components */
  131. /* of the deflation-adjusted updating row vector. */
  132. /* INFO (output) INTEGER */
  133. /* = 0: successful exit. */
  134. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  135. /* > 0: if INFO = 1, an singular value did not converge */
  136. /* Further Details */
  137. /* =============== */
  138. /* Based on contributions by */
  139. /* Ming Gu and Huan Ren, Computer Science Division, University of */
  140. /* California at Berkeley, USA */
  141. /* ===================================================================== */
  142. /* .. Parameters .. */
  143. /* .. */
  144. /* .. Local Scalars .. */
  145. /* .. */
  146. /* .. External Functions .. */
  147. /* .. */
  148. /* .. External Subroutines .. */
  149. /* .. */
  150. /* .. Intrinsic Functions .. */
  151. /* .. */
  152. /* .. Executable Statements .. */
  153. /* Test the input parameters. */
  154. /* Parameter adjustments */
  155. --d__;
  156. q_dim1 = *ldq;
  157. q_offset = 1 + q_dim1;
  158. q -= q_offset;
  159. --dsigma;
  160. u_dim1 = *ldu;
  161. u_offset = 1 + u_dim1;
  162. u -= u_offset;
  163. u2_dim1 = *ldu2;
  164. u2_offset = 1 + u2_dim1;
  165. u2 -= u2_offset;
  166. vt_dim1 = *ldvt;
  167. vt_offset = 1 + vt_dim1;
  168. vt -= vt_offset;
  169. vt2_dim1 = *ldvt2;
  170. vt2_offset = 1 + vt2_dim1;
  171. vt2 -= vt2_offset;
  172. --idxc;
  173. --ctot;
  174. --z__;
  175. /* Function Body */
  176. *info = 0;
  177. if (*nl < 1) {
  178. *info = -1;
  179. } else if (*nr < 1) {
  180. *info = -2;
  181. } else if (*sqre != 1 && *sqre != 0) {
  182. *info = -3;
  183. }
  184. n = *nl + *nr + 1;
  185. m = n + *sqre;
  186. nlp1 = *nl + 1;
  187. nlp2 = *nl + 2;
  188. if (*k < 1 || *k > n) {
  189. *info = -4;
  190. } else if (*ldq < *k) {
  191. *info = -7;
  192. } else if (*ldu < n) {
  193. *info = -10;
  194. } else if (*ldu2 < n) {
  195. *info = -12;
  196. } else if (*ldvt < m) {
  197. *info = -14;
  198. } else if (*ldvt2 < m) {
  199. *info = -16;
  200. }
  201. if (*info != 0) {
  202. i__1 = -(*info);
  203. _starpu_xerbla_("DLASD3", &i__1);
  204. return 0;
  205. }
  206. /* Quick return if possible */
  207. if (*k == 1) {
  208. d__[1] = abs(z__[1]);
  209. _starpu_dcopy_(&m, &vt2[vt2_dim1 + 1], ldvt2, &vt[vt_dim1 + 1], ldvt);
  210. if (z__[1] > 0.) {
  211. _starpu_dcopy_(&n, &u2[u2_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
  212. } else {
  213. i__1 = n;
  214. for (i__ = 1; i__ <= i__1; ++i__) {
  215. u[i__ + u_dim1] = -u2[i__ + u2_dim1];
  216. /* L10: */
  217. }
  218. }
  219. return 0;
  220. }
  221. /* Modify values DSIGMA(i) to make sure all DSIGMA(i)-DSIGMA(j) can */
  222. /* be computed with high relative accuracy (barring over/underflow). */
  223. /* This is a problem on machines without a guard digit in */
  224. /* add/subtract (Cray XMP, Cray YMP, Cray C 90 and Cray 2). */
  225. /* The following code replaces DSIGMA(I) by 2*DSIGMA(I)-DSIGMA(I), */
  226. /* which on any of these machines zeros out the bottommost */
  227. /* bit of DSIGMA(I) if it is 1; this makes the subsequent */
  228. /* subtractions DSIGMA(I)-DSIGMA(J) unproblematic when cancellation */
  229. /* occurs. On binary machines with a guard digit (almost all */
  230. /* machines) it does not change DSIGMA(I) at all. On hexadecimal */
  231. /* and decimal machines with a guard digit, it slightly */
  232. /* changes the bottommost bits of DSIGMA(I). It does not account */
  233. /* for hexadecimal or decimal machines without guard digits */
  234. /* (we know of none). We use a subroutine call to compute */
  235. /* 2*DSIGMA(I) to prevent optimizing compilers from eliminating */
  236. /* this code. */
  237. i__1 = *k;
  238. for (i__ = 1; i__ <= i__1; ++i__) {
  239. dsigma[i__] = _starpu_dlamc3_(&dsigma[i__], &dsigma[i__]) - dsigma[i__];
  240. /* L20: */
  241. }
  242. /* Keep a copy of Z. */
  243. _starpu_dcopy_(k, &z__[1], &c__1, &q[q_offset], &c__1);
  244. /* Normalize Z. */
  245. rho = _starpu_dnrm2_(k, &z__[1], &c__1);
  246. _starpu_dlascl_("G", &c__0, &c__0, &rho, &c_b13, k, &c__1, &z__[1], k, info);
  247. rho *= rho;
  248. /* Find the new singular values. */
  249. i__1 = *k;
  250. for (j = 1; j <= i__1; ++j) {
  251. _starpu_dlasd4_(k, &j, &dsigma[1], &z__[1], &u[j * u_dim1 + 1], &rho, &d__[j],
  252. &vt[j * vt_dim1 + 1], info);
  253. /* If the zero finder fails, the computation is terminated. */
  254. if (*info != 0) {
  255. return 0;
  256. }
  257. /* L30: */
  258. }
  259. /* Compute updated Z. */
  260. i__1 = *k;
  261. for (i__ = 1; i__ <= i__1; ++i__) {
  262. z__[i__] = u[i__ + *k * u_dim1] * vt[i__ + *k * vt_dim1];
  263. i__2 = i__ - 1;
  264. for (j = 1; j <= i__2; ++j) {
  265. z__[i__] *= u[i__ + j * u_dim1] * vt[i__ + j * vt_dim1] / (dsigma[
  266. i__] - dsigma[j]) / (dsigma[i__] + dsigma[j]);
  267. /* L40: */
  268. }
  269. i__2 = *k - 1;
  270. for (j = i__; j <= i__2; ++j) {
  271. z__[i__] *= u[i__ + j * u_dim1] * vt[i__ + j * vt_dim1] / (dsigma[
  272. i__] - dsigma[j + 1]) / (dsigma[i__] + dsigma[j + 1]);
  273. /* L50: */
  274. }
  275. d__2 = sqrt((d__1 = z__[i__], abs(d__1)));
  276. z__[i__] = d_sign(&d__2, &q[i__ + q_dim1]);
  277. /* L60: */
  278. }
  279. /* Compute left singular vectors of the modified diagonal matrix, */
  280. /* and store related information for the right singular vectors. */
  281. i__1 = *k;
  282. for (i__ = 1; i__ <= i__1; ++i__) {
  283. vt[i__ * vt_dim1 + 1] = z__[1] / u[i__ * u_dim1 + 1] / vt[i__ *
  284. vt_dim1 + 1];
  285. u[i__ * u_dim1 + 1] = -1.;
  286. i__2 = *k;
  287. for (j = 2; j <= i__2; ++j) {
  288. vt[j + i__ * vt_dim1] = z__[j] / u[j + i__ * u_dim1] / vt[j + i__
  289. * vt_dim1];
  290. u[j + i__ * u_dim1] = dsigma[j] * vt[j + i__ * vt_dim1];
  291. /* L70: */
  292. }
  293. temp = _starpu_dnrm2_(k, &u[i__ * u_dim1 + 1], &c__1);
  294. q[i__ * q_dim1 + 1] = u[i__ * u_dim1 + 1] / temp;
  295. i__2 = *k;
  296. for (j = 2; j <= i__2; ++j) {
  297. jc = idxc[j];
  298. q[j + i__ * q_dim1] = u[jc + i__ * u_dim1] / temp;
  299. /* L80: */
  300. }
  301. /* L90: */
  302. }
  303. /* Update the left singular vector matrix. */
  304. if (*k == 2) {
  305. _starpu_dgemm_("N", "N", &n, k, k, &c_b13, &u2[u2_offset], ldu2, &q[q_offset],
  306. ldq, &c_b26, &u[u_offset], ldu);
  307. goto L100;
  308. }
  309. if (ctot[1] > 0) {
  310. _starpu_dgemm_("N", "N", nl, k, &ctot[1], &c_b13, &u2[(u2_dim1 << 1) + 1],
  311. ldu2, &q[q_dim1 + 2], ldq, &c_b26, &u[u_dim1 + 1], ldu);
  312. if (ctot[3] > 0) {
  313. ktemp = ctot[1] + 2 + ctot[2];
  314. _starpu_dgemm_("N", "N", nl, k, &ctot[3], &c_b13, &u2[ktemp * u2_dim1 + 1]
  315. , ldu2, &q[ktemp + q_dim1], ldq, &c_b13, &u[u_dim1 + 1],
  316. ldu);
  317. }
  318. } else if (ctot[3] > 0) {
  319. ktemp = ctot[1] + 2 + ctot[2];
  320. _starpu_dgemm_("N", "N", nl, k, &ctot[3], &c_b13, &u2[ktemp * u2_dim1 + 1],
  321. ldu2, &q[ktemp + q_dim1], ldq, &c_b26, &u[u_dim1 + 1], ldu);
  322. } else {
  323. _starpu_dlacpy_("F", nl, k, &u2[u2_offset], ldu2, &u[u_offset], ldu);
  324. }
  325. _starpu_dcopy_(k, &q[q_dim1 + 1], ldq, &u[nlp1 + u_dim1], ldu);
  326. ktemp = ctot[1] + 2;
  327. ctemp = ctot[2] + ctot[3];
  328. _starpu_dgemm_("N", "N", nr, k, &ctemp, &c_b13, &u2[nlp2 + ktemp * u2_dim1], ldu2,
  329. &q[ktemp + q_dim1], ldq, &c_b26, &u[nlp2 + u_dim1], ldu);
  330. /* Generate the right singular vectors. */
  331. L100:
  332. i__1 = *k;
  333. for (i__ = 1; i__ <= i__1; ++i__) {
  334. temp = _starpu_dnrm2_(k, &vt[i__ * vt_dim1 + 1], &c__1);
  335. q[i__ + q_dim1] = vt[i__ * vt_dim1 + 1] / temp;
  336. i__2 = *k;
  337. for (j = 2; j <= i__2; ++j) {
  338. jc = idxc[j];
  339. q[i__ + j * q_dim1] = vt[jc + i__ * vt_dim1] / temp;
  340. /* L110: */
  341. }
  342. /* L120: */
  343. }
  344. /* Update the right singular vector matrix. */
  345. if (*k == 2) {
  346. _starpu_dgemm_("N", "N", k, &m, k, &c_b13, &q[q_offset], ldq, &vt2[vt2_offset]
  347. , ldvt2, &c_b26, &vt[vt_offset], ldvt);
  348. return 0;
  349. }
  350. ktemp = ctot[1] + 1;
  351. _starpu_dgemm_("N", "N", k, &nlp1, &ktemp, &c_b13, &q[q_dim1 + 1], ldq, &vt2[
  352. vt2_dim1 + 1], ldvt2, &c_b26, &vt[vt_dim1 + 1], ldvt);
  353. ktemp = ctot[1] + 2 + ctot[2];
  354. if (ktemp <= *ldvt2) {
  355. _starpu_dgemm_("N", "N", k, &nlp1, &ctot[3], &c_b13, &q[ktemp * q_dim1 + 1],
  356. ldq, &vt2[ktemp + vt2_dim1], ldvt2, &c_b13, &vt[vt_dim1 + 1],
  357. ldvt);
  358. }
  359. ktemp = ctot[1] + 1;
  360. nrp1 = *nr + *sqre;
  361. if (ktemp > 1) {
  362. i__1 = *k;
  363. for (i__ = 1; i__ <= i__1; ++i__) {
  364. q[i__ + ktemp * q_dim1] = q[i__ + q_dim1];
  365. /* L130: */
  366. }
  367. i__1 = m;
  368. for (i__ = nlp2; i__ <= i__1; ++i__) {
  369. vt2[ktemp + i__ * vt2_dim1] = vt2[i__ * vt2_dim1 + 1];
  370. /* L140: */
  371. }
  372. }
  373. ctemp = ctot[2] + 1 + ctot[3];
  374. _starpu_dgemm_("N", "N", k, &nrp1, &ctemp, &c_b13, &q[ktemp * q_dim1 + 1], ldq, &
  375. vt2[ktemp + nlp2 * vt2_dim1], ldvt2, &c_b26, &vt[nlp2 * vt_dim1 +
  376. 1], ldvt);
  377. return 0;
  378. /* End of DLASD3 */
  379. } /* _starpu_dlasd3_ */