# include # include # include # include # include # include # include using namespace std; # include "fresnel.hpp" int main ( ); void fresnel_cos_plot ( ); void fresnel_cos_values ( int &n_data, double &x, double &fx ); void fresnel_cos_values_test ( ); void fresnel_phase_plot ( ); void fresnel_sin_plot ( ); void fresnel_sin_values ( int &n_data, double &x, double &fx ); void fresnel_sin_values_test ( ); void gnuplot_fx ( int n, double *x, double *y, string header ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // fresnel_test() tests fresnel(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "fresnel_test():\n"; cout << " C++ version\n"; cout << " Test fresnel()\n"; fresnel_cos_values_test ( ); fresnel_sin_values_test ( ); fresnel_cos_plot ( ); fresnel_sin_plot ( ); fresnel_phase_plot ( ); // // Terminate. // cout << "\n"; cout << "fresnel_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void fresnel_cos_plot ( ) //****************************************************************************80 // // Purpose: // // fresnel_cos_plot() tests fresnel_cos(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n; double *x; double *y; cout << "\n"; cout << "fresnel_cos_plot():\n"; cout << " Plot (X,C(X)), where C(X) is the Fresnel cosine integral.\n"; a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = fresnel_cos ( x[i] ); } header = "fresnel_cos_plot"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void fresnel_cos_values ( int &n_data, double &x, double &fx ) //****************************************************************************80 // // Purpose: // // fresnel_cos_values() returns values of the Fresnel cosine integral function. // // Discussion: // // The Fresnel cosine integral is defined by: // // C(X) = integral ( 0 <= T <= X ) cos ( PI * T^2 / 2 ) dT // // In Mathematica, the function can be evaluated by: // // FresnelC[x] // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 16 November 2015 // // Author: // // John Burkardt // // Reference: // // Milton Abramowitz, Irene Stegun, // Handbook of Mathematical Functions, // National Bureau of Standards, 1964, // ISBN: 0-486-61272-4, // LC: QA47.A34. // // Stephen Wolfram, // The Mathematica Book, // Fourth Edition, // Cambridge University Press, 1999, // ISBN: 0-521-64314-7, // LC: QA76.95.W65. // // Input: // // int &N_DATA. The user sets N_DATA to 0 before the first call. // // Output: // // int &N_DATA: the routine increments N_DATA by 1, and // returns the corresponding data; when there is no more data, the // output value of N_DATA will be 0 again. // // double &X, the argument of the function. // // double &FX, the value of the function. // { # define N_MAX 16 static double fx_vec[N_MAX] = { 0.0000000000000000E+00, 0.1999210575944531E+00, 0.3974807591723594E+00, 0.5810954469916523E+00, 0.7228441718963561E+00, 0.7798934003768228E+00, 0.7154377229230734E+00, 0.5430957835462564E+00, 0.3654616834404877E+00, 0.3336329272215571E+00, 0.4882534060753408E+00, 0.6362860449033195E+00, 0.5549614058564281E+00, 0.3889374961919690E+00, 0.4674916516989059E+00, 0.6057207892976856E+00 }; static double x_vec[N_MAX] = { 0.0E+00, 0.2E+00, 0.4E+00, 0.6E+00, 0.8E+00, 1.0E+00, 1.2E+00, 1.4E+00, 1.6E+00, 1.8E+00, 2.0E+00, 2.2E+00, 2.4E+00, 2.6E+00, 2.8E+00, 3.0E+00 }; if ( n_data < 0 ) { n_data = 0; } n_data = n_data + 1; if ( N_MAX < n_data ) { n_data = 0; x = 0.0; fx = 0.0; } else { x = x_vec[n_data-1]; fx = fx_vec[n_data-1]; } return; # undef N_MAX } //****************************************************************************80 void fresnel_cos_values_test ( ) //****************************************************************************80 // // Purpose: // // fresnel_cos_values_test() tests fresnel_cos_values(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // { double fx1; double fx2; int n_data; double x; cout << "\n"; cout << "fresnel_cos_values_test():\n"; cout << " fresnel_cos_values() stores values of\n"; cout << " the Fresnel cosine integral C(X).\n"; cout << " fresnel_cos() evaluates the Fresnel cosine integral C(X).\n"; cout << "\n"; cout << " X C(X)\n"; cout << "\n"; n_data = 0; for ( ; ; ) { fresnel_cos_values ( n_data, x, fx1 ); if ( n_data == 0 ) { break; } fx2 = fresnel_cos ( x ); cout << " " << setw(12) << x << " " << setw(24) << fx1 << " " << setw(24) << fx2 << "\n"; } return; } //****************************************************************************80 void fresnel_phase_plot ( ) //****************************************************************************80 // // Purpose: // // fresnel_phase_plot() tests fresnel_cos() and fresnel_sin(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // // { double a; double b; double *c; string header; int i; int n; double *s; double *x; cout << "\n"; cout << "fresnel_phase_plot():\n"; cout << " Plot (C(X),S(X)), where C(X) and S(X) are\n"; cout << " the Fresnel cosine and sine integrals.\n"; a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); c = new double[n]; s = new double[n]; for ( i = 0; i < n; i++ ) { fresnel ( x[i], c[i], s[i] ); } header = "fresnel_phase_plot"; gnuplot_fx ( n, c, s, header ); delete [] c; delete [] s; delete [] x;; return; } //****************************************************************************80 void fresnel_sin_plot ( ) //****************************************************************************80 // // Purpose: // // fresnel_sin_plot() tests fresnel_sin(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n; double *x; double *y; cout << "\n"; cout << "fresnel_sin_plot():\n"; cout << " Plot (X,S(X)), where S(X) is the Fresnel sine integral.\n"; a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = fresnel_sin ( x[i] ); } header = "fresnel_sin_plot"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void fresnel_sin_values ( int &n_data, double &x, double &fx ) //****************************************************************************80 // // Purpose: // // fresnel_sin_values() returns some values of the Fresnel sine integral function. // // Discussion: // // The Fresnel sine integral is defined by // // S(X) = integral ( 0 <= T <= X ) sin ( pi * T^2 / 2 ) dT // // In Mathematica, the function can be evaluated by: // // FresnelS[x] // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 16 November 2015 // // Author: // // John Burkardt // // Reference: // // Milton Abramowitz, Irene Stegun, // Handbook of Mathematical Functions, // National Bureau of Standards, 1964, // ISBN: 0-486-61272-4, // LC: QA47.A34. // // Stephen Wolfram, // The Mathematica Book, // Fourth Edition, // Cambridge University Press, 1999, // ISBN: 0-521-64314-7, // LC: QA76.95.W65. // // Input: // // int &N_DATA. The user sets N_DATA to 0 before the first call. // // Output: // // int &N_DATA: the routine increments N_DATA by 1, and // returns the corresponding data; when there is no more data, the // output value of N_DATA will be 0 again. // // double &X, the argument of the function. // // double &FX, the value of the function. // { # define N_MAX 16 static double fx_vec[N_MAX] = { 0.0000000000000000E+00, 0.4187609161656762E-02, 0.3335943266061318E-01, 0.1105402073593870E+00, 0.2493413930539178E+00, 0.4382591473903548E+00, 0.6234009185462497E+00, 0.7135250773634121E+00, 0.6388876835093809E+00, 0.4509387692675831E+00, 0.3434156783636982E+00, 0.4557046121246569E+00, 0.6196899649456836E+00, 0.5499893231527195E+00, 0.3915284435431718E+00, 0.4963129989673750E+00 }; static double x_vec[N_MAX] = { 0.0E+00, 0.2E+00, 0.4E+00, 0.6E+00, 0.8E+00, 1.0E+00, 1.2E+00, 1.4E+00, 1.6E+00, 1.8E+00, 2.0E+00, 2.2E+00, 2.4E+00, 2.6E+00, 2.8E+00, 3.0E+00 }; if ( n_data < 0 ) { n_data = 0; } n_data = n_data + 1; if ( N_MAX < n_data ) { n_data = 0; x = 0.0; fx = 0.0; } else { x = x_vec[n_data-1]; fx = fx_vec[n_data-1]; } return; # undef N_MAX } //****************************************************************************80 void fresnel_sin_values_test ( ) //****************************************************************************80 // // Purpose: // // fresnel_sin_values_test() tests fresnel_sin_values(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 July 2025 // // Author: // // John Burkardt // { double fx1; double fx2; int n_data; double x; cout << "\n"; cout << "fresnel_sin_values_test():\n"; cout << " fresnel_sin_values() stores values of\n"; cout << " the Fresnel sine integral S(X).\n"; cout << " fresnel_sin() evaluates the Fresnel sine integral S(X).\n"; cout << "\n"; cout << " X S(X)\n"; cout << "\n"; n_data = 0; for ( ; ; ) { fresnel_sin_values ( n_data, x, fx1 ); if ( n_data == 0 ) { break; } fx2 = fresnel_sin ( x ); cout << " " << setw(12) << x << " " << setw(24) << fx1 << " " << setw(24) << fx2 << "\n"; } return; } //****************************************************************************80 void gnuplot_fx ( int n, double *x, double *y, string header ) //****************************************************************************80 // // Purpose: // // gnuplot_fx() plots functional data x, f(x). // // Discussion: // // Actually, we simply create two files for processing by gnuplot(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; int i; string png_filename; // // Create a graphics data file. // data_filename = header + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < n; i++ ) { data_unit << x[i] << " " << y[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; png_filename = header + ".png"; // // Create graphics command file. // command_filename = header + "_commands.txt"; command_unit.open ( command_filename.c_str ( ) ); command_unit << "# " << command_filename << "\n"; command_unit << "#\n"; command_unit << "# Usage:\n"; command_unit << "# gnuplot < " << command_filename << "\n"; command_unit << "#\n"; command_unit << "set term png\n"; command_unit << "set output '" << png_filename << "'\n"; command_unit << "set xlabel '<--- X --->'\n"; command_unit << "set ylabel '<-- Y(X) -->'\n"; command_unit << "set title '" << header << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue'\n"; command_unit.close ( ); cout << " Created command file '" << command_filename << "'\n"; return; } //****************************************************************************80 double *r8vec_linspace_new ( int n, double a_first, double a_last ) //****************************************************************************80 // // Purpose: // // r8vec_linspace_new() creates a vector of linearly spaced values. // // Discussion: // // An R8VEC is a vector of R8's. // // 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. // // In other words, the interval is divided into N-1 even subintervals, // and the endpoints of intervals are used as the points. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2011 // // Author: // // John Burkardt // // Input: // // int N, the number of entries in the vector. // // double A_FIRST, A_LAST, the first and last entries. // // Output: // // double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. // { double *a; int i; a = new double[n]; if ( n == 1 ) { a[0] = ( a_first + a_last ) / 2.0; } else { for ( i = 0; i < n; i++ ) { a[i] = ( ( double ) ( n - 1 - i ) * a_first + ( double ) ( i ) * a_last ) / ( double ) ( n - 1 ); } } return a; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }