/* nag_lapackeig_dsbev (f08hac) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.0, 2024.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
double eerrbd, eps;
Integer exit_status = 0, i, j, kd, n, pdab, pdz;
/* Arrays */
char nag_enum_arg[40];
double *ab = 0, *rcondz = 0, *w = 0, *z = 0, *zerrbd = 0;
/* Nag Types */
Nag_OrderType order;
Nag_UploType uplo;
NagError fail;
#ifdef NAG_COLUMN_MAJOR
#define AB_UPPER(I, J) ab[(J - 1) * pdab + kd + I - J]
#define AB_LOWER(I, J) ab[(J - 1) * pdab + I - J]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
order = Nag_ColMajor;
#else
#define AB_UPPER(I, J) ab[(I - 1) * pdab + J - I]
#define AB_LOWER(I, J) ab[(I - 1) * pdab + kd + J - I]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_lapackeig_dsbev (f08hac) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &kd);
/* Read uplo */
scanf("%39s%*[^\n]", nag_enum_arg);
/* nag_enum_name_to_value (x04nac).
* Converts NAG enum member name to value
*/
uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);
/* Allocate memory */
if (!(ab = NAG_ALLOC((kd + 1) * n, double)) ||
!(rcondz = NAG_ALLOC(n, double)) || !(w = NAG_ALLOC(n, double)) ||
!(z = NAG_ALLOC(n * n, double)) || !(zerrbd = NAG_ALLOC(n, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
pdab = kd + 1;
pdz = n;
/* Read the upper or lower triangular part of the symmetric band
* matrix A from data file.
*/
if (uplo == Nag_Upper) {
for (i = 1; i <= n; ++i)
for (j = i; j <= MIN(n, i + kd); ++j)
scanf("%lf", &AB_UPPER(i, j));
scanf("%*[^\n]");
} else if (uplo == Nag_Lower) {
for (i = 1; i <= n; ++i)
for (j = MAX(1, i - kd); j <= i; ++j)
scanf("%lf", &AB_LOWER(i, j));
scanf("%*[^\n]");
}
/* nag_lapackeig_dsbev (f08hac).
* Solve the band symmetric eigenvalue problem.
*/
nag_lapackeig_dsbev(order, Nag_DoBoth, uplo, n, kd, ab, pdab, w, z, pdz,
&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_dsbev (f08hac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Normalize the eigenvectors */
for (j = 1; j <= n; j++)
for (i = n; i >= 1; i--)
Z(i, j) = Z(i, j) / Z(1, j);
/* Print solution */
printf("Eigenvalues\n");
for (j = 0; j < n; ++j)
printf("%8.4f%s", w[j], (j + 1) % 8 == 0 ? "\n" : " ");
printf("\n");
/* nag_file_print_matrix_real_gen (x04cac).
* Print eigenvectors.
*/
fflush(stdout);
nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
n, z, pdz, "Eigenvectors", 0, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Get the machine precision, eps, using nag_machine_precision (X02AJC)
* and compute the approximate error bound for the computed eigenvalues.
* Note that for the 2-norm, ||A|| = max {|w[i]|, i=0..n-1}, and since
* the eigenvalues are in ascending order ||A|| = max( |w[0]|, |w[n-1]|).
*/
eps = nag_machine_precision;
eerrbd = eps * MAX(fabs(w[0]), fabs(w[n - 1]));
/* nag_lapackeig_ddisna (f08flc).
* Estimate reciprocal condition numbers for eigenvectors.
*/
nag_lapackeig_ddisna(Nag_EigVecs, n, n, w, rcondz, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_ddisna (f08flc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Compute the error estimates for the eigenvectors. */
for (i = 0; i < n; ++i)
zerrbd[i] = eerrbd / rcondz[i];
/* Print the approximate error bounds for the eigenvalues and vectors. */
printf("\nError estimate for the eigenvalues\n");
printf("%11.1e\n", eerrbd);
printf("\nError estimates for the eigenvectors\n");
for (i = 0; i < n; ++i)
printf("%11.1e%s", zerrbd[i], (i + 1) % 6 == 0 || i == n - 1 ? "\n" : " ");
END:
NAG_FREE(ab);
NAG_FREE(rcondz);
NAG_FREE(w);
NAG_FREE(z);
NAG_FREE(zerrbd);
return exit_status;
}
#undef AB_UPPER
#undef AB_LOWER
#undef Z