/* nag_complex_sparse_eigensystem_init (f12anc) Example Program. * * Copyright 2005 Numerical Algorithms Group. * * Mark 8, 2005. */ #include #include #include #include #include #include #include static void av(Integer, Complex *, Complex *); static void tv(Integer, Complex *, Complex *); int main(void) { /* Constants */ Integer licomm=140, imon=0; /* Scalars */ Complex sigma; double estnrm; Integer exit_status, i, irevcm, lcomm, n, nconv, ncv; Integer nev, niter, nshift, nx; /* Nag types */ NagError fail; /* Arrays */ Complex *comm=0, *eigest=0, *eigv=0, *resid=0, *v=0; Integer *icomm=0; /* Ponters */ Complex *mx=0, *x=0, *y=0; exit_status = 0; INIT_FAIL(fail); Vprintf("nag_complex_sparse_eigensystem_init (f12anc) Example Program " "Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%ld%ld%*[^\n] ", &nx, &nev, &ncv); n = nx * nx; lcomm = 3*n + 3*ncv*ncv + 5*ncv + 60; /* Allocate memory */ if ( !(comm = NAG_ALLOC(lcomm, Complex)) || !(eigv = NAG_ALLOC(ncv, Complex)) || !(eigest = NAG_ALLOC(ncv, Complex)) || !(resid = NAG_ALLOC(n, Complex)) || !(v = NAG_ALLOC(n * ncv, Complex)) || !(icomm = NAG_ALLOC(licomm, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Initialise communication arrays for problem using nag_complex_sparse_eigensystem_init (f12anc). */ nag_complex_sparse_eigensystem_init(n, nev, ncv, icomm, licomm, comm, lcomm, &fail); irevcm = 0; REVCOMLOOP: /* repeated calls to reverse communication routine nag_complex_sparse_eigensystem_iter (f12apc). */ nag_complex_sparse_eigensystem_iter(&irevcm, resid, v, &x, &y, &mx, &nshift, comm, icomm, &fail); if (irevcm != 5 && irevcm != 0) { if (irevcm == -1 || irevcm == 1) { /* Perform matrix vector multiplication y <--- Op*x */ av(nx, x, y); } else if (irevcm == 4 && imon == 1) { /* If imon=1, get monitoring information using nag_complex_sparse_eigensystem_monit (f12asc). */ nag_complex_sparse_eigensystem_monit(&niter, &nconv, eigv, eigest, icomm, comm); /* Compute 2-norm of Ritz estimates using nag_zge_norm (f16uac). */ nag_zge_norm(Nag_ColMajor, Nag_FrobeniusNorm, nev, 1, eigest, nev, &estnrm, &fail); Vprintf("Iteration %3ld, ", niter); Vprintf(" No. converged = %3ld,", nconv); Vprintf(" norm of estimates = %16.8e\n", estnrm); } goto REVCOMLOOP; } if (fail.code == NE_NOERROR) { /* Post-Process using nag_complex_sparse_eigensystem_sol (f12aqc) to compute eigenvalues/vectors. */ nag_complex_sparse_eigensystem_sol(&nconv, eigv, v, sigma, resid, v, comm, icomm, &fail); Vprintf("\n The %ld Ritz values", nconv); Vprintf(" of largest magnitude are:\n\n"); for (i = 0; i <= nconv-1; ++i) { Vprintf("%8ld%5s(%12.4f, %12.4f)\n", i+1, "", eigv[i].re, eigv[i].im); } } else { Vprintf(" Error from nag_complex_sparse_eigensystem_iter (f12apc)." "\n%s\n", fail.message); exit_status = 1; goto END; } END: if (comm) NAG_FREE(comm); if (eigv) NAG_FREE(eigv); if (eigest) NAG_FREE(eigest); if (resid) NAG_FREE(resid); if (v) NAG_FREE(v); if (icomm) NAG_FREE(icomm); return exit_status; } static void av(Integer nx, Complex *x, Complex *y) { /* Scalars */ double hr; Integer i, j, lo; /* Function Body */ /* Allocate memory */ hr = (double) -(nx + 1) * (nx + 1); tv(nx, x, y); for (j = 0; j <= nx - 1; ++j) { y[j].re = y[j].re + hr*x[nx+j].re; y[j].im = y[j].im + hr*x[nx+j].im; } for (j = 2; j <= nx - 1; ++j) { lo = (j - 1) * nx; tv(nx, &x[lo], &y[lo]); for (i = 0; i <= nx - 1; ++i) { y[lo+i].re = y[lo+i].re + hr*(x[lo-nx+i].re+x[lo+nx+i].re); y[lo+i].im = y[lo+i].im + hr*(x[lo-nx+i].im+x[lo+nx+i].im); } } lo = (nx - 1) * nx; tv(nx, &x[lo], &y[lo]); for (j = 0; j <= nx - 1; ++j) { y[lo+j].re = y[lo+j].re + hr*x[lo-nx+j].re; y[lo+j].im = y[lo+j].im + hr*x[lo-nx+j].im; } } /* av */ static void tv(Integer nx, Complex *x, Complex *y) { /* Compute the matrix vector multiplication y<---T*x where T is a */ /* nx by nx tridiagonal matrix. */ /* Scalars */ Complex dd, dl, du, h2, h, rho, z1, z2, z3; Integer j; /* Function Body */ /* Assign to Complex type using nag_complex (a02bac) */ h = nag_complex((double) (nx + 1),0.); /* Compute Complex multiply using nag_complex_multiply (a02ccc). */ h2 = nag_complex_multiply(h, h); dd = nag_complex_multiply(nag_complex(4.0,0.0), h2); z1 = nag_complex_multiply(nag_complex(-1.0, 0.0), h2); /* Assign to Complex type using nag_complex (a02bac) */ rho = nag_complex(1.0e2, 0.0); z2 = nag_complex_multiply(rho, h); z3 = nag_complex_multiply(nag_complex(5.0e-1, 0.0),z2); /* Compute Complex subtraction using nag_complex_subtract (a02cbc). */ dl = nag_complex_subtract(z1, z3); /* Compute Complex addition using nag_complex_add (a02cac). */ du = nag_complex_add(z1, z3); /* Compute Complex multiply using nag_complex_multiply (a02ccc). */ z1 = nag_complex_multiply(dd, x[0]); z2 = nag_complex_multiply(du, x[1]); /* Compute Complex addition using nag_complex_add (a02cac). */ y[0] = nag_complex_add(z1,z2); for (j = 1; j <= nx - 2; ++j) { /* Compute Complex multiply using nag_complex_multiply (a02ccc). */ z1 = nag_complex_multiply(dl, x[j-1]); z2 = nag_complex_multiply(dd, x[j]); z3 = nag_complex_multiply(du, x[j+1]); /* Compute Complex addition using nag_complex_add (a02cac). */ y[j] = nag_complex_add(z1, z2); y[j] = nag_complex_add(y[j], z3); } /* Compute Complex multiply using nag_complex_multiply (a02ccc). */ z1 = nag_complex_multiply(dl, x[nx-2]); z2 = nag_complex_multiply(dd, x[nx-1]); /* Compute Complex addition using nag_complex_add (a02cac). */ y[nx-1] = nag_complex_add(z1, z2); return; } /* tv */