00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015
00016
00017
00018 static integer c__1 = 1;
00019 static doublereal c_b36 = 10.;
00020 static doublereal c_b72 = .5;
00021
00022 int zggbal_(char *job, integer *n, doublecomplex *a, integer
00023 *lda, doublecomplex *b, integer *ldb, integer *ilo, integer *ihi,
00024 doublereal *lscale, doublereal *rscale, doublereal *work, integer *
00025 info)
00026 {
00027
00028 integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
00029 doublereal d__1, d__2, d__3;
00030
00031
00032 double d_lg10(doublereal *), d_imag(doublecomplex *), z_abs(doublecomplex
00033 *), d_sign(doublereal *, doublereal *), pow_di(doublereal *,
00034 integer *);
00035
00036
00037 integer i__, j, k, l, m;
00038 doublereal t;
00039 integer jc;
00040 doublereal ta, tb, tc;
00041 integer ir;
00042 doublereal ew;
00043 integer it, nr, ip1, jp1, lm1;
00044 doublereal cab, rab, ewc, cor, sum;
00045 integer nrp2, icab, lcab;
00046 doublereal beta, coef;
00047 integer irab, lrab;
00048 doublereal basl, cmax;
00049 extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *,
00050 integer *);
00051 doublereal coef2, coef5, gamma, alpha;
00052 extern int dscal_(integer *, doublereal *, doublereal *,
00053 integer *);
00054 extern logical lsame_(char *, char *);
00055 doublereal sfmin, sfmax;
00056 integer iflow;
00057 extern int daxpy_(integer *, doublereal *, doublereal *,
00058 integer *, doublereal *, integer *);
00059 integer kount;
00060 extern int zswap_(integer *, doublecomplex *, integer *,
00061 doublecomplex *, integer *);
00062 extern doublereal dlamch_(char *);
00063 doublereal pgamma;
00064 extern int xerbla_(char *, integer *), zdscal_(
00065 integer *, doublereal *, doublecomplex *, integer *);
00066 integer lsfmin;
00067 extern integer izamax_(integer *, doublecomplex *, integer *);
00068 integer lsfmax;
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 a_dim1 = *lda;
00189 a_offset = 1 + a_dim1;
00190 a -= a_offset;
00191 b_dim1 = *ldb;
00192 b_offset = 1 + b_dim1;
00193 b -= b_offset;
00194 --lscale;
00195 --rscale;
00196 --work;
00197
00198
00199 *info = 0;
00200 if (! lsame_(job, "N") && ! lsame_(job, "P") && ! lsame_(job, "S")
00201 && ! lsame_(job, "B")) {
00202 *info = -1;
00203 } else if (*n < 0) {
00204 *info = -2;
00205 } else if (*lda < max(1,*n)) {
00206 *info = -4;
00207 } else if (*ldb < max(1,*n)) {
00208 *info = -6;
00209 }
00210 if (*info != 0) {
00211 i__1 = -(*info);
00212 xerbla_("ZGGBAL", &i__1);
00213 return 0;
00214 }
00215
00216
00217
00218 if (*n == 0) {
00219 *ilo = 1;
00220 *ihi = *n;
00221 return 0;
00222 }
00223
00224 if (*n == 1) {
00225 *ilo = 1;
00226 *ihi = *n;
00227 lscale[1] = 1.;
00228 rscale[1] = 1.;
00229 return 0;
00230 }
00231
00232 if (lsame_(job, "N")) {
00233 *ilo = 1;
00234 *ihi = *n;
00235 i__1 = *n;
00236 for (i__ = 1; i__ <= i__1; ++i__) {
00237 lscale[i__] = 1.;
00238 rscale[i__] = 1.;
00239
00240 }
00241 return 0;
00242 }
00243
00244 k = 1;
00245 l = *n;
00246 if (lsame_(job, "S")) {
00247 goto L190;
00248 }
00249
00250 goto L30;
00251
00252
00253
00254
00255
00256 L20:
00257 l = lm1;
00258 if (l != 1) {
00259 goto L30;
00260 }
00261
00262 rscale[1] = 1.;
00263 lscale[1] = 1.;
00264 goto L190;
00265
00266 L30:
00267 lm1 = l - 1;
00268 for (i__ = l; i__ >= 1; --i__) {
00269 i__1 = lm1;
00270 for (j = 1; j <= i__1; ++j) {
00271 jp1 = j + 1;
00272 i__2 = i__ + j * a_dim1;
00273 i__3 = i__ + j * b_dim1;
00274 if (a[i__2].r != 0. || a[i__2].i != 0. || (b[i__3].r != 0. || b[
00275 i__3].i != 0.)) {
00276 goto L50;
00277 }
00278
00279 }
00280 j = l;
00281 goto L70;
00282
00283 L50:
00284 i__1 = l;
00285 for (j = jp1; j <= i__1; ++j) {
00286 i__2 = i__ + j * a_dim1;
00287 i__3 = i__ + j * b_dim1;
00288 if (a[i__2].r != 0. || a[i__2].i != 0. || (b[i__3].r != 0. || b[
00289 i__3].i != 0.)) {
00290 goto L80;
00291 }
00292
00293 }
00294 j = jp1 - 1;
00295
00296 L70:
00297 m = l;
00298 iflow = 1;
00299 goto L160;
00300 L80:
00301 ;
00302 }
00303 goto L100;
00304
00305
00306
00307 L90:
00308 ++k;
00309
00310 L100:
00311 i__1 = l;
00312 for (j = k; j <= i__1; ++j) {
00313 i__2 = lm1;
00314 for (i__ = k; i__ <= i__2; ++i__) {
00315 ip1 = i__ + 1;
00316 i__3 = i__ + j * a_dim1;
00317 i__4 = i__ + j * b_dim1;
00318 if (a[i__3].r != 0. || a[i__3].i != 0. || (b[i__4].r != 0. || b[
00319 i__4].i != 0.)) {
00320 goto L120;
00321 }
00322
00323 }
00324 i__ = l;
00325 goto L140;
00326 L120:
00327 i__2 = l;
00328 for (i__ = ip1; i__ <= i__2; ++i__) {
00329 i__3 = i__ + j * a_dim1;
00330 i__4 = i__ + j * b_dim1;
00331 if (a[i__3].r != 0. || a[i__3].i != 0. || (b[i__4].r != 0. || b[
00332 i__4].i != 0.)) {
00333 goto L150;
00334 }
00335
00336 }
00337 i__ = ip1 - 1;
00338 L140:
00339 m = k;
00340 iflow = 2;
00341 goto L160;
00342 L150:
00343 ;
00344 }
00345 goto L190;
00346
00347
00348
00349 L160:
00350 lscale[m] = (doublereal) i__;
00351 if (i__ == m) {
00352 goto L170;
00353 }
00354 i__1 = *n - k + 1;
00355 zswap_(&i__1, &a[i__ + k * a_dim1], lda, &a[m + k * a_dim1], lda);
00356 i__1 = *n - k + 1;
00357 zswap_(&i__1, &b[i__ + k * b_dim1], ldb, &b[m + k * b_dim1], ldb);
00358
00359
00360
00361 L170:
00362 rscale[m] = (doublereal) j;
00363 if (j == m) {
00364 goto L180;
00365 }
00366 zswap_(&l, &a[j * a_dim1 + 1], &c__1, &a[m * a_dim1 + 1], &c__1);
00367 zswap_(&l, &b[j * b_dim1 + 1], &c__1, &b[m * b_dim1 + 1], &c__1);
00368
00369 L180:
00370 switch (iflow) {
00371 case 1: goto L20;
00372 case 2: goto L90;
00373 }
00374
00375 L190:
00376 *ilo = k;
00377 *ihi = l;
00378
00379 if (lsame_(job, "P")) {
00380 i__1 = *ihi;
00381 for (i__ = *ilo; i__ <= i__1; ++i__) {
00382 lscale[i__] = 1.;
00383 rscale[i__] = 1.;
00384
00385 }
00386 return 0;
00387 }
00388
00389 if (*ilo == *ihi) {
00390 return 0;
00391 }
00392
00393
00394
00395 nr = *ihi - *ilo + 1;
00396 i__1 = *ihi;
00397 for (i__ = *ilo; i__ <= i__1; ++i__) {
00398 rscale[i__] = 0.;
00399 lscale[i__] = 0.;
00400
00401 work[i__] = 0.;
00402 work[i__ + *n] = 0.;
00403 work[i__ + (*n << 1)] = 0.;
00404 work[i__ + *n * 3] = 0.;
00405 work[i__ + (*n << 2)] = 0.;
00406 work[i__ + *n * 5] = 0.;
00407
00408 }
00409
00410
00411
00412 basl = d_lg10(&c_b36);
00413 i__1 = *ihi;
00414 for (i__ = *ilo; i__ <= i__1; ++i__) {
00415 i__2 = *ihi;
00416 for (j = *ilo; j <= i__2; ++j) {
00417 i__3 = i__ + j * a_dim1;
00418 if (a[i__3].r == 0. && a[i__3].i == 0.) {
00419 ta = 0.;
00420 goto L210;
00421 }
00422 i__3 = i__ + j * a_dim1;
00423 d__3 = (d__1 = a[i__3].r, abs(d__1)) + (d__2 = d_imag(&a[i__ + j *
00424 a_dim1]), abs(d__2));
00425 ta = d_lg10(&d__3) / basl;
00426
00427 L210:
00428 i__3 = i__ + j * b_dim1;
00429 if (b[i__3].r == 0. && b[i__3].i == 0.) {
00430 tb = 0.;
00431 goto L220;
00432 }
00433 i__3 = i__ + j * b_dim1;
00434 d__3 = (d__1 = b[i__3].r, abs(d__1)) + (d__2 = d_imag(&b[i__ + j *
00435 b_dim1]), abs(d__2));
00436 tb = d_lg10(&d__3) / basl;
00437
00438 L220:
00439 work[i__ + (*n << 2)] = work[i__ + (*n << 2)] - ta - tb;
00440 work[j + *n * 5] = work[j + *n * 5] - ta - tb;
00441
00442 }
00443
00444 }
00445
00446 coef = 1. / (doublereal) (nr << 1);
00447 coef2 = coef * coef;
00448 coef5 = coef2 * .5;
00449 nrp2 = nr + 2;
00450 beta = 0.;
00451 it = 1;
00452
00453
00454
00455 L250:
00456
00457 gamma = ddot_(&nr, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + (*n << 2)]
00458 , &c__1) + ddot_(&nr, &work[*ilo + *n * 5], &c__1, &work[*ilo + *
00459 n * 5], &c__1);
00460
00461 ew = 0.;
00462 ewc = 0.;
00463 i__1 = *ihi;
00464 for (i__ = *ilo; i__ <= i__1; ++i__) {
00465 ew += work[i__ + (*n << 2)];
00466 ewc += work[i__ + *n * 5];
00467
00468 }
00469
00470
00471 d__1 = ew;
00472
00473 d__2 = ewc;
00474
00475 d__3 = ew - ewc;
00476 gamma = coef * gamma - coef2 * (d__1 * d__1 + d__2 * d__2) - coef5 * (
00477 d__3 * d__3);
00478 if (gamma == 0.) {
00479 goto L350;
00480 }
00481 if (it != 1) {
00482 beta = gamma / pgamma;
00483 }
00484 t = coef5 * (ewc - ew * 3.);
00485 tc = coef5 * (ew - ewc * 3.);
00486
00487 dscal_(&nr, &beta, &work[*ilo], &c__1);
00488 dscal_(&nr, &beta, &work[*ilo + *n], &c__1);
00489
00490 daxpy_(&nr, &coef, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + *n], &
00491 c__1);
00492 daxpy_(&nr, &coef, &work[*ilo + *n * 5], &c__1, &work[*ilo], &c__1);
00493
00494 i__1 = *ihi;
00495 for (i__ = *ilo; i__ <= i__1; ++i__) {
00496 work[i__] += tc;
00497 work[i__ + *n] += t;
00498
00499 }
00500
00501
00502
00503 i__1 = *ihi;
00504 for (i__ = *ilo; i__ <= i__1; ++i__) {
00505 kount = 0;
00506 sum = 0.;
00507 i__2 = *ihi;
00508 for (j = *ilo; j <= i__2; ++j) {
00509 i__3 = i__ + j * a_dim1;
00510 if (a[i__3].r == 0. && a[i__3].i == 0.) {
00511 goto L280;
00512 }
00513 ++kount;
00514 sum += work[j];
00515 L280:
00516 i__3 = i__ + j * b_dim1;
00517 if (b[i__3].r == 0. && b[i__3].i == 0.) {
00518 goto L290;
00519 }
00520 ++kount;
00521 sum += work[j];
00522 L290:
00523 ;
00524 }
00525 work[i__ + (*n << 1)] = (doublereal) kount * work[i__ + *n] + sum;
00526
00527 }
00528
00529 i__1 = *ihi;
00530 for (j = *ilo; j <= i__1; ++j) {
00531 kount = 0;
00532 sum = 0.;
00533 i__2 = *ihi;
00534 for (i__ = *ilo; i__ <= i__2; ++i__) {
00535 i__3 = i__ + j * a_dim1;
00536 if (a[i__3].r == 0. && a[i__3].i == 0.) {
00537 goto L310;
00538 }
00539 ++kount;
00540 sum += work[i__ + *n];
00541 L310:
00542 i__3 = i__ + j * b_dim1;
00543 if (b[i__3].r == 0. && b[i__3].i == 0.) {
00544 goto L320;
00545 }
00546 ++kount;
00547 sum += work[i__ + *n];
00548 L320:
00549 ;
00550 }
00551 work[j + *n * 3] = (doublereal) kount * work[j] + sum;
00552
00553 }
00554
00555 sum = ddot_(&nr, &work[*ilo + *n], &c__1, &work[*ilo + (*n << 1)], &c__1)
00556 + ddot_(&nr, &work[*ilo], &c__1, &work[*ilo + *n * 3], &c__1);
00557 alpha = gamma / sum;
00558
00559
00560
00561 cmax = 0.;
00562 i__1 = *ihi;
00563 for (i__ = *ilo; i__ <= i__1; ++i__) {
00564 cor = alpha * work[i__ + *n];
00565 if (abs(cor) > cmax) {
00566 cmax = abs(cor);
00567 }
00568 lscale[i__] += cor;
00569 cor = alpha * work[i__];
00570 if (abs(cor) > cmax) {
00571 cmax = abs(cor);
00572 }
00573 rscale[i__] += cor;
00574
00575 }
00576 if (cmax < .5) {
00577 goto L350;
00578 }
00579
00580 d__1 = -alpha;
00581 daxpy_(&nr, &d__1, &work[*ilo + (*n << 1)], &c__1, &work[*ilo + (*n << 2)]
00582 , &c__1);
00583 d__1 = -alpha;
00584 daxpy_(&nr, &d__1, &work[*ilo + *n * 3], &c__1, &work[*ilo + *n * 5], &
00585 c__1);
00586
00587 pgamma = gamma;
00588 ++it;
00589 if (it <= nrp2) {
00590 goto L250;
00591 }
00592
00593
00594
00595 L350:
00596 sfmin = dlamch_("S");
00597 sfmax = 1. / sfmin;
00598 lsfmin = (integer) (d_lg10(&sfmin) / basl + 1.);
00599 lsfmax = (integer) (d_lg10(&sfmax) / basl);
00600 i__1 = *ihi;
00601 for (i__ = *ilo; i__ <= i__1; ++i__) {
00602 i__2 = *n - *ilo + 1;
00603 irab = izamax_(&i__2, &a[i__ + *ilo * a_dim1], lda);
00604 rab = z_abs(&a[i__ + (irab + *ilo - 1) * a_dim1]);
00605 i__2 = *n - *ilo + 1;
00606 irab = izamax_(&i__2, &b[i__ + *ilo * b_dim1], ldb);
00607
00608 d__1 = rab, d__2 = z_abs(&b[i__ + (irab + *ilo - 1) * b_dim1]);
00609 rab = max(d__1,d__2);
00610 d__1 = rab + sfmin;
00611 lrab = (integer) (d_lg10(&d__1) / basl + 1.);
00612 ir = (integer) (lscale[i__] + d_sign(&c_b72, &lscale[i__]));
00613
00614 i__2 = max(ir,lsfmin), i__2 = min(i__2,lsfmax), i__3 = lsfmax - lrab;
00615 ir = min(i__2,i__3);
00616 lscale[i__] = pow_di(&c_b36, &ir);
00617 icab = izamax_(ihi, &a[i__ * a_dim1 + 1], &c__1);
00618 cab = z_abs(&a[icab + i__ * a_dim1]);
00619 icab = izamax_(ihi, &b[i__ * b_dim1 + 1], &c__1);
00620
00621 d__1 = cab, d__2 = z_abs(&b[icab + i__ * b_dim1]);
00622 cab = max(d__1,d__2);
00623 d__1 = cab + sfmin;
00624 lcab = (integer) (d_lg10(&d__1) / basl + 1.);
00625 jc = (integer) (rscale[i__] + d_sign(&c_b72, &rscale[i__]));
00626
00627 i__2 = max(jc,lsfmin), i__2 = min(i__2,lsfmax), i__3 = lsfmax - lcab;
00628 jc = min(i__2,i__3);
00629 rscale[i__] = pow_di(&c_b36, &jc);
00630
00631 }
00632
00633
00634
00635 i__1 = *ihi;
00636 for (i__ = *ilo; i__ <= i__1; ++i__) {
00637 i__2 = *n - *ilo + 1;
00638 zdscal_(&i__2, &lscale[i__], &a[i__ + *ilo * a_dim1], lda);
00639 i__2 = *n - *ilo + 1;
00640 zdscal_(&i__2, &lscale[i__], &b[i__ + *ilo * b_dim1], ldb);
00641
00642 }
00643
00644
00645
00646 i__1 = *ihi;
00647 for (j = *ilo; j <= i__1; ++j) {
00648 zdscal_(ihi, &rscale[j], &a[j * a_dim1 + 1], &c__1);
00649 zdscal_(ihi, &rscale[j], &b[j * b_dim1 + 1], &c__1);
00650
00651 }
00652
00653 return 0;
00654
00655
00656
00657 }