/* nag_zaxpby (f16gcc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 2013.
 */

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

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_zaxpby (f16gcc) Example Program Results\n\n");

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

  xlen = MAX(1, 1 + (n - 1)*ABS(incx));
  ylen = MAX(1, 1 + (n - 1)*ABS(incy));

  if (n > 0)
    {
      /* Allocate memory */
      if (!(x = NAG_ALLOC(xlen, Complex)) ||
          !(y = NAG_ALLOC(ylen, Complex)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
    }
  else
    {
      printf("Invalid n\n");
      exit_status = 1;
      goto END;
    }

  /* Input vector x */
  for (i = 0; i < xlen ; i = i + abs(incx))
    scanf(" ( %lf , %lf )", &x[i].re, &x[i].im);
  scanf("%*[^\n] ");

  /* Input vector y */
  for (i = 0; i < ylen; i = i + abs(incy))
    scanf(" ( %lf , %lf )", &y[i].re, &y[i].im);
  scanf("%*[^\n] ");

  /* nag_zaxpby (f16gcc).
   * Performs y := alpha*x + beta*y  */
  nag_zaxpby(n, alpha, x, incx, beta, y, incy, &fail);
  
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zaxpby (f16gcc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print the result */
  printf("Result of the scaled vector accumulation is\n");
  printf("y = (\n");

  for (i = 0; i < ylen; i = i + abs(incy))
    {
      printf("     ( %9.4f, %9.4f )", y[i].re, y[i].im);
      (i != ylen - 1) ? printf(",") : printf(" )");
      printf("\n");
    }

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

  return exit_status;
}