dsytf2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* dsytf2.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 dsytf2_(char *uplo, integer *n, doublereal *a, integer *
  16. lda, integer *ipiv, integer *info)
  17. {
  18. /* System generated locals */
  19. integer a_dim1, a_offset, i__1, i__2;
  20. doublereal d__1, d__2, d__3;
  21. /* Builtin functions */
  22. double sqrt(doublereal);
  23. /* Local variables */
  24. integer i__, j, k;
  25. doublereal t, r1, d11, d12, d21, d22;
  26. integer kk, kp;
  27. doublereal wk, wkm1, wkp1;
  28. integer imax, jmax;
  29. extern /* Subroutine */ int dsyr_(char *, integer *, doublereal *,
  30. doublereal *, integer *, doublereal *, integer *);
  31. doublereal alpha;
  32. extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *,
  33. integer *);
  34. extern logical lsame_(char *, char *);
  35. extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *,
  36. doublereal *, integer *);
  37. integer kstep;
  38. logical upper;
  39. doublereal absakk;
  40. extern integer idamax_(integer *, doublereal *, integer *);
  41. extern logical disnan_(doublereal *);
  42. extern /* Subroutine */ int xerbla_(char *, integer *);
  43. doublereal colmax, rowmax;
  44. /* -- LAPACK routine (version 3.2) -- */
  45. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  46. /* November 2006 */
  47. /* .. Scalar Arguments .. */
  48. /* .. */
  49. /* .. Array Arguments .. */
  50. /* .. */
  51. /* Purpose */
  52. /* ======= */
  53. /* DSYTF2 computes the factorization of a real symmetric matrix A using */
  54. /* the Bunch-Kaufman diagonal pivoting method: */
  55. /* A = U*D*U' or A = L*D*L' */
  56. /* where U (or L) is a product of permutation and unit upper (lower) */
  57. /* triangular matrices, U' is the transpose of U, and D is symmetric and */
  58. /* block diagonal with 1-by-1 and 2-by-2 diagonal blocks. */
  59. /* This is the unblocked version of the algorithm, calling Level 2 BLAS. */
  60. /* Arguments */
  61. /* ========= */
  62. /* UPLO (input) CHARACTER*1 */
  63. /* Specifies whether the upper or lower triangular part of the */
  64. /* symmetric matrix A is stored: */
  65. /* = 'U': Upper triangular */
  66. /* = 'L': Lower triangular */
  67. /* N (input) INTEGER */
  68. /* The order of the matrix A. N >= 0. */
  69. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  70. /* On entry, the symmetric matrix A. If UPLO = 'U', the leading */
  71. /* n-by-n upper triangular part of A contains the upper */
  72. /* triangular part of the matrix A, and the strictly lower */
  73. /* triangular part of A is not referenced. If UPLO = 'L', the */
  74. /* leading n-by-n lower triangular part of A contains the lower */
  75. /* triangular part of the matrix A, and the strictly upper */
  76. /* triangular part of A is not referenced. */
  77. /* On exit, the block diagonal matrix D and the multipliers used */
  78. /* to obtain the factor U or L (see below for further details). */
  79. /* LDA (input) INTEGER */
  80. /* The leading dimension of the array A. LDA >= max(1,N). */
  81. /* IPIV (output) INTEGER array, dimension (N) */
  82. /* Details of the interchanges and the block structure of D. */
  83. /* If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
  84. /* interchanged and D(k,k) is a 1-by-1 diagonal block. */
  85. /* If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and */
  86. /* columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k) */
  87. /* is a 2-by-2 diagonal block. If UPLO = 'L' and IPIV(k) = */
  88. /* IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were */
  89. /* interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block. */
  90. /* INFO (output) INTEGER */
  91. /* = 0: successful exit */
  92. /* < 0: if INFO = -k, the k-th argument had an illegal value */
  93. /* > 0: if INFO = k, D(k,k) is exactly zero. The factorization */
  94. /* has been completed, but the block diagonal matrix D is */
  95. /* exactly singular, and division by zero will occur if it */
  96. /* is used to solve a system of equations. */
  97. /* Further Details */
  98. /* =============== */
  99. /* 09-29-06 - patch from */
  100. /* Bobby Cheng, MathWorks */
  101. /* Replace l.204 and l.372 */
  102. /* IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN */
  103. /* by */
  104. /* IF( (MAX( ABSAKK, COLMAX ).EQ.ZERO) .OR. DISNAN(ABSAKK) ) THEN */
  105. /* 01-01-96 - Based on modifications by */
  106. /* J. Lewis, Boeing Computer Services Company */
  107. /* A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA */
  108. /* 1-96 - Based on modifications by J. Lewis, Boeing Computer Services */
  109. /* Company */
  110. /* If UPLO = 'U', then A = U*D*U', where */
  111. /* U = P(n)*U(n)* ... *P(k)U(k)* ..., */
  112. /* i.e., U is a product of terms P(k)*U(k), where k decreases from n to */
  113. /* 1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
  114. /* and 2-by-2 diagonal blocks D(k). P(k) is a permutation matrix as */
  115. /* defined by IPIV(k), and U(k) is a unit upper triangular matrix, such */
  116. /* that if the diagonal block D(k) is of order s (s = 1 or 2), then */
  117. /* ( I v 0 ) k-s */
  118. /* U(k) = ( 0 I 0 ) s */
  119. /* ( 0 0 I ) n-k */
  120. /* k-s s n-k */
  121. /* If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k). */
  122. /* If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k), */
  123. /* and A(k,k), and v overwrites A(1:k-2,k-1:k). */
  124. /* If UPLO = 'L', then A = L*D*L', where */
  125. /* L = P(1)*L(1)* ... *P(k)*L(k)* ..., */
  126. /* i.e., L is a product of terms P(k)*L(k), where k increases from 1 to */
  127. /* n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
  128. /* and 2-by-2 diagonal blocks D(k). P(k) is a permutation matrix as */
  129. /* defined by IPIV(k), and L(k) is a unit lower triangular matrix, such */
  130. /* that if the diagonal block D(k) is of order s (s = 1 or 2), then */
  131. /* ( I 0 0 ) k-1 */
  132. /* L(k) = ( 0 I 0 ) s */
  133. /* ( 0 v I ) n-k-s+1 */
  134. /* k-1 s n-k-s+1 */
  135. /* If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k). */
  136. /* If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k), */
  137. /* and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1). */
  138. /* ===================================================================== */
  139. /* .. Parameters .. */
  140. /* .. */
  141. /* .. Local Scalars .. */
  142. /* .. */
  143. /* .. External Functions .. */
  144. /* .. */
  145. /* .. External Subroutines .. */
  146. /* .. */
  147. /* .. Intrinsic Functions .. */
  148. /* .. */
  149. /* .. Executable Statements .. */
  150. /* Test the input parameters. */
  151. /* Parameter adjustments */
  152. a_dim1 = *lda;
  153. a_offset = 1 + a_dim1;
  154. a -= a_offset;
  155. --ipiv;
  156. /* Function Body */
  157. *info = 0;
  158. upper = lsame_(uplo, "U");
  159. if (! upper && ! lsame_(uplo, "L")) {
  160. *info = -1;
  161. } else if (*n < 0) {
  162. *info = -2;
  163. } else if (*lda < max(1,*n)) {
  164. *info = -4;
  165. }
  166. if (*info != 0) {
  167. i__1 = -(*info);
  168. xerbla_("DSYTF2", &i__1);
  169. return 0;
  170. }
  171. /* Initialize ALPHA for use in choosing pivot block size. */
  172. alpha = (sqrt(17.) + 1.) / 8.;
  173. if (upper) {
  174. /* Factorize A as U*D*U' using the upper triangle of A */
  175. /* K is the main loop index, decreasing from N to 1 in steps of */
  176. /* 1 or 2 */
  177. k = *n;
  178. L10:
  179. /* If K < 1, exit from loop */
  180. if (k < 1) {
  181. goto L70;
  182. }
  183. kstep = 1;
  184. /* Determine rows and columns to be interchanged and whether */
  185. /* a 1-by-1 or 2-by-2 pivot block will be used */
  186. absakk = (d__1 = a[k + k * a_dim1], abs(d__1));
  187. /* IMAX is the row-index of the largest off-diagonal element in */
  188. /* column K, and COLMAX is its absolute value */
  189. if (k > 1) {
  190. i__1 = k - 1;
  191. imax = idamax_(&i__1, &a[k * a_dim1 + 1], &c__1);
  192. colmax = (d__1 = a[imax + k * a_dim1], abs(d__1));
  193. } else {
  194. colmax = 0.;
  195. }
  196. if (max(absakk,colmax) == 0. || disnan_(&absakk)) {
  197. /* Column K is zero or contains a NaN: set INFO and continue */
  198. if (*info == 0) {
  199. *info = k;
  200. }
  201. kp = k;
  202. } else {
  203. if (absakk >= alpha * colmax) {
  204. /* no interchange, use 1-by-1 pivot block */
  205. kp = k;
  206. } else {
  207. /* JMAX is the column-index of the largest off-diagonal */
  208. /* element in row IMAX, and ROWMAX is its absolute value */
  209. i__1 = k - imax;
  210. jmax = imax + idamax_(&i__1, &a[imax + (imax + 1) * a_dim1],
  211. lda);
  212. rowmax = (d__1 = a[imax + jmax * a_dim1], abs(d__1));
  213. if (imax > 1) {
  214. i__1 = imax - 1;
  215. jmax = idamax_(&i__1, &a[imax * a_dim1 + 1], &c__1);
  216. /* Computing MAX */
  217. d__2 = rowmax, d__3 = (d__1 = a[jmax + imax * a_dim1],
  218. abs(d__1));
  219. rowmax = max(d__2,d__3);
  220. }
  221. if (absakk >= alpha * colmax * (colmax / rowmax)) {
  222. /* no interchange, use 1-by-1 pivot block */
  223. kp = k;
  224. } else if ((d__1 = a[imax + imax * a_dim1], abs(d__1)) >=
  225. alpha * rowmax) {
  226. /* interchange rows and columns K and IMAX, use 1-by-1 */
  227. /* pivot block */
  228. kp = imax;
  229. } else {
  230. /* interchange rows and columns K-1 and IMAX, use 2-by-2 */
  231. /* pivot block */
  232. kp = imax;
  233. kstep = 2;
  234. }
  235. }
  236. kk = k - kstep + 1;
  237. if (kp != kk) {
  238. /* Interchange rows and columns KK and KP in the leading */
  239. /* submatrix A(1:k,1:k) */
  240. i__1 = kp - 1;
  241. dswap_(&i__1, &a[kk * a_dim1 + 1], &c__1, &a[kp * a_dim1 + 1],
  242. &c__1);
  243. i__1 = kk - kp - 1;
  244. dswap_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + (kp +
  245. 1) * a_dim1], lda);
  246. t = a[kk + kk * a_dim1];
  247. a[kk + kk * a_dim1] = a[kp + kp * a_dim1];
  248. a[kp + kp * a_dim1] = t;
  249. if (kstep == 2) {
  250. t = a[k - 1 + k * a_dim1];
  251. a[k - 1 + k * a_dim1] = a[kp + k * a_dim1];
  252. a[kp + k * a_dim1] = t;
  253. }
  254. }
  255. /* Update the leading submatrix */
  256. if (kstep == 1) {
  257. /* 1-by-1 pivot block D(k): column k now holds */
  258. /* W(k) = U(k)*D(k) */
  259. /* where U(k) is the k-th column of U */
  260. /* Perform a rank-1 update of A(1:k-1,1:k-1) as */
  261. /* A := A - U(k)*D(k)*U(k)' = A - W(k)*1/D(k)*W(k)' */
  262. r1 = 1. / a[k + k * a_dim1];
  263. i__1 = k - 1;
  264. d__1 = -r1;
  265. dsyr_(uplo, &i__1, &d__1, &a[k * a_dim1 + 1], &c__1, &a[
  266. a_offset], lda);
  267. /* Store U(k) in column k */
  268. i__1 = k - 1;
  269. dscal_(&i__1, &r1, &a[k * a_dim1 + 1], &c__1);
  270. } else {
  271. /* 2-by-2 pivot block D(k): columns k and k-1 now hold */
  272. /* ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k) */
  273. /* where U(k) and U(k-1) are the k-th and (k-1)-th columns */
  274. /* of U */
  275. /* Perform a rank-2 update of A(1:k-2,1:k-2) as */
  276. /* A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )' */
  277. /* = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )' */
  278. if (k > 2) {
  279. d12 = a[k - 1 + k * a_dim1];
  280. d22 = a[k - 1 + (k - 1) * a_dim1] / d12;
  281. d11 = a[k + k * a_dim1] / d12;
  282. t = 1. / (d11 * d22 - 1.);
  283. d12 = t / d12;
  284. for (j = k - 2; j >= 1; --j) {
  285. wkm1 = d12 * (d11 * a[j + (k - 1) * a_dim1] - a[j + k
  286. * a_dim1]);
  287. wk = d12 * (d22 * a[j + k * a_dim1] - a[j + (k - 1) *
  288. a_dim1]);
  289. for (i__ = j; i__ >= 1; --i__) {
  290. a[i__ + j * a_dim1] = a[i__ + j * a_dim1] - a[i__
  291. + k * a_dim1] * wk - a[i__ + (k - 1) *
  292. a_dim1] * wkm1;
  293. /* L20: */
  294. }
  295. a[j + k * a_dim1] = wk;
  296. a[j + (k - 1) * a_dim1] = wkm1;
  297. /* L30: */
  298. }
  299. }
  300. }
  301. }
  302. /* Store details of the interchanges in IPIV */
  303. if (kstep == 1) {
  304. ipiv[k] = kp;
  305. } else {
  306. ipiv[k] = -kp;
  307. ipiv[k - 1] = -kp;
  308. }
  309. /* Decrease K and return to the start of the main loop */
  310. k -= kstep;
  311. goto L10;
  312. } else {
  313. /* Factorize A as L*D*L' using the lower triangle of A */
  314. /* K is the main loop index, increasing from 1 to N in steps of */
  315. /* 1 or 2 */
  316. k = 1;
  317. L40:
  318. /* If K > N, exit from loop */
  319. if (k > *n) {
  320. goto L70;
  321. }
  322. kstep = 1;
  323. /* Determine rows and columns to be interchanged and whether */
  324. /* a 1-by-1 or 2-by-2 pivot block will be used */
  325. absakk = (d__1 = a[k + k * a_dim1], abs(d__1));
  326. /* IMAX is the row-index of the largest off-diagonal element in */
  327. /* column K, and COLMAX is its absolute value */
  328. if (k < *n) {
  329. i__1 = *n - k;
  330. imax = k + idamax_(&i__1, &a[k + 1 + k * a_dim1], &c__1);
  331. colmax = (d__1 = a[imax + k * a_dim1], abs(d__1));
  332. } else {
  333. colmax = 0.;
  334. }
  335. if (max(absakk,colmax) == 0. || disnan_(&absakk)) {
  336. /* Column K is zero or contains a NaN: set INFO and continue */
  337. if (*info == 0) {
  338. *info = k;
  339. }
  340. kp = k;
  341. } else {
  342. if (absakk >= alpha * colmax) {
  343. /* no interchange, use 1-by-1 pivot block */
  344. kp = k;
  345. } else {
  346. /* JMAX is the column-index of the largest off-diagonal */
  347. /* element in row IMAX, and ROWMAX is its absolute value */
  348. i__1 = imax - k;
  349. jmax = k - 1 + idamax_(&i__1, &a[imax + k * a_dim1], lda);
  350. rowmax = (d__1 = a[imax + jmax * a_dim1], abs(d__1));
  351. if (imax < *n) {
  352. i__1 = *n - imax;
  353. jmax = imax + idamax_(&i__1, &a[imax + 1 + imax * a_dim1],
  354. &c__1);
  355. /* Computing MAX */
  356. d__2 = rowmax, d__3 = (d__1 = a[jmax + imax * a_dim1],
  357. abs(d__1));
  358. rowmax = max(d__2,d__3);
  359. }
  360. if (absakk >= alpha * colmax * (colmax / rowmax)) {
  361. /* no interchange, use 1-by-1 pivot block */
  362. kp = k;
  363. } else if ((d__1 = a[imax + imax * a_dim1], abs(d__1)) >=
  364. alpha * rowmax) {
  365. /* interchange rows and columns K and IMAX, use 1-by-1 */
  366. /* pivot block */
  367. kp = imax;
  368. } else {
  369. /* interchange rows and columns K+1 and IMAX, use 2-by-2 */
  370. /* pivot block */
  371. kp = imax;
  372. kstep = 2;
  373. }
  374. }
  375. kk = k + kstep - 1;
  376. if (kp != kk) {
  377. /* Interchange rows and columns KK and KP in the trailing */
  378. /* submatrix A(k:n,k:n) */
  379. if (kp < *n) {
  380. i__1 = *n - kp;
  381. dswap_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + 1
  382. + kp * a_dim1], &c__1);
  383. }
  384. i__1 = kp - kk - 1;
  385. dswap_(&i__1, &a[kk + 1 + kk * a_dim1], &c__1, &a[kp + (kk +
  386. 1) * a_dim1], lda);
  387. t = a[kk + kk * a_dim1];
  388. a[kk + kk * a_dim1] = a[kp + kp * a_dim1];
  389. a[kp + kp * a_dim1] = t;
  390. if (kstep == 2) {
  391. t = a[k + 1 + k * a_dim1];
  392. a[k + 1 + k * a_dim1] = a[kp + k * a_dim1];
  393. a[kp + k * a_dim1] = t;
  394. }
  395. }
  396. /* Update the trailing submatrix */
  397. if (kstep == 1) {
  398. /* 1-by-1 pivot block D(k): column k now holds */
  399. /* W(k) = L(k)*D(k) */
  400. /* where L(k) is the k-th column of L */
  401. if (k < *n) {
  402. /* Perform a rank-1 update of A(k+1:n,k+1:n) as */
  403. /* A := A - L(k)*D(k)*L(k)' = A - W(k)*(1/D(k))*W(k)' */
  404. d11 = 1. / a[k + k * a_dim1];
  405. i__1 = *n - k;
  406. d__1 = -d11;
  407. dsyr_(uplo, &i__1, &d__1, &a[k + 1 + k * a_dim1], &c__1, &
  408. a[k + 1 + (k + 1) * a_dim1], lda);
  409. /* Store L(k) in column K */
  410. i__1 = *n - k;
  411. dscal_(&i__1, &d11, &a[k + 1 + k * a_dim1], &c__1);
  412. }
  413. } else {
  414. /* 2-by-2 pivot block D(k) */
  415. if (k < *n - 1) {
  416. /* Perform a rank-2 update of A(k+2:n,k+2:n) as */
  417. /* A := A - ( (A(k) A(k+1))*D(k)**(-1) ) * (A(k) A(k+1))' */
  418. /* where L(k) and L(k+1) are the k-th and (k+1)-th */
  419. /* columns of L */
  420. d21 = a[k + 1 + k * a_dim1];
  421. d11 = a[k + 1 + (k + 1) * a_dim1] / d21;
  422. d22 = a[k + k * a_dim1] / d21;
  423. t = 1. / (d11 * d22 - 1.);
  424. d21 = t / d21;
  425. i__1 = *n;
  426. for (j = k + 2; j <= i__1; ++j) {
  427. wk = d21 * (d11 * a[j + k * a_dim1] - a[j + (k + 1) *
  428. a_dim1]);
  429. wkp1 = d21 * (d22 * a[j + (k + 1) * a_dim1] - a[j + k
  430. * a_dim1]);
  431. i__2 = *n;
  432. for (i__ = j; i__ <= i__2; ++i__) {
  433. a[i__ + j * a_dim1] = a[i__ + j * a_dim1] - a[i__
  434. + k * a_dim1] * wk - a[i__ + (k + 1) *
  435. a_dim1] * wkp1;
  436. /* L50: */
  437. }
  438. a[j + k * a_dim1] = wk;
  439. a[j + (k + 1) * a_dim1] = wkp1;
  440. /* L60: */
  441. }
  442. }
  443. }
  444. }
  445. /* Store details of the interchanges in IPIV */
  446. if (kstep == 1) {
  447. ipiv[k] = kp;
  448. } else {
  449. ipiv[k] = -kp;
  450. ipiv[k + 1] = -kp;
  451. }
  452. /* Increase K and return to the start of the main loop */
  453. k += kstep;
  454. goto L40;
  455. }
  456. L70:
  457. return 0;
  458. /* End of DSYTF2 */
  459. } /* dsytf2_ */