Debian Codegraph @

packages/seacas/libraries/chaco/eigen/bidir.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 "defs.h" // for FALSE, TRUE
10 #include <math.h> // for fabs
11
12 /* NOTE: This should only be called if j >= 2. It returns a residual and
13 the point where the forward and backward recurrences met. */
14
15 /* NOTE: The paper has an error in the residual calculation. And the
16 heuristic of increasing the cut-off for the local maximum
17 by a factor of 10 when the residual tolerance is not met
18 appears to work poorly - letting the recursion run further
19 to find a bigger local max often results in <less> accurate
20 results. Static cut-off of 1 (recall we set the first element
21 to 1) worked better. */
22
23 /* Finds eigenvector s of T and returns residual norm. */
24 double bidir(double *alpha, /* vector of Lanczos scalars */
25 double *beta, /* vector of Lanczos scalars */
26 int j, /* number of Lanczos iterations taken */
27 double ritz, /* approximate eigenvalue of T */
28 double *s, /* approximate eigenvector of T */
29 double hurdle /* hurdle for local maximum in recurrence */
30 )
31
32 {
33 int i; /* index */
34 int k; /* meeting point for bidirect. recurrence */
35 double sav_val; /* value at meeting point */
36 int iterate; /* keep iterating? */
37 double scale; /* scale factor for bidirectional recurrence */
38 double residual; /* eigen residual */
39
40 s[2] = -(alpha[1] - ritz) / beta[2];
41 i = 3;
42 iterate = TRUE;
43 while (i <= j && iterate) {
44 /* follow the forward recurrence until a local maximum > 1.0 */
45 s[i] = -((alpha[i - 1] - ritz) * s[i - 1] + beta[i - 1] * s[i - 2]) / beta[i];
46 if (fabs(s[i - 1]) > hurdle) {
47 if (fabs(s[i]) < fabs(s[i - 1]) && fabs(s[i - 1]) > fabs(s[i - 2])) {
48 iterate = FALSE;
49 k = i - 1;
50 sav_val = s[k];
51 }
52 }
53 i++;
54 }
55 if (!iterate) {
56 /* we stopped at an intermediate point */
57 s[j] = 1.0;
58 s[j - 1] = -(alpha[j] - ritz) / beta[j];
59 for (i = j; i >= k + 2; i--) {
60 s[i - 2] = -((alpha[i - 1] - ritz) * s[i - 1] + beta[i] * s[i]) / beta[i - 1];
61 }
62 scale = s[k] / sav_val;
63 for (i = 1; i <= k - 1; i++) {
64 s[i] = s[i] * scale;
65 }
66 /* paper gets this expression wrong */
67 residual = beta[k] * s[k - 1] + (alpha[k] - ritz) * s[k] + beta[k + 1] * s[k + 1];
68 }
69 else {
70 /* we went all the way forward */
71 residual = (alpha[j] - ritz) * s[j] + beta[j] * s[j - 1];
72 }
73 residual = fabs(residual) / sign_normalize(s, 1, j, j);
74 return (residual);
75 }