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

#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("%" NAG_IFMT "%*[^\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("%" NAG_IFMT "%*[^\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%" NAG_IFMT "%*[^\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;
}