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

#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, colcnd, rowcnd, small;
  Integer i, j, m, n, pda;
  Integer exit_status = 0;

  /* Arrays */
  Complex *a = 0;
  double *c = 0, *r = 0;

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_Boolean scaled = Nag_FALSE;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_zgeequ (f07atc) 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;
    return exit_status;
  }

  m = n;
  pda = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m * n, Complex)) ||
      !(c = NAG_ALLOC(n, double)) || !(r = NAG_ALLOC(m, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  /* Print the matrix A using nag_gen_complx_mat_print_comp (x04dbc). */
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                n, a, pda, 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_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");

  /* Compute row and column scaling factors */
  nag_zgeequ(order, n, n, a, pda, r, c, &rowcnd, &colcnd, &amax, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zgeequ (f07atc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print rowcnd, colcnd, amax and the scale factors */
  printf("rowcnd = %10.1e, colcnd = %10.1e, amax = %10.1e\n\n", rowcnd,
         colcnd, amax);
  printf("Row scale factors\n");
  for (i = 1; i <= n; ++i)
    printf("%11.2e%s", r[i - 1], i % 7 == 0 ? "\n" : " ");
  printf("\n\nColumn scale factors\n");
  for (i = 1; i <= n; ++i)
    printf("%11.2e%s", c[i - 1], i % 7 == 0 ? "\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 (colcnd < 0.1) {
    /* column scale A */
    scaled = Nag_TRUE;
    for (j = 1; j <= n; ++j)
      for (i = 1; i <= n; ++i) {
        A(i, j).re *= c[j - 1];
        A(i, j).im *= c[j - 1];
      }
  }
  if (rowcnd < 0.1 || amax < small || amax > big) {
    /* row scale A */
    scaled = Nag_TRUE;
    for (j = 1; j <= n; ++j)
      for (i = 1; i <= n; ++i) {
        A(i, j).re *= r[i - 1];
        A(i, j).im *= r[i - 1];
      }
  }
  if (scaled) {
    /* Print the scaled matrix using nag_gen_complx_mat_print_comp (x04dbc) */
    fflush(stdout);
    nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                  n, n, a, pda, Nag_BracketForm, 0,
                                  "Scaled matrix", Nag_IntegerLabels, 0,
                                  Nag_IntegerLabels, 0, 80, 0, 0, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  }

END:
  NAG_FREE(a);
  NAG_FREE(c);
  NAG_FREE(r);

  return exit_status;
}