/* nag_sparseig_feast_real_symm_solve (f12jjc) Example Program.
*
* Copyright 2022 Numerical Algorithms Group.
*
* Mark 28.6, 2022.
*/
#include <nag.h>
#define A(I, J) a[(J - 1) * pda + I - 1]
#define AZ(I, J) az[(J - 1) * n + I - 1]
#define X(I, J) x[(J - 1) * pdx + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
int main(void) {
/* Scalars */
Integer exit_status = 0;
Integer i, j, n, pda, pdx, pdy, pdz, m0, iter, nconv, irevcm, exit_loop;
double emin, emax, eps;
Complex ze;
/* Arrays */
double *a = 0, *x = 0, *z = 0, *resid = 0, *d = 0;
Complex *y = 0, *az = 0;
Integer *ipiv = 0;
void *handle = 0;
/* Nag Types */
Nag_OrderType order = Nag_ColMajor;
NagError fail;
INIT_FAIL(fail);
/* Output preamble */
printf("nag_sparseig_feast_real_symm_solve (f12jjc) ");
printf("Example Program Results\n\n");
fflush(stdout);
/* Skip heading in data file */
scanf("%*[^\n] ");
/* Read in the matrix size and the required rank */
scanf("%" NAG_IFMT "%*[^\n]", &n);
pda = n;
pdx = n;
pdy = n;
pdz = n;
m0 = n;
/* Allocate memory */
if (!(a = NAG_ALLOC(pda * n, double)) || !(x = NAG_ALLOC(pdx * m0, double)) ||
!(y = NAG_ALLOC(pdy * m0, Complex)) ||
!(z = NAG_ALLOC(pdz * m0, double)) || !(resid = NAG_ALLOC(m0, double)) ||
!(d = NAG_ALLOC(m0, double)) || !(az = NAG_ALLOC(n * n, Complex)) ||
!(ipiv = NAG_ALLOC(n, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the matrix A from data file */
for (i = 1; i <= n; i++)
for (j = i; j <= n; j++)
scanf("%lf", &A(i, j));
scanf("%*[^\n] ");
/* Initialize the data handle using nag_sparseig_feast_init (f12jac) */
nag_sparseig_feast_init(&handle, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_init (f12jac)\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Set options using nag_sparseig_feast_option (f12jbc) */
nag_sparseig_feast_option(handle, "Contour Points Hermitian = 12", &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n",
fail.message);
exit_status = 2;
goto END;
}
emin = -3.0;
emax = 3.0;
/* Set the contour using nag_sparseig_feast_symm_contour (f12jec) */
nag_sparseig_feast_symm_contour(handle, emin, emax, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_symm_contour (f12jec)\n%s\n",
fail.message);
exit_status = 3;
goto END;
}
/* Initialize az to zero */
for (j = 1; j <= n; j++) {
for (i = 1; i <= j; i++) {
AZ(i, j) = nag_complex_create(0.0, 0.0);
}
}
exit_loop = 0;
irevcm = 0;
do {
/* Call solver nag_sparseig_feast_real_symm_solve (f12jjc) */
nag_sparseig_feast_real_symm_solve(handle, &irevcm, &ze, n, x, pdx, y, pdy,
&m0, &nconv, d, z, pdz, &eps, &iter,
resid, &fail);
switch (irevcm) {
case 1:
/* Form the matrix ze I-A */
for (j = 1; j <= n; j++) {
AZ(j, j).re = ze.re;
AZ(j, j).im = ze.im;
for (i = 1; i <= j; i++) {
AZ(i, j).re = AZ(i, j).re - A(i, j);
}
}
/* Compute Bunch Kaufman factorization of ze I - A */
nag_lapacklin_zsytrf(Nag_ColMajor, Nag_Upper, n, az, n, ipiv, &fail);
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 2:
/* Solve the linear system (ze I -A)w = y, overwriting y with w */
nag_lapacklin_zsytrs(Nag_ColMajor, Nag_Upper, n, m0, az, n, ipiv, y, pdy,
&fail);
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 3:
/* Compute x <- Az */
nag_dsymm(Nag_ColMajor, Nag_LeftSide, Nag_Upper, n, m0, 1.0, a, pda, z,
pdz, 0.0, x, pdx, &fail);
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 4:
/* Since we are not solving a generalized eigenvalue problem set x = z */
for (j = 1; j <= m0; j++) {
for (i = 1; i <= n; i++) {
X(i, j) = Z(i, j);
}
}
break;
}
} while (irevcm != 0 && exit_loop == 0);
if (fail.code != NE_NOERROR) {
printf("Error during reverse communication solve\n%s\n", fail.message);
exit_status = 4;
goto END;
}
/* Print solution */
printf(" Eigenvalues\n");
for (i = 0; i < nconv; ++i)
printf("%8.4f%s", d[i], (i + 1) % 8 == 0 ? "\n" : " ");
printf("\n");
/* Normalize the eigenvectors: first element positive */
for (j = 1; j <= nconv; j++) {
if (Z(1, j) < 0.0) {
for (i = 1; i <= n; i++) {
Z(i, j) = -Z(i, j);
}
}
}
printf("\n");
/* Print eigenvectors using nag_file_print_matrix_real_gen (x04cac) */
nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
nconv, z, pdz, "Eigenvectors", NULL, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_real_gen (x04cac)\n%s\n",
fail.message);
exit_status = 5;
goto END;
}
END:
NAG_FREE(a);
NAG_FREE(az);
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(z);
NAG_FREE(resid);
NAG_FREE(d);
NAG_FREE(ipiv);
/* Destroy the data handle using nag_sparseig_feast_free (f12jzc) */
nag_sparseig_feast_free(&handle, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_free (f12jzc)\n%s\n",
fail.message);
exit_status = 6;
}
return exit_status;
}