# include # include # include # include # include "gaussian.h" int main ( ); void gaussian_antideriv_test ( ); void gaussian_n_test ( ); void gaussian_mu_test ( ); void gaussian_sigma_test ( ); void filename_inc ( char *filename ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: gaussian_test() tests gaussian(). Licensing: This code is distributed under the MIT license. Modified: 09 July 2025 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "gaussian_test():\n" ); printf ( " C version\n" ); printf ( " gaussian() evaluates a Gaussian function,\n" ); printf ( " its antiderivative, or derivatives of any order.\n" ); gaussian_antideriv_test ( ); gaussian_n_test ( ); gaussian_mu_test ( ); gaussian_sigma_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "gaussian_test()\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ void gaussian_antideriv_test ( ) /******************************************************************************/ /* Purpose: gaussian_antideriv_test() tests gaussian_antideriv(). Licensing: This code is distributed under the MIT license. Modified: 09 July 2025 Author: John Burkardt */ { double a; double b; char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char *header = "gaussian_antideriv"; int i; double mu; int n; char png_filename[255]; double sigma; double *x; double *y; printf ( "\n" ); printf ( "gaussian_antideriv_test():\n" ); printf ( " Compute gaussian_antideriv(mu=0,sigma=1;x).\n" ); printf ( "\n" ); mu = 0.0; sigma = 1.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = gaussian_antideriv ( mu, sigma, x[i] ); } /* Create a graphics data file. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data_unit, "%14.6g %14.6g\n", x[i], y[i] ); } fclose ( data_unit ); printf ( "\n" ); printf ( " Created graphics data file '%s'\n", data_filename ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); /* Create graphics command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "set term png\n" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set xlabel '<--- X --->'\n" ); fprintf ( command_unit, "set ylabel '<-- Y(X) -->'\n" ); fprintf ( command_unit, "set title 'antideriv(mu=0,sigma=1;x)'\n" ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue'\n", data_filename ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); /* Free memory. */ free ( x ); free ( y ); return; } /******************************************************************************/ void gaussian_mu_test ( ) /******************************************************************************/ /* Purpose: gaussian_mu() varies the mu parameter. Licensing: This code is distributed under the MIT license. Modified: 09 July 2025 Author: John Burkardt */ { double a; double b; char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char *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; char png_filename[255]; double sigma; double *x; double *y; printf ( "\n" ); printf ( "gaussian_sigma_test():\n" ); printf ( " Compute gaussian(n,mu,sigma;x) for various values of mu.\n" ); printf ( "\n" ); n_deriv = 0; sigma = 1.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( 5 * sizeof ( double ) ); /* Create a graphics data file. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data_unit, " %14.6g", x[i] ); for ( j = 0; j < 5; j++ ) { y[j] = gaussian_value ( n_deriv, mu[j], sigma, x[i] ); fprintf ( data_unit, " %14.6g", y[j] ); } fprintf ( data_unit, "\n" ); } fclose ( data_unit ); printf ( "\n" ); printf ( " Created graphics data file '%s'\n", data_filename ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); /* Create graphics command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "set term png\n" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set xlabel '<--- X --->'\n" ); fprintf ( command_unit, "set ylabel '<-- Y(X) -->'\n" ); fprintf ( command_unit, "set title 'g(mu=[-1,-1/2,0,1/2,1],sigma;x)'\n" ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:3 lw 3 linecolor rgb 'red',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:4 lw 3 linecolor rgb 'green',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:5 lw 3 linecolor rgb 'cyan',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:6 lw 3 linecolor rgb 'magenta',\n", data_filename ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); /* Free memory. */ free ( x ); free ( y ); return; } /******************************************************************************/ void gaussian_n_test ( ) /******************************************************************************/ /* Purpose: gaussian_n_test() varies the n parameter. Licensing: This code is distributed under the MIT license. Modified: 09 July 2025 Author: John Burkardt */ { double a; double b; char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; int deriv; char header[80]; int i; double mu; char png_filename[255]; double sigma; int n = 101; double *x; double *y; printf ( "\n" ); printf ( "gaussian_n_test():\n" ); printf ( " gaussian(n,mu,sigma;x) evaluates the nth derivative.\n" ); printf ( "\n" ); mu = 0.0; sigma = 1.0; a = - 4.0; b = + 4.0; strcpy ( header, "gaussian_deriv_0" ); x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); 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. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data_unit, "%14.6g %14.6g\n", x[i], y[i] ); } fclose ( data_unit ); printf ( "\n" ); printf ( " Created graphics data file '%s'\n", data_filename ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); /* Create graphics command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "set term png\n" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set xlabel '<--- X --->'\n" ); fprintf ( command_unit, "set ylabel '<-- Y(X) -->'\n" ); fprintf ( command_unit, "set title '%s'\n", header ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue'\n", data_filename ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); filename_inc ( header ); } /* Free memory. */ free ( x ); free ( y ); return; } /******************************************************************************/ void gaussian_sigma_test ( ) /******************************************************************************/ /* Purpose: gaussian_sigma_test() varies the sigma parameter. Licensing: This code is distributed under the MIT license. Modified: 09 July 2025 Author: John Burkardt */ { double a; double b; char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char *header = "gaussian_sigma"; int i; int j; double mu; int n; int n_deriv; char png_filename[255]; double sigma[5] = { 0.25, 0.50, 1.0, 1.5, 2.0 }; double *x; double *y; printf ( "\n" ); printf ( "gaussian_sigma_test():\n" ); printf ( " Compute gaussian(n,mu,sigma,x) for various values of sigma.\n" ); printf ( "\n" ); n_deriv = 0; mu = 0.0; a = - 4.0; b = + 4.0; n = 101; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( 5 * sizeof ( double ) ); /* Create a graphics data file. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data_unit, " %14.6g", x[i] ); for ( j = 0; j < 5; j++ ) { y[j] = gaussian_value ( n_deriv, mu, sigma[j], x[i] ); fprintf ( data_unit, " %14.6g", y[j] ); } fprintf ( data_unit, "\n" ); } fclose ( data_unit ); printf ( "\n" ); printf ( " Created graphics data file '%s'\n", data_filename ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); /* Create graphics command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "set term png\n" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set xlabel '<--- X --->'\n" ); fprintf ( command_unit, "set ylabel '<-- Y(X) -->'\n" ); fprintf ( command_unit, "set title 'g(mu,sigma=[0.25,0.50,1,1.5,2];x)'\n" ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:3 lw 3 linecolor rgb 'red',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:4 lw 3 linecolor rgb 'green',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:5 lw 3 linecolor rgb 'cyan',\\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:6 lw 3 linecolor rgb 'magenta',\n", data_filename ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); /* Free memory. */ free ( x ); free ( y ); return; } /******************************************************************************/ void filename_inc ( char *filename ) /******************************************************************************/ /* 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: 07 April 2014 Author: John Burkardt Input: char *FILENAME, the filename to be incremented. Output: char *FILENAME, the incremented filename. */ { int change; int n; char *t; n = strlen ( filename ); if ( n <= 0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "filename_inc(): Fatal error\n" ); fprintf ( stderr, " The input string is empty.\n" ); exit ( 1 ); } change = 0; t = filename + n - 1; while ( 0 < n ) { if ( '0' <= *t && *t <= '9' ) { change = change + 1; if ( *t == '9' ) { *t = '0'; } else { *t = *t + 1; return; } } t--; n--; } /* No digits were found. Return blank. */ if ( change == 0 ) { n = strlen ( filename ); t = filename + n - 1; while ( 0 < n ) { *t = ' '; t--; n--; } } return; } /******************************************************************************/ double *r8vec_linspace_new ( int n, double a, double b ) /******************************************************************************/ /* 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, B, the first and last entries. Output: double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. */ { int i; double *x; x = ( double * ) malloc ( n * sizeof ( double ) ); if ( n == 1 ) { x[0] = ( a + b ) / 2.0; } else { for ( i = 0; i < n; i++ ) { x[i] = ( ( double ) ( n - 1 - i ) * a + ( double ) ( i ) * b ) / ( double ) ( n - 1 ); } } return x; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }