dlasdq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* dlasdq.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_dlasdq_(char *uplo, integer *sqre, integer *n, integer *
  16. ncvt, integer *nru, integer *ncc, doublereal *d__, doublereal *e,
  17. doublereal *vt, integer *ldvt, doublereal *u, integer *ldu,
  18. doublereal *c__, integer *ldc, doublereal *work, integer *info)
  19. {
  20. /* System generated locals */
  21. integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1,
  22. i__2;
  23. /* Local variables */
  24. integer i__, j;
  25. doublereal r__, cs, sn;
  26. integer np1, isub;
  27. doublereal smin;
  28. integer sqre1;
  29. extern logical _starpu_lsame_(char *, char *);
  30. extern /* Subroutine */ int _starpu_dlasr_(char *, char *, char *, integer *,
  31. integer *, doublereal *, doublereal *, doublereal *, integer *), _starpu_dswap_(integer *, doublereal *, integer *
  32. , doublereal *, integer *);
  33. integer iuplo;
  34. extern /* Subroutine */ int _starpu_dlartg_(doublereal *, doublereal *,
  35. doublereal *, doublereal *, doublereal *), _starpu_xerbla_(char *,
  36. integer *), _starpu_dbdsqr_(char *, integer *, integer *, integer
  37. *, integer *, doublereal *, doublereal *, doublereal *, integer *,
  38. doublereal *, integer *, doublereal *, integer *, doublereal *,
  39. integer *);
  40. logical rotate;
  41. /* -- LAPACK auxiliary routine (version 3.2) -- */
  42. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  43. /* November 2006 */
  44. /* .. Scalar Arguments .. */
  45. /* .. */
  46. /* .. Array Arguments .. */
  47. /* .. */
  48. /* Purpose */
  49. /* ======= */
  50. /* DLASDQ computes the singular value decomposition (SVD) of a real */
  51. /* (upper or lower) bidiagonal matrix with diagonal D and offdiagonal */
  52. /* E, accumulating the transformations if desired. Letting B denote */
  53. /* the input bidiagonal matrix, the algorithm computes orthogonal */
  54. /* matrices Q and P such that B = Q * S * P' (P' denotes the transpose */
  55. /* of P). The singular values S are overwritten on D. */
  56. /* The input matrix U is changed to U * Q if desired. */
  57. /* The input matrix VT is changed to P' * VT if desired. */
  58. /* The input matrix C is changed to Q' * C if desired. */
  59. /* See "Computing Small Singular Values of Bidiagonal Matrices With */
  60. /* Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
  61. /* LAPACK Working Note #3, for a detailed description of the algorithm. */
  62. /* Arguments */
  63. /* ========= */
  64. /* UPLO (input) CHARACTER*1 */
  65. /* On entry, UPLO specifies whether the input bidiagonal matrix */
  66. /* is upper or lower bidiagonal, and wether it is square are */
  67. /* not. */
  68. /* UPLO = 'U' or 'u' B is upper bidiagonal. */
  69. /* UPLO = 'L' or 'l' B is lower bidiagonal. */
  70. /* SQRE (input) INTEGER */
  71. /* = 0: then the input matrix is N-by-N. */
  72. /* = 1: then the input matrix is N-by-(N+1) if UPLU = 'U' and */
  73. /* (N+1)-by-N if UPLU = 'L'. */
  74. /* The bidiagonal matrix has */
  75. /* N = NL + NR + 1 rows and */
  76. /* M = N + SQRE >= N columns. */
  77. /* N (input) INTEGER */
  78. /* On entry, N specifies the number of rows and columns */
  79. /* in the matrix. N must be at least 0. */
  80. /* NCVT (input) INTEGER */
  81. /* On entry, NCVT specifies the number of columns of */
  82. /* the matrix VT. NCVT must be at least 0. */
  83. /* NRU (input) INTEGER */
  84. /* On entry, NRU specifies the number of rows of */
  85. /* the matrix U. NRU must be at least 0. */
  86. /* NCC (input) INTEGER */
  87. /* On entry, NCC specifies the number of columns of */
  88. /* the matrix C. NCC must be at least 0. */
  89. /* D (input/output) DOUBLE PRECISION array, dimension (N) */
  90. /* On entry, D contains the diagonal entries of the */
  91. /* bidiagonal matrix whose SVD is desired. On normal exit, */
  92. /* D contains the singular values in ascending order. */
  93. /* E (input/output) DOUBLE PRECISION array. */
  94. /* dimension is (N-1) if SQRE = 0 and N if SQRE = 1. */
  95. /* On entry, the entries of E contain the offdiagonal entries */
  96. /* of the bidiagonal matrix whose SVD is desired. On normal */
  97. /* exit, E will contain 0. If the algorithm does not converge, */
  98. /* D and E will contain the diagonal and superdiagonal entries */
  99. /* of a bidiagonal matrix orthogonally equivalent to the one */
  100. /* given as input. */
  101. /* VT (input/output) DOUBLE PRECISION array, dimension (LDVT, NCVT) */
  102. /* On entry, contains a matrix which on exit has been */
  103. /* premultiplied by P', dimension N-by-NCVT if SQRE = 0 */
  104. /* and (N+1)-by-NCVT if SQRE = 1 (not referenced if NCVT=0). */
  105. /* LDVT (input) INTEGER */
  106. /* On entry, LDVT specifies the leading dimension of VT as */
  107. /* declared in the calling (sub) program. LDVT must be at */
  108. /* least 1. If NCVT is nonzero LDVT must also be at least N. */
  109. /* U (input/output) DOUBLE PRECISION array, dimension (LDU, N) */
  110. /* On entry, contains a matrix which on exit has been */
  111. /* postmultiplied by Q, dimension NRU-by-N if SQRE = 0 */
  112. /* and NRU-by-(N+1) if SQRE = 1 (not referenced if NRU=0). */
  113. /* LDU (input) INTEGER */
  114. /* On entry, LDU specifies the leading dimension of U as */
  115. /* declared in the calling (sub) program. LDU must be at */
  116. /* least max( 1, NRU ) . */
  117. /* C (input/output) DOUBLE PRECISION array, dimension (LDC, NCC) */
  118. /* On entry, contains an N-by-NCC matrix which on exit */
  119. /* has been premultiplied by Q' dimension N-by-NCC if SQRE = 0 */
  120. /* and (N+1)-by-NCC if SQRE = 1 (not referenced if NCC=0). */
  121. /* LDC (input) INTEGER */
  122. /* On entry, LDC specifies the leading dimension of C as */
  123. /* declared in the calling (sub) program. LDC must be at */
  124. /* least 1. If NCC is nonzero, LDC must also be at least N. */
  125. /* WORK (workspace) DOUBLE PRECISION array, dimension (4*N) */
  126. /* Workspace. Only referenced if one of NCVT, NRU, or NCC is */
  127. /* nonzero, and if N is at least 2. */
  128. /* INFO (output) INTEGER */
  129. /* On exit, a value of 0 indicates a successful exit. */
  130. /* If INFO < 0, argument number -INFO is illegal. */
  131. /* If INFO > 0, the algorithm did not converge, and INFO */
  132. /* specifies how many superdiagonals did not converge. */
  133. /* Further Details */
  134. /* =============== */
  135. /* Based on contributions by */
  136. /* Ming Gu and Huan Ren, Computer Science Division, University of */
  137. /* California at Berkeley, USA */
  138. /* ===================================================================== */
  139. /* .. Parameters .. */
  140. /* .. */
  141. /* .. Local Scalars .. */
  142. /* .. */
  143. /* .. External Subroutines .. */
  144. /* .. */
  145. /* .. External Functions .. */
  146. /* .. */
  147. /* .. Intrinsic Functions .. */
  148. /* .. */
  149. /* .. Executable Statements .. */
  150. /* Test the input parameters. */
  151. /* Parameter adjustments */
  152. --d__;
  153. --e;
  154. vt_dim1 = *ldvt;
  155. vt_offset = 1 + vt_dim1;
  156. vt -= vt_offset;
  157. u_dim1 = *ldu;
  158. u_offset = 1 + u_dim1;
  159. u -= u_offset;
  160. c_dim1 = *ldc;
  161. c_offset = 1 + c_dim1;
  162. c__ -= c_offset;
  163. --work;
  164. /* Function Body */
  165. *info = 0;
  166. iuplo = 0;
  167. if (_starpu_lsame_(uplo, "U")) {
  168. iuplo = 1;
  169. }
  170. if (_starpu_lsame_(uplo, "L")) {
  171. iuplo = 2;
  172. }
  173. if (iuplo == 0) {
  174. *info = -1;
  175. } else if (*sqre < 0 || *sqre > 1) {
  176. *info = -2;
  177. } else if (*n < 0) {
  178. *info = -3;
  179. } else if (*ncvt < 0) {
  180. *info = -4;
  181. } else if (*nru < 0) {
  182. *info = -5;
  183. } else if (*ncc < 0) {
  184. *info = -6;
  185. } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < max(1,*n)) {
  186. *info = -10;
  187. } else if (*ldu < max(1,*nru)) {
  188. *info = -12;
  189. } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < max(1,*n)) {
  190. *info = -14;
  191. }
  192. if (*info != 0) {
  193. i__1 = -(*info);
  194. _starpu_xerbla_("DLASDQ", &i__1);
  195. return 0;
  196. }
  197. if (*n == 0) {
  198. return 0;
  199. }
  200. /* ROTATE is true if any singular vectors desired, false otherwise */
  201. rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
  202. np1 = *n + 1;
  203. sqre1 = *sqre;
  204. /* If matrix non-square upper bidiagonal, rotate to be lower */
  205. /* bidiagonal. The rotations are on the right. */
  206. if (iuplo == 1 && sqre1 == 1) {
  207. i__1 = *n - 1;
  208. for (i__ = 1; i__ <= i__1; ++i__) {
  209. _starpu_dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
  210. d__[i__] = r__;
  211. e[i__] = sn * d__[i__ + 1];
  212. d__[i__ + 1] = cs * d__[i__ + 1];
  213. if (rotate) {
  214. work[i__] = cs;
  215. work[*n + i__] = sn;
  216. }
  217. /* L10: */
  218. }
  219. _starpu_dlartg_(&d__[*n], &e[*n], &cs, &sn, &r__);
  220. d__[*n] = r__;
  221. e[*n] = 0.;
  222. if (rotate) {
  223. work[*n] = cs;
  224. work[*n + *n] = sn;
  225. }
  226. iuplo = 2;
  227. sqre1 = 0;
  228. /* Update singular vectors if desired. */
  229. if (*ncvt > 0) {
  230. _starpu_dlasr_("L", "V", "F", &np1, ncvt, &work[1], &work[np1], &vt[
  231. vt_offset], ldvt);
  232. }
  233. }
  234. /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
  235. /* by applying Givens rotations on the left. */
  236. if (iuplo == 2) {
  237. i__1 = *n - 1;
  238. for (i__ = 1; i__ <= i__1; ++i__) {
  239. _starpu_dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
  240. d__[i__] = r__;
  241. e[i__] = sn * d__[i__ + 1];
  242. d__[i__ + 1] = cs * d__[i__ + 1];
  243. if (rotate) {
  244. work[i__] = cs;
  245. work[*n + i__] = sn;
  246. }
  247. /* L20: */
  248. }
  249. /* If matrix (N+1)-by-N lower bidiagonal, one additional */
  250. /* rotation is needed. */
  251. if (sqre1 == 1) {
  252. _starpu_dlartg_(&d__[*n], &e[*n], &cs, &sn, &r__);
  253. d__[*n] = r__;
  254. if (rotate) {
  255. work[*n] = cs;
  256. work[*n + *n] = sn;
  257. }
  258. }
  259. /* Update singular vectors if desired. */
  260. if (*nru > 0) {
  261. if (sqre1 == 0) {
  262. _starpu_dlasr_("R", "V", "F", nru, n, &work[1], &work[np1], &u[
  263. u_offset], ldu);
  264. } else {
  265. _starpu_dlasr_("R", "V", "F", nru, &np1, &work[1], &work[np1], &u[
  266. u_offset], ldu);
  267. }
  268. }
  269. if (*ncc > 0) {
  270. if (sqre1 == 0) {
  271. _starpu_dlasr_("L", "V", "F", n, ncc, &work[1], &work[np1], &c__[
  272. c_offset], ldc);
  273. } else {
  274. _starpu_dlasr_("L", "V", "F", &np1, ncc, &work[1], &work[np1], &c__[
  275. c_offset], ldc);
  276. }
  277. }
  278. }
  279. /* Call DBDSQR to compute the SVD of the reduced real */
  280. /* N-by-N upper bidiagonal matrix. */
  281. _starpu_dbdsqr_("U", n, ncvt, nru, ncc, &d__[1], &e[1], &vt[vt_offset], ldvt, &u[
  282. u_offset], ldu, &c__[c_offset], ldc, &work[1], info);
  283. /* Sort the singular values into ascending order (insertion sort on */
  284. /* singular values, but only one transposition per singular vector) */
  285. i__1 = *n;
  286. for (i__ = 1; i__ <= i__1; ++i__) {
  287. /* Scan for smallest D(I). */
  288. isub = i__;
  289. smin = d__[i__];
  290. i__2 = *n;
  291. for (j = i__ + 1; j <= i__2; ++j) {
  292. if (d__[j] < smin) {
  293. isub = j;
  294. smin = d__[j];
  295. }
  296. /* L30: */
  297. }
  298. if (isub != i__) {
  299. /* Swap singular values and vectors. */
  300. d__[isub] = d__[i__];
  301. d__[i__] = smin;
  302. if (*ncvt > 0) {
  303. _starpu_dswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[i__ + vt_dim1],
  304. ldvt);
  305. }
  306. if (*nru > 0) {
  307. _starpu_dswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[i__ * u_dim1 + 1]
  308. , &c__1);
  309. }
  310. if (*ncc > 0) {
  311. _starpu_dswap_(ncc, &c__[isub + c_dim1], ldc, &c__[i__ + c_dim1], ldc)
  312. ;
  313. }
  314. }
  315. /* L40: */
  316. }
  317. return 0;
  318. /* End of DLASDQ */
  319. } /* _starpu_dlasdq_ */