/* nag_dtgsna (f08ylc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagx02.h>
#include <nagf08.h>
#include <nagf16.h>

int main(void)
{
  /* Scalars */
  double eps, snorm, stnrm, tnorm, tol;
  Integer i, j, m, n, pds, pdt, pdvl, pdvr;
  Integer exit_status = 0;

  /* Arrays */
  double *dif = 0, *s = 0, *scon = 0, *t = 0, *vl = 0, *vr = 0;

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;

#ifdef NAG_COLUMN_MAJOR
#define S(I, J) s[(J-1)*pds + I - 1]
#define T(I, J) t[(J-1)*pdt + I - 1]
  order = Nag_ColMajor;
#else
#define S(I, J) s[(I-1)*pds + J - 1]
#define T(I, J) t[(I-1)*pdt + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dtgsna (f08ylc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  if (n < 0) {
    printf("Invalid n\n");
    exit_status = 1;
    goto END;
  }
  m = n;
  pds = n;
  pdt = n;
  pdvl = n;
  pdvr = n;

  /* Allocate memory */
  if (!(dif = NAG_ALLOC(n, double)) ||
      !(scon = NAG_ALLOC(n, double)) ||
      !(s = NAG_ALLOC(n * n, double)) ||
      !(t = NAG_ALLOC(n * n, double)) ||
      !(vl = NAG_ALLOC(n * m, double)) || !(vr = NAG_ALLOC(n * m, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read S and T from data file */
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &S(i, j));
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &T(i, j));
  scanf("%*[^\n]");

  /* Calculate the left and right generalized eigenvectors of the 
   * matrix pair (S,T) using nag_dtgevc (f08ykc).
   * NULL may be passed here in place of the select array since all
   * eigenvectors are requested.
   */
  nag_dtgevc(order, Nag_BothSides, Nag_ComputeAll, NULL, n, s, pds, t, pdt,
             vl, pdvl, vr, pdvr, n, &m, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtgevc (f08ykc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Estimate condition numbers for all the generalized eigenvalues and right
   * eigenvectors of the pair (S,T) using nag_dtgsna (f08ylc).
   * NULL may be passed here in place of the select array since all
   * eigenvectors are requested.
   */
  nag_dtgsna(order, Nag_DoBoth, Nag_ComputeAll, NULL, n, s, pds, t, pdt,
             vl, pdvl, vr, pdvr, scon, dif, n, &m, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtgsna (f08ylc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print condition numbers of eigenvalues and right eigenvectors */
  printf("Condition numbers of eigenvalues (scon) and right eigenvectors "
         "(diff),\n");
  printf("scon:   ");
  for (i = 0; i < m; ++i)
    printf(" %10.1e%s", scon[i], i % 7 == 6 ? "\n       " : "");
  printf("\ndif:    ");
  for (i = 0; i < m; ++i)
    printf(" %10.1e%s", dif[i], i % 7 == 6 ? "\n       " : "");

  /* Compute the norm of (S,T) using nag_dge_norm (f16rac). */
  eps = nag_machine_precision;
  nag_dge_norm(order, Nag_OneNorm, n, n, s, pds, &snorm, &fail);
  nag_dge_norm(order, Nag_OneNorm, n, n, t, pdt, &tnorm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  if (snorm == 0.0)
    stnrm = ABS(tnorm);
  else if (tnorm == 0.0)
    stnrm = ABS(snorm);
  else if (ABS(snorm) >= ABS(tnorm))
    stnrm = ABS(snorm) * sqrt(1.0 + (tnorm / snorm) * (tnorm / snorm));
  else
    stnrm = ABS(tnorm) * sqrt(1.0 + (snorm / tnorm) * (snorm / tnorm));

  /* Calculate approximate error estimates */
  tol = eps * stnrm;

  printf("\n\nError estimates for eigenvalues (errval) and right eigenvectors"
         " (errvec),\n");
  printf("errval: ");
  for (i = 0; i < m; ++i)
    printf(" %10.1e%s", tol / scon[i], i % 7 == 6 ? "\n        " : "");
  printf("\nerrvec: ");
  for (i = 0; i < m; ++i)
    printf(" %10.1e%s", tol / dif[i], i % 7 == 6 ? "\n        " : "");
  printf("\n");
END:
  NAG_FREE(dif);
  NAG_FREE(scon);
  NAG_FREE(s);
  NAG_FREE(t);
  NAG_FREE(vl);
  NAG_FREE(vr);

  return exit_status;
}