You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sggbal.c 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef blasint logical;
  52. typedef char logical1;
  53. typedef char integer1;
  54. #define TRUE_ (1)
  55. #define FALSE_ (0)
  56. /* Extern is for use with -E */
  57. #ifndef Extern
  58. #define Extern extern
  59. #endif
  60. /* I/O stuff */
  61. typedef int flag;
  62. typedef int ftnlen;
  63. typedef int ftnint;
  64. /*external read, write*/
  65. typedef struct
  66. { flag cierr;
  67. ftnint ciunit;
  68. flag ciend;
  69. char *cifmt;
  70. ftnint cirec;
  71. } cilist;
  72. /*internal read, write*/
  73. typedef struct
  74. { flag icierr;
  75. char *iciunit;
  76. flag iciend;
  77. char *icifmt;
  78. ftnint icirlen;
  79. ftnint icirnum;
  80. } icilist;
  81. /*open*/
  82. typedef struct
  83. { flag oerr;
  84. ftnint ounit;
  85. char *ofnm;
  86. ftnlen ofnmlen;
  87. char *osta;
  88. char *oacc;
  89. char *ofm;
  90. ftnint orl;
  91. char *oblnk;
  92. } olist;
  93. /*close*/
  94. typedef struct
  95. { flag cerr;
  96. ftnint cunit;
  97. char *csta;
  98. } cllist;
  99. /*rewind, backspace, endfile*/
  100. typedef struct
  101. { flag aerr;
  102. ftnint aunit;
  103. } alist;
  104. /* inquire */
  105. typedef struct
  106. { flag inerr;
  107. ftnint inunit;
  108. char *infile;
  109. ftnlen infilen;
  110. ftnint *inex; /*parameters in standard's order*/
  111. ftnint *inopen;
  112. ftnint *innum;
  113. ftnint *innamed;
  114. char *inname;
  115. ftnlen innamlen;
  116. char *inacc;
  117. ftnlen inacclen;
  118. char *inseq;
  119. ftnlen inseqlen;
  120. char *indir;
  121. ftnlen indirlen;
  122. char *infmt;
  123. ftnlen infmtlen;
  124. char *inform;
  125. ftnint informlen;
  126. char *inunf;
  127. ftnlen inunflen;
  128. ftnint *inrecl;
  129. ftnint *innrec;
  130. char *inblank;
  131. ftnlen inblanklen;
  132. } inlist;
  133. #define VOID void
  134. union Multitype { /* for multiple entry points */
  135. integer1 g;
  136. shortint h;
  137. integer i;
  138. /* longint j; */
  139. real r;
  140. doublereal d;
  141. complex c;
  142. doublecomplex z;
  143. };
  144. typedef union Multitype Multitype;
  145. struct Vardesc { /* for Namelist */
  146. char *name;
  147. char *addr;
  148. ftnlen *dims;
  149. int type;
  150. };
  151. typedef struct Vardesc Vardesc;
  152. struct Namelist {
  153. char *name;
  154. Vardesc **vars;
  155. int nvars;
  156. };
  157. typedef struct Namelist Namelist;
  158. #define abs(x) ((x) >= 0 ? (x) : -(x))
  159. #define dabs(x) (fabs(x))
  160. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  161. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  162. #define dmin(a,b) (f2cmin(a,b))
  163. #define dmax(a,b) (f2cmax(a,b))
  164. #define bit_test(a,b) ((a) >> (b) & 1)
  165. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  166. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  167. #define abort_() { sig_die("Fortran abort routine called", 1); }
  168. #define c_abs(z) (cabsf(Cf(z)))
  169. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  170. #ifdef _MSC_VER
  171. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  172. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
  173. #else
  174. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  175. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  176. #endif
  177. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  178. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  179. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  180. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  181. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  182. #define d_abs(x) (fabs(*(x)))
  183. #define d_acos(x) (acos(*(x)))
  184. #define d_asin(x) (asin(*(x)))
  185. #define d_atan(x) (atan(*(x)))
  186. #define d_atn2(x, y) (atan2(*(x),*(y)))
  187. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  188. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  189. #define d_cos(x) (cos(*(x)))
  190. #define d_cosh(x) (cosh(*(x)))
  191. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  192. #define d_exp(x) (exp(*(x)))
  193. #define d_imag(z) (cimag(Cd(z)))
  194. #define r_imag(z) (cimagf(Cf(z)))
  195. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  196. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  198. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define d_log(x) (log(*(x)))
  200. #define d_mod(x, y) (fmod(*(x), *(y)))
  201. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  202. #define d_nint(x) u_nint(*(x))
  203. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  204. #define d_sign(a,b) u_sign(*(a),*(b))
  205. #define r_sign(a,b) u_sign(*(a),*(b))
  206. #define d_sin(x) (sin(*(x)))
  207. #define d_sinh(x) (sinh(*(x)))
  208. #define d_sqrt(x) (sqrt(*(x)))
  209. #define d_tan(x) (tan(*(x)))
  210. #define d_tanh(x) (tanh(*(x)))
  211. #define i_abs(x) abs(*(x))
  212. #define i_dnnt(x) ((integer)u_nint(*(x)))
  213. #define i_len(s, n) (n)
  214. #define i_nint(x) ((integer)u_nint(*(x)))
  215. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  216. #define pow_ri(B,E) spow_ui(*(B),*(E))
  217. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  218. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  219. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  220. #define sig_die(s, kill) { exit(1); }
  221. #define s_stop(s, n) {exit(0);}
  222. #define z_abs(z) (cabs(Cd(z)))
  223. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  224. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  225. #define myexit_() break;
  226. #define mycycle() continue;
  227. #define myceiling(w) {ceil(w)}
  228. #define myhuge(w) {HUGE_VAL}
  229. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  230. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  231. static float spow_ui(float x, integer n) {
  232. float pow=1.0; unsigned long int u;
  233. if(n != 0) {
  234. if(n < 0) n = -n, x = 1/x;
  235. for(u = n; ; ) {
  236. if(u & 01) pow *= x;
  237. if(u >>= 1) x *= x;
  238. else break;
  239. }
  240. }
  241. return pow;
  242. }
  243. /* -- translated by f2c (version 20000121).
  244. You must link the resulting object file with the libraries:
  245. -lf2c -lm (in that order)
  246. */
  247. /* Table of constant values */
  248. static integer c__1 = 1;
  249. static real c_b35 = 10.f;
  250. static real c_b71 = .5f;
  251. /* > \brief \b SGGBAL */
  252. /* =========== DOCUMENTATION =========== */
  253. /* Online html documentation available at */
  254. /* http://www.netlib.org/lapack/explore-html/ */
  255. /* > \htmlonly */
  256. /* > Download SGGBAL + dependencies */
  257. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggbal.
  258. f"> */
  259. /* > [TGZ]</a> */
  260. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggbal.
  261. f"> */
  262. /* > [ZIP]</a> */
  263. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggbal.
  264. f"> */
  265. /* > [TXT]</a> */
  266. /* > \endhtmlonly */
  267. /* Definition: */
  268. /* =========== */
  269. /* SUBROUTINE SGGBAL( JOB, N, A, LDA, B, LDB, ILO, IHI, LSCALE, */
  270. /* RSCALE, WORK, INFO ) */
  271. /* CHARACTER JOB */
  272. /* INTEGER IHI, ILO, INFO, LDA, LDB, N */
  273. /* REAL A( LDA, * ), B( LDB, * ), LSCALE( * ), */
  274. /* $ RSCALE( * ), WORK( * ) */
  275. /* > \par Purpose: */
  276. /* ============= */
  277. /* > */
  278. /* > \verbatim */
  279. /* > */
  280. /* > SGGBAL balances a pair of general real matrices (A,B). This */
  281. /* > involves, first, permuting A and B by similarity transformations to */
  282. /* > isolate eigenvalues in the first 1 to ILO$-$1 and last IHI+1 to N */
  283. /* > elements on the diagonal; and second, applying a diagonal similarity */
  284. /* > transformation to rows and columns ILO to IHI to make the rows */
  285. /* > and columns as close in norm as possible. Both steps are optional. */
  286. /* > */
  287. /* > Balancing may reduce the 1-norm of the matrices, and improve the */
  288. /* > accuracy of the computed eigenvalues and/or eigenvectors in the */
  289. /* > generalized eigenvalue problem A*x = lambda*B*x. */
  290. /* > \endverbatim */
  291. /* Arguments: */
  292. /* ========== */
  293. /* > \param[in] JOB */
  294. /* > \verbatim */
  295. /* > JOB is CHARACTER*1 */
  296. /* > Specifies the operations to be performed on A and B: */
  297. /* > = 'N': none: simply set ILO = 1, IHI = N, LSCALE(I) = 1.0 */
  298. /* > and RSCALE(I) = 1.0 for i = 1,...,N. */
  299. /* > = 'P': permute only; */
  300. /* > = 'S': scale only; */
  301. /* > = 'B': both permute and scale. */
  302. /* > \endverbatim */
  303. /* > */
  304. /* > \param[in] N */
  305. /* > \verbatim */
  306. /* > N is INTEGER */
  307. /* > The order of the matrices A and B. N >= 0. */
  308. /* > \endverbatim */
  309. /* > */
  310. /* > \param[in,out] A */
  311. /* > \verbatim */
  312. /* > A is REAL array, dimension (LDA,N) */
  313. /* > On entry, the input matrix A. */
  314. /* > On exit, A is overwritten by the balanced matrix. */
  315. /* > If JOB = 'N', A is not referenced. */
  316. /* > \endverbatim */
  317. /* > */
  318. /* > \param[in] LDA */
  319. /* > \verbatim */
  320. /* > LDA is INTEGER */
  321. /* > The leading dimension of the array A. LDA >= f2cmax(1,N). */
  322. /* > \endverbatim */
  323. /* > */
  324. /* > \param[in,out] B */
  325. /* > \verbatim */
  326. /* > B is REAL array, dimension (LDB,N) */
  327. /* > On entry, the input matrix B. */
  328. /* > On exit, B is overwritten by the balanced matrix. */
  329. /* > If JOB = 'N', B is not referenced. */
  330. /* > \endverbatim */
  331. /* > */
  332. /* > \param[in] LDB */
  333. /* > \verbatim */
  334. /* > LDB is INTEGER */
  335. /* > The leading dimension of the array B. LDB >= f2cmax(1,N). */
  336. /* > \endverbatim */
  337. /* > */
  338. /* > \param[out] ILO */
  339. /* > \verbatim */
  340. /* > ILO is INTEGER */
  341. /* > \endverbatim */
  342. /* > */
  343. /* > \param[out] IHI */
  344. /* > \verbatim */
  345. /* > IHI is INTEGER */
  346. /* > ILO and IHI are set to integers such that on exit */
  347. /* > A(i,j) = 0 and B(i,j) = 0 if i > j and */
  348. /* > j = 1,...,ILO-1 or i = IHI+1,...,N. */
  349. /* > If JOB = 'N' or 'S', ILO = 1 and IHI = N. */
  350. /* > \endverbatim */
  351. /* > */
  352. /* > \param[out] LSCALE */
  353. /* > \verbatim */
  354. /* > LSCALE is REAL array, dimension (N) */
  355. /* > Details of the permutations and scaling factors applied */
  356. /* > to the left side of A and B. If P(j) is the index of the */
  357. /* > row interchanged with row j, and D(j) */
  358. /* > is the scaling factor applied to row j, then */
  359. /* > LSCALE(j) = P(j) for J = 1,...,ILO-1 */
  360. /* > = D(j) for J = ILO,...,IHI */
  361. /* > = P(j) for J = IHI+1,...,N. */
  362. /* > The order in which the interchanges are made is N to IHI+1, */
  363. /* > then 1 to ILO-1. */
  364. /* > \endverbatim */
  365. /* > */
  366. /* > \param[out] RSCALE */
  367. /* > \verbatim */
  368. /* > RSCALE is REAL array, dimension (N) */
  369. /* > Details of the permutations and scaling factors applied */
  370. /* > to the right side of A and B. If P(j) is the index of the */
  371. /* > column interchanged with column j, and D(j) */
  372. /* > is the scaling factor applied to column j, then */
  373. /* > LSCALE(j) = P(j) for J = 1,...,ILO-1 */
  374. /* > = D(j) for J = ILO,...,IHI */
  375. /* > = P(j) for J = IHI+1,...,N. */
  376. /* > The order in which the interchanges are made is N to IHI+1, */
  377. /* > then 1 to ILO-1. */
  378. /* > \endverbatim */
  379. /* > */
  380. /* > \param[out] WORK */
  381. /* > \verbatim */
  382. /* > WORK is REAL array, dimension (lwork) */
  383. /* > lwork must be at least f2cmax(1,6*N) when JOB = 'S' or 'B', and */
  384. /* > at least 1 when JOB = 'N' or 'P'. */
  385. /* > \endverbatim */
  386. /* > */
  387. /* > \param[out] INFO */
  388. /* > \verbatim */
  389. /* > INFO is INTEGER */
  390. /* > = 0: successful exit */
  391. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  392. /* > \endverbatim */
  393. /* Authors: */
  394. /* ======== */
  395. /* > \author Univ. of Tennessee */
  396. /* > \author Univ. of California Berkeley */
  397. /* > \author Univ. of Colorado Denver */
  398. /* > \author NAG Ltd. */
  399. /* > \date December 2016 */
  400. /* > \ingroup realGBcomputational */
  401. /* > \par Further Details: */
  402. /* ===================== */
  403. /* > */
  404. /* > \verbatim */
  405. /* > */
  406. /* > See R.C. WARD, Balancing the generalized eigenvalue problem, */
  407. /* > SIAM J. Sci. Stat. Comp. 2 (1981), 141-152. */
  408. /* > \endverbatim */
  409. /* > */
  410. /* ===================================================================== */
  411. /* Subroutine */ void sggbal_(char *job, integer *n, real *a, integer *lda,
  412. real *b, integer *ldb, integer *ilo, integer *ihi, real *lscale, real
  413. *rscale, real *work, integer *info)
  414. {
  415. /* System generated locals */
  416. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3;
  417. real r__1, r__2, r__3;
  418. /* Local variables */
  419. integer lcab;
  420. real beta, coef;
  421. integer irab, lrab;
  422. real basl, cmax;
  423. extern real sdot_(integer *, real *, integer *, real *, integer *);
  424. real coef2, coef5;
  425. integer i__, j, k, l, m;
  426. real gamma, t, alpha;
  427. extern logical lsame_(char *, char *);
  428. extern /* Subroutine */ void sscal_(integer *, real *, real *, integer *);
  429. real sfmin, sfmax;
  430. integer iflow;
  431. extern /* Subroutine */ void sswap_(integer *, real *, integer *, real *,
  432. integer *);
  433. integer kount;
  434. extern /* Subroutine */ void saxpy_(integer *, real *, real *, integer *,
  435. real *, integer *);
  436. integer jc;
  437. real ta, tb, tc;
  438. integer ir, it;
  439. real ew;
  440. integer nr;
  441. real pgamma;
  442. extern real slamch_(char *);
  443. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  444. extern integer isamax_(integer *, real *, integer *);
  445. integer lsfmin, lsfmax, ip1, jp1, lm1;
  446. real cab, rab, ewc, cor, sum;
  447. integer nrp2, icab;
  448. /* -- LAPACK computational routine (version 3.7.0) -- */
  449. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  450. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  451. /* December 2016 */
  452. /* ===================================================================== */
  453. /* Test the input parameters */
  454. /* Parameter adjustments */
  455. a_dim1 = *lda;
  456. a_offset = 1 + a_dim1 * 1;
  457. a -= a_offset;
  458. b_dim1 = *ldb;
  459. b_offset = 1 + b_dim1 * 1;
  460. b -= b_offset;
  461. --lscale;
  462. --rscale;
  463. --work;
  464. /* Function Body */
  465. *info = 0;
  466. if (! lsame_(job, "N") && ! lsame_(job, "P") && ! lsame_(job, "S")
  467. && ! lsame_(job, "B")) {
  468. *info = -1;
  469. } else if (*n < 0) {
  470. *info = -2;
  471. } else if (*lda < f2cmax(1,*n)) {
  472. *info = -4;
  473. } else if (*ldb < f2cmax(1,*n)) {
  474. *info = -6;
  475. }
  476. if (*info != 0) {
  477. i__1 = -(*info);
  478. xerbla_("SGGBAL", &i__1, (ftnlen)6);
  479. return;
  480. }
  481. /* Quick return if possible */
  482. if (*n == 0) {
  483. *ilo = 1;
  484. *ihi = *n;
  485. return;
  486. }
  487. if (*n == 1) {
  488. *ilo = 1;
  489. *ihi = *n;
  490. lscale[1] = 1.f;
  491. rscale[1] = 1.f;
  492. return;
  493. }
  494. if (lsame_(job, "N")) {
  495. *ilo = 1;
  496. *ihi = *n;
  497. i__1 = *n;
  498. for (i__ = 1; i__ <= i__1; ++i__) {
  499. lscale[i__] = 1.f;
  500. rscale[i__] = 1.f;
  501. /* L10: */
  502. }
  503. return;
  504. }
  505. k = 1;
  506. l = *n;
  507. if (lsame_(job, "S")) {
  508. goto L190;
  509. }
  510. goto L30;
  511. /* Permute the matrices A and B to isolate the eigenvalues. */
  512. /* Find row with one nonzero in columns 1 through L */
  513. L20:
  514. l = lm1;
  515. if (l != 1) {
  516. goto L30;
  517. }
  518. rscale[1] = 1.f;
  519. lscale[1] = 1.f;
  520. goto L190;
  521. L30:
  522. lm1 = l - 1;
  523. for (i__ = l; i__ >= 1; --i__) {
  524. i__1 = lm1;
  525. for (j = 1; j <= i__1; ++j) {
  526. jp1 = j + 1;
  527. if (a[i__ + j * a_dim1] != 0.f || b[i__ + j * b_dim1] != 0.f) {
  528. goto L50;
  529. }
  530. /* L40: */
  531. }
  532. j = l;
  533. goto L70;
  534. L50:
  535. i__1 = l;
  536. for (j = jp1; j <= i__1; ++j) {
  537. if (a[i__ + j * a_dim1] != 0.f || b[i__ + j * b_dim1] != 0.f) {
  538. goto L80;
  539. }
  540. /* L60: */
  541. }
  542. j = jp1 - 1;
  543. L70:
  544. m = l;
  545. iflow = 1;
  546. goto L160;
  547. L80:
  548. ;
  549. }
  550. goto L100;
  551. /* Find column with one nonzero in rows K through N */
  552. L90:
  553. ++k;
  554. L100:
  555. i__1 = l;
  556. for (j = k; j <= i__1; ++j) {
  557. i__2 = lm1;
  558. for (i__ = k; i__ <= i__2; ++i__) {
  559. ip1 = i__ + 1;
  560. if (a[i__ + j * a_dim1] != 0.f || b[i__ + j * b_dim1] != 0.f) {
  561. goto L120;
  562. }
  563. /* L110: */
  564. }
  565. i__ = l;
  566. goto L140;
  567. L120:
  568. i__2 = l;
  569. for (i__ = ip1; i__ <= i__2; ++i__) {
  570. if (a[i__ + j * a_dim1] != 0.f || b[i__ + j * b_dim1] != 0.f) {
  571. goto L150;
  572. }
  573. /* L130: */
  574. }
  575. i__ = ip1 - 1;
  576. L140:
  577. m = k;
  578. iflow = 2;
  579. goto L160;
  580. L150:
  581. ;
  582. }
  583. goto L190;
  584. /* Permute rows M and I */
  585. L160:
  586. lscale[m] = (real) i__;
  587. if (i__ == m) {
  588. goto L170;
  589. }
  590. i__1 = *n - k + 1;
  591. sswap_(&i__1, &a[i__ + k * a_dim1], lda, &a[m + k * a_dim1], lda);
  592. i__1 = *n - k + 1;
  593. sswap_(&i__1, &b[i__ + k * b_dim1], ldb, &b[m + k * b_dim1], ldb);
  594. /* Permute columns M and J */
  595. L170:
  596. rscale[m] = (real) j;
  597. if (j == m) {
  598. goto L180;
  599. }
  600. sswap_(&l, &a[j * a_dim1 + 1], &c__1, &a[m * a_dim1 + 1], &c__1);
  601. sswap_(&l, &b[j * b_dim1 + 1], &c__1, &b[m * b_dim1 + 1], &c__1);
  602. L180:
  603. switch (iflow) {
  604. case 1: goto L20;
  605. case 2: goto L90;
  606. }
  607. L190:
  608. *ilo = k;
  609. *ihi = l;
  610. if (lsame_(job, "P")) {
  611. i__1 = *ihi;
  612. for (i__ = *ilo; i__ <= i__1; ++i__) {
  613. lscale[i__] = 1.f;
  614. rscale[i__] = 1.f;
  615. /* L195: */
  616. }
  617. return;
  618. }
  619. if (*ilo == *ihi) {
  620. return;
  621. }
  622. /* Balance the submatrix in rows ILO to IHI. */
  623. nr = *ihi - *ilo + 1;
  624. i__1 = *ihi;
  625. for (i__ = *ilo; i__ <= i__1; ++i__) {
  626. rscale[i__] = 0.f;
  627. lscale[i__] = 0.f;
  628. work[i__] = 0.f;
  629. work[i__ + *n] = 0.f;
  630. work[i__ + (*n << 1)] = 0.f;
  631. work[i__ + *n * 3] = 0.f;
  632. work[i__ + (*n << 2)] = 0.f;
  633. work[i__ + *n * 5] = 0.f;
  634. /* L200: */
  635. }
  636. /* Compute right side vector in resulting linear equations */
  637. basl = r_lg10(&c_b35);
  638. i__1 = *ihi;
  639. for (i__ = *ilo; i__ <= i__1; ++i__) {
  640. i__2 = *ihi;
  641. for (j = *ilo; j <= i__2; ++j) {
  642. tb = b[i__ + j * b_dim1];
  643. ta = a[i__ + j * a_dim1];
  644. if (ta == 0.f) {
  645. goto L210;
  646. }
  647. r__1 = abs(ta);
  648. ta = r_lg10(&r__1) / basl;
  649. L210:
  650. if (tb == 0.f) {
  651. goto L220;
  652. }
  653. r__1 = abs(tb);
  654. tb = r_lg10(&r__1) / basl;
  655. L220:
  656. work[i__ + (*n << 2)] = work[i__ + (*n << 2)] - ta - tb;
  657. work[j + *n * 5] = work[j + *n * 5] - ta - tb;
  658. /* L230: */
  659. }
  660. /* L240: */
  661. }
  662. coef = 1.f / (real) (nr << 1);
  663. coef2 = coef * coef;
  664. coef5 = coef2 * .5f;
  665. nrp2 = nr + 2;
  666. beta = 0.f;
  667. it = 1;
  668. /* Start generalized conjugate gradient iteration */
  669. L250:
  670. gamma = sdot_(&nr, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + (*n << 2)]
  671. , &c__1) + sdot_(&nr, &work[*ilo + *n * 5], &c__1, &work[*ilo + *
  672. n * 5], &c__1);
  673. ew = 0.f;
  674. ewc = 0.f;
  675. i__1 = *ihi;
  676. for (i__ = *ilo; i__ <= i__1; ++i__) {
  677. ew += work[i__ + (*n << 2)];
  678. ewc += work[i__ + *n * 5];
  679. /* L260: */
  680. }
  681. /* Computing 2nd power */
  682. r__1 = ew;
  683. /* Computing 2nd power */
  684. r__2 = ewc;
  685. /* Computing 2nd power */
  686. r__3 = ew - ewc;
  687. gamma = coef * gamma - coef2 * (r__1 * r__1 + r__2 * r__2) - coef5 * (
  688. r__3 * r__3);
  689. if (gamma == 0.f) {
  690. goto L350;
  691. }
  692. if (it != 1) {
  693. beta = gamma / pgamma;
  694. }
  695. t = coef5 * (ewc - ew * 3.f);
  696. tc = coef5 * (ew - ewc * 3.f);
  697. sscal_(&nr, &beta, &work[*ilo], &c__1);
  698. sscal_(&nr, &beta, &work[*ilo + *n], &c__1);
  699. saxpy_(&nr, &coef, &work[*ilo + (*n << 2)], &c__1, &work[*ilo + *n], &
  700. c__1);
  701. saxpy_(&nr, &coef, &work[*ilo + *n * 5], &c__1, &work[*ilo], &c__1);
  702. i__1 = *ihi;
  703. for (i__ = *ilo; i__ <= i__1; ++i__) {
  704. work[i__] += tc;
  705. work[i__ + *n] += t;
  706. /* L270: */
  707. }
  708. /* Apply matrix to vector */
  709. i__1 = *ihi;
  710. for (i__ = *ilo; i__ <= i__1; ++i__) {
  711. kount = 0;
  712. sum = 0.f;
  713. i__2 = *ihi;
  714. for (j = *ilo; j <= i__2; ++j) {
  715. if (a[i__ + j * a_dim1] == 0.f) {
  716. goto L280;
  717. }
  718. ++kount;
  719. sum += work[j];
  720. L280:
  721. if (b[i__ + j * b_dim1] == 0.f) {
  722. goto L290;
  723. }
  724. ++kount;
  725. sum += work[j];
  726. L290:
  727. ;
  728. }
  729. work[i__ + (*n << 1)] = (real) kount * work[i__ + *n] + sum;
  730. /* L300: */
  731. }
  732. i__1 = *ihi;
  733. for (j = *ilo; j <= i__1; ++j) {
  734. kount = 0;
  735. sum = 0.f;
  736. i__2 = *ihi;
  737. for (i__ = *ilo; i__ <= i__2; ++i__) {
  738. if (a[i__ + j * a_dim1] == 0.f) {
  739. goto L310;
  740. }
  741. ++kount;
  742. sum += work[i__ + *n];
  743. L310:
  744. if (b[i__ + j * b_dim1] == 0.f) {
  745. goto L320;
  746. }
  747. ++kount;
  748. sum += work[i__ + *n];
  749. L320:
  750. ;
  751. }
  752. work[j + *n * 3] = (real) kount * work[j] + sum;
  753. /* L330: */
  754. }
  755. sum = sdot_(&nr, &work[*ilo + *n], &c__1, &work[*ilo + (*n << 1)], &c__1)
  756. + sdot_(&nr, &work[*ilo], &c__1, &work[*ilo + *n * 3], &c__1);
  757. alpha = gamma / sum;
  758. /* Determine correction to current iteration */
  759. cmax = 0.f;
  760. i__1 = *ihi;
  761. for (i__ = *ilo; i__ <= i__1; ++i__) {
  762. cor = alpha * work[i__ + *n];
  763. if (abs(cor) > cmax) {
  764. cmax = abs(cor);
  765. }
  766. lscale[i__] += cor;
  767. cor = alpha * work[i__];
  768. if (abs(cor) > cmax) {
  769. cmax = abs(cor);
  770. }
  771. rscale[i__] += cor;
  772. /* L340: */
  773. }
  774. if (cmax < .5f) {
  775. goto L350;
  776. }
  777. r__1 = -alpha;
  778. saxpy_(&nr, &r__1, &work[*ilo + (*n << 1)], &c__1, &work[*ilo + (*n << 2)]
  779. , &c__1);
  780. r__1 = -alpha;
  781. saxpy_(&nr, &r__1, &work[*ilo + *n * 3], &c__1, &work[*ilo + *n * 5], &
  782. c__1);
  783. pgamma = gamma;
  784. ++it;
  785. if (it <= nrp2) {
  786. goto L250;
  787. }
  788. /* End generalized conjugate gradient iteration */
  789. L350:
  790. sfmin = slamch_("S");
  791. sfmax = 1.f / sfmin;
  792. lsfmin = (integer) (r_lg10(&sfmin) / basl + 1.f);
  793. lsfmax = (integer) (r_lg10(&sfmax) / basl);
  794. i__1 = *ihi;
  795. for (i__ = *ilo; i__ <= i__1; ++i__) {
  796. i__2 = *n - *ilo + 1;
  797. irab = isamax_(&i__2, &a[i__ + *ilo * a_dim1], lda);
  798. rab = (r__1 = a[i__ + (irab + *ilo - 1) * a_dim1], abs(r__1));
  799. i__2 = *n - *ilo + 1;
  800. irab = isamax_(&i__2, &b[i__ + *ilo * b_dim1], ldb);
  801. /* Computing MAX */
  802. r__2 = rab, r__3 = (r__1 = b[i__ + (irab + *ilo - 1) * b_dim1], abs(
  803. r__1));
  804. rab = f2cmax(r__2,r__3);
  805. r__1 = rab + sfmin;
  806. lrab = (integer) (r_lg10(&r__1) / basl + 1.f);
  807. ir = lscale[i__] + r_sign(&c_b71, &lscale[i__]);
  808. /* Computing MIN */
  809. i__2 = f2cmax(ir,lsfmin), i__2 = f2cmin(i__2,lsfmax), i__3 = lsfmax - lrab;
  810. ir = f2cmin(i__2,i__3);
  811. lscale[i__] = pow_ri(&c_b35, &ir);
  812. icab = isamax_(ihi, &a[i__ * a_dim1 + 1], &c__1);
  813. cab = (r__1 = a[icab + i__ * a_dim1], abs(r__1));
  814. icab = isamax_(ihi, &b[i__ * b_dim1 + 1], &c__1);
  815. /* Computing MAX */
  816. r__2 = cab, r__3 = (r__1 = b[icab + i__ * b_dim1], abs(r__1));
  817. cab = f2cmax(r__2,r__3);
  818. r__1 = cab + sfmin;
  819. lcab = (integer) (r_lg10(&r__1) / basl + 1.f);
  820. jc = rscale[i__] + r_sign(&c_b71, &rscale[i__]);
  821. /* Computing MIN */
  822. i__2 = f2cmax(jc,lsfmin), i__2 = f2cmin(i__2,lsfmax), i__3 = lsfmax - lcab;
  823. jc = f2cmin(i__2,i__3);
  824. rscale[i__] = pow_ri(&c_b35, &jc);
  825. /* L360: */
  826. }
  827. /* Row scaling of matrices A and B */
  828. i__1 = *ihi;
  829. for (i__ = *ilo; i__ <= i__1; ++i__) {
  830. i__2 = *n - *ilo + 1;
  831. sscal_(&i__2, &lscale[i__], &a[i__ + *ilo * a_dim1], lda);
  832. i__2 = *n - *ilo + 1;
  833. sscal_(&i__2, &lscale[i__], &b[i__ + *ilo * b_dim1], ldb);
  834. /* L370: */
  835. }
  836. /* Column scaling of matrices A and B */
  837. i__1 = *ihi;
  838. for (j = *ilo; j <= i__1; ++j) {
  839. sscal_(ihi, &rscale[j], &a[j * a_dim1 + 1], &c__1);
  840. sscal_(ihi, &rscale[j], &b[j * b_dim1 + 1], &c__1);
  841. /* L380: */
  842. }
  843. return;
  844. /* End of SGGBAL */
  845. } /* sggbal_ */