/* nag_sparseig_feast_complex_herm_solve (f12jrc) 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,
nnza, nnzb, nnzz, nnzzh, npivm, itn, nnzc, nnzch, la;
double eps, tol, rnorm, emin, emax;
Complex ze, cone, czero;
/* Arrays */
Complex *a = 0, *x = 0, *z = 0, *b = 0;
double *resid = 0, *d = 0;
Complex *y = 0, *w = 0, *az = 0, *azh = 0;
Integer *ipiv = 0, *irowa = 0, *irowb = 0, *icola = 0, *icolb = 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_complex_herm_solve (f12jrc) ");
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 "", &nnza);
scanf("%" NAG_IFMT "%*[^\n]", &nnzb);
pdx = n;
pdy = n;
pdz = n;
m0 = n;
m = MIN(n, 50);
la = 4 * (nnza + nnzb);
tol = sqrt(X02AJC);
cone = nag_complex_create(1.0, 0.0);
czero = nag_complex_create(0.0, 0.0);
/* Allocate memory */
if (!(a = NAG_ALLOC(nnza, Complex)) || !(b = NAG_ALLOC(nnzb, Complex)) ||
!(icola = NAG_ALLOC(nnza, Integer)) ||
!(icolb = NAG_ALLOC(nnzb, Integer)) ||
!(irowa = NAG_ALLOC(nnza, Integer)) ||
!(irowb = NAG_ALLOC(nnzb, Integer)) ||
!(x = NAG_ALLOC(pdx * m0, Complex)) ||
!(y = NAG_ALLOC(pdy * m0, Complex)) ||
!(z = NAG_ALLOC(pdz * m0, Complex)) || !(resid = NAG_ALLOC(m0, double)) ||
!(d = NAG_ALLOC(m0, double)) || !(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 < nnza; i++)
scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i].re,
&a[i].im, &irowa[i], &icola[i]);
scanf("%*[^\n] ");
/* Read in the matrix B from data file */
for (i = 0; i < nnzb; i++)
scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &b[i].re,
&b[i].im, &irowb[i], &icolb[i]);
/* Initialize data the 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, "Integration Type = Zolotarev", &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 = -1.0;
emax = 0.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;
}
exit_loop = 0;
irevcm = 0;
do {
/* Call solver nag_sparseig_feast_complex_herm_solve (f12jrc) */
nag_sparseig_feast_complex_herm_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 B-A */
nnzz = 2 * (nnza + nnzb);
/* We store it in the arrays az, irowz and icolz */
for (i = 0; i < nnza; i++) {
az[i] = nag_complex_subtract(czero, a[i]);
irowz[i] = irowa[i];
icolz[i] = icola[i];
az[nnza + i] = nag_complex_subtract(czero, a[i]);
irowz[nnza + i] = icola[i];
icolz[nnza + i] = irowa[i];
}
/* Add the elements of ze B */
for (i = 0; i < nnzb; i++) {
irowz[2 * nnza + i] = irowb[i];
icolz[2 * nnza + i] = icolb[i];
az[2 * nnza + i] = nag_complex_multiply(ze, b[i]);
irowz[2 * nnza + nnzb + i] = icolb[i];
icolz[2 * nnza + nnzb + i] = irowb[i];
az[2 * nnza + nnzb + i] =
nag_complex_multiply(nag_complex_conjg(ze), b[i]);
}
/* 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);
/* We have double counted the diagonal elements so halve them */
for (i = 0; i < nnzz; i++) {
if (irowz[i] == icolz[i]) {
az[i].re = 0.5 * az[i].re;
az[i].im = 0.5 * az[i].im;
}
}
/* Form incomplete LU factorization of ze B - 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 B - 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 B-A)^H */
nnzzh = 2 * (nnza + nnzb);
/* We store it in the arrays azh, irowzh and icolzh */
for (i = 0; i < nnza; i++) {
azh[i] = nag_complex_subtract(czero, a[i]);
irowzh[i] = irowa[i];
icolzh[i] = icola[i];
azh[nnza + i] = nag_complex_subtract(czero, a[i]);
irowzh[nnza + i] = icola[i];
icolzh[nnza + i] = irowa[i];
}
/* Add the elements of (ze B)^H */
for (i = 0; i < nnzb; i++) {
irowzh[2 * nnza + i] = irowb[i];
icolzh[2 * nnza + i] = icolb[i];
azh[2 * nnza + i] = nag_complex_multiply(nag_complex_conjg(ze), b[i]);
irowzh[2 * nnza + nnzb + i] = icolb[i];
icolzh[2 * nnza + nnzb + i] = irowb[i];
azh[2 * nnza + nnzb + i] = nag_complex_multiply(ze, b[i]);
}
/* 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);
/* We have double counted the diagonal elements so halve them */
for (i = 0; i < nnzzh; i++) {
if (irowzh[i] == icolzh[i]) {
azh[i].re = 0.5 * azh[i].re;
azh[i].im = 0.5 * azh[i].im;
}
}
/* Form incomplete LU factorization of ze B - A 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 B - 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, 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_complex_herm_matvec(n, nnza, a, irowa, icola,
Nag_SparseSym_NoCheck, &Z(1, j),
&X(1, j), &fail);
}
if (fail.code != NE_NOERROR) {
exit_loop = 1;
}
break;
case 6:
/* Compute x <- Bz */
for (j = 1; j <= m0; j++) {
nag_sparse_complex_herm_matvec(n, nnzb, b, irowb, icolb,
Nag_SparseSym_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)
printf("%8.4f%s", d[i], (i + 1) % 8 == 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, "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;
}
END:
NAG_FREE(a);
NAG_FREE(b);
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(icola);
NAG_FREE(irowa);
NAG_FREE(icolb);
NAG_FREE(irowb);
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 = 6;
}
return exit_status;
}