/* nag_sum_fft_real_2d (c06pvc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

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

  INIT_FAIL(fail);

  printf("nag_sum_fft_real_2d (c06pvc) Example Program Results\n");
  fflush(stdout);

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

  if (!(x = NAG_ALLOC(m * n, double)) ||
      !(y = NAG_ALLOC((m / 2 + 1) * n, Complex)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read array values from data file and print out. */
  for (i = 0; i < m * n; i++)
    scanf("%lf", &x[i]);
  sprintf(title, "\n Original data values\n");
  nag_gen_real_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                              Nag_NonUnitDiag, n, m, x, m, "%6.3f",
                              title, Nag_NoLabels, 0, Nag_NoLabels,
                              0, 80, 0, NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Compute two-dimensional real-to-complex discrete Fourier transform using
   * nag_sum_fft_real_2d (c06pvc) and print out.
   */
  nag_sum_fft_real_2d(m, n, x, y, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_fft_real_2d (c06pvc).\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 / 2 + 1, y, m / 2 + 1,
                                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-to-real discrete Fourier transform using
   * nag_sum_fft_hermitian_2d (c06pwc) and print out.
   */
  nag_sum_fft_hermitian_2d(m, n, y, x, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_fft_hermitian_2d (c06pwc).\n%s\n",
           fail.message);
    exit_status = 4;
    goto END;
  }
  sprintf(title, "\n Original sequence as restored by inverse transform\n");
  nag_gen_real_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                              Nag_NonUnitDiag, n, m, x, m, "%6.3f",
                              title, Nag_NoLabels, 0, Nag_NoLabels,
                              0, 80, 0, NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 5;
    goto END;
  }

END:
  NAG_FREE(x);
  NAG_FREE(y);
  return exit_status;
}