Actual source code: cgeig.c

```
2: /*
3:       Code for calculating extreme eigenvalues via the Lanczo method
4:    running with CG. Note this only works for symmetric real and Hermitian
5:    matrices (not complex matrices that are symmetric).
6: */
7: #include <../src/ksp/ksp/impls/cg/cgimpl.h>

9: /* tql1.f -- translated by f2c (version of 25 March 1992  12:58:56).
10:    By Barry Smith on March 27, 1994.
11:    Eispack routine to determine eigenvalues of symmetric
12:    tridiagonal matrix

14:   Note that this routine always uses real numbers (not complex) even if the underlying
15:   matrix is Hermitian. This is because the Lanczos process applied to Hermitian matrices
16:   always produces a real, symmetric tridiagonal matrix.
17: */

19: static PetscReal LINPACKcgpthy(PetscReal *a, PetscReal *b)
20: {
21:   /* System generated locals */
22:   PetscReal d__1, d__2, d__3;

24:   /* Local variables */
25:   PetscReal p, r, s, t, u;

27:   /*     FINDS DSQRT(A**2+B**2) WITHOUT OVERFLOW OR DESTRUCTIVE UNDERFLOW */

29:   /* Computing MAX */
30:   d__1 = PetscAbsReal(*a);
31:   d__2 = PetscAbsReal(*b);
32:   p    = PetscMax(d__1, d__2);
33:   if (!p) goto L20;
34:   /* Computing MIN */
35:   d__2 = PetscAbsReal(*a);
36:   d__3 = PetscAbsReal(*b);
37:   /* Computing 2nd power */
38:   d__1 = PetscMin(d__2, d__3) / p;
39:   r    = d__1 * d__1;
40: L10:
41:   t = r + 4.;
42:   if (t == 4.) goto L20;
43:   s = r / t;
44:   u = s * 2. + 1.;
45:   p = u * p;
46:   /* Computing 2nd power */
47:   d__1 = s / u;
48:   r    = d__1 * d__1 * r;
49:   goto L10;
50: L20:
51:   return p;
52: } /* cgpthy_ */

54: static PetscErrorCode LINPACKcgtql1(PetscInt *n, PetscReal *d, PetscReal *e, PetscInt *ierr)
55: {
56:   /* System generated locals */
57:   PetscInt  i__1, i__2;
58:   PetscReal d__1, d__2, c_b10 = 1.0;

60:   /* Local variables */
61:   PetscReal c, f, g, h;
62:   PetscInt  i, j, l, m;
63:   PetscReal p, r, s, c2, c3 = 0.0;
64:   PetscInt  l1, l2;
65:   PetscReal s2 = 0.0;
66:   PetscInt  ii;
67:   PetscReal dl1, el1;
68:   PetscInt  mml;
69:   PetscReal tst1, tst2;

71:   /*     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TQL1, */
72:   /*     NUM. MATH. 11, 293-306(1968) BY BOWDLER, MARTIN, REINSCH, AND */
73:   /*     WILKINSON. */
74:   /*     HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 227-240(1971). */

76:   /*     THIS SUBROUTINE FINDS THE EIGENVALUES OF A SYMMETRIC */
77:   /*     TRIDIAGONAL MATRIX BY THE QL METHOD. */

79:   /*     ON INPUT */

81:   /*        N IS THE ORDER OF THE MATRIX. */

83:   /*        D CONTAINS THE DIAGONAL ELEMENTS OF THE INPUT MATRIX. */

85:   /*        E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE INPUT MATRIX */
86:   /*          IN ITS LAST N-1 POSITIONS.  E(1) IS ARBITRARY. */

88:   /*      ON OUTPUT */

90:   /*        D CONTAINS THE EIGENVALUES IN ASCENDING ORDER.  IF AN */
91:   /*          ERROR EXIT IS MADE, THE EIGENVALUES ARE CORRECT AND */
92:   /*          ORDERED FOR INDICES 1,2,...IERR-1, BUT MAY NOT BE */
93:   /*          THE SMALLEST EIGENVALUES. */

95:   /*        E HAS BEEN DESTROYED. */

97:   /*        IERR IS SET TO */
98:   /*          ZERO       FOR NORMAL RETURN, */
99:   /*          J          IF THE J-TH EIGENVALUE HAS NOT BEEN */
100:   /*                     DETERMINED AFTER 30 ITERATIONS. */

102:   /*     CALLS CGPTHY FOR  DSQRT(A*A + B*B) . */

104:   /*     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW, */
105:   /*     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
106: */

108:   /*     THIS VERSION DATED AUGUST 1983. */

110:   /*     ------------------------------------------------------------------
111: */
112:   PetscReal ds;

114:   --e;
115:   --d;

117:   *0;
118:   if (*n == 1) goto L1001;

120:   i__1 = *n;
121:   for (i = 2; i <= i__1; ++i) e[i - 1] = e[i];

123:   f     = 0.;
124:   tst1  = 0.;
125:   e[*n] = 0.;

127:   i__1 = *n;
128:   for (l = 1; l <= i__1; ++l) {
129:     j    = 0;
130:     d__1 = d[l];
131:     d__2 = e[l];
132:     h    = PetscAbsReal(d__1) + PetscAbsReal(d__2);
133:     if (tst1 < h) tst1 = h;
134:     /*     .......... LOOK FOR SMALL SUB-DIAGONAL ELEMENT .......... */
135:     i__2 = *n;
136:     for (m = l; m <= i__2; ++m) {
137:       d__1 = e[m];
138:       tst2 = tst1 + PetscAbsReal(d__1);
139:       if (tst2 == tst1) goto L120;
140:       /*     .......... E(N) IS ALWAYS ZERO,SO THERE IS NO EXIT */
141:       /*                THROUGH THE BOTTOM OF THE LOOP .......... */
142:     }
143:   L120:
144:     if (m == l) goto L210;
145:   L130:
146:     if (j == 30) goto L1000;
147:     ++j;
148:     /*     .......... FORM SHIFT .......... */
149:     l1 = l + 1;
150:     l2 = l1 + 1;
151:     g  = d[l];
152:     p  = (d[l1] - g) / (e[l] * 2.);
153:     r  = LINPACKcgpthy(&p, &c_b10);
154:     ds = 1.0;
155:     if (p < 0.0) ds = -1.0;
156:     d[l]  = e[l] / (p + ds * r);
157:     d[l1] = e[l] * (p + ds * r);
158:     dl1   = d[l1];
159:     h     = g - d[l];
160:     if (l2 > *n) goto L145;

162:     i__2 = *n;
163:     for (i = l2; i <= i__2; ++i) d[i] -= h;

165:   L145:
166:     f += h;
167:     /*     .......... QL TRANSFORMATION .......... */
168:     p   = d[m];
169:     c   = 1.;
170:     c2  = c;
171:     el1 = e[l1];
172:     s   = 0.;
173:     mml = m - l;
174:     /*     .......... FOR I=M-1 STEP -1 UNTIL L DO -- .......... */
175:     i__2 = mml;
176:     for (ii = 1; ii <= i__2; ++ii) {
177:       c3       = c2;
178:       c2       = c;
179:       s2       = s;
180:       i        = m - ii;
181:       g        = c * e[i];
182:       h        = c * p;
183:       r        = LINPACKcgpthy(&p, &e[i]);
184:       e[i + 1] = s * r;
185:       s        = e[i] / r;
186:       c        = p / r;
187:       p        = c * d[i] - s * g;
188:       d[i + 1] = h + s * (c * g + s * d[i]);
189:     }

191:     p    = -s * s2 * c3 * el1 * e[l] / dl1;
192:     e[l] = s * p;
193:     d[l] = c * p;
194:     d__1 = e[l];
195:     tst2 = tst1 + PetscAbsReal(d__1);
196:     if (tst2 > tst1) goto L130;
197:   L210:
198:     p = d[l] + f;
199:     /*     .......... ORDER EIGENVALUES .......... */
200:     if (l == 1) goto L250;
201:     /*     .......... FOR I=L STEP -1 UNTIL 2 DO -- .......... */
202:     i__2 = l;
203:     for (ii = 2; ii <= i__2; ++ii) {
204:       i = l + 2 - ii;
205:       if (p >= d[i - 1]) goto L270;
206:       d[i] = d[i - 1];
207:     }

209:   L250:
210:     i = 1;
211:   L270:
212:     d[i] = p;
213:   }

215:   goto L1001;
216: /*     .......... SET ERROR -- NO CONVERGENCE TO AN */
217: /*                EIGENVALUE AFTER 30 ITERATIONS .......... */
218: L1000:
219:   *l;
220: L1001:
221:   return 0;
222: } /* cgtql1_ */

224: PetscErrorCode KSPComputeEigenvalues_CG(KSP ksp, PetscInt nmax, PetscReal *r, PetscReal *c, PetscInt *neig)
225: {
226:   KSP_CG      *cgP = (KSP_CG *)ksp->data;
227:   PetscScalar *d, *e;
228:   PetscReal   *ee;
229:   PetscInt     j, n = ksp->its;

232:   *neig = n;

234:   PetscArrayzero(c, nmax);
235:   if (!n) return 0;
236:   d  = cgP->d;
237:   e  = cgP->e;
238:   ee = cgP->ee;

240:   /* copy tridiagonal matrix to work space */
241:   for (j = 0; j < n; j++) {
242:     r[j]  = PetscRealPart(d[j]);
243:     ee[j] = PetscRealPart(e[j]);
244:   }

246:   LINPACKcgtql1(&n, r, ee, &j);
248:   PetscSortReal(n, r);
249:   return 0;
250: }

252: PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP ksp, PetscReal *emax, PetscReal *emin)
253: {
254:   KSP_CG      *cgP = (KSP_CG *)ksp->data;
255:   PetscScalar *d, *e;
256:   PetscReal   *dd, *ee;
257:   PetscInt     j, n = ksp->its;

259:   if (!n) {
260:     *emax = *emin = 1.0;
261:     return 0;
262:   }
263:   d  = cgP->d;
264:   e  = cgP->e;
265:   dd = cgP->dd;
266:   ee = cgP->ee;

268:   /* copy tridiagonal matrix to work space */
269:   for (j = 0; j < n; j++) {
270:     dd[j] = PetscRealPart(d[j]);
271:     ee[j] = PetscRealPart(e[j]);
272:   }

274:   LINPACKcgtql1(&n, dd, ee, &j);
276:   *emin = dd[0];
277:   *emax = dd[n - 1];
278:   return 0;
279: }
```