/* nag_complex_sparse_eigensystem_init (f12anc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2005.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <nag_string.h>
#include <stdio.h>
#include <naga02.h>
#include <nagf12.h>
#include <nagf16.h>

static void av(Integer, Complex *, Complex *);
static void tv(Integer, Complex *, Complex *);


int main(void)
{
  /* Constants */
  Integer  imon = 0;
  /* Scalars */
  Complex  sigma;
  double   estnrm;
  Integer  exit_status, i, irevcm, lcomm, licomm, 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;

  /* Assign to Complex type using nag_complex (a02bac) */
  sigma = nag_complex(0.0, 0.0);
  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_complex_sparse_eigensystem_init (f12anc) Example "
          "Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%ld%*[^\n] ", &nx, &nev, &ncv);
  n = nx * nx;
  /* Allocate memory */
  if (!(eigv = NAG_ALLOC(ncv, Complex)) ||
      !(eigest = NAG_ALLOC(ncv, Complex)) ||
      !(resid = NAG_ALLOC(n, Complex)) ||
      !(v = NAG_ALLOC(n * ncv, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  /* Initialise communication arrays for problem using
     nag_complex_sparse_eigensystem_init (f12anc).
     The first call sets lcomm = licomm = -1 to perform a workspace
     query. */
  lcomm = licomm = -1;
  if (!(comm = NAG_ALLOC(1, Complex)) ||
      !(icomm = NAG_ALLOC(1, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  nag_complex_sparse_eigensystem_init(n, nev, ncv, icomm, licomm,
                                      comm, lcomm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_complex_sparse_eigensystem_init (f12anc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  lcomm = (Integer)comm[0].re;
  licomm = icomm[0];
  NAG_FREE(comm);
  NAG_FREE(icomm);
  if (!(comm = NAG_ALLOC(lcomm, Complex)) ||
      !(icomm = NAG_ALLOC(licomm, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  nag_complex_sparse_eigensystem_init(n, nev, ncv, icomm, licomm,
                                      comm, lcomm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_complex_sparse_eigensystem_init (f12anc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  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 (fail.code != NE_NOERROR)
    {
      printf("Error from nag_complex_sparse_eigensystem_iter (f12apc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  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);
          if (fail.code != NE_NOERROR)
            {
              printf("Error from nag_complex_sparse_eigensystem_monit"
                      " (f12asc).\n%s\n", fail.message);
              exit_status = 1;
              goto END;
            }
          printf("Iteration %3ld, ", niter);
          printf(" No. converged = %3ld,", nconv);
          printf(" norm of estimates = %17.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);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_complex_sparse_eigensystem_sol "
                  "(f12aqc).\n%s\n", fail.message);
          exit_status = 1;
          goto END;
        }

      printf("\n The %ld Ritz values", nconv);
      printf(" of largest magnitude are:\n\n");
      for (i = 0; i <= nconv-1; ++i)
        {
          printf("%8ld%5s(%12.4f, %12.4f)\n", i+1, "",
                  eigv[i].re, eigv[i].im);
        }
    }
  else
    {
      printf("Error from nag_complex_sparse_eigensystem_iter "
              "(f12apc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
 END:
  NAG_FREE(comm);
  NAG_FREE(eigv);
  NAG_FREE(eigest);
  NAG_FREE(resid);
  NAG_FREE(v);
  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 */