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

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf16.h>

int main(void)
{

  /* Scalars */
  double r_one, r_inf, r_f, r_max;
  Integer ab_size, exit_status, i, j, kl, ku;
  Integer m, n, pdab;

  /* Arrays */
  Complex *ab = 0;

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

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

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_zgb_norm (f16ubc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read the problem dimensions */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ",
        &m, &n, &kl, &ku);

  pdab = kl + ku + 1;
#ifdef NAG_COLUMN_MAJOR
  ab_size = pdab * n;
#else
  ab_size = pdab * m;
#endif

  if (m > 0 && n > 0) {
    /* Allocate memory */
    if (!(ab = NAG_ALLOC(ab_size, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid m or n\n");
    exit_status = 1;
    return exit_status;
  }

  /* Input matrix A. */

  for (i = 1; i <= m; ++i) {
    for (j = MAX(1, i - kl); j <= MIN(n, i + ku); ++j)
      scanf(" ( %lf , %lf )", &AB(i, j).re, &AB(i, j).im);
    scanf("%*[^\n] ");
  }

  /* nag_zgb_norm (f16ubc).
   * calculates norm of Complex valued general band matrix.
   *
   */
  nag_zgb_norm(order, Nag_OneNorm, m, n, kl, ku, ab, pdab, &r_one, &fail);

  if (fail.code != NE_NOERROR)
    goto GB_FAIL;

  nag_zgb_norm(order, Nag_InfNorm, m, n, kl, ku, ab, pdab, &r_inf, &fail);

  if (fail.code != NE_NOERROR)
    goto GB_FAIL;

  nag_zgb_norm(order, Nag_FrobeniusNorm, m, n, kl, ku, ab, pdab, &r_f, &fail);

  if (fail.code != NE_NOERROR)
    goto GB_FAIL;

  nag_zgb_norm(order, Nag_MaxNorm, m, n, kl, ku, ab, pdab, &r_max, &fail);

  if (fail.code != NE_NOERROR)
    goto GB_FAIL;

  /* Print norms of A. */
  printf(" Norms of banded matrix A:\n\n");
  printf(" One norm       = %7.4f\n", r_one);
  printf(" Infinity norm  = %7.4f\n", r_inf);
  printf(" Frobenius norm = %7.4f\n", r_f);
  printf(" Maximum norm   = %7.4f\n", r_max);
  goto END;

GB_FAIL:
  printf("Error from nag_zgb_norm.\n%s\n", fail.message);
  exit_status = 1;

END:
  NAG_FREE(ab);

  return exit_status;
}