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

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

int main(void)
{
  double       anorm;
  Integer      exit_status = 0, i, n, nnz;
  double       *a = 0;
  Integer      *icolzp = 0, *irowix = 0;
  /* Nag types */
  Nag_NormType norm;
  NagError     fail;

  INIT_FAIL(fail);

  printf(
          "nag_superlu_matrix_norm (f11mlc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix and number of right hand sides */
  scanf("%ld%*[^\n] ", &n);
  /* Read the matrix A */
  if (!(icolzp = NAG_ALLOC(n+1, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 1; i <= n + 1; ++i)
    scanf("%ld%*[^\n] ", &icolzp[i - 1]);
  nnz = icolzp[n] - 1;
  /* Allocate memory */
  if (!(a = NAG_ALLOC(nnz, double)) ||
      !(irowix = NAG_ALLOC(nnz, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 1; i <= nnz; ++i)
    scanf("%lf%ld%*[^\n] ", &a[i - 1], &irowix[i - 1]);
  /* Calculate 1-norm */
  norm = Nag_RealOneNorm;
  /* nag_superlu_matrix_norm (f11mlc).
   * 1-norm, infinity-norm, largest absolute element, real
   * general matrix
   */
  nag_superlu_matrix_norm(norm, &anorm, n, icolzp, irowix, a, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_superlu_matrix_norm (f11mlc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Output norm */
  printf("%s\n%7.3f\n", "One-norm", anorm);

  /* Calculate M-norm */
  norm = Nag_RealMaxNorm;
  /* nag_superlu_matrix_norm (f11mlc), see above. */
  nag_superlu_matrix_norm(norm, &anorm, n, icolzp, irowix, a, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_superlu_matrix_norm (f11mlc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Output norm */
  printf("\n");
  printf("%s\n%7.3f\n", "Max", anorm);

  /* Calculate I-norm */
  norm = Nag_RealInfNorm;
  /* nag_superlu_matrix_norm (f11mlc), see above. */
  nag_superlu_matrix_norm(norm, &anorm, n, icolzp, irowix, a, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_superlu_matrix_norm (f11mlc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Output norm */
  printf("\n");
  printf("%s\n%7.3f\n", "Infinity-norm", anorm);

 END:
  NAG_FREE(a);
  NAG_FREE(icolzp);
  NAG_FREE(irowix);

  return exit_status;
}