NAG Library Manual, Mark 30.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_sparse_direct_real_gen_matmul (f11mkc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

#include <nag.h>
#include <stdio.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_sparse_direct_real_gen_matmul (f11mkc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\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("%" NAG_IFMT "%*[^\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%" NAG_IFMT "%*[^\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_sparse_direct_real_gen_matmul (f11mkc).
   * Real sparse nonsymmetric matrix matrix multiply,
   * compressed column storage
   */
  nag_sparse_direct_real_gen_matmul(order, trans, n, m, alpha, icolzp, irowix,
                                    a, b, n, beta, c, n, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_direct_real_gen_matmul (f11mkc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output results */
  printf("\n");
  /* nag_file_print_matrix_real_gen (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, matrix, diag, n, m, c, n,
                                 "Matrix-vector product", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

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

  /* Output results */
  printf("\n");
  /* nag_file_print_matrix_real_gen (x04cac), see above. */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, matrix, diag, n, m, c, n,
                                 "Transposed matrix-vector product", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (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;
}