Debian Codegraph @

packages/seacas/libraries/chaco/eigen/get_extval.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 "prototypes.h"
10 #include <math.h>
11 #include <stdio.h>
12
13 /* Finds first extended eigenpair of system corresponding to
14 tridiagonal T using using Rafael's bisection technique. */
15
16 void get_extval(double *alpha, /* j-vector of Lanczos scalars (using elements 1 to j) */
17 double *beta, /* (j+1)-vector of " " (has 0 element but using 1 to j-1) */
18 int j, /* number of Lanczos iterations taken */
19 double ritzval, /* Ritz value */
20 double *s, /* Ritz vector (length n, re-computed in this routine) */
21 double eigtol, /* tolerance on eigenpair */
22 double wnorm_g, /* W-norm of n-vector g, the rhs in the extended eig. problem */
23 double sigma, /* the norm constraint on the extended eigenvector */
24 double *extval, /* the extended eigenvalue this routine computes */
25 double *v, /* the j-vector solving the extended eig problem in T */
26 double *work1, /* j-vector of workspace */
27 double *work2 /* j-vector of workspace */
28 )
29 {
30 extern int DEBUG_EVECS; /* debug flag for eigen computation */
31 double lambda_low; /* lower bound on extended eval */
32 double lambda_high; /* upper bound on extended eval */
33 double tol; /* bisection tolerance */
34 double norm_v; /* norm of the extended T eigenvector v */
35 double lambda; /* the parameter that iterates to extval */
36 int cnt; /* debug iteration counter */
37 double diff; /* distance between lambda limits */
38
39 /* Compute the Ritz vector */
40 Tevec(alpha, beta - 1, j, ritzval, s);
41
42 /* Shouldn't happen, but just in case ... */
43 if (wnorm_g == 0.0) {
44 *extval = ritzval;
45 cpvec(v, 1, j, s);
46 if (DEBUG_EVECS > 0) {
47 printf("Degenerate extended eigenvector problem (g = 0).\n");
48 }
49 return;
50 /* ... not really an extended eigenproblem; just return Ritz pair */
51 }
52
53 /* Set up the bisection parameters */
54 lambda_low = ritzval - wnorm_g / sigma;
55 lambda_high = ritzval - (wnorm_g / sigma) * s[1];
56 lambda = 0.5 * (lambda_low + lambda_high);
57 tol = eigtol * eigtol * (1 + fabs(lambda_low) + fabs(lambda_high));
58
59 if (DEBUG_EVECS > 2) {
60 printf("Computing extended eigenpairs of T\n");
61 printf(" target norm_v (= sigma) %g\n", sigma);
62 printf(" bisection tolerance %g\n", tol);
63 }
64 if (DEBUG_EVECS > 3) {
65 printf(" lambda iterates to the extended eigenvalue\n");
66 printf(" lambda_low lambda lambda_high norm_v\n");
67 }
68
69 /* Bisection loop - iterate until norm constraint is satisfied */
70 cnt = 1;
71 diff = 2 * tol;
72 while (diff > tol) {
73 lambda = 0.5 * (lambda_low + lambda_high);
74 tri_solve(alpha, beta, j, lambda, v, wnorm_g, work1, work2);
75 norm_v = ch_norm(v, 1, j);
76 if (DEBUG_EVECS > 3) {
77 printf("%2i %18.16f %18.16f %18.16f %g\n", cnt++, lambda_low, lambda, lambda_high,
78 norm_v);
79 }
80 if (norm_v <= sigma) {
81 lambda_low = lambda;
82 }
83 if (norm_v >= sigma) {
84 lambda_high = lambda;
85 }
86 diff = lambda_high - lambda_low;
87 }
88
89 /* Return the extended eigenvalue (eigvec is automatically returned) */
90 *extval = lambda;
91 }