Debian Codegraph @

packages/seacas/libraries/chaco/eigen/ql.c

raw
1 /*
2 * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 /* Eigensolution of real symmetric tridiagonal matrix using the algorithm
10 of Numerical Recipes p. 380. Removed eigenvector calculation and added
11 return codes: 1 if maximum number of iterations is exceeded, 0 otherwise.
12 NOTE CAREFULLY: the vector e is used as workspace, the eigenvals are
13 returned in the vector d. */
14
15 #include <math.h>
16
17 #define SIGN(a, b) ((b) < 0 ? -fabs(a) : fabs(a))
18
19 int ql(double d[], double e[], int n)
20
21 {
22 int m, l, iter, i;
23 double s, r, p, g, f, dd, c, b;
24
25 e[n] = 0.0;
26
27 for (l = 1; l <= n; l++) {
28 iter = 0;
29 do {
30 for (m = l; m <= n - 1; m++) {
31 dd = fabs(d[m]) + fabs(d[m + 1]);
32 if (fabs(e[m]) + dd == dd) {
33 break;
34 }
35 }
36 if (m != l) {
37 if (iter++ == 50) {
38 return (1);
39 /* ... not converging; bail out with error code. */
40 }
41 g = (d[l + 1] - d[l]) / (2.0 * e[l]);
42 r = sqrt((g * g) + 1.0);
43 g = d[m] - d[l] + e[l] / (g + SIGN(r, g));
44 s = c = 1.0;
45 p = 0.0;
46 for (i = m - 1; i >= l; i--) {
47 f = s * e[i];
48 b = c * e[i];
49 if (fabs(f) >= fabs(g)) {
50 c = g / f;
51 r = sqrt((c * c) + 1.0);
52 e[i + 1] = f * r;
53 c *= (s = 1.0 / r);
54 }
55 else {
56 s = f / g;
57 r = sqrt((s * s) + 1.0);
58 e[i + 1] = g * r;
59 s *= (c = 1.0 / r);
60 }
61 g = d[i + 1] - p;
62 r = (d[i] - g) * s + 2.0 * c * b;
63 p = s * r;
64 d[i + 1] = g + p;
65 g = c * r - b;
66 }
67 d[l] = d[l] - p;
68 e[l] = g;
69 e[m] = 0.0;
70 }
71 } while (m != l);
72 }
73 return (0); /* ... things seem ok */
74 }