/* nag_fft_3d (c06pxc) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /* Scalars */ Integer i, j, k, n1, n2, n3; Integer exit_status = 0; NagError fail; /* Arrays */ Complex *x = 0; #define X(I, J, K) x[(K-1)*n2*n1 + (J-1)*n1 + I - 1] INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); fprintf(fpout, "nag_fft_3d (c06pxc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld%ld%ld", &n1, &n2, &n3); fscanf(fpin, "%*[^\n]"); if (n1*n2*n3 >= 1) { /* Allocate memory */ if (!(x = NAG_ALLOC(n1 * n2 * n3, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in complex data and print out. */ for (i = 1; i <= n1; ++i) { for (j = 1; j <= n2; ++j) { for (k = 1; k <= n3; ++k) { fscanf(fpin, " ( %lf, %lf ) ", &X(i, j, k).re, &X(i, j, k).im); } } } fscanf(fpin, "%*[^\n]"); fprintf(fpout, "\nOriginal data values\n\n"); /* nag_gen_complx_mat_print_comp (x04dbc). * Print complex general matrix (comprehensive) */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, x, n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 1", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } fprintf(fpout, "\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, &X(2, 1, 1), n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 2", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute transform */ /* nag_fft_3d (c06pxc). * Three-dimensional complex discrete Fourier transform, * complex data format */ nag_fft_3d(Nag_ForwardTransform, n1, n2, n3, x, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_fft_3d (c06pxc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nComponents of discrete Fourier transforms\n\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, x, n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 1", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } fprintf(fpout, "\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, &X(2, 1, 1), n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 2", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute inverse transform */ /* nag_fft_3d (c06pxc), see above. */ nag_fft_3d(Nag_BackwardTransform, n1, n2, n3, x, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_fft_3d (c06pxc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nOriginal data as restored by inverse transform\n\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, x, n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 1", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } fprintf(fpout, "\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n2, n3, &X(2, 1, 1), n1*n2, Nag_BracketForm, "%6.3f", "X(i,j,k) for i = 2", Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } } else fprintf(fpout, "\nInvalid value of n1, n2 or n3.\n"); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); return exit_status; }