dsyequb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* dsyequb.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_dsyequb_(char *uplo, integer *n, doublereal *a, integer *
  16. lda, doublereal *s, doublereal *scond, doublereal *amax, doublereal *
  17. work, integer *info)
  18. {
  19. /* System generated locals */
  20. integer a_dim1, a_offset, i__1, i__2;
  21. doublereal d__1, d__2, d__3;
  22. /* Builtin functions */
  23. double sqrt(doublereal), log(doublereal), pow_di(doublereal *, integer *);
  24. /* Local variables */
  25. doublereal d__;
  26. integer i__, j;
  27. doublereal t, u, c0, c1, c2, si;
  28. logical up;
  29. doublereal avg, std, tol, base;
  30. integer iter;
  31. doublereal smin, smax, scale;
  32. extern logical _starpu_lsame_(char *, char *);
  33. doublereal sumsq;
  34. extern doublereal _starpu_dlamch_(char *);
  35. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  36. doublereal bignum;
  37. extern /* Subroutine */ int _starpu_dlassq_(integer *, doublereal *, integer *,
  38. doublereal *, doublereal *);
  39. doublereal smlnum;
  40. /* -- LAPACK routine (version 3.2) -- */
  41. /* -- Contributed by James Demmel, Deaglan Halligan, Yozo Hida and -- */
  42. /* -- Jason Riedy of Univ. of California Berkeley. -- */
  43. /* -- November 2008 -- */
  44. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  45. /* -- Univ. of California Berkeley and NAG Ltd. -- */
  46. /* .. */
  47. /* .. Scalar Arguments .. */
  48. /* .. */
  49. /* .. Array Arguments .. */
  50. /* .. */
  51. /* Purpose */
  52. /* ======= */
  53. /* DSYEQUB computes row and column scalings intended to equilibrate a */
  54. /* symmetric matrix A and reduce its condition number */
  55. /* (with respect to the two-norm). S contains the scale factors, */
  56. /* S(i) = 1/sqrt(A(i,i)), chosen so that the scaled matrix B with */
  57. /* elements B(i,j) = S(i)*A(i,j)*S(j) has ones on the diagonal. This */
  58. /* choice of S puts the condition number of B within a factor N of the */
  59. /* smallest possible condition number over all possible diagonal */
  60. /* scalings. */
  61. /* Arguments */
  62. /* ========= */
  63. /* N (input) INTEGER */
  64. /* The order of the matrix A. N >= 0. */
  65. /* A (input) DOUBLE PRECISION array, dimension (LDA,N) */
  66. /* The N-by-N symmetric matrix whose scaling */
  67. /* factors are to be computed. Only the diagonal elements of A */
  68. /* are referenced. */
  69. /* LDA (input) INTEGER */
  70. /* The leading dimension of the array A. LDA >= max(1,N). */
  71. /* S (output) DOUBLE PRECISION array, dimension (N) */
  72. /* If INFO = 0, S contains the scale factors for A. */
  73. /* SCOND (output) DOUBLE PRECISION */
  74. /* If INFO = 0, S contains the ratio of the smallest S(i) to */
  75. /* the largest S(i). If SCOND >= 0.1 and AMAX is neither too */
  76. /* large nor too small, it is not worth scaling by S. */
  77. /* AMAX (output) DOUBLE PRECISION */
  78. /* Absolute value of largest matrix element. If AMAX is very */
  79. /* close to overflow or very close to underflow, the matrix */
  80. /* should be scaled. */
  81. /* INFO (output) INTEGER */
  82. /* = 0: successful exit */
  83. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  84. /* > 0: if INFO = i, the i-th diagonal element is nonpositive. */
  85. /* Further Details */
  86. /* ======= ======= */
  87. /* Reference: Livne, O.E. and Golub, G.H., "Scaling by Binormalization", */
  88. /* Numerical Algorithms, vol. 35, no. 1, pp. 97-120, January 2004. */
  89. /* DOI 10.1023/B:NUMA.0000016606.32820.69 */
  90. /* Tech report version: http://ruready.utah.edu/archive/papers/bin.pdf */
  91. /* ===================================================================== */
  92. /* .. Parameters .. */
  93. /* .. */
  94. /* .. Local Scalars .. */
  95. /* .. */
  96. /* .. External Functions .. */
  97. /* .. */
  98. /* .. External Subroutines .. */
  99. /* .. */
  100. /* .. Executable Statements .. */
  101. /* Test input parameters. */
  102. /* Parameter adjustments */
  103. a_dim1 = *lda;
  104. a_offset = 1 + a_dim1;
  105. a -= a_offset;
  106. --s;
  107. --work;
  108. /* Function Body */
  109. *info = 0;
  110. if (! (_starpu_lsame_(uplo, "U") || _starpu_lsame_(uplo, "L"))) {
  111. *info = -1;
  112. } else if (*n < 0) {
  113. *info = -2;
  114. } else if (*lda < max(1,*n)) {
  115. *info = -4;
  116. }
  117. if (*info != 0) {
  118. i__1 = -(*info);
  119. _starpu_xerbla_("DSYEQUB", &i__1);
  120. return 0;
  121. }
  122. up = _starpu_lsame_(uplo, "U");
  123. *amax = 0.;
  124. /* Quick return if possible. */
  125. if (*n == 0) {
  126. *scond = 1.;
  127. return 0;
  128. }
  129. i__1 = *n;
  130. for (i__ = 1; i__ <= i__1; ++i__) {
  131. s[i__] = 0.;
  132. }
  133. *amax = 0.;
  134. if (up) {
  135. i__1 = *n;
  136. for (j = 1; j <= i__1; ++j) {
  137. i__2 = j - 1;
  138. for (i__ = 1; i__ <= i__2; ++i__) {
  139. /* Computing MAX */
  140. d__2 = s[i__], d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  141. s[i__] = max(d__2,d__3);
  142. /* Computing MAX */
  143. d__2 = s[j], d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  144. s[j] = max(d__2,d__3);
  145. /* Computing MAX */
  146. d__2 = *amax, d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  147. *amax = max(d__2,d__3);
  148. }
  149. /* Computing MAX */
  150. d__2 = s[j], d__3 = (d__1 = a[j + j * a_dim1], abs(d__1));
  151. s[j] = max(d__2,d__3);
  152. /* Computing MAX */
  153. d__2 = *amax, d__3 = (d__1 = a[j + j * a_dim1], abs(d__1));
  154. *amax = max(d__2,d__3);
  155. }
  156. } else {
  157. i__1 = *n;
  158. for (j = 1; j <= i__1; ++j) {
  159. /* Computing MAX */
  160. d__2 = s[j], d__3 = (d__1 = a[j + j * a_dim1], abs(d__1));
  161. s[j] = max(d__2,d__3);
  162. /* Computing MAX */
  163. d__2 = *amax, d__3 = (d__1 = a[j + j * a_dim1], abs(d__1));
  164. *amax = max(d__2,d__3);
  165. i__2 = *n;
  166. for (i__ = j + 1; i__ <= i__2; ++i__) {
  167. /* Computing MAX */
  168. d__2 = s[i__], d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  169. s[i__] = max(d__2,d__3);
  170. /* Computing MAX */
  171. d__2 = s[j], d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  172. s[j] = max(d__2,d__3);
  173. /* Computing MAX */
  174. d__2 = *amax, d__3 = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  175. *amax = max(d__2,d__3);
  176. }
  177. }
  178. }
  179. i__1 = *n;
  180. for (j = 1; j <= i__1; ++j) {
  181. s[j] = 1. / s[j];
  182. }
  183. tol = 1. / sqrt(*n * 2.);
  184. for (iter = 1; iter <= 100; ++iter) {
  185. scale = 0.;
  186. sumsq = 0.;
  187. /* BETA = |A|S */
  188. i__1 = *n;
  189. for (i__ = 1; i__ <= i__1; ++i__) {
  190. work[i__] = 0.;
  191. }
  192. if (up) {
  193. i__1 = *n;
  194. for (j = 1; j <= i__1; ++j) {
  195. i__2 = j - 1;
  196. for (i__ = 1; i__ <= i__2; ++i__) {
  197. t = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  198. work[i__] += (d__1 = a[i__ + j * a_dim1], abs(d__1)) * s[
  199. j];
  200. work[j] += (d__1 = a[i__ + j * a_dim1], abs(d__1)) * s[
  201. i__];
  202. }
  203. work[j] += (d__1 = a[j + j * a_dim1], abs(d__1)) * s[j];
  204. }
  205. } else {
  206. i__1 = *n;
  207. for (j = 1; j <= i__1; ++j) {
  208. work[j] += (d__1 = a[j + j * a_dim1], abs(d__1)) * s[j];
  209. i__2 = *n;
  210. for (i__ = j + 1; i__ <= i__2; ++i__) {
  211. t = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  212. work[i__] += (d__1 = a[i__ + j * a_dim1], abs(d__1)) * s[
  213. j];
  214. work[j] += (d__1 = a[i__ + j * a_dim1], abs(d__1)) * s[
  215. i__];
  216. }
  217. }
  218. }
  219. /* avg = s^T beta / n */
  220. avg = 0.;
  221. i__1 = *n;
  222. for (i__ = 1; i__ <= i__1; ++i__) {
  223. avg += s[i__] * work[i__];
  224. }
  225. avg /= *n;
  226. std = 0.;
  227. i__1 = *n * 3;
  228. for (i__ = (*n << 1) + 1; i__ <= i__1; ++i__) {
  229. work[i__] = s[i__ - (*n << 1)] * work[i__ - (*n << 1)] - avg;
  230. }
  231. _starpu_dlassq_(n, &work[(*n << 1) + 1], &c__1, &scale, &sumsq);
  232. std = scale * sqrt(sumsq / *n);
  233. if (std < tol * avg) {
  234. goto L999;
  235. }
  236. i__1 = *n;
  237. for (i__ = 1; i__ <= i__1; ++i__) {
  238. t = (d__1 = a[i__ + i__ * a_dim1], abs(d__1));
  239. si = s[i__];
  240. c2 = (*n - 1) * t;
  241. c1 = (*n - 2) * (work[i__] - t * si);
  242. c0 = -(t * si) * si + work[i__] * 2 * si - *n * avg;
  243. d__ = c1 * c1 - c0 * 4 * c2;
  244. if (d__ <= 0.) {
  245. *info = -1;
  246. return 0;
  247. }
  248. si = c0 * -2 / (c1 + sqrt(d__));
  249. d__ = si - s[i__];
  250. u = 0.;
  251. if (up) {
  252. i__2 = i__;
  253. for (j = 1; j <= i__2; ++j) {
  254. t = (d__1 = a[j + i__ * a_dim1], abs(d__1));
  255. u += s[j] * t;
  256. work[j] += d__ * t;
  257. }
  258. i__2 = *n;
  259. for (j = i__ + 1; j <= i__2; ++j) {
  260. t = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  261. u += s[j] * t;
  262. work[j] += d__ * t;
  263. }
  264. } else {
  265. i__2 = i__;
  266. for (j = 1; j <= i__2; ++j) {
  267. t = (d__1 = a[i__ + j * a_dim1], abs(d__1));
  268. u += s[j] * t;
  269. work[j] += d__ * t;
  270. }
  271. i__2 = *n;
  272. for (j = i__ + 1; j <= i__2; ++j) {
  273. t = (d__1 = a[j + i__ * a_dim1], abs(d__1));
  274. u += s[j] * t;
  275. work[j] += d__ * t;
  276. }
  277. }
  278. avg += (u + work[i__]) * d__ / *n;
  279. s[i__] = si;
  280. }
  281. }
  282. L999:
  283. smlnum = _starpu_dlamch_("SAFEMIN");
  284. bignum = 1. / smlnum;
  285. smin = bignum;
  286. smax = 0.;
  287. t = 1. / sqrt(avg);
  288. base = _starpu_dlamch_("B");
  289. u = 1. / log(base);
  290. i__1 = *n;
  291. for (i__ = 1; i__ <= i__1; ++i__) {
  292. i__2 = (integer) (u * log(s[i__] * t));
  293. s[i__] = pow_di(&base, &i__2);
  294. /* Computing MIN */
  295. d__1 = smin, d__2 = s[i__];
  296. smin = min(d__1,d__2);
  297. /* Computing MAX */
  298. d__1 = smax, d__2 = s[i__];
  299. smax = max(d__1,d__2);
  300. }
  301. *scond = max(smin,smlnum) / min(smax,bignum);
  302. return 0;
  303. } /* _starpu_dsyequb_ */