/* nag_ztrsna (f08qyc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  Integer i, j, m, n, pdt, pdvl, pdvr;
  Integer s_len;
  Integer exit_status = 0;
  double eps, tnorm;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  double *s = 0, *sep = 0;
  Complex *t = 0, *vl = 0, *vr = 0;

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

  INIT_FAIL(fail);

  printf("nag_ztrsna (f08qyc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
#ifdef NAG_COLUMN_MAJOR
  pdt = n;
  pdvl = n;
  pdvr = n;
#else
  pdt = n;
  pdvl = n;
  pdvr = n;
#endif
  s_len = n;

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

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

  /* Calculate right and left eigrnvectors of T */
  /* nag_ztrevc (f08qxc).
   * Left and right eigenvectors of complex upper triangular
   * matrix
   */
  nag_ztrevc(order, Nag_BothSides, Nag_ComputeAll, NULL, n, t, pdt,
             vl, pdvl, vr, pdvr, n, &m, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztrevc (f08qxc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Estimate condition numbers for all the eigenvalues and */
  /* right eigenvectors of T */
  /* nag_ztrsna (f08qyc).
   * Estimates of sensitivities of selected eigenvalues and
   * eigenvectors of complex upper triangular matrix
   */
  nag_ztrsna(order, Nag_DoBoth, Nag_ComputeAll, NULL, n, t, pdt,
             vl, pdvl, vr, pdvr, s, sep, n, &m, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztrsna (f08qyc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print condition numbers of eigenvalues and right eigenvectors */
  printf("\nS\n");
  for (i = 0; i < n; ++i)
    printf("%11.1e", s[i]);
  printf("\n\nSep\n");
  for (i = 0; i < n; ++i)
    printf("%11.1e", sep[i]);
  printf("\n");
  /* Calculate approximate error estimates (using the 1-norm) */
  /* nag_zge_norm (f16uac).
   * 1-norm, infinity-norm, Frobenius norm, largest absolute
   * element, complex general matrix
   */
  nag_zge_norm(order, Nag_OneNorm, n, n, t, pdt, &tnorm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zge_norm (f16uac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* nag_machine_precision (x02ajc).
   * The machine precision
   */
  eps = nag_machine_precision;
  printf("\nApproximate error estimates for eigenvalues"
         "of T (machine dependent)\n");
  for (i = 0; i < m; ++i)
    printf("%11.1e", eps * tnorm / s[i]);
  printf("\n\nApproximate error estimates for right eigenvectors"
         "of T (machine dependent)\n");
  for (i = 0; i < m; ++i)
    printf("%11.1e", eps * tnorm / sep[i]);
  printf("\n");
END:
  NAG_FREE(t);
  NAG_FREE(s);
  NAG_FREE(sep);
  NAG_FREE(vl);
  NAG_FREE(vr);

  return exit_status;
}