# include # include # include # include # include # include "sinc.h" 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, char *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: 28 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; } /******************************************************************************/ void sincn_antideriv_test ( ) /******************************************************************************/ /* Purpose: sincn_antideriv_test() tests sincn_antideriv(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincn_antideriv"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincn_antideriv_test():\n" ); printf ( " sincn_antideriv() evaluates the antiderivative\n" ); printf ( " of the sincn function.\n" ); a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincn_antideriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincn_deriv_test ( ) /******************************************************************************/ /* Purpose: sincn_deriv_test() tests sincn_deriv(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincn_deriv"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincn_deriv_test():\n" ); printf ( " sincn_deriv() evaluates the derivative\n" ); printf ( " of the sincn function.\n" ); a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincn_deriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincn_deriv2_test ( ) /******************************************************************************/ /* Purpose: sincn_deriv2_test() tests sincn_deriv2(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincn_deriv2"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincn_deriv2_test():\n" ); printf ( " sincn_deriv2() evaluates the second derivative\n" ); printf ( " of the sincn function.\n" ); a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincn_deriv2 ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincn_fun_test ( ) /******************************************************************************/ /* Purpose: sincn_fun_test() tests sincn_fun(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincn_fun"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincn_fun_test():\n" ); printf ( " sincn_fun() evaluates the sincn function.\n" ); a = -7.0; b = +7.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincn_fun ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincu_antideriv_test ( ) /******************************************************************************/ /* Purpose: sincu_antideriv_test() tests sincu_antideriv(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincu_antideriv"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincu_antideriv_test():\n" ); printf ( " sincu_antideriv() evaluates the antiderivative\n" ); printf ( " of the sincu function.\n" ); a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincu_antideriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincu_deriv_test ( ) /******************************************************************************/ /* Purpose: sincu_deriv_test() tests sincu_deriv(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincu_deriv"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincu_deriv_test():\n" ); printf ( " sincu_deriv() evaluates the derivative\n" ); printf ( " of the sincu function.\n" ); a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincu_deriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincu_deriv2_test ( ) /******************************************************************************/ /* Purpose: sincu_deriv2_test() tests sincu_deriv2(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincu_deriv2"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincu_deriv2_test():\n" ); printf ( " sincu_deriv2() evaluates the second derivative\n" ); printf ( " of the sincu function.\n" ); a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincu_deriv2 ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void sincu_fun_test ( ) /******************************************************************************/ /* Purpose: sincu_fun_test() tests sincu_fun(). Licensing: This code is distributed under the MIT license. Modified: 28 March 2025 Author: John Burkardt */ { double a; double b; char *header = "sincu_fun"; int i; int n = 201; double *x; double *y; printf ( "\n" ); printf ( "sincu_fun_test():\n" ); printf ( " sincu_fun() evaluates the sincu function.\n" ); a = -20.0; b = +20.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = sincu_fun ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void gnuplot_fx ( int n, double *x, double *y, char *header ) /******************************************************************************/ /* Purpose: gnuplot_fx() plots n values y=f(x). Discussion: Actually, we simply create two files for processing by gnuplot(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; int i; char png_filename[255]; /* 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 ( " 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 ); 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 }