/* nag_conjugate_complex (c06gcc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 1, 1990.
 * Mark 8 revised, 2004.
 */

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

int main(void)
{
  Integer  exit_status = 0, j, n;
  NagError fail;
  double   *x = 0, *y = 0;

  INIT_FAIL(fail);

  printf("nag_conjugate_complex (c06gcc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%ld", &n) != EOF)
    {
      if (n > 1)
        {
          if (!(x = NAG_ALLOC(n, double)) ||
              !(y = NAG_ALLOC(n, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("\nInvalid n.\n");
          exit_status = 1;
          return exit_status;
        }
      /* Read in complex data */
      for (j = 0; j < n; ++j)
        scanf("%lf%lf", &x[j], &y[j]);
      /* Compute inverse transform */
      /* Calculate conjugates of data */
      /* nag_conjugate_complex (c06gcc).
       * Complex conjugate of complex sequence
       */
      nag_conjugate_complex(n, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_conjugate_complex (c06gcc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Calculate transform of conjugated data */
      /* nag_fft_complex (c06ecc).
       * Single one-dimensional complex discrete Fourier transform
       */
      nag_fft_complex(n, x, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_complex (c06ecc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Conjugate to give inverse transfom */
      /* nag_conjugate_complex (c06gcc), see above. */
      nag_conjugate_complex(n, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_conjugate_complex (c06gcc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("\nComponents of inverse discrete Fourier transform\n");
      printf("\n         Real       Imag\n\n");
      for (j = 0; j < n; ++j)
        printf("%3ld %10.5f %10.5f\n", j, x[j], y[j]);
 END:
      NAG_FREE(x);
      NAG_FREE(y);
    }
  return exit_status;
}