dgebal.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* dgebal.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_dgebal_(char *job, integer *n, doublereal *a, integer *
  16. lda, integer *ilo, integer *ihi, doublereal *scale, integer *info)
  17. {
  18. /* System generated locals */
  19. integer a_dim1, a_offset, i__1, i__2;
  20. doublereal d__1, d__2;
  21. /* Local variables */
  22. doublereal c__, f, g;
  23. integer i__, j, k, l, m;
  24. doublereal r__, s, ca, ra;
  25. integer ica, ira, iexc;
  26. extern /* Subroutine */ int _starpu_dscal_(integer *, doublereal *, doublereal *,
  27. integer *);
  28. extern logical _starpu_lsame_(char *, char *);
  29. extern /* Subroutine */ int _starpu_dswap_(integer *, doublereal *, integer *,
  30. doublereal *, integer *);
  31. doublereal sfmin1, sfmin2, sfmax1, sfmax2;
  32. extern doublereal _starpu_dlamch_(char *);
  33. extern integer _starpu_idamax_(integer *, doublereal *, integer *);
  34. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  35. logical noconv;
  36. /* -- LAPACK routine (version 3.2) -- */
  37. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  38. /* November 2006 */
  39. /* .. Scalar Arguments .. */
  40. /* .. */
  41. /* .. Array Arguments .. */
  42. /* .. */
  43. /* Purpose */
  44. /* ======= */
  45. /* DGEBAL balances a general real matrix A. This involves, first, */
  46. /* permuting A by a similarity transformation to isolate eigenvalues */
  47. /* in the first 1 to ILO-1 and last IHI+1 to N elements on the */
  48. /* diagonal; and second, applying a diagonal similarity transformation */
  49. /* to rows and columns ILO to IHI to make the rows and columns as */
  50. /* close in norm as possible. Both steps are optional. */
  51. /* Balancing may reduce the 1-norm of the matrix, and improve the */
  52. /* accuracy of the computed eigenvalues and/or eigenvectors. */
  53. /* Arguments */
  54. /* ========= */
  55. /* JOB (input) CHARACTER*1 */
  56. /* Specifies the operations to be performed on A: */
  57. /* = 'N': none: simply set ILO = 1, IHI = N, SCALE(I) = 1.0 */
  58. /* for i = 1,...,N; */
  59. /* = 'P': permute only; */
  60. /* = 'S': scale only; */
  61. /* = 'B': both permute and scale. */
  62. /* N (input) INTEGER */
  63. /* The order of the matrix A. N >= 0. */
  64. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  65. /* On entry, the input matrix A. */
  66. /* On exit, A is overwritten by the balanced matrix. */
  67. /* If JOB = 'N', A is not referenced. */
  68. /* See Further Details. */
  69. /* LDA (input) INTEGER */
  70. /* The leading dimension of the array A. LDA >= max(1,N). */
  71. /* ILO (output) INTEGER */
  72. /* IHI (output) INTEGER */
  73. /* ILO and IHI are set to integers such that on exit */
  74. /* A(i,j) = 0 if i > j and j = 1,...,ILO-1 or I = IHI+1,...,N. */
  75. /* If JOB = 'N' or 'S', ILO = 1 and IHI = N. */
  76. /* SCALE (output) DOUBLE PRECISION array, dimension (N) */
  77. /* Details of the permutations and scaling factors applied to */
  78. /* A. If P(j) is the index of the row and column interchanged */
  79. /* with row and column j and D(j) is the scaling factor */
  80. /* applied to row and column j, then */
  81. /* SCALE(j) = P(j) for j = 1,...,ILO-1 */
  82. /* = D(j) for j = ILO,...,IHI */
  83. /* = P(j) for j = IHI+1,...,N. */
  84. /* The order in which the interchanges are made is N to IHI+1, */
  85. /* then 1 to ILO-1. */
  86. /* INFO (output) INTEGER */
  87. /* = 0: successful exit. */
  88. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  89. /* Further Details */
  90. /* =============== */
  91. /* The permutations consist of row and column interchanges which put */
  92. /* the matrix in the form */
  93. /* ( T1 X Y ) */
  94. /* P A P = ( 0 B Z ) */
  95. /* ( 0 0 T2 ) */
  96. /* where T1 and T2 are upper triangular matrices whose eigenvalues lie */
  97. /* along the diagonal. The column indices ILO and IHI mark the starting */
  98. /* and ending columns of the submatrix B. Balancing consists of applying */
  99. /* a diagonal similarity transformation inv(D) * B * D to make the */
  100. /* 1-norms of each row of B and its corresponding column nearly equal. */
  101. /* The output matrix is */
  102. /* ( T1 X*D Y ) */
  103. /* ( 0 inv(D)*B*D inv(D)*Z ). */
  104. /* ( 0 0 T2 ) */
  105. /* Information about the permutations P and the diagonal matrix D is */
  106. /* returned in the vector SCALE. */
  107. /* This subroutine is based on the EISPACK routine BALANC. */
  108. /* Modified by Tzu-Yi Chen, Computer Science Division, University of */
  109. /* California at Berkeley, USA */
  110. /* ===================================================================== */
  111. /* .. Parameters .. */
  112. /* .. */
  113. /* .. Local Scalars .. */
  114. /* .. */
  115. /* .. External Functions .. */
  116. /* .. */
  117. /* .. External Subroutines .. */
  118. /* .. */
  119. /* .. Intrinsic Functions .. */
  120. /* .. */
  121. /* .. Executable Statements .. */
  122. /* Test the input parameters */
  123. /* Parameter adjustments */
  124. a_dim1 = *lda;
  125. a_offset = 1 + a_dim1;
  126. a -= a_offset;
  127. --scale;
  128. /* Function Body */
  129. *info = 0;
  130. if (! _starpu_lsame_(job, "N") && ! _starpu_lsame_(job, "P") && ! _starpu_lsame_(job, "S")
  131. && ! _starpu_lsame_(job, "B")) {
  132. *info = -1;
  133. } else if (*n < 0) {
  134. *info = -2;
  135. } else if (*lda < max(1,*n)) {
  136. *info = -4;
  137. }
  138. if (*info != 0) {
  139. i__1 = -(*info);
  140. _starpu_xerbla_("DGEBAL", &i__1);
  141. return 0;
  142. }
  143. k = 1;
  144. l = *n;
  145. if (*n == 0) {
  146. goto L210;
  147. }
  148. if (_starpu_lsame_(job, "N")) {
  149. i__1 = *n;
  150. for (i__ = 1; i__ <= i__1; ++i__) {
  151. scale[i__] = 1.;
  152. /* L10: */
  153. }
  154. goto L210;
  155. }
  156. if (_starpu_lsame_(job, "S")) {
  157. goto L120;
  158. }
  159. /* Permutation to isolate eigenvalues if possible */
  160. goto L50;
  161. /* Row and column exchange. */
  162. L20:
  163. scale[m] = (doublereal) j;
  164. if (j == m) {
  165. goto L30;
  166. }
  167. _starpu_dswap_(&l, &a[j * a_dim1 + 1], &c__1, &a[m * a_dim1 + 1], &c__1);
  168. i__1 = *n - k + 1;
  169. _starpu_dswap_(&i__1, &a[j + k * a_dim1], lda, &a[m + k * a_dim1], lda);
  170. L30:
  171. switch (iexc) {
  172. case 1: goto L40;
  173. case 2: goto L80;
  174. }
  175. /* Search for rows isolating an eigenvalue and push them down. */
  176. L40:
  177. if (l == 1) {
  178. goto L210;
  179. }
  180. --l;
  181. L50:
  182. for (j = l; j >= 1; --j) {
  183. i__1 = l;
  184. for (i__ = 1; i__ <= i__1; ++i__) {
  185. if (i__ == j) {
  186. goto L60;
  187. }
  188. if (a[j + i__ * a_dim1] != 0.) {
  189. goto L70;
  190. }
  191. L60:
  192. ;
  193. }
  194. m = l;
  195. iexc = 1;
  196. goto L20;
  197. L70:
  198. ;
  199. }
  200. goto L90;
  201. /* Search for columns isolating an eigenvalue and push them left. */
  202. L80:
  203. ++k;
  204. L90:
  205. i__1 = l;
  206. for (j = k; j <= i__1; ++j) {
  207. i__2 = l;
  208. for (i__ = k; i__ <= i__2; ++i__) {
  209. if (i__ == j) {
  210. goto L100;
  211. }
  212. if (a[i__ + j * a_dim1] != 0.) {
  213. goto L110;
  214. }
  215. L100:
  216. ;
  217. }
  218. m = k;
  219. iexc = 2;
  220. goto L20;
  221. L110:
  222. ;
  223. }
  224. L120:
  225. i__1 = l;
  226. for (i__ = k; i__ <= i__1; ++i__) {
  227. scale[i__] = 1.;
  228. /* L130: */
  229. }
  230. if (_starpu_lsame_(job, "P")) {
  231. goto L210;
  232. }
  233. /* Balance the submatrix in rows K to L. */
  234. /* Iterative loop for norm reduction */
  235. sfmin1 = _starpu_dlamch_("S") / _starpu_dlamch_("P");
  236. sfmax1 = 1. / sfmin1;
  237. sfmin2 = sfmin1 * 2.;
  238. sfmax2 = 1. / sfmin2;
  239. L140:
  240. noconv = FALSE_;
  241. i__1 = l;
  242. for (i__ = k; i__ <= i__1; ++i__) {
  243. c__ = 0.;
  244. r__ = 0.;
  245. i__2 = l;
  246. for (j = k; j <= i__2; ++j) {
  247. if (j == i__) {
  248. goto L150;
  249. }
  250. c__ += (d__1 = a[j + i__ * a_dim1], abs(d__1));
  251. r__ += (d__1 = a[i__ + j * a_dim1], abs(d__1));
  252. L150:
  253. ;
  254. }
  255. ica = _starpu_idamax_(&l, &a[i__ * a_dim1 + 1], &c__1);
  256. ca = (d__1 = a[ica + i__ * a_dim1], abs(d__1));
  257. i__2 = *n - k + 1;
  258. ira = _starpu_idamax_(&i__2, &a[i__ + k * a_dim1], lda);
  259. ra = (d__1 = a[i__ + (ira + k - 1) * a_dim1], abs(d__1));
  260. /* Guard against zero C or R due to underflow. */
  261. if (c__ == 0. || r__ == 0.) {
  262. goto L200;
  263. }
  264. g = r__ / 2.;
  265. f = 1.;
  266. s = c__ + r__;
  267. L160:
  268. /* Computing MAX */
  269. d__1 = max(f,c__);
  270. /* Computing MIN */
  271. d__2 = min(r__,g);
  272. if (c__ >= g || max(d__1,ca) >= sfmax2 || min(d__2,ra) <= sfmin2) {
  273. goto L170;
  274. }
  275. f *= 2.;
  276. c__ *= 2.;
  277. ca *= 2.;
  278. r__ /= 2.;
  279. g /= 2.;
  280. ra /= 2.;
  281. goto L160;
  282. L170:
  283. g = c__ / 2.;
  284. L180:
  285. /* Computing MIN */
  286. d__1 = min(f,c__), d__1 = min(d__1,g);
  287. if (g < r__ || max(r__,ra) >= sfmax2 || min(d__1,ca) <= sfmin2) {
  288. goto L190;
  289. }
  290. f /= 2.;
  291. c__ /= 2.;
  292. g /= 2.;
  293. ca /= 2.;
  294. r__ *= 2.;
  295. ra *= 2.;
  296. goto L180;
  297. /* Now balance. */
  298. L190:
  299. if (c__ + r__ >= s * .95) {
  300. goto L200;
  301. }
  302. if (f < 1. && scale[i__] < 1.) {
  303. if (f * scale[i__] <= sfmin1) {
  304. goto L200;
  305. }
  306. }
  307. if (f > 1. && scale[i__] > 1.) {
  308. if (scale[i__] >= sfmax1 / f) {
  309. goto L200;
  310. }
  311. }
  312. g = 1. / f;
  313. scale[i__] *= f;
  314. noconv = TRUE_;
  315. i__2 = *n - k + 1;
  316. _starpu_dscal_(&i__2, &g, &a[i__ + k * a_dim1], lda);
  317. _starpu_dscal_(&l, &f, &a[i__ * a_dim1 + 1], &c__1);
  318. L200:
  319. ;
  320. }
  321. if (noconv) {
  322. goto L140;
  323. }
  324. L210:
  325. *ilo = k;
  326. *ihi = l;
  327. return 0;
  328. /* End of DGEBAL */
  329. } /* _starpu_dgebal_ */