Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 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 #include "params.h" // for MAXDIMS
10 #include "prototypes.h"
11 #include <math.h> // for fabs, sqrt
12
13 int SRES_SWITCHES = 0; /* # switches to backup routine for computing evec of T */
14
15 /* NOTE: Uses a modified form of the bidirectional recurrence similar to Parlett
16 and Reid's version. Made a minor correction and modified one heuristic
17 that didn't seem to work well for our class of graphs. Code switches to
18 Tinvit, Eispack's inverse iteration routine if the bidirectional recurrence
19 fails to meet tolerance. Switches back to the bidirectional recurrence result
20 if Tinvit fails to converge or gives a worse residual. There's code for the
21 simple forward and backward recurrences in the comments at the end. */
22
23 /* NOTE: In this routine the diagonal of T is alpha[1] ... alpha[j], and the
24 off-diagonal is beta[2] ... beta[j]. Because the various literature
25 sources do not agree on indexing, some of the Lanczos algorithms have
26 to call this routine with the beta vector off-set by one. */
27
28 /* NOTE: The residual calculations look too simple, but they are right because
29 the recurrences force (in exact arithmetic) most of the terms in the
30 residual (T - ritz*I)s to zero. It's only the end points in the forward
31 or backward recurrences or the merge point in the bidirectional recurrence
32 where the resdual is not forced to zero. The values here are the residuals
33 if we ignore round-off in the other terms (which appears to be valid). */
34
35 /* NOTE: This routine is expected to return a normalized vector s such that it's
36 last entry, s[j], is positive, hence the calls to sign_normalize(). This
37 is not an issue in the backward recurrence since we set s[j] positive. */
38
39 /* Finds eigenvector s of T and returns residual norm. */
40 double Tevec(double *alpha, /* vector of Lanczos scalars */
41 double *beta, /* vector of Lanczos scalars */
42 int j, /* number of Lanczos iterations taken */
43 double ritz, /* approximate eigenvalue of T */
44 double *s /* approximate eigenvector of T */
45 )
46 {
47 extern double SRESTOL; /* limit on relative residual tol for evec of T */
48 extern double DOUBLE_MAX; /* maximum double precision value */
49 int i; /* index */
50 double residual = 0.0; /* how well recurrence gives eigenvector */
51 double temp; /* used to compute residual */
52 double *work; /* temporary work vector allocated within if used */
53 double w[MAXDIMS + 1]; /* holds eigenvalue for tinvit */
54 long index[MAXDIMS + 1]; /* index vector for tinvit */
55 long ierr; /* error flag for tinvit */
56 long nevals; /* number of evals sought */
57 long long_j; /* long copy of j for tinvit interface */
58 double hurdle; /* hurdle for local maximum in recurrence */
59 double prev_resid; /* stores residual from previous computation */
60
61 s[1] = 1.0;
62
63 if (j == 1) {
64 residual = fabs(alpha[1] - ritz);
65 }
66
67 if (j >= 2) {
68 /*Bidirectional recurrence - corrected and modified from Parlett and Reid,
69 "Tracking the Progress of the Lanczos Algorithm ..., IMA JNA 1, 1981 */
70 hurdle = 1.0;
71 residual = bidir(alpha, beta, j, ritz, s, hurdle);
72 }
73
74 if (residual > SRESTOL) {
75 /* Try again with Eispack's Tinvit iteration */
76 SRES_SWITCHES++;
77 index[1] = 1;
78 work = mkvec(1, 7 * j); /* lump things to save mallocs */
79 w[1] = ritz;
80 work[1] = 0;
81 for (i = 2; i <= j; i++) {
82 work[i] = beta[i] * beta[i];
83 }
84 nevals = 1;
85 long_j = j;
86
87 /* save the previously computed evec in case it's better */
88 cpvec(&(work[6 * j]), 1, j, s);
89 prev_resid = residual;
90
91 tinvit(&long_j, &long_j, &(alpha[1]), &(beta[1]), &(work[1]), &nevals, &(w[1]), &(index[1]),
92 &(s[1]), &ierr, &(work[j + 1]), &(work[(2 * j) + 1]), &(work[(3 * j) + 1]),
93 &(work[(4 * j) + 1]), &(work[(5 * j) + 1]));
94
95 /* fix up sign if needed */
96 if (s[j] < 0) {
97 for (i = 1; i <= j; i++) {
98 s[i] = -s[i];
99 }
100 }
101
102 if (ierr != 0) {
103 residual = DOUBLE_MAX;
104 /* ... don't want to use evec since it is set to zero */
105 }
106
107 else {
108 temp = (alpha[1] - ritz) * s[1] + beta[2] * s[2];
109 residual = temp * temp;
110 for (i = 2; i < j; i++) {
111 temp = beta[i] * s[i - 1] + (alpha[i] - ritz) * s[i] + beta[i + 1] * s[i + 1];
112 residual += temp * temp;
113 }
114 temp = beta[j] * s[j - 1] + (alpha[j] - ritz) * s[j];
115 residual += temp * temp;
116 residual = sqrt(residual);
117 /* tinvit normalizes, so we don't need to. */
118 }
119
120 /* restore previous evec if it had a better residual */
121 if (prev_resid < residual) {
122 residual = prev_resid;
123 cpvec(s, 1, j, &(work[6 * j]));
124 SRES_SWITCHES++; /* count since switching back as well */
125 }
126
127 frvec(work, 1);
128 }
129
130 return (residual);
131 }
132
133 /* Keep this code around in case we have problems with the
134 bidirectional recurrence. */
135
136 /* Backward recurrence
137 s[j] = 1.0;
138
139 if (j == 1) {
140 residual = (alpha[1] - ritz);
141 }
142
143 if (j >= 2) {
144 s[j - 1] = -(alpha[j] - ritz) / beta[j];
145 for (i = j; i >= 3; i--) {
146 s[i-2] = -((alpha[i - 1] - ritz) * s[i - 1]
147 + beta[i] * s[i]) / beta[i - 1];
148 }
149 residual = (alpha[1] - ritz) * s[1] + beta[2] * s[2];
150 }
151 residual = fabs(residual) / ch_normalize(s,1,j);
152 */
153
154 /* Forward recurrence
155 s[1] = 1.0;
156
157 if (j == 1) {
158 residual = (alpha[1] - ritz);
159 }
160
161 if (j >= 2) {
162 s[2] = -(alpha[1] - ritz) / beta[2];
163 for (i = 3; i <= j; i++) {
164 s[i] = -((alpha[i - 1] - ritz) * s[i - 1]
165 + beta[i - 1] * s[i - 2]) / beta[i];
166 }
167 residual = (alpha[j] - ritz) * s[j] + beta[j] * s[j - 1];
168 }
169 residual = fabs(residual) / sign_normalize(s,1,j,j);
170 */