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

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

int main(void)
{
  /* Scalars */
  Integer  exit_status = 0, i, m, n;
  /* Arrays */
  Complex  *x = 0;
  char     title[60];
  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sum_fft_complex_2d (c06puc) Example Program Results\n");
  fflush(stdout);

  /* Read dimensions of array and array values from data file. */
  scanf("%*[^\n] %ld%ld%*[^\n]", &m, &n);

  if (!(x = NAG_ALLOC(m*n, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  /* The data is read in order as n groups of m complex values. */
  for (i = 0; i < m*n; i++)
    scanf(" ( %lf , %lf ) ", &x[i].re, &x[i].im);

  /* nag_gen_complx_mat_print_comp (x04dbc).
   * Print complex general matrix (comprehensive).
   * The data can be viewed as an n-by-m matrix stored in row order, or as an
   * m-by-n matrix stored in column order. We choose the former here.
   */
  sprintf(title, "\n Original data values\n");
  nag_gen_complx_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                                Nag_NonUnitDiag, n, m, x, m, Nag_BracketForm,
                                "%6.3f", title, Nag_NoLabels, 0, Nag_NoLabels,
                                0, 80, 0, NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

  /* Compute two-dimensional complex discrete Fourier transform using
   * nag_sum_fft_complex_2d (c06puc) and print out.
   */
  nag_sum_fft_complex_2d(Nag_ForwardTransform, m, n, x, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_sum_fft_complex_2d (c06puc).\n%s\n",
             fail.message);
      exit_status = 2;
      goto END;
    }
  sprintf(title, "\n Components of discrete Fourier transform\n");
  nag_gen_complx_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                                Nag_NonUnitDiag, n, m, x, m, Nag_BracketForm,
                                "%6.3f", title, Nag_NoLabels, 0, Nag_NoLabels,
                                0, 80, 0, NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
             fail.message);
      exit_status = 3;
      goto END;
    }

  /* Compute two-dimensional complex discrete Fourier backward transform using
   * nag_sum_fft_complex_2d (c06puc) and print out.
   */
  nag_sum_fft_complex_2d(Nag_BackwardTransform, m, n, x, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_sum_fft_complex_2d (c06puc).\n%s\n",
             fail.message);
      exit_status = 4;
      goto END;
    }
  sprintf(title, "\n Original sequence as restored by inverse transform\n");
  nag_gen_complx_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                                Nag_NonUnitDiag, n, m, x, m, Nag_BracketForm,
                                "%6.3f", title, Nag_NoLabels, 0, Nag_NoLabels,
                                0, 80, 0, NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
             fail.message);
      exit_status = 5;
    }

 END:
  NAG_FREE(x);
  return exit_status;
}