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

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

/* Table of constant values */

static double alpha = 1.;
static double beta = 0.;

int main(void)
{
  Integer        exit_status = 0, i, j, m, n, nnz;
  double         *a = 0, *b = 0, *c = 0;
  Integer        *icolzp = 0, *irowix = 0;
  /* Nag types */
  NagError       fail;
  Nag_OrderType  order = Nag_ColMajor;
  Nag_MatrixType matrix = Nag_GeneralMatrix;
  Nag_DiagType   diag = Nag_NonUnitDiag;
  Nag_TransType  trans;

  INIT_FAIL(fail);

  printf(
          "nag_superlu_matrix_product (f11mkc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix */
  scanf("%ld%ld%*[^\n] ", &n, &m);
  /* Read the matrix A */
  if (!(icolzp = NAG_ALLOC(n+1, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 0; i < n + 1; ++i)
    scanf("%ld%*[^\n] ", &icolzp[i]);
  nnz = icolzp[n] - 1;
  /* Allocate memory */
  if (!(irowix = NAG_ALLOC(nnz, Integer)) ||
      !(a = NAG_ALLOC(nnz, double)) ||
      !(b = NAG_ALLOC(n * m, double)) ||
      !(c = NAG_ALLOC(n * m, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 0; i < nnz; ++i)
    scanf("%lf%ld%*[^\n] ", &a[i], &irowix[i]);
  /* Read the matrix B */
  for (j = 0; j < m; ++j)
    {
      for (i = 0; i < n; ++i)
        {
          scanf("%lf", &b[j*n + i]);
          c[j*n + i] = 0.0;
        }
      scanf("%*[^\n] ");
    }
  /* Calculate matrix-matrix product */
  trans = Nag_NoTrans;
  /* nag_superlu_matrix_product (f11mkc).
   * Real sparse nonsymmetric matrix matrix multiply,
   * compressed column storage
   */
  nag_superlu_matrix_product(order, trans, n, m, alpha, icolzp, irowix, a, b,
                             n, beta, c, n, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_superlu_matrix_product (f11mkc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Output results */
  printf("\n");
  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, matrix, diag, n, m, c, n,
                         "Matrix-vector product", 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;
    }

  /* Calculate transposed matrix-matrix product */
  trans = Nag_Trans;
  /* nag_superlu_matrix_product (f11mkc), see above. */
  nag_superlu_matrix_product(order, trans, n, m, alpha, icolzp, irowix, a, b,
                             n, beta, c, n, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_superlu_matrix_product (f11mkc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Output results */
  printf("\n");
  /* nag_gen_real_mat_print (x04cac), see above. */
  fflush(stdout);
  nag_gen_real_mat_print(order, matrix, diag, n, m, c, n,
                         "Transposed matrix-vector product", 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(c);
  NAG_FREE(icolzp);
  NAG_FREE(irowix);

  return exit_status;
}