# include # include # include # include # include # include # include using namespace std; # include "sinc.hpp" int main ( ); void sincn_antideriv_test ( ); void sincn_deriv_test ( ); void sincn_deriv2_test ( ); void sincn_fun_test ( ); void sincu_antideriv_test ( ); void sincu_deriv_test ( ); void sincu_deriv2_test ( ); void sincu_fun_test ( ); void gnuplot_fx ( int n, double *x, double *y, string header ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ // // Purpose: // // sinc_test() tests sinc(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { timestamp ( ); printf ( "\n" ); printf ( "sinc_test():\n" ); printf ( " C++ version\n" ); printf ( " Test sinc().\n" ); sincn_antideriv_test ( ); sincn_deriv_test ( ); sincn_deriv2_test ( ); sincn_fun_test ( ); sincu_antideriv_test ( ); sincu_deriv_test ( ); sincu_deriv2_test ( ); sincu_fun_test ( ); // // Terminate. // printf ( "\n" ); printf ( "sinc_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } //****************************************************************************80 void sincn_antideriv_test ( ) //****************************************************************************80 // // Purpose: // // sincn_antideriv_test() tests sincn_antideriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincn_antideriv_test():\n"; cout << " sincn_antideriv() evaluates the antiderivative\n"; cout << " of the sincn function.\n"; a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincn_antideriv ( x[i] ); } header = "sincn_antideriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincn_deriv_test ( ) //****************************************************************************80 // // Purpose: // // sincn_deriv_test() tests sincn_deriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincn_deriv_test():\n"; cout << " sincn_deriv() evaluates the derivative\n"; cout << " of the sincn function.\n"; a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincn_deriv ( x[i] ); } header = "sincn_deriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincn_deriv2_test ( ) //****************************************************************************80 // // Purpose: // // sincn_deriv2_test() tests sincn_deriv2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincn_deriv2_test():\n"; cout << " sincn_deriv2() evaluates the second derivative\n"; cout << " of the sincn function.\n"; a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincn_deriv2 ( x[i] ); } header = "sincn_deriv2"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincn_fun_test ( ) //****************************************************************************80 // // Purpose: // // sincn_fun_test() tests sincn_fun(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincn_fun_test():\n"; cout << " sincn_fun() evaluates the sincn function.\n"; a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincn_fun ( x[i] ); } header = "sincn_fun"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincu_antideriv_test ( ) //****************************************************************************80 // // Purpose: // // sincu_antideriv_test() tests sincu_antideriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincu_antideriv_test():\n"; cout << " sincu_antideriv() evaluates the antiderivative\n"; cout << " of the sincu function.\n"; a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincu_antideriv ( x[i] ); } header = "sincu_antideriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincu_deriv_test ( ) //****************************************************************************80 // // Purpose: // // sincu_deriv_test() tests sincu_deriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincu_deriv_test():\n"; cout << " sincu_deriv() evaluates the derivative\n"; cout << " of the sincu function.\n"; a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincu_deriv ( x[i] ); } header = "sincu_deriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincu_deriv2_test ( ) //****************************************************************************80 // // Purpose: // // sincu_deriv2_test() tests sincu_deriv2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincu_deriv2_test():\n"; cout << " sincu_deriv2() evaluates the second derivative\n"; cout << " of the sincu function.\n"; a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincu_deriv2 ( x[i] ); } header = "sincu_deriv2"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void sincu_fun_test ( ) //****************************************************************************80 // // Purpose: // // sincu_fun_test() tests sincu_fun(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2025 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 201; double *x; double *y; cout << "\n"; cout << "sincu_fun_test():\n"; cout << " sincu_fun() evaluates the sincu function.\n"; a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = sincu_fun ( x[i] ); } header = "sincu_fun"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; 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: // // 29 March 2025 // // 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 }