/* nag_dwaxpby (f16ehc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 9, 2009.
 */

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

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, incw, incx, incy, n, wlen, xlen, ylen;
  double   alpha, beta;
  /* Arrays */
  double   *w = 0, *x = 0, *y = 0;
  /* Nag Types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_dwaxpby (f16ehc) 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%ld%*[^\n] ", &incx, &incy, &incw);
  /* Read factors alpha and beta */
  scanf("%lf%lf%*[^\n] ", &alpha, &beta);

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

  if (n > 0)
    {
      /* Allocate memory */
      if (!(w = NAG_ALLOC(wlen, double)) ||
          !(x = NAG_ALLOC(xlen, double)) ||
          !(y = NAG_ALLOC(ylen, double)))
        {
          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 + incx)
    scanf("%lf", &x[i]);
  scanf("%*[^\n] ");

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

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

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

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

  for (i = 0; i < wlen - 1; i = i + incw)
    printf("%9.4f, ", w[i]);
  printf("%9.4f)\n", w[wlen - 1]);

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

  return exit_status;
}