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

NAG CL Interface Introduction
Example description
/* nag_matop_real_gen_matrix_fun_usd (f01emc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

#include <math.h>
#include <nag.h>

#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL f(Integer m, Integer *iflag, Integer nz, const Complex z[],
                       Complex fz[], Nag_Comm *comm);
#ifdef __cplusplus
}
#endif

int main(void) {

  /* Scalars */
  Integer exit_status = 0;
  Integer i, iflag, j, n, pda;
  double imnorm;

  /* Arrays */
  static double ruser[1] = {-1.0};
  double *a = 0;

  /* Nag Types */
  Nag_Comm comm;
  Nag_OrderType order;
  NagError fail;

  INIT_FAIL(fail);

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
  order = Nag_RowMajor;
#endif

  /* Output preamble */
  printf("nag_matop_real_gen_matrix_fun_usd (f01emc) ");
  printf("Example Program Results\n\n");

  /* For communication with user-supplied functions: */
  comm.user = ruser;

  fflush(stdout);

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

  /* Read in the problem size */
  scanf("%" NAG_IFMT "%*[^\n]", &n);

  pda = n;

  if (!(a = NAG_ALLOC((pda) * (n), double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the matrix a from data file */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf("%lf", &A(i, j));
  scanf("%*[^\n]");

  /* Find the matrix function using
   * nag_matop_real_gen_matrix_fun_usd (f01emc)
   * Function of a real matrix
   */
  nag_matop_real_gen_matrix_fun_usd(order, n, a, pda, f, &comm, &iflag, &imnorm,
                                    &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_matop_real_gen_matrix_fun_usd (f01emc)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  if (fabs(imnorm) > pow(nag_machine_precision, 0.8)) {
    printf("\nWARNING: the error estimate returned in imnorm is larger than"
           " expected;\n");
    printf("         the matrix solution printed below is suspect:\n");
    printf("         imnorm = %13.4e.\n\n", imnorm);
  }

  /* Print solution using
   * nag_file_print_matrix_real_gen (x04dac)
   * Print real general matrix (easy-to-use)
   */
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 n, a, pda, "f(A)", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04dac)\n%s\n",
           fail.message);
    exit_status = 2;
    goto END;
  }

END:
  NAG_FREE(a);
  return exit_status;
}

static void NAG_CALL f(Integer m, Integer *iflag, Integer nz, const Complex z[],
                       Complex fz[], Nag_Comm *comm) {
  /* Scalars */
  Integer j;
#ifdef _OPENMP
#pragma omp master
#endif
  if (comm->user[0] == -1.0) {
    printf("(User-supplied callback f, first invocation.)\n");
    fflush(stdout);
    comm->user[0] = 0.0;
  }
  for (j = 0; j < nz; j++) {
    /* The m^th derivative of exp(2z) for complex z */
    fz[j].re = pow(2.0, m) * exp(2.0 * z[j].re) * cos(2.0 * z[j].im);
    fz[j].im = pow(2.0, m) * exp(2.0 * z[j].re) * sin(2.0 * z[j].im);
  }
  /* Set iflag nonzero to terminate execution for any reason. */
  *iflag = 0;
}