/* nag_dpoequ (f07ffc) 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       i, j, n, pda;
  Integer       exit_status = 0;
  /* Arrays */
  double        *a = 0, *s = 0;

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

#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_dpoequ (f07ffc) Example Program Results\n\n");

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

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

  /* Read the upper triangular part of the matrix A from data file */
  for (i = 1; i <= n; ++i)
    for (j = i; j <= n; ++j) scanf("%lf", &A(i, j));
  scanf("%*[^\n]");

  /* Print the matrix A using nag_gen_real_mat_print (x04cac). */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_UpperMatrix, Nag_NonUnitDiag, n, n, a, pda,
                         "Matrix A", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\n");
  /* Compute diagonal scaling factors using nag_dpoequ (f07ffc).*/
  nag_dpoequ(order, n, a, pda, s, &scond, &amax, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dpoequ (f07ffc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print scond, amax and the scale factors */
  printf("scond = %10.1e, amax = %10.1e\n\n", scond, amax);
  printf("Diagonal 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 */
      for (j = 1; j <= n; ++j)
        for (i = 1; i <= j; ++i) A(i, j) *= s[i-1] * s[j-1];

      /* Print the scaled matrix using nag_gen_real_mat_print (x04cac). */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_UpperMatrix, Nag_NonUnitDiag, n, n, a,
                             pda, "Scaled matrix", 0, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
    }

 END:
  NAG_FREE(a);
  NAG_FREE(s);

  return exit_status;
}
#undef A