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

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

static void av(Integer, double, double *, double *);
static void mv(Integer, double *, double *);
static void my_dpttrf(Integer, double *, double *, Integer *);
static void my_dpttrs(Integer, double *, double *, double *);

int main(void)
{
  /* Constants */
  Integer  licomm = 140, imon = 0;
  /* Scalars */
  double   estnrm, h, rho, sigmai = 0.0, sigmar = 0.0;
  Integer  exit_status, info, irevcm, j, lcomm, n, nconv, ncv;
  Integer  nev, niter, nshift, nx;
  /* Nag types */
  NagError fail;
  /* Arrays */
  double   *comm = 0, *eigvr = 0, *eigvi = 0, *eigest = 0, *md = 0, *me = 0;
  double   *resid = 0, *v = 0;
  Integer  *icomm = 0;
  /* Pointers */
  double   *mx = 0, *x = 0, *y = 0;

  exit_status = 0;
  INIT_FAIL(fail);

  printf(
          "nag_real_sparse_eigensystem_sol (f12acc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read problem parameter values from data file. */
  scanf("%ld%ld%ld%lf%*[^\n] ", &nx, &nev, &ncv, &rho);
  n = nx * nx;
  lcomm = 3*n + 3*ncv*ncv + 6*ncv + 60;
  /* Allocate memory */
  if (!(comm = NAG_ALLOC(lcomm, double)) ||
      !(eigvr = NAG_ALLOC(ncv, double)) ||
      !(eigvi = NAG_ALLOC(ncv, double)) ||
      !(eigest = NAG_ALLOC(ncv, double)) ||
      !(md = NAG_ALLOC(n, double)) ||
      !(me = NAG_ALLOC(n, double)) ||
      !(resid = NAG_ALLOC(n, double)) ||
      !(v = NAG_ALLOC(n * ncv, double)) ||
      !(icomm = NAG_ALLOC(licomm, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  /* Initialise communication arrays for problem using
     nag_real_sparse_eigensystem_init (f12aac). */
  nag_real_sparse_eigensystem_init(n, nev, ncv, icomm, licomm, comm,
                                   lcomm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf(
              "Error from nag_real_sparse_eigensystem_init (f12aac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  /* Set the mode. */
  /* Select the mode using
     nag_real_sparse_eigensystem_option (f12adc). */
  nag_real_sparse_eigensystem_option("REGULAR INVERSE", icomm, comm,
                                     &fail);
  /* Select the problem type using
     nag_real_sparse_eigensystem_option (f12adc). */
  nag_real_sparse_eigensystem_option("GENERALIZED", icomm, comm, &fail);

  /* Construct M, and factorize using my_dpttrf. */
  h = 1.0 / (double)(n + 1);
  for (j = 0; j <= n - 2; ++j)
    {
      md[j] = h * 4.0;
      me[j] = h;
    }
  md[n - 1] = h * 4.0;

  my_dpttrf(n, md, me, &info);

  irevcm = 0;
 REVCOMLOOP:
  /* repeated calls to reverse communication routine
     nag_real_sparse_eigensystem_iter (f12abc). */
  nag_real_sparse_eigensystem_iter(&irevcm, resid, v, &x, &y, &mx,
                                   &nshift, comm, icomm, &fail);
  if (irevcm != 5)
    {
      if (irevcm == -1 || irevcm == 1)
        {
          /* Perform  y <--- OP*x = inv[M]*A*x using my_dpttrs. */
          av(nx, rho, x, y);
          my_dpttrs(n, md, me, y);
        }
      else if (irevcm == 2)
        {
          /* Perform  y <--- M*x. */
          mv(nx, x, y);
        }
      else if (irevcm == 4 && imon == 1)
        {
          /* If imon=1, get monitoring information using
             nag_real_sparse_eigensystem_monit (f12aec). */
          nag_real_sparse_eigensystem_monit(&niter, &nconv, eigvr,
                                            eigvi, eigest, icomm, comm);
          /* Compute 2-norm of Ritz estimates using
             nag_dge_norm (f16rac).*/
          nag_dge_norm(Nag_ColMajor, Nag_FrobeniusNorm, nev, 1, eigest,
                       nev, &estnrm, &fail);
          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_real_sparse_eigensystem_sol (f12acc)
         to compute eigenvalues/vectors. */
      nag_real_sparse_eigensystem_sol(&nconv, eigvr, eigvi, v, sigmar,
                                      sigmai, resid, v, comm, icomm,
                                      &fail);
      /* Print computed eigenvalues. */
      printf("\n  The %4ld generalized", nconv);
      printf(" Ritz values of largest magnitude are:\n\n");
      for (j = 0; j <= nconv-1; ++j)
        {
          printf("%8ld%5s( %12.4f ,%12.4f )\n", j+1, "",
                  eigvr[j], eigvi[j]);
        }
    }
  else
    {
      printf(
              " Error from nag_real_sparse_eigensystem_iter (f12abc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
 END:
  NAG_FREE(comm);
  NAG_FREE(eigvr);
  NAG_FREE(eigvi);
  NAG_FREE(eigest);
  NAG_FREE(md);
  NAG_FREE(me);
  NAG_FREE(resid);
  NAG_FREE(v);
  NAG_FREE(icomm);
  return exit_status;
}

static void av(Integer nx, double rho, double *v, double *y)
{
  /* Scalars */
  double  dd, dl, du, h, s;
  Integer j, n;
  /* Function Body */
  n = nx * nx;
  h = 1.0 / (double)(n + 1);
  s = rho / 2.0;
  dd = 2.0 / h;
  dl = -1.0 / h - s;
  du = -1.0 / h + s;
  y[0] = dd * v[0] + du * v[1];
  for (j = 1; j <= n - 2; ++j)
    {
      y[j] = dl * v[j-1] + dd * v[j] + du * v[j+1];
    }
  y[n-1] = dl * v[n-2] + dd * v[n-1];
  return;
} /* av */

static void mv(Integer nx, double *v, double *y)
{
  /* Scalars */
  double  h;
  Integer j, n;
  /* Function Body */
  n = nx * nx;
  h = 1. / (double)(n + 1);
  y[0] = h*(v[0] * 4. + v[1]);
  for (j = 1; j <= n - 2; ++j)
    {
      y[j] = h*(v[j-1] + v[j] * 4. + v[j+1]);
    }
  y[n-1] = h*(v[n-2] + v[n-1] * 4.);
  return;
} /* mv */

static void my_dpttrf(Integer n, double d[], double e[], Integer *info)
{
  /* A simple C version of the Lapack routine dpttrf with argument
     checking removed */
  /* Scalars */
  double  ei;
  Integer i;
  /* Function Body */
  *info = 0;
  for (i = 0; i < n-1; ++i)
    {
      if (d[i] <= 0.0)
        {
          *info = i+1;
          goto END_DPTTRF;
        }
      ei = e[i];
      e[i] = ei/d[i];
      d[i+1] = d[i+1] - e[i]*ei;
    }
  if (d[n-1] <= 0.0)
    {
      *info = n;
    }
 END_DPTTRF:
  return;
}

static void my_dpttrs(Integer n, double d[], double e[], double b[])
{
  /* A simple C version of the Lapack routine dpttrs with argument
     checking removed and nrhs=1 */
  /* Scalars */
  Integer i;
  /* Function Body */
  for (i = 1; i < n; ++i)
    {
      b[i] = b[i] - b[i-1]*e[i-1];
    }
  b[n-1] = b[n-1]/d[n-1];
  for (i = n-2; i >= 0; --i)
    {
      b[i] = b[i]/d[i] - b[i+1]*e[i];
    }
  return;
}