dlals0.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* dlals0.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 doublereal c_b5 = -1.;
  15. static integer c__1 = 1;
  16. static doublereal c_b11 = 1.;
  17. static doublereal c_b13 = 0.;
  18. static integer c__0 = 0;
  19. /* Subroutine */ int _starpu_dlals0_(integer *icompq, integer *nl, integer *nr,
  20. integer *sqre, integer *nrhs, doublereal *b, integer *ldb, doublereal
  21. *bx, integer *ldbx, integer *perm, integer *givptr, integer *givcol,
  22. integer *ldgcol, doublereal *givnum, integer *ldgnum, doublereal *
  23. poles, doublereal *difl, doublereal *difr, doublereal *z__, integer *
  24. k, doublereal *c__, doublereal *s, doublereal *work, integer *info)
  25. {
  26. /* System generated locals */
  27. integer givcol_dim1, givcol_offset, b_dim1, b_offset, bx_dim1, bx_offset,
  28. difr_dim1, difr_offset, givnum_dim1, givnum_offset, poles_dim1,
  29. poles_offset, i__1, i__2;
  30. doublereal d__1;
  31. /* Local variables */
  32. integer i__, j, m, n;
  33. doublereal dj;
  34. integer nlp1;
  35. doublereal temp;
  36. extern /* Subroutine */ int _starpu_drot_(integer *, doublereal *, integer *,
  37. doublereal *, integer *, doublereal *, doublereal *);
  38. extern doublereal _starpu_dnrm2_(integer *, doublereal *, integer *);
  39. extern /* Subroutine */ int _starpu_dscal_(integer *, doublereal *, doublereal *,
  40. integer *);
  41. doublereal diflj, difrj, dsigj;
  42. extern /* Subroutine */ int _starpu_dgemv_(char *, integer *, integer *,
  43. doublereal *, doublereal *, integer *, doublereal *, integer *,
  44. doublereal *, doublereal *, integer *), _starpu_dcopy_(integer *,
  45. doublereal *, integer *, doublereal *, integer *);
  46. extern doublereal _starpu_dlamc3_(doublereal *, doublereal *);
  47. extern /* Subroutine */ int _starpu_dlascl_(char *, integer *, integer *,
  48. doublereal *, doublereal *, integer *, integer *, doublereal *,
  49. integer *, integer *), _starpu_dlacpy_(char *, integer *, integer
  50. *, doublereal *, integer *, doublereal *, integer *),
  51. _starpu_xerbla_(char *, integer *);
  52. doublereal dsigjp;
  53. /* -- LAPACK routine (version 3.2) -- */
  54. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  55. /* November 2006 */
  56. /* .. Scalar Arguments .. */
  57. /* .. */
  58. /* .. Array Arguments .. */
  59. /* .. */
  60. /* Purpose */
  61. /* ======= */
  62. /* DLALS0 applies back the multiplying factors of either the left or the */
  63. /* right singular vector matrix of a diagonal matrix appended by a row */
  64. /* to the right hand side matrix B in solving the least squares problem */
  65. /* using the divide-and-conquer SVD approach. */
  66. /* For the left singular vector matrix, three types of orthogonal */
  67. /* matrices are involved: */
  68. /* (1L) Givens rotations: the number of such rotations is GIVPTR; the */
  69. /* pairs of columns/rows they were applied to are stored in GIVCOL; */
  70. /* and the C- and S-values of these rotations are stored in GIVNUM. */
  71. /* (2L) Permutation. The (NL+1)-st row of B is to be moved to the first */
  72. /* row, and for J=2:N, PERM(J)-th row of B is to be moved to the */
  73. /* J-th row. */
  74. /* (3L) The left singular vector matrix of the remaining matrix. */
  75. /* For the right singular vector matrix, four types of orthogonal */
  76. /* matrices are involved: */
  77. /* (1R) The right singular vector matrix of the remaining matrix. */
  78. /* (2R) If SQRE = 1, one extra Givens rotation to generate the right */
  79. /* null space. */
  80. /* (3R) The inverse transformation of (2L). */
  81. /* (4R) The inverse transformation of (1L). */
  82. /* Arguments */
  83. /* ========= */
  84. /* ICOMPQ (input) INTEGER */
  85. /* Specifies whether singular vectors are to be computed in */
  86. /* factored form: */
  87. /* = 0: Left singular vector matrix. */
  88. /* = 1: Right singular vector matrix. */
  89. /* NL (input) INTEGER */
  90. /* The row dimension of the upper block. NL >= 1. */
  91. /* NR (input) INTEGER */
  92. /* The row dimension of the lower block. NR >= 1. */
  93. /* SQRE (input) INTEGER */
  94. /* = 0: the lower block is an NR-by-NR square matrix. */
  95. /* = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
  96. /* The bidiagonal matrix has row dimension N = NL + NR + 1, */
  97. /* and column dimension M = N + SQRE. */
  98. /* NRHS (input) INTEGER */
  99. /* The number of columns of B and BX. NRHS must be at least 1. */
  100. /* B (input/output) DOUBLE PRECISION array, dimension ( LDB, NRHS ) */
  101. /* On input, B contains the right hand sides of the least */
  102. /* squares problem in rows 1 through M. On output, B contains */
  103. /* the solution X in rows 1 through N. */
  104. /* LDB (input) INTEGER */
  105. /* The leading dimension of B. LDB must be at least */
  106. /* max(1,MAX( M, N ) ). */
  107. /* BX (workspace) DOUBLE PRECISION array, dimension ( LDBX, NRHS ) */
  108. /* LDBX (input) INTEGER */
  109. /* The leading dimension of BX. */
  110. /* PERM (input) INTEGER array, dimension ( N ) */
  111. /* The permutations (from deflation and sorting) applied */
  112. /* to the two blocks. */
  113. /* GIVPTR (input) INTEGER */
  114. /* The number of Givens rotations which took place in this */
  115. /* subproblem. */
  116. /* GIVCOL (input) INTEGER array, dimension ( LDGCOL, 2 ) */
  117. /* Each pair of numbers indicates a pair of rows/columns */
  118. /* involved in a Givens rotation. */
  119. /* LDGCOL (input) INTEGER */
  120. /* The leading dimension of GIVCOL, must be at least N. */
  121. /* GIVNUM (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
  122. /* Each number indicates the C or S value used in the */
  123. /* corresponding Givens rotation. */
  124. /* LDGNUM (input) INTEGER */
  125. /* The leading dimension of arrays DIFR, POLES and */
  126. /* GIVNUM, must be at least K. */
  127. /* POLES (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
  128. /* On entry, POLES(1:K, 1) contains the new singular */
  129. /* values obtained from solving the secular equation, and */
  130. /* POLES(1:K, 2) is an array containing the poles in the secular */
  131. /* equation. */
  132. /* DIFL (input) DOUBLE PRECISION array, dimension ( K ). */
  133. /* On entry, DIFL(I) is the distance between I-th updated */
  134. /* (undeflated) singular value and the I-th (undeflated) old */
  135. /* singular value. */
  136. /* DIFR (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ). */
  137. /* On entry, DIFR(I, 1) contains the distances between I-th */
  138. /* updated (undeflated) singular value and the I+1-th */
  139. /* (undeflated) old singular value. And DIFR(I, 2) is the */
  140. /* normalizing factor for the I-th right singular vector. */
  141. /* Z (input) DOUBLE PRECISION array, dimension ( K ) */
  142. /* Contain the components of the deflation-adjusted updating row */
  143. /* vector. */
  144. /* K (input) INTEGER */
  145. /* Contains the dimension of the non-deflated matrix, */
  146. /* This is the order of the related secular equation. 1 <= K <=N. */
  147. /* C (input) DOUBLE PRECISION */
  148. /* C contains garbage if SQRE =0 and the C-value of a Givens */
  149. /* rotation related to the right null space if SQRE = 1. */
  150. /* S (input) DOUBLE PRECISION */
  151. /* S contains garbage if SQRE =0 and the S-value of a Givens */
  152. /* rotation related to the right null space if SQRE = 1. */
  153. /* WORK (workspace) DOUBLE PRECISION array, dimension ( K ) */
  154. /* INFO (output) INTEGER */
  155. /* = 0: successful exit. */
  156. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  157. /* Further Details */
  158. /* =============== */
  159. /* Based on contributions by */
  160. /* Ming Gu and Ren-Cang Li, Computer Science Division, University of */
  161. /* California at Berkeley, USA */
  162. /* Osni Marques, LBNL/NERSC, USA */
  163. /* ===================================================================== */
  164. /* .. Parameters .. */
  165. /* .. */
  166. /* .. Local Scalars .. */
  167. /* .. */
  168. /* .. External Subroutines .. */
  169. /* .. */
  170. /* .. External Functions .. */
  171. /* .. */
  172. /* .. Intrinsic Functions .. */
  173. /* .. */
  174. /* .. Executable Statements .. */
  175. /* Test the input parameters. */
  176. /* Parameter adjustments */
  177. b_dim1 = *ldb;
  178. b_offset = 1 + b_dim1;
  179. b -= b_offset;
  180. bx_dim1 = *ldbx;
  181. bx_offset = 1 + bx_dim1;
  182. bx -= bx_offset;
  183. --perm;
  184. givcol_dim1 = *ldgcol;
  185. givcol_offset = 1 + givcol_dim1;
  186. givcol -= givcol_offset;
  187. difr_dim1 = *ldgnum;
  188. difr_offset = 1 + difr_dim1;
  189. difr -= difr_offset;
  190. poles_dim1 = *ldgnum;
  191. poles_offset = 1 + poles_dim1;
  192. poles -= poles_offset;
  193. givnum_dim1 = *ldgnum;
  194. givnum_offset = 1 + givnum_dim1;
  195. givnum -= givnum_offset;
  196. --difl;
  197. --z__;
  198. --work;
  199. /* Function Body */
  200. *info = 0;
  201. if (*icompq < 0 || *icompq > 1) {
  202. *info = -1;
  203. } else if (*nl < 1) {
  204. *info = -2;
  205. } else if (*nr < 1) {
  206. *info = -3;
  207. } else if (*sqre < 0 || *sqre > 1) {
  208. *info = -4;
  209. }
  210. n = *nl + *nr + 1;
  211. if (*nrhs < 1) {
  212. *info = -5;
  213. } else if (*ldb < n) {
  214. *info = -7;
  215. } else if (*ldbx < n) {
  216. *info = -9;
  217. } else if (*givptr < 0) {
  218. *info = -11;
  219. } else if (*ldgcol < n) {
  220. *info = -13;
  221. } else if (*ldgnum < n) {
  222. *info = -15;
  223. } else if (*k < 1) {
  224. *info = -20;
  225. }
  226. if (*info != 0) {
  227. i__1 = -(*info);
  228. _starpu_xerbla_("DLALS0", &i__1);
  229. return 0;
  230. }
  231. m = n + *sqre;
  232. nlp1 = *nl + 1;
  233. if (*icompq == 0) {
  234. /* Apply back orthogonal transformations from the left. */
  235. /* Step (1L): apply back the Givens rotations performed. */
  236. i__1 = *givptr;
  237. for (i__ = 1; i__ <= i__1; ++i__) {
  238. _starpu_drot_(nrhs, &b[givcol[i__ + (givcol_dim1 << 1)] + b_dim1], ldb, &
  239. b[givcol[i__ + givcol_dim1] + b_dim1], ldb, &givnum[i__ +
  240. (givnum_dim1 << 1)], &givnum[i__ + givnum_dim1]);
  241. /* L10: */
  242. }
  243. /* Step (2L): permute rows of B. */
  244. _starpu_dcopy_(nrhs, &b[nlp1 + b_dim1], ldb, &bx[bx_dim1 + 1], ldbx);
  245. i__1 = n;
  246. for (i__ = 2; i__ <= i__1; ++i__) {
  247. _starpu_dcopy_(nrhs, &b[perm[i__] + b_dim1], ldb, &bx[i__ + bx_dim1],
  248. ldbx);
  249. /* L20: */
  250. }
  251. /* Step (3L): apply the inverse of the left singular vector */
  252. /* matrix to BX. */
  253. if (*k == 1) {
  254. _starpu_dcopy_(nrhs, &bx[bx_offset], ldbx, &b[b_offset], ldb);
  255. if (z__[1] < 0.) {
  256. _starpu_dscal_(nrhs, &c_b5, &b[b_offset], ldb);
  257. }
  258. } else {
  259. i__1 = *k;
  260. for (j = 1; j <= i__1; ++j) {
  261. diflj = difl[j];
  262. dj = poles[j + poles_dim1];
  263. dsigj = -poles[j + (poles_dim1 << 1)];
  264. if (j < *k) {
  265. difrj = -difr[j + difr_dim1];
  266. dsigjp = -poles[j + 1 + (poles_dim1 << 1)];
  267. }
  268. if (z__[j] == 0. || poles[j + (poles_dim1 << 1)] == 0.) {
  269. work[j] = 0.;
  270. } else {
  271. work[j] = -poles[j + (poles_dim1 << 1)] * z__[j] / diflj /
  272. (poles[j + (poles_dim1 << 1)] + dj);
  273. }
  274. i__2 = j - 1;
  275. for (i__ = 1; i__ <= i__2; ++i__) {
  276. if (z__[i__] == 0. || poles[i__ + (poles_dim1 << 1)] ==
  277. 0.) {
  278. work[i__] = 0.;
  279. } else {
  280. work[i__] = poles[i__ + (poles_dim1 << 1)] * z__[i__]
  281. / (_starpu_dlamc3_(&poles[i__ + (poles_dim1 << 1)], &
  282. dsigj) - diflj) / (poles[i__ + (poles_dim1 <<
  283. 1)] + dj);
  284. }
  285. /* L30: */
  286. }
  287. i__2 = *k;
  288. for (i__ = j + 1; i__ <= i__2; ++i__) {
  289. if (z__[i__] == 0. || poles[i__ + (poles_dim1 << 1)] ==
  290. 0.) {
  291. work[i__] = 0.;
  292. } else {
  293. work[i__] = poles[i__ + (poles_dim1 << 1)] * z__[i__]
  294. / (_starpu_dlamc3_(&poles[i__ + (poles_dim1 << 1)], &
  295. dsigjp) + difrj) / (poles[i__ + (poles_dim1 <<
  296. 1)] + dj);
  297. }
  298. /* L40: */
  299. }
  300. work[1] = -1.;
  301. temp = _starpu_dnrm2_(k, &work[1], &c__1);
  302. _starpu_dgemv_("T", k, nrhs, &c_b11, &bx[bx_offset], ldbx, &work[1], &
  303. c__1, &c_b13, &b[j + b_dim1], ldb);
  304. _starpu_dlascl_("G", &c__0, &c__0, &temp, &c_b11, &c__1, nrhs, &b[j +
  305. b_dim1], ldb, info);
  306. /* L50: */
  307. }
  308. }
  309. /* Move the deflated rows of BX to B also. */
  310. if (*k < max(m,n)) {
  311. i__1 = n - *k;
  312. _starpu_dlacpy_("A", &i__1, nrhs, &bx[*k + 1 + bx_dim1], ldbx, &b[*k + 1
  313. + b_dim1], ldb);
  314. }
  315. } else {
  316. /* Apply back the right orthogonal transformations. */
  317. /* Step (1R): apply back the new right singular vector matrix */
  318. /* to B. */
  319. if (*k == 1) {
  320. _starpu_dcopy_(nrhs, &b[b_offset], ldb, &bx[bx_offset], ldbx);
  321. } else {
  322. i__1 = *k;
  323. for (j = 1; j <= i__1; ++j) {
  324. dsigj = poles[j + (poles_dim1 << 1)];
  325. if (z__[j] == 0.) {
  326. work[j] = 0.;
  327. } else {
  328. work[j] = -z__[j] / difl[j] / (dsigj + poles[j +
  329. poles_dim1]) / difr[j + (difr_dim1 << 1)];
  330. }
  331. i__2 = j - 1;
  332. for (i__ = 1; i__ <= i__2; ++i__) {
  333. if (z__[j] == 0.) {
  334. work[i__] = 0.;
  335. } else {
  336. d__1 = -poles[i__ + 1 + (poles_dim1 << 1)];
  337. work[i__] = z__[j] / (_starpu_dlamc3_(&dsigj, &d__1) - difr[
  338. i__ + difr_dim1]) / (dsigj + poles[i__ +
  339. poles_dim1]) / difr[i__ + (difr_dim1 << 1)];
  340. }
  341. /* L60: */
  342. }
  343. i__2 = *k;
  344. for (i__ = j + 1; i__ <= i__2; ++i__) {
  345. if (z__[j] == 0.) {
  346. work[i__] = 0.;
  347. } else {
  348. d__1 = -poles[i__ + (poles_dim1 << 1)];
  349. work[i__] = z__[j] / (_starpu_dlamc3_(&dsigj, &d__1) - difl[
  350. i__]) / (dsigj + poles[i__ + poles_dim1]) /
  351. difr[i__ + (difr_dim1 << 1)];
  352. }
  353. /* L70: */
  354. }
  355. _starpu_dgemv_("T", k, nrhs, &c_b11, &b[b_offset], ldb, &work[1], &
  356. c__1, &c_b13, &bx[j + bx_dim1], ldbx);
  357. /* L80: */
  358. }
  359. }
  360. /* Step (2R): if SQRE = 1, apply back the rotation that is */
  361. /* related to the right null space of the subproblem. */
  362. if (*sqre == 1) {
  363. _starpu_dcopy_(nrhs, &b[m + b_dim1], ldb, &bx[m + bx_dim1], ldbx);
  364. _starpu_drot_(nrhs, &bx[bx_dim1 + 1], ldbx, &bx[m + bx_dim1], ldbx, c__,
  365. s);
  366. }
  367. if (*k < max(m,n)) {
  368. i__1 = n - *k;
  369. _starpu_dlacpy_("A", &i__1, nrhs, &b[*k + 1 + b_dim1], ldb, &bx[*k + 1 +
  370. bx_dim1], ldbx);
  371. }
  372. /* Step (3R): permute rows of B. */
  373. _starpu_dcopy_(nrhs, &bx[bx_dim1 + 1], ldbx, &b[nlp1 + b_dim1], ldb);
  374. if (*sqre == 1) {
  375. _starpu_dcopy_(nrhs, &bx[m + bx_dim1], ldbx, &b[m + b_dim1], ldb);
  376. }
  377. i__1 = n;
  378. for (i__ = 2; i__ <= i__1; ++i__) {
  379. _starpu_dcopy_(nrhs, &bx[i__ + bx_dim1], ldbx, &b[perm[i__] + b_dim1],
  380. ldb);
  381. /* L90: */
  382. }
  383. /* Step (4R): apply back the Givens rotations performed. */
  384. for (i__ = *givptr; i__ >= 1; --i__) {
  385. d__1 = -givnum[i__ + givnum_dim1];
  386. _starpu_drot_(nrhs, &b[givcol[i__ + (givcol_dim1 << 1)] + b_dim1], ldb, &
  387. b[givcol[i__ + givcol_dim1] + b_dim1], ldb, &givnum[i__ +
  388. (givnum_dim1 << 1)], &d__1);
  389. /* L100: */
  390. }
  391. }
  392. return 0;
  393. /* End of DLALS0 */
  394. } /* _starpu_dlals0_ */