Debian Codegraph @

packages/seacas/libraries/chaco/eigen/tri_solve.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"
10 #include <math.h> // for fabs, sqrt
11 #include <stdio.h> // for printf
12
13 /* Solve the shifted, symmetric tridiagonal linear system (T - lambda*I)v = b
14 where b is a multiple of e1 (the first column of the identity matrix).
15 T is constructed with diagonal alpha[1], alpha[2] ... alpha[j] and
16 off diagonal beta[1], beta[2] ... beta[j-1]. The algorithm is based on
17 p. 156 of Golub and Van Loan, 2nd ed. Modified this to (1) incorporate
18 shift by lambda, (2) use work vectors rather than overwriting so
19 won't have to copy input as this is called repeatedly in the bisection
20 loop for the extended eigenproblem, and (3) to squawk and die if a
21 pivot below machine precision is encountered and (4) exploit the fact
22 that all elements of the rhs except the first are 0. */
23 void tri_solve(double *alpha, /* vector of Lanczos scalars */
24 double *beta, /* vector of Lanczos scalars */
25 int j, /* system size */
26 double lambda, /* diagonal shift */
27 double *v, /* solution vector */
28 double b, /* scalar multiple of e1 specifying the rhs */
29 double *d, /* work vec. for diagonal of Cholesky factor */
30 double *e /* work vec. for off diagonal of Cholesky factor */
31 )
32 {
33 extern int DEBUG_EVECS; /* debug flag for eigen computation */
34 int i; /* loop index */
35 double tol; /* cutoff for numerical singularity */
36 double diff; /* an element of the residual vector */
37 double res; /* the norm of the residual vector */
38
39 /* set cut-off for "zero" pivot */
40 tol = 1.0e-15;
41
42 /* Cholesky factorization of shifted symmetric tridiagonal */
43 d[1] = alpha[1] - lambda;
44 if (fabs(d[1]) < tol) {
45 bail("ERROR: Zero pivot in tri_solve().", 1);
46 }
47 if (j == 1) {
48 v[1] = b / d[1];
49 return;
50 /* It's a scalar equation, so we're done. */
51 }
52 for (i = 2; i <= j; i++) {
53 e[i - 1] = beta[i - 1] / d[i - 1];
54 d[i] = alpha[i] - lambda - d[i - 1] * e[i - 1] * e[i - 1];
55 if (fabs(d[i]) < tol) {
56 bail("ERROR: Zero pivot in tri_solve().", 1);
57 }
58 }
59
60 /* Substitution solution using Cholesky factors */
61 v[1] = b;
62 for (i = 2; i <= j; i++) {
63 v[i] = -e[i - 1] * v[i - 1];
64 }
65 v[j] = v[j] / d[j];
66 for (i = j - 1; i >= 1; i--) {
67 v[i] = v[i] / d[i] - e[i] * v[i + 1];
68 }
69
70 /* Check residual */
71 if (DEBUG_EVECS > 1) {
72 diff = b - ((alpha[1] - lambda) * v[1] + beta[1] * v[2]);
73 res = diff * diff;
74 for (i = 2; i <= j - 1; i++) {
75 diff = beta[i - 1] * v[i - 1] + (alpha[i] - lambda) * v[i] + beta[i] * v[i + 1];
76 res += diff * diff;
77 }
78 diff = beta[j - 1] * v[j - 1] + (alpha[j] - lambda) * v[j];
79 res += diff * diff;
80 res = sqrt(res);
81 if (res > 1.0e-13) {
82 printf("Tridiagonal solve residual %g\n", res);
83 }
84 }
85 }