# include # include # include # include # include # include using namespace std; # include "gaussian.hpp" int main ( ); void gaussian_antideriv_test ( ); void gaussian_n_test ( ); void gaussian_mu_test ( ); void gaussian_sigma_test ( ); void filename_inc ( string *filename ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // gaussian_test() tests gaussian(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 10 July 2025 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "gaussian_test():\n"; cout << " C++ version\n"; cout << " gaussian() evaluates a Gaussian function,\n"; cout << " its antiderivative, or derivatives of any order.\n"; gaussian_antideriv_test ( ); gaussian_n_test ( ); gaussian_mu_test ( ); gaussian_sigma_test ( ); // // Terminate. // cout << "\n"; cout << "gaussian_test()\n"; cout << " Normal end of execution.\n"; timestamp ( ); return 0; } //****************************************************************************80 void gaussian_antideriv_test ( ) //****************************************************************************80 // // Purpose: // // gaussian_antideriv_test() tests gaussian_antideriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 10 July 2025 // // Author: // // John Burkardt // { double a; double b; string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string header = "gaussian_antideriv"; int i; double mu; int n; string png_filename; double sigma; double *x; double *y; cout << "\n"; cout << "gaussian_antideriv_test():\n"; cout << " Compute gaussian_antideriv(mu=0,sigma=1;x).\n"; cout << "\n"; mu = 0.0; sigma = 1.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = gaussian_antideriv ( mu, sigma, x[i] ); } // // 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 << "\n"; 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 'antideriv(mu=0,sigma=1;x)'\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"; // // Free memory. // delete [] x; delete [] y; return; } //****************************************************************************80 void gaussian_mu_test ( ) //****************************************************************************80 // // Purpose: // // gaussian_mu() varies the mu parameter. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 10 July 2025 // // Author: // // John Burkardt // { double a; double b; string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string header = "gaussian_mu"; int i; int j; double mu[5] = { -1.0, -0.5, 0.0, 0.5, 1.0 }; int n; int n_deriv; string png_filename; double sigma; double *x; double *y; cout << "\n"; cout << "gaussian_sigma_test():\n"; cout << " Compute gaussian(n,mu,sigma;x) for various values of mu.\n"; cout << "\n"; n_deriv = 0; sigma = 1.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = new double[5]; // // 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]; for ( j = 0; j < 5; j++ ) { y[j] = gaussian_value ( n_deriv, mu[j], sigma, x[i] ); data_unit << " " << y[j]; } data_unit << "\n"; } data_unit.close ( ); cout << "\n"; 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 'g(mu=[-1,-1/2,0,1/2,1],sigma;x)'\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 << " '" << data_filename << "' using 1:3 lw 3 linecolor rgb 'red',\\\n"; command_unit << " '" << data_filename << "' using 1:4 lw 3 linecolor rgb 'green',\\\n"; command_unit << " '" << data_filename << "' using 1:5 lw 3 linecolor rgb 'cyan',\\\n"; command_unit << " '" << data_filename << "' using 1:6 lw 3 linecolor rgb 'magenta',\n"; command_unit.close ( ); cout << " Created command file '" << command_filename << "'\n"; // // Free memory. // delete [] x; delete [] y; return; } //****************************************************************************80 void gaussian_n_test ( ) //****************************************************************************80 // // Purpose: // // gaussian_n_test() varies the n parameter. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 10 July 2025 // // Author: // // John Burkardt // { double a; double b; string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; int deriv; string header; int i; double mu; string png_filename; double sigma; int n = 101; double *x; double *y; cout << "\n"; cout << "gaussian_n_test():\n"; cout << " gaussian(n,mu,sigma;x) evaluates the nth derivative.\n"; cout << "\n"; mu = 0.0; sigma = 1.0; a = - 4.0; b = + 4.0; header = "gaussian_deriv_0"; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( deriv = 0; deriv <= 5; deriv++ ) { for ( i = 0; i < n; i++ ) { y[i] = gaussian_value ( deriv, mu, sigma, x[i] ); } // // 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 << "\n"; 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"; filename_inc ( &header ); } // // Free memory. // delete [] x; delete [] y; return; } //****************************************************************************80 void gaussian_sigma_test ( ) //****************************************************************************80 // // Purpose: // // gaussian_sigma_test() varies the sigma parameter. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 10 July 2025 // // Author: // // John Burkardt // { double a; double b; string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string header = "gaussian_sigma"; int i; int j; double mu; int n; int n_deriv; string png_filename; double sigma[5] = { 0.25, 0.50, 1.0, 1.5, 2.0 }; double *x; double *y; cout << "\n"; cout << "gaussian_sigma_test():\n"; cout << " Compute gaussian(n,mu,sigma,x) for various values of sigma.\n"; cout << "\n"; n_deriv = 0; mu = 0.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = new double[5]; // // 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]; for ( j = 0; j < 5; j++ ) { y[j] = gaussian_value ( n_deriv, mu, sigma[j], x[i] ); data_unit << " " << y[j]; } data_unit << "\n"; } data_unit.close ( ); cout << "\n"; 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 'g(mu,sigma=[0.25,0.50,1,1.5,2];x)'\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 << " '" << data_filename << "' using 1:3 lw 3 linecolor rgb 'red',\\\n"; command_unit << " '" << data_filename << "' using 1:4 lw 3 linecolor rgb 'green',\\\n"; command_unit << " '" << data_filename << "' using 1:5 lw 3 linecolor rgb 'cyan',\\\n"; command_unit << " '" << data_filename << "' using 1:6 lw 3 linecolor rgb 'magenta',\n"; command_unit.close ( ); cout << " Created command file '" << command_filename << "'\n"; // // Free memory. // delete [] x; delete [] y; return; } //****************************************************************************80 void filename_inc ( string *filename ) //****************************************************************************80 // // Purpose: // // filename_inc() increments a partially numeric file name. // // Discussion: // // It is assumed that the digits in the name, whether scattered or // connected, represent a number that is to be increased by 1 on // each call. If this number is all 9's on input, the output number // is all 0's. Non-numeric letters of the name are unaffected. // // If the name is empty, then the routine stops. // // If the name contains no digits, the empty string is returned. // // Example: // // Input Output // ----- ------ // "a7to11.txt" "a7to12.txt" (typical case. Last digit incremented) // "a7to99.txt" "a8to00.txt" (last digit incremented, with carry.) // "a9to99.txt" "a0to00.txt" (wrap around) // "cat.txt" " " (no digits to increment) // " " STOP! (error) // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 22 November 2011 // // Author: // // John Burkardt // // Parameters: // // Input/output, string *FILENAME, the filename to be incremented. // { char c; int change; int i; int lens; lens = (*filename).length ( ); if ( lens <= 0 ) { cerr << "\n"; cerr << "filename_inc(): Fatal error!\n"; cerr << " The input string is empty.\n"; exit ( 1 ); } change = 0; for ( i = lens - 1; 0 <= i; i-- ) { c = (*filename)[i]; if ( '0' <= c && c <= '9' ) { change = change + 1; if ( c == '9' ) { c = '0'; (*filename)[i] = c; } else { c = c + 1; (*filename)[i] = c; return; } } } // // No digits were found. Return blank. // if ( change == 0 ) { for ( i = lens - 1; 0 <= i; i-- ) { (*filename)[i] = ' '; } } 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 }