dggsvd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* dggsvd.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_dggsvd_(char *jobu, char *jobv, char *jobq, integer *m,
  16. integer *n, integer *p, integer *k, integer *l, doublereal *a,
  17. integer *lda, doublereal *b, integer *ldb, doublereal *alpha,
  18. doublereal *beta, doublereal *u, integer *ldu, doublereal *v, integer
  19. *ldv, doublereal *q, integer *ldq, doublereal *work, integer *iwork,
  20. integer *info)
  21. {
  22. /* System generated locals */
  23. integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, u_dim1,
  24. u_offset, v_dim1, v_offset, i__1, i__2;
  25. /* Local variables */
  26. integer i__, j;
  27. doublereal ulp;
  28. integer ibnd;
  29. doublereal tola;
  30. integer isub;
  31. doublereal tolb, unfl, temp, smax;
  32. extern logical _starpu_lsame_(char *, char *);
  33. doublereal anorm, bnorm;
  34. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  35. doublereal *, integer *);
  36. logical wantq, wantu, wantv;
  37. extern doublereal _starpu_dlamch_(char *), _starpu_dlange_(char *, integer *,
  38. integer *, doublereal *, integer *, doublereal *);
  39. extern /* Subroutine */ int _starpu_dtgsja_(char *, char *, char *, integer *,
  40. integer *, integer *, integer *, integer *, doublereal *, integer
  41. *, doublereal *, integer *, doublereal *, doublereal *,
  42. doublereal *, doublereal *, doublereal *, integer *, doublereal *,
  43. integer *, doublereal *, integer *, doublereal *, integer *,
  44. integer *);
  45. integer ncycle;
  46. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *), _starpu_dggsvp_(
  47. char *, char *, char *, integer *, integer *, integer *,
  48. doublereal *, integer *, doublereal *, integer *, doublereal *,
  49. doublereal *, integer *, integer *, doublereal *, integer *,
  50. doublereal *, integer *, doublereal *, integer *, integer *,
  51. doublereal *, doublereal *, integer *);
  52. /* -- LAPACK driver routine (version 3.2) -- */
  53. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  54. /* November 2006 */
  55. /* .. Scalar Arguments .. */
  56. /* .. */
  57. /* .. Array Arguments .. */
  58. /* .. */
  59. /* Purpose */
  60. /* ======= */
  61. /* DGGSVD computes the generalized singular value decomposition (GSVD) */
  62. /* of an M-by-N real matrix A and P-by-N real matrix B: */
  63. /* U'*A*Q = D1*( 0 R ), V'*B*Q = D2*( 0 R ) */
  64. /* where U, V and Q are orthogonal matrices, and Z' is the transpose */
  65. /* of Z. Let K+L = the effective numerical rank of the matrix (A',B')', */
  66. /* then R is a K+L-by-K+L nonsingular upper triangular matrix, D1 and */
  67. /* D2 are M-by-(K+L) and P-by-(K+L) "diagonal" matrices and of the */
  68. /* following structures, respectively: */
  69. /* If M-K-L >= 0, */
  70. /* K L */
  71. /* D1 = K ( I 0 ) */
  72. /* L ( 0 C ) */
  73. /* M-K-L ( 0 0 ) */
  74. /* K L */
  75. /* D2 = L ( 0 S ) */
  76. /* P-L ( 0 0 ) */
  77. /* N-K-L K L */
  78. /* ( 0 R ) = K ( 0 R11 R12 ) */
  79. /* L ( 0 0 R22 ) */
  80. /* where */
  81. /* C = diag( ALPHA(K+1), ... , ALPHA(K+L) ), */
  82. /* S = diag( BETA(K+1), ... , BETA(K+L) ), */
  83. /* C**2 + S**2 = I. */
  84. /* R is stored in A(1:K+L,N-K-L+1:N) on exit. */
  85. /* If M-K-L < 0, */
  86. /* K M-K K+L-M */
  87. /* D1 = K ( I 0 0 ) */
  88. /* M-K ( 0 C 0 ) */
  89. /* K M-K K+L-M */
  90. /* D2 = M-K ( 0 S 0 ) */
  91. /* K+L-M ( 0 0 I ) */
  92. /* P-L ( 0 0 0 ) */
  93. /* N-K-L K M-K K+L-M */
  94. /* ( 0 R ) = K ( 0 R11 R12 R13 ) */
  95. /* M-K ( 0 0 R22 R23 ) */
  96. /* K+L-M ( 0 0 0 R33 ) */
  97. /* where */
  98. /* C = diag( ALPHA(K+1), ... , ALPHA(M) ), */
  99. /* S = diag( BETA(K+1), ... , BETA(M) ), */
  100. /* C**2 + S**2 = I. */
  101. /* (R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N), and R33 is stored */
  102. /* ( 0 R22 R23 ) */
  103. /* in B(M-K+1:L,N+M-K-L+1:N) on exit. */
  104. /* The routine computes C, S, R, and optionally the orthogonal */
  105. /* transformation matrices U, V and Q. */
  106. /* In particular, if B is an N-by-N nonsingular matrix, then the GSVD of */
  107. /* A and B implicitly gives the SVD of A*inv(B): */
  108. /* A*inv(B) = U*(D1*inv(D2))*V'. */
  109. /* If ( A',B')' has orthonormal columns, then the GSVD of A and B is */
  110. /* also equal to the CS decomposition of A and B. Furthermore, the GSVD */
  111. /* can be used to derive the solution of the eigenvalue problem: */
  112. /* A'*A x = lambda* B'*B x. */
  113. /* In some literature, the GSVD of A and B is presented in the form */
  114. /* U'*A*X = ( 0 D1 ), V'*B*X = ( 0 D2 ) */
  115. /* where U and V are orthogonal and X is nonsingular, D1 and D2 are */
  116. /* ``diagonal''. The former GSVD form can be converted to the latter */
  117. /* form by taking the nonsingular matrix X as */
  118. /* X = Q*( I 0 ) */
  119. /* ( 0 inv(R) ). */
  120. /* Arguments */
  121. /* ========= */
  122. /* JOBU (input) CHARACTER*1 */
  123. /* = 'U': Orthogonal matrix U is computed; */
  124. /* = 'N': U is not computed. */
  125. /* JOBV (input) CHARACTER*1 */
  126. /* = 'V': Orthogonal matrix V is computed; */
  127. /* = 'N': V is not computed. */
  128. /* JOBQ (input) CHARACTER*1 */
  129. /* = 'Q': Orthogonal matrix Q is computed; */
  130. /* = 'N': Q is not computed. */
  131. /* M (input) INTEGER */
  132. /* The number of rows of the matrix A. M >= 0. */
  133. /* N (input) INTEGER */
  134. /* The number of columns of the matrices A and B. N >= 0. */
  135. /* P (input) INTEGER */
  136. /* The number of rows of the matrix B. P >= 0. */
  137. /* K (output) INTEGER */
  138. /* L (output) INTEGER */
  139. /* On exit, K and L specify the dimension of the subblocks */
  140. /* described in the Purpose section. */
  141. /* K + L = effective numerical rank of (A',B')'. */
  142. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  143. /* On entry, the M-by-N matrix A. */
  144. /* On exit, A contains the triangular matrix R, or part of R. */
  145. /* See Purpose for details. */
  146. /* LDA (input) INTEGER */
  147. /* The leading dimension of the array A. LDA >= max(1,M). */
  148. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,N) */
  149. /* On entry, the P-by-N matrix B. */
  150. /* On exit, B contains the triangular matrix R if M-K-L < 0. */
  151. /* See Purpose for details. */
  152. /* LDB (input) INTEGER */
  153. /* The leading dimension of the array B. LDB >= max(1,P). */
  154. /* ALPHA (output) DOUBLE PRECISION array, dimension (N) */
  155. /* BETA (output) DOUBLE PRECISION array, dimension (N) */
  156. /* On exit, ALPHA and BETA contain the generalized singular */
  157. /* value pairs of A and B; */
  158. /* ALPHA(1:K) = 1, */
  159. /* BETA(1:K) = 0, */
  160. /* and if M-K-L >= 0, */
  161. /* ALPHA(K+1:K+L) = C, */
  162. /* BETA(K+1:K+L) = S, */
  163. /* or if M-K-L < 0, */
  164. /* ALPHA(K+1:M)=C, ALPHA(M+1:K+L)=0 */
  165. /* BETA(K+1:M) =S, BETA(M+1:K+L) =1 */
  166. /* and */
  167. /* ALPHA(K+L+1:N) = 0 */
  168. /* BETA(K+L+1:N) = 0 */
  169. /* U (output) DOUBLE PRECISION array, dimension (LDU,M) */
  170. /* If JOBU = 'U', U contains the M-by-M orthogonal matrix U. */
  171. /* If JOBU = 'N', U is not referenced. */
  172. /* LDU (input) INTEGER */
  173. /* The leading dimension of the array U. LDU >= max(1,M) if */
  174. /* JOBU = 'U'; LDU >= 1 otherwise. */
  175. /* V (output) DOUBLE PRECISION array, dimension (LDV,P) */
  176. /* If JOBV = 'V', V contains the P-by-P orthogonal matrix V. */
  177. /* If JOBV = 'N', V is not referenced. */
  178. /* LDV (input) INTEGER */
  179. /* The leading dimension of the array V. LDV >= max(1,P) if */
  180. /* JOBV = 'V'; LDV >= 1 otherwise. */
  181. /* Q (output) DOUBLE PRECISION array, dimension (LDQ,N) */
  182. /* If JOBQ = 'Q', Q contains the N-by-N orthogonal matrix Q. */
  183. /* If JOBQ = 'N', Q is not referenced. */
  184. /* LDQ (input) INTEGER */
  185. /* The leading dimension of the array Q. LDQ >= max(1,N) if */
  186. /* JOBQ = 'Q'; LDQ >= 1 otherwise. */
  187. /* WORK (workspace) DOUBLE PRECISION array, */
  188. /* dimension (max(3*N,M,P)+N) */
  189. /* IWORK (workspace/output) INTEGER array, dimension (N) */
  190. /* On exit, IWORK stores the sorting information. More */
  191. /* precisely, the following loop will sort ALPHA */
  192. /* for I = K+1, min(M,K+L) */
  193. /* swap ALPHA(I) and ALPHA(IWORK(I)) */
  194. /* endfor */
  195. /* such that ALPHA(1) >= ALPHA(2) >= ... >= ALPHA(N). */
  196. /* INFO (output) INTEGER */
  197. /* = 0: successful exit */
  198. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  199. /* > 0: if INFO = 1, the Jacobi-type procedure failed to */
  200. /* converge. For further details, see subroutine DTGSJA. */
  201. /* Internal Parameters */
  202. /* =================== */
  203. /* TOLA DOUBLE PRECISION */
  204. /* TOLB DOUBLE PRECISION */
  205. /* TOLA and TOLB are the thresholds to determine the effective */
  206. /* rank of (A',B')'. Generally, they are set to */
  207. /* TOLA = MAX(M,N)*norm(A)*MAZHEPS, */
  208. /* TOLB = MAX(P,N)*norm(B)*MAZHEPS. */
  209. /* The size of TOLA and TOLB may affect the size of backward */
  210. /* errors of the decomposition. */
  211. /* Further Details */
  212. /* =============== */
  213. /* 2-96 Based on modifications by */
  214. /* Ming Gu and Huan Ren, Computer Science Division, University of */
  215. /* California at Berkeley, USA */
  216. /* ===================================================================== */
  217. /* .. Local Scalars .. */
  218. /* .. */
  219. /* .. External Functions .. */
  220. /* .. */
  221. /* .. External Subroutines .. */
  222. /* .. */
  223. /* .. Intrinsic Functions .. */
  224. /* .. */
  225. /* .. Executable Statements .. */
  226. /* Test the input parameters */
  227. /* Parameter adjustments */
  228. a_dim1 = *lda;
  229. a_offset = 1 + a_dim1;
  230. a -= a_offset;
  231. b_dim1 = *ldb;
  232. b_offset = 1 + b_dim1;
  233. b -= b_offset;
  234. --alpha;
  235. --beta;
  236. u_dim1 = *ldu;
  237. u_offset = 1 + u_dim1;
  238. u -= u_offset;
  239. v_dim1 = *ldv;
  240. v_offset = 1 + v_dim1;
  241. v -= v_offset;
  242. q_dim1 = *ldq;
  243. q_offset = 1 + q_dim1;
  244. q -= q_offset;
  245. --work;
  246. --iwork;
  247. /* Function Body */
  248. wantu = _starpu_lsame_(jobu, "U");
  249. wantv = _starpu_lsame_(jobv, "V");
  250. wantq = _starpu_lsame_(jobq, "Q");
  251. *info = 0;
  252. if (! (wantu || _starpu_lsame_(jobu, "N"))) {
  253. *info = -1;
  254. } else if (! (wantv || _starpu_lsame_(jobv, "N"))) {
  255. *info = -2;
  256. } else if (! (wantq || _starpu_lsame_(jobq, "N"))) {
  257. *info = -3;
  258. } else if (*m < 0) {
  259. *info = -4;
  260. } else if (*n < 0) {
  261. *info = -5;
  262. } else if (*p < 0) {
  263. *info = -6;
  264. } else if (*lda < max(1,*m)) {
  265. *info = -10;
  266. } else if (*ldb < max(1,*p)) {
  267. *info = -12;
  268. } else if (*ldu < 1 || wantu && *ldu < *m) {
  269. *info = -16;
  270. } else if (*ldv < 1 || wantv && *ldv < *p) {
  271. *info = -18;
  272. } else if (*ldq < 1 || wantq && *ldq < *n) {
  273. *info = -20;
  274. }
  275. if (*info != 0) {
  276. i__1 = -(*info);
  277. _starpu_xerbla_("DGGSVD", &i__1);
  278. return 0;
  279. }
  280. /* Compute the Frobenius norm of matrices A and B */
  281. anorm = _starpu_dlange_("1", m, n, &a[a_offset], lda, &work[1]);
  282. bnorm = _starpu_dlange_("1", p, n, &b[b_offset], ldb, &work[1]);
  283. /* Get machine precision and set up threshold for determining */
  284. /* the effective numerical rank of the matrices A and B. */
  285. ulp = _starpu_dlamch_("Precision");
  286. unfl = _starpu_dlamch_("Safe Minimum");
  287. tola = max(*m,*n) * max(anorm,unfl) * ulp;
  288. tolb = max(*p,*n) * max(bnorm,unfl) * ulp;
  289. /* Preprocessing */
  290. _starpu_dggsvp_(jobu, jobv, jobq, m, p, n, &a[a_offset], lda, &b[b_offset], ldb, &
  291. tola, &tolb, k, l, &u[u_offset], ldu, &v[v_offset], ldv, &q[
  292. q_offset], ldq, &iwork[1], &work[1], &work[*n + 1], info);
  293. /* Compute the GSVD of two upper "triangular" matrices */
  294. _starpu_dtgsja_(jobu, jobv, jobq, m, p, n, k, l, &a[a_offset], lda, &b[b_offset],
  295. ldb, &tola, &tolb, &alpha[1], &beta[1], &u[u_offset], ldu, &v[
  296. v_offset], ldv, &q[q_offset], ldq, &work[1], &ncycle, info);
  297. /* Sort the singular values and store the pivot indices in IWORK */
  298. /* Copy ALPHA to WORK, then sort ALPHA in WORK */
  299. _starpu_dcopy_(n, &alpha[1], &c__1, &work[1], &c__1);
  300. /* Computing MIN */
  301. i__1 = *l, i__2 = *m - *k;
  302. ibnd = min(i__1,i__2);
  303. i__1 = ibnd;
  304. for (i__ = 1; i__ <= i__1; ++i__) {
  305. /* Scan for largest ALPHA(K+I) */
  306. isub = i__;
  307. smax = work[*k + i__];
  308. i__2 = ibnd;
  309. for (j = i__ + 1; j <= i__2; ++j) {
  310. temp = work[*k + j];
  311. if (temp > smax) {
  312. isub = j;
  313. smax = temp;
  314. }
  315. /* L10: */
  316. }
  317. if (isub != i__) {
  318. work[*k + isub] = work[*k + i__];
  319. work[*k + i__] = smax;
  320. iwork[*k + i__] = *k + isub;
  321. } else {
  322. iwork[*k + i__] = *k + i__;
  323. }
  324. /* L20: */
  325. }
  326. return 0;
  327. /* End of DGGSVD */
  328. } /* _starpu_dggsvd_ */