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

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf07.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer        i, j, n, nrhs, pda, pdb;
  Integer        exit_status = 0;
  NagError       fail;
  Nag_OrderType  order;
  Nag_TransType  trans;
  Nag_MatrixType matrix;
  Nag_DiagType   unitdiag;

  /* Arrays */
  double         *a = 0, *b = 0;
  Integer        *ipiv = 0;
  char           nag_enum_arg[20];

  INIT_FAIL(fail);

  printf("nag_enum_name_to_value (x04nac) Example Program Results\n\n");

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

  /* Read the problem dimensions. */
  scanf("%ld%ld%*[^\n] ", &n, &nrhs);

  /* Read the storage order of the matrices and convert to value. */
  scanf("%19s%*[^\n] ", nag_enum_arg);

  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  order = (Nag_OrderType) nag_enum_name_to_value(nag_enum_arg);

  /* Read whether matrix A is to be transposed and convert. */
  scanf("%19s%*[^\n] ", nag_enum_arg);
  trans = (Nag_TransType) nag_enum_name_to_value(nag_enum_arg);

  /* Read and convert parameters for writing solution using
   * nag_gen_real_mat_print (x04cac). */
  scanf("%19s%*[^\n] ", nag_enum_arg);
  matrix = (Nag_MatrixType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%19s%*[^\n] ", nag_enum_arg);
  unitdiag = (Nag_DiagType) nag_enum_name_to_value(nag_enum_arg);

  /* Set up defines for reading matrices using given order. */
#define A(I, J) a[(J-1)*pda + I - 1]
#define B(I, J) b[(J-1)*pdb + I - 1]
  if (order == (int) Nag_ColMajor)
    {
      pda = n;
      pdb = n;
    }
  else
    {
      pda = n;
      pdb = nrhs;
    }

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

  /* Read A and B from data file */
  if (order == Nag_ColMajor)
    {
      for (i = 1; i <= n; ++i)
        {
          for (j = 1; j <= n; ++j)
            scanf("%lf", &A(i, j));
        }
      scanf("%*[^\n] ");
      for (i = 1; i <= n; ++i)
        {
          for (j = 1; j <= nrhs; ++j)
            scanf("%lf", &B(i, j));
        }
      scanf("%*[^\n] ");
    }
  else
    {
      for (i = 1; i <= n; ++i)
        {
          for (j = 1; j <= n; ++j)
            scanf("%lf", &A(j, i));
        }
      scanf("%*[^\n] ");
      for (i = 1; i <= n; ++i)
        {
          for (j = 1; j <= nrhs; ++j)
            scanf("%lf", &B(j, i));
        }
      scanf("%*[^\n] ");
    }

  /* Factorize A */
  /* nag_dgetrf (f07adc).
   * LU factorization of real m by n matrix
   */
  nag_dgetrf(order, n, n, a, pda, ipiv, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgetrf (f07adc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Compute solution */
  /* nag_dgetrs (f07aec).
   * Solution of real system of linear equations, multiple
   * right-hand sides, matrix already factorized by nag_dgetrf
   * (f07adc)
   */
  nag_dgetrs(order, trans, n, nrhs, a, pda, ipiv, b, pdb, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgetrs (f07aec).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_enum_value_to_name (x04nbc).
   * Converts NAG enum member value to its name
   */
  printf("Array storage scheme used in nag_dgetrs (f07aec) is %s\n",
          nag_enum_value_to_name(order));

  /* nag_error_name_to_code (x04ncc).
   * Converts NAG error name to its code value
   */
  printf("nag_dgetrs (f07aec) returns with the error code "
          "(fail.code) set to %d\n\n", nag_error_name_to_code("NE_NOERROR"));

  /* Print solution */
  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, matrix, unitdiag, n, nrhs, b, pdb,
                         "Solution(s)", 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(b);
  NAG_FREE(ipiv);
  return exit_status;
}