/* nag_zpbequ (f07htc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <stdio.h>
#include <nag.h>
#include <nagx04.h>
#include <nag_stdlib.h>
#include <nagf07.h>
#include <nagx02.h>

int main(void)
{

  /* Scalars */
  double        amax, big, scond, small;
  Integer       pd1, pd2, exit_status = 0, i, j, kd, n, pdab;

  /* Arrays */
  Complex       *ab = 0;
  double        *s = 0;
  char          nag_enum_arg[40];

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

#ifdef NAG_COLUMN_MAJOR
#define AB_UPPER(I, J) ab[(J-1)*pdab + kd + I - J]
#define AB_LOWER(I, J) ab[(J-1)*pdab + I - J]
  order = Nag_ColMajor;
#else
#define AB_UPPER(I, J) ab[(I-1)*pdab + J - I]
#define AB_LOWER(I, J) ab[(I-1)*pdab + kd + J - I]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_zpbequ (f07htc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%ld%*[^\n]", &n, &kd);
  if (n < 0 || kd < 0)
    {
      printf("%s\n", "Invalid n or kd");
      exit_status = 1;
      goto END;
    }
  scanf(" %39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);

  /* Allocate memory */
  pdab = kd+1;
  if (!(ab = NAG_ALLOC((kd+1)*n, Complex)) ||
      !(s  = NAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  /* Read the upper or lower triangular part of the band matrix A */
  /* from data file */
  if (uplo == Nag_Upper) 
    {
      pd1 = 0;
      pd2 = kd;
      for (i = 1; i <= n; ++i)
        for (j = i; j <= MIN(n, i + kd); ++j)
          scanf(" ( %lf , %lf )", &AB_UPPER(i, j).re, &AB_UPPER(i, j).im);
    }
  else
    {
      pd1 = kd;
      pd2 = 0;
      for (i = 1; i <= n; ++i)
        for (j = MAX(1, i - kd); j <= i; ++j)
          scanf(" ( %lf , %lf )", &AB_LOWER(i, j).re, &AB_LOWER(i, j).im);
    }
  scanf("%*[^\n]");

  /* Print the matrix A using nag_band_complx_mat_print_comp (x04dfc). */
  fflush(stdout);
  nag_band_complx_mat_print_comp(order, n, n, pd1, pd2, ab, pdab,
                                 Nag_BracketForm, "%11.2e", "Matrix A",
                                 Nag_IntegerLabels, 0, Nag_IntegerLabels,
                                 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_band_complx_mat_print_comp (x04dfc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");

  /* Compute diagonal scaling factors using nag_zpbequ (f07htc). */
  nag_zpbequ(order, uplo, n, kd, ab, pdab, s, &scond, &amax, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zpbequ (f07htc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  printf("scond = %10.1e, amax = %10.1e\n", scond, amax);
  printf("\nDiagonal scaling factors\n");
  for (i = 0; i < n; ++i) printf("%11.1e%s", s[i], i%7==6?"\n":" ");
  printf("\n\n");

  /* Compute values close to underflow and overflow using
   * nag_real_safe_small_number (x02amc), nag_machine_precision (x02ajc) and
   * nag_real_base (x02bhc)
   */
  small = nag_real_safe_small_number / (nag_machine_precision * nag_real_base);
  big = 1.0 / small;
  if (scond < 0.1 || amax < small || amax > big)
    {
      /* Scale A */
      if (uplo == Nag_Upper)
        for (j = 1; j <= n; ++j)
          for (i = MAX(1, j - kd); i <= j; ++i)
            {
              AB_UPPER(i, j).re *= s[i-1]*s[j-1];
              AB_UPPER(i, j).im *= s[i-1]*s[j-1];
            }
      else
        for (j = 1; j <= n; ++j)
          for (i = j; i <= MIN(n, j + kd); ++i)
            {
              AB_LOWER(i, j).re *= s[i-1]*s[j-1];
              AB_LOWER(i, j).im *= s[i-1]*s[j-1];
            }

      /* Print the scaled matrix using 
       * nag_band_complx_mat_print_comp (x04dfc). 
       */
      fflush(stdout);
      nag_band_complx_mat_print_comp(order, n, n, pd1, pd2, ab, pdab,
                                     Nag_BracketForm, "%7.4f", "Scaled matrix",
                                     Nag_IntegerLabels, 0, Nag_IntegerLabels,
                                     0, 80, 0, 0,  &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_band_complx_mat_print_comp (x04dfc).\n%s\n",
                 fail.message);
          exit_status = 1;
          goto END;
        }
    }
 END:
  NAG_FREE(ab);
  NAG_FREE(s);

  return exit_status;
}

#undef AB_UPPER
#undef AB_LOWER