/* nag_zwaxpby (f16ghc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf16.h>

int main(void)
{
  /* Scalars */
  Integer exit_status, i, incw, incx, incy, iw, ix, iy, n;
  Complex alpha, beta;
  /* Arrays */
  Complex *w = 0, *x = 0, *y = 0;
  /* Nag Types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_zwaxpby (f16ghc) 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 "%" NAG_IFMT "%*[^\n] ", &incx, &incy,
        &incw);
  /* Read factors alpha and beta */
  scanf(" ( %lf , %lf ) ", &alpha.re, &alpha.im);
  scanf(" ( %lf , %lf ) %*[^\n] ", &beta.re, &beta.im);

  if (n > 0) {
    /* Allocate memory */
    if (!(w = NAG_ALLOC(MAX(1, 1 + (n - 1) * ABS(incw)), Complex)) ||
        !(x = NAG_ALLOC(MAX(1, 1 + (n - 1) * ABS(incx)), Complex)) ||
        !(y = NAG_ALLOC(MAX(1, 1 + (n - 1) * ABS(incy)), Complex)))
    {
      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 , %lf ) ", &x[ix].re, &x[ix].im);
  scanf("%*[^\n] ");

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

  /* nag_zwaxpby (f16ghc).
   * Performs w := alpha*x + beta*y  */
  nag_zwaxpby(n, alpha, x, incx, beta, y, incy, w, incw, &fail);

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

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

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

  return exit_status;
}