/* nag_sparseig_feast_real_gen_solve (f12jkc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.1, 2024.
*/
#include <math.h>
#include <nag.h>
#define X(I, J) x[(J - 1) * pdx + I - 1]
#define Y(I, J) y[(J - 1) * pdy + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
int main(void) {
/* Scalars */
Integer exit_status = 0;
Integer i, j, m, n, pdx, pdy, pdz, m0, iter, nconv, irevcm, exit_loop,
nnz, nnzz, nnzzh, npivm, itn, nnzc, nnzch, la;
double r, eps, tol, rnorm;
Complex ze, emid, cone;
/* Arrays */
double *a = 0, *x = 0, *z = 0;
Complex *w = 0, *d = 0;
double *resid = 0;
Complex *y = 0, *az = 0, *azh = 0;
Integer *ipiv = 0, *irow = 0, *icol = 0, *icolz = 0, *irowz = 0, *istr = 0,
*ipivp = 0, *ipivq = 0, *idiag = 0, *istrh = 0,
*idiagh = 0, *icolzh = 0, *ipivph = 0, *ipivqh = 0, *irowzh = 0;
void *handle = 0;
/* Nag Types */
Nag_OrderType order = Nag_ColMajor;
NagError fail;
INIT_FAIL(fail);
/* Output preamble */
printf("nag_sparseig_feast_real_gen_solve (f12jkc) ");
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);
scanf("%" NAG_IFMT "%*[^\n]", &nnz);
pdx = n;
pdy = n;
pdz = n;
m0 = n;
m = MIN(n, 50);
la = 2 * (nnz + n);
cone = nag_complex_create(1.0, 0.0);
tol = sqrt(X02AJC);
/* Allocate memory */
if (!(a = NAG_ALLOC(nnz, double)) || !(icol = NAG_ALLOC(nnz, Integer)) ||
!(irow = NAG_ALLOC(nnz, Integer)) || !(x = NAG_ALLOC(pdx * m0, double)) ||
!(y = NAG_ALLOC(pdy * 2 * m0, Complex)) ||
!(z = NAG_ALLOC(pdz * m0, double)) ||
!(resid = NAG_ALLOC(2 * m0, double)) || !(d = NAG_ALLOC(m0, Complex)) ||
!(ipiv = NAG_ALLOC(n, Integer)) || !(w = NAG_ALLOC(n, Complex)) ||
!(az = NAG_ALLOC(la, Complex)) || !(azh = NAG_ALLOC(la, Complex)) ||
!(icolz = NAG_ALLOC(la, Integer)) || !(irowz = NAG_ALLOC(la, Integer)) ||
!(icolzh = NAG_ALLOC(la, Integer)) ||
!(irowzh = NAG_ALLOC(la, Integer)) ||
!(idiag = NAG_ALLOC(n, Integer)) || !(ipivp = NAG_ALLOC(n, Integer)) ||
!(ipivq = NAG_ALLOC(n, Integer)) || !(ipivph = NAG_ALLOC(n, Integer)) ||
!(ipivqh = NAG_ALLOC(n, Integer)) ||
!(istrh = NAG_ALLOC(n + 1, Integer)) ||
!(idiagh = NAG_ALLOC(n, Integer)) ||
!(istr = NAG_ALLOC(n + 1, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the matrix A from data file */
for (i = 0; i < nnz; i++)
scanf("%lf"
"%" NAG_IFMT "%" NAG_IFMT "%*[^\n]",
&a[i], &irow[i], &icol[i]);
/* 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, "Ellipse Rotation Angle = 42", &fail);
nag_sparseig_feast_option(handle, "Ellipse Contour Ratio = 20", &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_option (12jbc)\n%s\n", fail.message);
exit_status = 2;
goto END;
}
emid = nag_complex_create(2.5, 1.2);
r = 1.4;
/* Generate the contour using nag_sparseig_feast_gen_contour (f12jfc) */
nag_sparseig_feast_gen_contour(handle, emid, r, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_gen_contour (f12jfc)\n%s\n",
fail.message);
exit_status = 3;
goto END;
}
exit_loop = 0;
irevcm = 0;
do {
/* Call solver nag_sparseig_feast_real_gen_solve (f12jkc) */
nag_sparseig_feast_real_gen_solve(handle, &irevcm, &ze, n, x, pdx, y, pdy,
&m0, &nconv, d, z, pdz, &eps, &iter,
resid, &fail);
switch (irevcm) {
case 1:
/* Form the sparse matrix ze I - A */
nnzz = nnz + n;
for (i = 0; i < nnz; i++) {
az[i] = nag_complex_create(-a[i], 0.0);
irowz[i] = irow[i];
icolz[i] = icol[i];
}
for (i = 0; i < n; i++) {
irowz[nnz + i] = i + 1;
icolz[nnz + i] = i + 1;
az[nnz + i] = ze;
}
/* Sort the elements into correct coordinate storage format using
* nag_sparse_complex_gen_sort (f11znc)
*/
nag_sparse_complex_gen_sort(n, &nnzz, az, irowz, icolz,
Nag_SparseNsym_SumDups,
Nag_SparseNsym_RemoveZeros, istr, &fail);
/* Form incomplete LU factorization of ze I - A using
* nag_sparse_complex_gen_precon_ilu (f11dnc)
*/
nag_sparse_complex_gen_precon_ilu(n, nnzz, az, la, irowz, icolz, 0, 0.0,
Nag_SparseNsym_PartialPiv,
Nag_SparseNsym_UnModFact, ipivp, ipivq,
istr, idiag, &nnzc, &npivm, &fail);
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 2:
/* Solve the linear system (ze I - A)w = y, with m0 righthand sides */
for (j = 1; j <= m0; j++) {
for (i = 1; i <= n; i++) {
w[i - 1] = Y(i, j);
/* Initial guess */
Y(i, j) = cone;
}
/* Call linear system solver for a single righthand side
* nag_sparse_complex_gen_solve_ilu (f11dqc)
*/
nag_sparse_complex_gen_solve_ilu(
Nag_SparseNsym_RGMRES, n, nnzz, az, la, irowz, icolz, ipivp, ipivq,
istr, idiag, w, m, tol, 500, &Y(1, j), &rnorm, &itn, &fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 3:
/* Form the sparse matrix (ze I - A)^H */
nnzzh = nnz + n;
for (i = 0; i < nnz; i++) {
azh[i] = nag_complex_create(-a[i], 0.0);
irowzh[i] = icol[i];
icolzh[i] = irow[i];
}
for (i = 0; i < n; i++) {
irowzh[nnz + i] = i + 1;
icolzh[nnz + i] = i + 1;
azh[nnz + i] = nag_complex_conjg(ze);
}
/* Sort the elements into correct coordinate storage format using
* nag_sparse_complex_gen_sort (f11znc)
*/
nag_sparse_complex_gen_sort(n, &nnzzh, azh, irowzh, icolzh,
Nag_SparseNsym_SumDups,
Nag_SparseNsym_RemoveZeros, istrh, &fail);
/* Form incomplete LU factorization of (ze I - A)^H using
* nag_sparse_complex_gen_precon_ilu (f11dnc)
*/
nag_sparse_complex_gen_precon_ilu(
n, nnzzh, azh, la, irowzh, icolzh, 0, 0.0, Nag_SparseNsym_PartialPiv,
Nag_SparseNsym_UnModFact, ipivph, ipivqh, istrh, idiagh, &nnzch,
&npivm, &fail);
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 4:
/* Solve the linear system (ze I - A)^H w = y, with m0 righthand sides */
for (j = 1; j <= m0; j++) {
for (i = 1; i <= n; i++) {
w[i - 1] = Y(i, j);
/* Initial guess*/
Y(i, j) = cone;
}
/* Call linear system solver for a single righthand side
* nag_sparse_complex_gen_solve_ilu (f11dqc)
*/
nag_sparse_complex_gen_solve_ilu(Nag_SparseNsym_RGMRES, n, nnzzh, azh,
la, irowzh, icolzh, ipivph, ipivqh,
istrh, idiagh, w, m, tol, 500,
&Y(1, j), &rnorm, &itn, &fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 5:
/* Compute x <- Az */
for (j = 1; j <= m0; j++) {
nag_sparse_real_gen_matvec(Nag_NoTrans, n, nnz, a, irow, icol,
Nag_SparseNsym_NoCheck, &Z(1, j), &X(1, j),
&fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 6:
/* Compute x <- A^T z */
for (j = 1; j <= m0; j++) {
nag_sparse_real_gen_matvec(Nag_Trans, n, nnz, a, irow, icol,
Nag_SparseNsym_NoCheck, &Z(1, j), &X(1, j),
&fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 7:
/* 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;
case 8:
/* 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) {
if (d[i].im == 0.0)
printf("%13.4e%s", d[i].re, (i + 1) % 4 == 0 ? "\n" : " ");
else
printf(" (%13.4e, %13.4e)%s", d[i].re, d[i].im,
(i + 1) % 4 == 0 ? "\n" : " ");
}
printf("\n\n");
/* Print eigenvectors using nag_file_print_matrix_complex_gen (x04dac) */
nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
n, nconv, y, pdy, "Right Eigenvectors",
NULL, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen (x04dac)\n%s\n",
fail.message);
exit_status = 5;
goto END;
}
nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
n, nconv, &y[m0 * pdz], pdy,
"Left Eigenvectors", NULL, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen (x04dac)\n%s\n",
fail.message);
exit_status = 6;
goto END;
}
END:
NAG_FREE(a);
NAG_FREE(w);
NAG_FREE(az);
NAG_FREE(azh);
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(z);
NAG_FREE(resid);
NAG_FREE(d);
NAG_FREE(ipiv);
NAG_FREE(icolz);
NAG_FREE(irowz);
NAG_FREE(icol);
NAG_FREE(irow);
NAG_FREE(icolzh);
NAG_FREE(irowzh);
NAG_FREE(idiag);
NAG_FREE(idiagh);
NAG_FREE(ipivp);
NAG_FREE(ipivph);
NAG_FREE(ipivq);
NAG_FREE(ipivqh);
NAG_FREE(idiagh);
NAG_FREE(istr);
NAG_FREE(istrh);
/* 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 = 7;
}
return exit_status;
}