/* nag_lapackeig_zstegr (f08jyc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.2, 2024.
*/
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
double vl = 0.0, vu = 0.0;
Integer i, j, il = 0, iu = 0, m, n, pdz;
Integer exit_status = 0;
/* Arrays */
char nag_enum_arg[40];
Complex *z = 0;
double *d = 0, *e = 0, *w = 0;
Integer *isuppz = 0;
/* Nag Types */
Nag_OrderType order;
Nag_JobType job;
Nag_RangeType range;
NagError fail;
#ifdef NAG_COLUMN_MAJOR
#define Z(I, J) z[(J - 1) * pdz + I - 1]
order = Nag_ColMajor;
#else
#define Z(I, J) z[(I - 1) * pdz + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_lapackeig_zstegr (f08jyc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%*[^\n]", &n);
m = n;
/* Read job and range */
scanf("%39s%*[^\n]", nag_enum_arg);
/* nag_enum_name_to_value (x04nac).
* Converts NAG enum member name to value.
*/
job = (Nag_JobType)nag_enum_name_to_value(nag_enum_arg);
scanf("%39s%*[^\n]", nag_enum_arg);
range = (Nag_RangeType)nag_enum_name_to_value(nag_enum_arg);
#ifdef NAG_COLUMN_MAJOR
pdz = n;
#else
pdz = n;
#endif
/* Allocate memory */
if (!(z = NAG_ALLOC(n * m, Complex)) || !(d = NAG_ALLOC(n, double)) ||
!(e = NAG_ALLOC(n, double)) || !(w = NAG_ALLOC(n, double)) ||
!(isuppz = NAG_ALLOC(2 * m, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read the symmetric tridiagonal matrix T from data file, first
* the diagonal elements, then the off diagonal elements.
*/
for (i = 0; i < n; ++i)
scanf("%lf", &d[i]);
scanf("%*[^\n]");
for (i = 0; i < n - 1; ++i)
scanf("%lf", &e[i]);
scanf("%*[^\n]");
/* nag_lapackeig_zstegr (f08jyc).
* Calculate all the eigenvalues of T.
*/
nag_lapackeig_zstegr(order, job, range, n, d, e, vl, vu, il, iu, &m, w, z,
pdz, isuppz, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_zstegr (f08jyc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_complex_divide (a02cdc).
* Normalize the eigenvectors.
*/
for (j = 1; j <= m; j++)
for (i = n; i >= 1; i--)
Z(i, j) = nag_complex_divide(Z(i, j), Z(1, j));
/* Print eigenvalues and eigenvectors */
printf("%s\n", "Eigenvalues");
for (i = 0; i < m; ++i)
printf("%8.4f%s", w[i], (i + 1) % 8 == 0 ? "\n" : " ");
printf("\n\n");
/* nag_file_print_matrix_complex_gen (x04dac).
* Print eigenvectors.
*/
fflush(stdout);
nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
n, m, z, pdz, "Eigenvectors", 0, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen (x04dac).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
END:
NAG_FREE(z);
NAG_FREE(d);
NAG_FREE(e);
NAG_FREE(w);
NAG_FREE(isuppz);
return exit_status;
}
#undef Z