/* nag_sparseig_feast_poly_gen_solve (f12jvc) Example Program.
*
* Copyright 2021 Numerical Algorithms Group.
*
* Mark 27.3, 2021.
*/
#include <math.h>
#include <nag.h>
#define A(I, J) a[I + J * nnzmax]
#define IROW(I, J) irow[I + J * nnzmax]
#define ICOL(I, J) icol[I + J * nnzmax]
#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, k, pdx, pdy, pdz, m0, iter, nconv, irevcm,
exit_loop, nnzz, nnzzh, npivm, itn, nnzc, nnzch, ccn, deg, nnzmax, nnzsum,
ind, la;
double eps, tol, rnorm;
Complex ze, cone;
/* Arrays */
Complex *a = 0, *x = 0, *z = 0, *w = 0, *zedge = 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,
*nedge = 0, *tedge = 0, *nnza = 0;
void *handle = 0;
/* Nag Types */
Nag_OrderType order = Nag_ColMajor;
NagError fail;
INIT_FAIL(fail);
/* Output preamble */
printf("nag_sparseig_feast_poly_gen_solve (f12jvc) ");
printf("Example Program Results\n\n");
fflush(stdout);
/* Skip heading in data file */
scanf("%*[^\n] ");
/* Read in the matrix size and the degree */
scanf("%" NAG_IFMT "", &n);
scanf("%" NAG_IFMT "", °);
scanf("%*[^\n]");
/* Initial memory allocation */
if (!(nnza = NAG_ALLOC(deg + 1, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
nnzmax = 0;
nnzsum = 0;
for (i = 0; i < deg + 1; i++) {
scanf("%" NAG_IFMT "", &nnza[i]);
nnzsum += nnza[i];
nnzmax = MAX(nnzmax, nnza[i]);
}
scanf("%*[^\n]");
pdx = n;
pdy = n;
pdz = n;
m0 = n;
m = MIN(n, 50);
la = 2 * (nnzsum);
tol = sqrt(X02AJC);
cone = nag_complex_create(1.0, 0.0);
/* Memory allocation */
if (!(a = NAG_ALLOC(nnzmax * (deg + 1), Complex)) ||
!(icol = NAG_ALLOC(nnzmax * (deg + 1), Integer)) ||
!(irow = NAG_ALLOC(nnzmax * (deg + 1), Integer)) ||
!(x = NAG_ALLOC(pdx * 2 * m0, Complex)) ||
!(y = NAG_ALLOC(pdy * m0, Complex)) ||
!(z = NAG_ALLOC(pdz * 2 * m0, Complex)) ||
!(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 = -2;
goto END;
}
/* Read in the matrices from data file */
for (j = 0; j < deg + 1; j++) {
for (i = 0; i < nnza[j]; i++)
scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &A(i, j).re,
&A(i, j).im, &IROW(i, j), &ICOL(i, j));
}
/* 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_fesat_init (f12jac)\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Set option using nag_sparseig_feast_option (f12jbc) */
nag_sparseig_feast_option(handle, "Integration Type = Trapezoidal", &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n", fail.message);
exit_status = 2;
goto END;
}
/* Define arrays for the custom contour */
ccn = 2;
if (!(nedge = NAG_ALLOC(ccn, Integer)) || !(tedge = NAG_ALLOC(ccn, Integer)) ||
!(zedge = NAG_ALLOC(ccn, Complex))) {
exit_status = -3;
goto END;
}
zedge[0] = nag_complex_create(-1.5, -0.5);
zedge[1] = nag_complex_create(0.5, 1.5);
tedge[0] = 100;
tedge[1] = 0;
nedge[0] = 20;
nedge[1] = 10;
/* Generate the contour using nag_sparseig_feast_custom_contour (f12jgc) */
nag_sparseig_feast_custom_contour(handle, ccn, nedge, tedge, zedge, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparseig_feast_custom_contour (f12jgc)\n%s\n",
fail.message);
exit_status = 3;
goto END;
}
exit_loop = 0;
irevcm = 0;
do {
/* Call solver nag_sparseig_feast_poly_gen_solve (f12jrc) */
nag_sparseig_feast_poly_gen_solve(handle, &irevcm, deg, &ze, n, x, pdx, y,
pdy, &k, &m0, &nconv, d, z, pdz, &eps,
&iter, resid, &fail);
switch (irevcm) {
case 1:
/* Form the sparse matrix \sum ze^i A_i */
nnzz = nnzsum;
ind = 0;
for (i = 0; i < deg + 1; i++) {
for (j = 0; j < nnza[i]; j++) {
az[ind] = nag_complex_multiply(nag_complex_i_power(ze, i), A(j, i));
irowz[ind] = IROW(j, i);
icolz[ind] = ICOL(j, i);
ind++;
}
}
/* 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 \sum ze^i A_i 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 (\sum ze^i A_i) 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 (\sum ze^i A_i)^H */
nnzzh = nnzsum;
ind = 0;
for (i = 0; i < deg + 1; i++) {
for (j = 0; j < nnza[i]; j++) {
azh[ind] = nag_complex_conjg(
nag_complex_multiply(nag_complex_i_power(ze, i), A(j, i)));
irowzh[ind] = ICOL(j, i);
icolzh[ind] = IROW(j, i);
ind++;
}
}
/* 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 (\sum ze^i A_i)^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 (\sum ze^i A_i)^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 <- A_k z */
for (j = 1; j <= m0; j++) {
nag_sparse_complex_gen_matvec(
Nag_NoTrans, n, nnza[k], &A(0, k), &IROW(0, k), &ICOL(0, k),
Nag_SparseNsym_NoCheck, &Z(1, j), &X(1, j), &fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 6:
/* Compute x <- (A_k)^H z */
for (j = 1; j <= m0; j++) {
nag_sparse_complex_gen_matvec(
Nag_ConjTrans, n, nnza[k], &A(0, k), &IROW(0, k), &ICOL(0, k),
Nag_SparseNsym_NoCheck, &Z(1, j), &X(1, j), &fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
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, z, pdz, "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, &z[m0 * pdz], pdz, "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(nnza);
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(z);
NAG_FREE(resid);
NAG_FREE(d);
NAG_FREE(ipiv);
NAG_FREE(icolz);
NAG_FREE(nedge);
NAG_FREE(tedge);
NAG_FREE(zedge);
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;
}