/* nag_sum_convcorr_real (c06fkc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0, j, n;
  /* Arrays */
  double *xa = 0, *xb = 0, *ya = 0, *yb = 0;
  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sum_convcorr_real (c06fkc) Example Program Results\n");
  scanf("%*[^\n]" "%" NAG_IFMT "%*[^\n]", &n);
  if (n < 2) {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }

  if (!(xa = NAG_ALLOC(n, double)) ||
      !(xb = NAG_ALLOC(n, double)) ||
      !(ya = NAG_ALLOC(n, double)) || !(yb = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  for (j = 0; j < n; ++j) {
    scanf("%lf%lf", &xa[j], &ya[j]);
    xb[j] = xa[j];
    yb[j] = ya[j];
  }

  /* nag_sum_convcorr_real (c06fkc).
   * Circular convolution or correlation of two real vectors
   */
  nag_sum_convcorr_real(Nag_Convolution, xa, ya, n, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_convcorr_real (c06fkc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }
  /* nag_sum_convcorr_real (c06fkc), see above. */
  nag_sum_convcorr_real(Nag_Correlation, xb, yb, n, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_convcorr_real (c06fkc).\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  printf("\n          Convolution   Correlation\n\n");
  for (j = 0; j < n; ++j)
    printf("%5" NAG_IFMT " %13.5f %13.5f\n", j, xa[j], xb[j]);

END:
  NAG_FREE(xa);
  NAG_FREE(xb);
  NAG_FREE(ya);
  NAG_FREE(yb);

  return exit_status;
}