/* nag_dgb_norm (f16rbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2005.
 */

#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 */
  double        *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_dgb_norm (f16rbc) Example Program Results\n\n");

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

  /* Read the problem dimensions */
  scanf("%ld%ld%ld%ld%*[^\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, double)))
        {
          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", &AB(i, j));
      scanf("%*[^\n] ");
    }

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

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

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

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

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

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

  nag_dgb_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_dgb_norm.\n%s\n", fail.message);
  exit_status = 1;

 END:
  NAG_FREE(ab);

  return exit_status;
}