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 real c_b36 = 10.f;
00020 static real c_b72 = .5f;
00021
00022 int cggbal_(char *job, integer *n, complex *a, integer *lda,
00023 complex *b, integer *ldb, integer *ilo, integer *ihi, real *lscale,
00024 real *rscale, real *work, integer *info)
00025 {
00026
00027 integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
00028 real r__1, r__2, r__3;
00029
00030
00031 double r_lg10(real *), r_imag(complex *), c_abs(complex *), r_sign(real *,
00032 real *), pow_ri(real *, integer *);
00033
00034
00035 integer i__, j, k, l, m;
00036 real t;
00037 integer jc;
00038 real ta, tb, tc;
00039 integer ir;
00040 real ew;
00041 integer it, nr, ip1, jp1, lm1;
00042 real cab, rab, ewc, cor, sum;
00043 integer nrp2, icab, lcab;
00044 real beta, coef;
00045 integer irab, lrab;
00046 real basl, cmax;
00047 extern doublereal sdot_(integer *, real *, integer *, real *, integer *);
00048 real coef2, coef5, gamma, alpha;
00049 extern logical lsame_(char *, char *);
00050 extern int sscal_(integer *, real *, real *, integer *);
00051 real sfmin;
00052 extern int cswap_(integer *, complex *, integer *,
00053 complex *, integer *);
00054 real sfmax;
00055 integer iflow, kount;
00056 extern int saxpy_(integer *, real *, real *, integer *,
00057 real *, integer *);
00058 real pgamma;
00059 extern integer icamax_(integer *, complex *, integer *);
00060 extern doublereal slamch_(char *);
00061 extern int csscal_(integer *, real *, complex *, integer
00062 *), xerbla_(char *, integer *);
00063 integer lsfmin, lsfmax;
00064
00065
00066
00067
00068
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 a_dim1 = *lda;
00184 a_offset = 1 + a_dim1;
00185 a -= a_offset;
00186 b_dim1 = *ldb;
00187 b_offset = 1 + b_dim1;
00188 b -= b_offset;
00189 --lscale;
00190 --rscale;
00191 --work;
00192
00193
00194 *info = 0;
00195 if (! lsame_(job, "N") && ! lsame_(job, "P") && ! lsame_(job, "S")
00196 && ! lsame_(job, "B")) {
00197 *info = -1;
00198 } else if (*n < 0) {
00199 *info = -2;
00200 } else if (*lda < max(1,*n)) {
00201 *info = -4;
00202 } else if (*ldb < max(1,*n)) {
00203 *info = -6;
00204 }
00205 if (*info != 0) {
00206 i__1 = -(*info);
00207 xerbla_("CGGBAL", &i__1);
00208 return 0;
00209 }
00210
00211
00212
00213 if (*n == 0) {
00214 *ilo = 1;
00215 *ihi = *n;
00216 return 0;
00217 }
00218
00219 if (*n == 1) {
00220 *ilo = 1;
00221 *ihi = *n;
00222 lscale[1] = 1.f;
00223 rscale[1] = 1.f;
00224 return 0;
00225 }
00226
00227 if (lsame_(job, "N")) {
00228 *ilo = 1;
00229 *ihi = *n;
00230 i__1 = *n;
00231 for (i__ = 1; i__ <= i__1; ++i__) {
00232 lscale[i__] = 1.f;
00233 rscale[i__] = 1.f;
00234
00235 }
00236 return 0;
00237 }
00238
00239 k = 1;
00240 l = *n;
00241 if (lsame_(job, "S")) {
00242 goto L190;
00243 }
00244
00245 goto L30;
00246
00247
00248
00249
00250
00251 L20:
00252 l = lm1;
00253 if (l != 1) {
00254 goto L30;
00255 }
00256
00257 rscale[1] = 1.f;
00258 lscale[1] = 1.f;
00259 goto L190;
00260
00261 L30:
00262 lm1 = l - 1;
00263 for (i__ = l; i__ >= 1; --i__) {
00264 i__1 = lm1;
00265 for (j = 1; j <= i__1; ++j) {
00266 jp1 = j + 1;
00267 i__2 = i__ + j * a_dim1;
00268 i__3 = i__ + j * b_dim1;
00269 if (a[i__2].r != 0.f || a[i__2].i != 0.f || (b[i__3].r != 0.f ||
00270 b[i__3].i != 0.f)) {
00271 goto L50;
00272 }
00273
00274 }
00275 j = l;
00276 goto L70;
00277
00278 L50:
00279 i__1 = l;
00280 for (j = jp1; j <= i__1; ++j) {
00281 i__2 = i__ + j * a_dim1;
00282 i__3 = i__ + j * b_dim1;
00283 if (a[i__2].r != 0.f || a[i__2].i != 0.f || (b[i__3].r != 0.f ||
00284 b[i__3].i != 0.f)) {
00285 goto L80;
00286 }
00287
00288 }
00289 j = jp1 - 1;
00290
00291 L70:
00292 m = l;
00293 iflow = 1;
00294 goto L160;
00295 L80:
00296 ;
00297 }
00298 goto L100;
00299
00300
00301
00302 L90:
00303 ++k;
00304
00305 L100:
00306 i__1 = l;
00307 for (j = k; j <= i__1; ++j) {
00308 i__2 = lm1;
00309 for (i__ = k; i__ <= i__2; ++i__) {
00310 ip1 = i__ + 1;
00311 i__3 = i__ + j * a_dim1;
00312 i__4 = i__ + j * b_dim1;
00313 if (a[i__3].r != 0.f || a[i__3].i != 0.f || (b[i__4].r != 0.f ||
00314 b[i__4].i != 0.f)) {
00315 goto L120;
00316 }
00317
00318 }
00319 i__ = l;
00320 goto L140;
00321 L120:
00322 i__2 = l;
00323 for (i__ = ip1; i__ <= i__2; ++i__) {
00324 i__3 = i__ + j * a_dim1;
00325 i__4 = i__ + j * b_dim1;
00326 if (a[i__3].r != 0.f || a[i__3].i != 0.f || (b[i__4].r != 0.f ||
00327 b[i__4].i != 0.f)) {
00328 goto L150;
00329 }
00330
00331 }
00332 i__ = ip1 - 1;
00333 L140:
00334 m = k;
00335 iflow = 2;
00336 goto L160;
00337 L150:
00338 ;
00339 }
00340 goto L190;
00341
00342
00343
00344 L160:
00345 lscale[m] = (real) i__;
00346 if (i__ == m) {
00347 goto L170;
00348 }
00349 i__1 = *n - k + 1;
00350 cswap_(&i__1, &a[i__ + k * a_dim1], lda, &a[m + k * a_dim1], lda);
00351 i__1 = *n - k + 1;
00352 cswap_(&i__1, &b[i__ + k * b_dim1], ldb, &b[m + k * b_dim1], ldb);
00353
00354
00355
00356 L170:
00357 rscale[m] = (real) j;
00358 if (j == m) {
00359 goto L180;
00360 }
00361 cswap_(&l, &a[j * a_dim1 + 1], &c__1, &a[m * a_dim1 + 1], &c__1);
00362 cswap_(&l, &b[j * b_dim1 + 1], &c__1, &b[m * b_dim1 + 1], &c__1);
00363
00364 L180:
00365 switch (iflow) {
00366 case 1: goto L20;
00367 case 2: goto L90;
00368 }
00369
00370 L190:
00371 *ilo = k;
00372 *ihi = l;
00373
00374 if (lsame_(job, "P")) {
00375 i__1 = *ihi;
00376 for (i__ = *ilo; i__ <= i__1; ++i__) {
00377 lscale[i__] = 1.f;
00378 rscale[i__] = 1.f;
00379
00380 }
00381 return 0;
00382 }
00383
00384 if (*ilo == *ihi) {
00385 return 0;
00386 }
00387
00388
00389
00390 nr = *ihi - *ilo + 1;
00391 i__1 = *ihi;
00392 for (i__ = *ilo; i__ <= i__1; ++i__) {
00393 rscale[i__] = 0.f;
00394 lscale[i__] = 0.f;
00395
00396 work[i__] = 0.f;
00397 work[i__ + *n] = 0.f;
00398 work[i__ + (*n << 1)] = 0.f;
00399 work[i__ + *n * 3] = 0.f;
00400 work[i__ + (*n << 2)] = 0.f;
00401 work[i__ + *n * 5] = 0.f;
00402
00403 }
00404
00405
00406
00407 basl = r_lg10(&c_b36);
00408 i__1 = *ihi;
00409 for (i__ = *ilo; i__ <= i__1; ++i__) {
00410 i__2 = *ihi;
00411 for (j = *ilo; j <= i__2; ++j) {
00412 i__3 = i__ + j * a_dim1;
00413 if (a[i__3].r == 0.f && a[i__3].i == 0.f) {
00414 ta = 0.f;
00415 goto L210;
00416 }
00417 i__3 = i__ + j * a_dim1;
00418 r__3 = (r__1 = a[i__3].r, dabs(r__1)) + (r__2 = r_imag(&a[i__ + j
00419 * a_dim1]), dabs(r__2));
00420 ta = r_lg10(&r__3) / basl;
00421
00422 L210:
00423 i__3 = i__ + j * b_dim1;
00424 if (b[i__3].r == 0.f && b[i__3].i == 0.f) {
00425 tb = 0.f;
00426 goto L220;
00427 }
00428 i__3 = i__ + j * b_dim1;
00429 r__3 = (r__1 = b[i__3].r, dabs(r__1)) + (r__2 = r_imag(&b[i__ + j
00430 * b_dim1]), dabs(r__2));
00431 tb = r_lg10(&r__3) / basl;
00432
00433 L220:
00434 work[i__ + (*n << 2)] = work[i__ + (*n << 2)] - ta - tb;
00435 work[j + *n * 5] = work[j + *n * 5] - ta - tb;
00436
00437 }
00438
00439 }
00440
00441 coef = 1.f / (real) (nr << 1);
00442 coef2 = coef * coef;
00443 coef5 = coef2 * .5f;
00444 nrp2 = nr + 2;
00445 beta = 0.f;
00446 it = 1;
00447
00448
00449
00450 L250:
00451
00452 gamma = sdot_(&nr, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + (*n << 2)]
00453 , &c__1) + sdot_(&nr, &work[*ilo + *n * 5], &c__1, &work[*ilo + *
00454 n * 5], &c__1);
00455
00456 ew = 0.f;
00457 ewc = 0.f;
00458 i__1 = *ihi;
00459 for (i__ = *ilo; i__ <= i__1; ++i__) {
00460 ew += work[i__ + (*n << 2)];
00461 ewc += work[i__ + *n * 5];
00462
00463 }
00464
00465
00466 r__1 = ew;
00467
00468 r__2 = ewc;
00469
00470 r__3 = ew - ewc;
00471 gamma = coef * gamma - coef2 * (r__1 * r__1 + r__2 * r__2) - coef5 * (
00472 r__3 * r__3);
00473 if (gamma == 0.f) {
00474 goto L350;
00475 }
00476 if (it != 1) {
00477 beta = gamma / pgamma;
00478 }
00479 t = coef5 * (ewc - ew * 3.f);
00480 tc = coef5 * (ew - ewc * 3.f);
00481
00482 sscal_(&nr, &beta, &work[*ilo], &c__1);
00483 sscal_(&nr, &beta, &work[*ilo + *n], &c__1);
00484
00485 saxpy_(&nr, &coef, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + *n], &
00486 c__1);
00487 saxpy_(&nr, &coef, &work[*ilo + *n * 5], &c__1, &work[*ilo], &c__1);
00488
00489 i__1 = *ihi;
00490 for (i__ = *ilo; i__ <= i__1; ++i__) {
00491 work[i__] += tc;
00492 work[i__ + *n] += t;
00493
00494 }
00495
00496
00497
00498 i__1 = *ihi;
00499 for (i__ = *ilo; i__ <= i__1; ++i__) {
00500 kount = 0;
00501 sum = 0.f;
00502 i__2 = *ihi;
00503 for (j = *ilo; j <= i__2; ++j) {
00504 i__3 = i__ + j * a_dim1;
00505 if (a[i__3].r == 0.f && a[i__3].i == 0.f) {
00506 goto L280;
00507 }
00508 ++kount;
00509 sum += work[j];
00510 L280:
00511 i__3 = i__ + j * b_dim1;
00512 if (b[i__3].r == 0.f && b[i__3].i == 0.f) {
00513 goto L290;
00514 }
00515 ++kount;
00516 sum += work[j];
00517 L290:
00518 ;
00519 }
00520 work[i__ + (*n << 1)] = (real) kount * work[i__ + *n] + sum;
00521
00522 }
00523
00524 i__1 = *ihi;
00525 for (j = *ilo; j <= i__1; ++j) {
00526 kount = 0;
00527 sum = 0.f;
00528 i__2 = *ihi;
00529 for (i__ = *ilo; i__ <= i__2; ++i__) {
00530 i__3 = i__ + j * a_dim1;
00531 if (a[i__3].r == 0.f && a[i__3].i == 0.f) {
00532 goto L310;
00533 }
00534 ++kount;
00535 sum += work[i__ + *n];
00536 L310:
00537 i__3 = i__ + j * b_dim1;
00538 if (b[i__3].r == 0.f && b[i__3].i == 0.f) {
00539 goto L320;
00540 }
00541 ++kount;
00542 sum += work[i__ + *n];
00543 L320:
00544 ;
00545 }
00546 work[j + *n * 3] = (real) kount * work[j] + sum;
00547
00548 }
00549
00550 sum = sdot_(&nr, &work[*ilo + *n], &c__1, &work[*ilo + (*n << 1)], &c__1)
00551 + sdot_(&nr, &work[*ilo], &c__1, &work[*ilo + *n * 3], &c__1);
00552 alpha = gamma / sum;
00553
00554
00555
00556 cmax = 0.f;
00557 i__1 = *ihi;
00558 for (i__ = *ilo; i__ <= i__1; ++i__) {
00559 cor = alpha * work[i__ + *n];
00560 if (dabs(cor) > cmax) {
00561 cmax = dabs(cor);
00562 }
00563 lscale[i__] += cor;
00564 cor = alpha * work[i__];
00565 if (dabs(cor) > cmax) {
00566 cmax = dabs(cor);
00567 }
00568 rscale[i__] += cor;
00569
00570 }
00571 if (cmax < .5f) {
00572 goto L350;
00573 }
00574
00575 r__1 = -alpha;
00576 saxpy_(&nr, &r__1, &work[*ilo + (*n << 1)], &c__1, &work[*ilo + (*n << 2)]
00577 , &c__1);
00578 r__1 = -alpha;
00579 saxpy_(&nr, &r__1, &work[*ilo + *n * 3], &c__1, &work[*ilo + *n * 5], &
00580 c__1);
00581
00582 pgamma = gamma;
00583 ++it;
00584 if (it <= nrp2) {
00585 goto L250;
00586 }
00587
00588
00589
00590 L350:
00591 sfmin = slamch_("S");
00592 sfmax = 1.f / sfmin;
00593 lsfmin = (integer) (r_lg10(&sfmin) / basl + 1.f);
00594 lsfmax = (integer) (r_lg10(&sfmax) / basl);
00595 i__1 = *ihi;
00596 for (i__ = *ilo; i__ <= i__1; ++i__) {
00597 i__2 = *n - *ilo + 1;
00598 irab = icamax_(&i__2, &a[i__ + *ilo * a_dim1], lda);
00599 rab = c_abs(&a[i__ + (irab + *ilo - 1) * a_dim1]);
00600 i__2 = *n - *ilo + 1;
00601 irab = icamax_(&i__2, &b[i__ + *ilo * b_dim1], ldb);
00602
00603 r__1 = rab, r__2 = c_abs(&b[i__ + (irab + *ilo - 1) * b_dim1]);
00604 rab = dmax(r__1,r__2);
00605 r__1 = rab + sfmin;
00606 lrab = (integer) (r_lg10(&r__1) / basl + 1.f);
00607 ir = lscale[i__] + r_sign(&c_b72, &lscale[i__]);
00608
00609 i__2 = max(ir,lsfmin), i__2 = min(i__2,lsfmax), i__3 = lsfmax - lrab;
00610 ir = min(i__2,i__3);
00611 lscale[i__] = pow_ri(&c_b36, &ir);
00612 icab = icamax_(ihi, &a[i__ * a_dim1 + 1], &c__1);
00613 cab = c_abs(&a[icab + i__ * a_dim1]);
00614 icab = icamax_(ihi, &b[i__ * b_dim1 + 1], &c__1);
00615
00616 r__1 = cab, r__2 = c_abs(&b[icab + i__ * b_dim1]);
00617 cab = dmax(r__1,r__2);
00618 r__1 = cab + sfmin;
00619 lcab = (integer) (r_lg10(&r__1) / basl + 1.f);
00620 jc = rscale[i__] + r_sign(&c_b72, &rscale[i__]);
00621
00622 i__2 = max(jc,lsfmin), i__2 = min(i__2,lsfmax), i__3 = lsfmax - lcab;
00623 jc = min(i__2,i__3);
00624 rscale[i__] = pow_ri(&c_b36, &jc);
00625
00626 }
00627
00628
00629
00630 i__1 = *ihi;
00631 for (i__ = *ilo; i__ <= i__1; ++i__) {
00632 i__2 = *n - *ilo + 1;
00633 csscal_(&i__2, &lscale[i__], &a[i__ + *ilo * a_dim1], lda);
00634 i__2 = *n - *ilo + 1;
00635 csscal_(&i__2, &lscale[i__], &b[i__ + *ilo * b_dim1], ldb);
00636
00637 }
00638
00639
00640
00641 i__1 = *ihi;
00642 for (j = *ilo; j <= i__1; ++j) {
00643 csscal_(ihi, &rscale[j], &a[j * a_dim1 + 1], &c__1);
00644 csscal_(ihi, &rscale[j], &b[j * b_dim1 + 1], &c__1);
00645
00646 }
00647
00648 return 0;
00649
00650
00651
00652 }