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

NAG CL Interface Introduction
Example description
/* nag_blast_daxpby (f16ecc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.3, 2023.
 */

#include <nag.h>
#include <stdio.h>
int main(void) {
  /* Scalars */
  Integer exit_status, i, incx, incy, ix, iy, n;
  double alpha, beta;
  /* Arrays */
  double *x = 0, *y = 0;
  /* Nag Types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_blast_daxpby (f16ecc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read number of elements */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  /* Read increments */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &incx, &incy);
  /* Read factors alpha and beta */
  scanf("%lf%lf%*[^\n] ", &alpha, &beta);

  if (n > 0) {
    /* Allocate memory */
    if (!(x = NAG_ALLOC(MAX(1, 1 + (n - 1) * ABS(incx)), double)) ||
        !(y = NAG_ALLOC(MAX(1, 1 + (n - 1) * ABS(incy)), double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Invalid n\n");
    exit_status = 1;
    goto END;
  }

  /* Read the vectors x and y and store forwards or backwards
   * as determined by incx (resp. incy). */
  for (i = 0, ix = (incx > 0 ? 0 : (1 - n) * incx); i < n; i++, ix += incx)
    scanf("%lf", &x[ix]);
  scanf("%*[^\n] ");

  for (i = 0, iy = (incy > 0 ? 0 : (1 - n) * incy); i < n; i++, iy += incy)
    scanf("%lf", &y[iy]);
  scanf("%*[^\n] ");

  /* nag_blast_daxpby (f16ecc).
   * Performs y := alpha*x + beta*y  */
  nag_blast_daxpby(n, alpha, x, incx, beta, y, incy, &fail);

  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_daxpby (f16ecc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the vector y forwards or backwards
   * as determined by incy. */
  printf("Result of the scaled vector accumulation is\n");
  printf("y = (");
  for (i = 0, iy = (incy > 0 ? 0 : (1 - n) * incy); i < n; i++, iy += incy)
    printf("%9.4f%s", y[iy], (i < n - 1 ? ", " : ")\n"));

END:
  NAG_FREE(x);
  NAG_FREE(y);

  return exit_status;
}