# include # include # include # include # include # include "sigmoid_derivative.h" int main ( ); void sigmoid_derivative_coef_test ( ); void sigmoid_derivative_value_test ( ); void sigmoid_derivative_plot ( int nvec, double *xvec, double *yvec, char *header ); void sigmoid_poly_print ( int n, double *a, char *title ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: sigmoid_derivative_test() tests sigmoid_derivative(). Licensing: This code is distributed under the MIT license. Modified: 23 May 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "sigmoid_derivative_test():\n" ); printf ( " C version\n" ); printf ( " Test sigmoid_derivative.\n" ); sigmoid_derivative_coef_test ( ); sigmoid_derivative_value_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "sigmoid_derivative_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void sigmoid_derivative_coef_test ( ) /******************************************************************************/ /* Purpose: sigmoid_derivative_coef_test() tests sigmoid_derivative_coef(). Licensing: This code is distributed under the MIT license. Modified: 23 May 2024 Author: John Burkardt */ { double *coef; char label[255]; int n; printf ( "\n" ); printf ( "sigmoid_derivative_coef_test():\n" ); printf ( " sigmoid_derivative_coef() returns the coefficients of\n" ); printf ( " the expansion of the nth derivative of the sigmoid\n" ); printf ( " function in terms of powers of the sigmoid function.\n" ); for ( n = 0; n <= 4; n++ ) { coef = sigmoid_derivative_coef ( n ); sprintf ( label, " s^(%d)(x)", n ); sigmoid_poly_print ( n + 2, coef, label ); free ( coef ); } return; } /******************************************************************************/ void sigmoid_derivative_value_test ( ) /******************************************************************************/ /* Purpose: sigmoid_derivative_value_test() tests sigmoid_derivative_value(). Licensing: This code is distributed under the MIT license. Modified: 23 May 2024 Author: John Burkardt */ { char header[255]; int i; int n; int nvec = 51; double *xvec; double *yvec; printf ( "\n" ); printf ( "sigmoid_derivative_value_test():\n" ); printf ( " sigmoid_derivative_value() evaluates the nth derivative\n" ); printf ( " of the sigmoid function at the location x.\n" ); xvec = r8vec_linspace_new ( nvec, -5.0, +5.0 ); yvec = ( double * ) malloc ( nvec * sizeof ( double ) ); for ( n = 0; n <= 3; n++ ) { for ( i = 0; i < nvec; i++ ) { yvec[i] = sigmoid_derivative_value ( n, xvec[i] ); } sprintf ( header, "sigmoid_derivative_%d", n ); sigmoid_derivative_plot ( nvec, xvec, yvec, header ); } free ( xvec ); free ( yvec ); return; } /******************************************************************************/ void sigmoid_derivative_plot ( int nvec, double *xvec, double *yvec, char *header ) /******************************************************************************/ /* Purpose: sigmoid_derivative_plot() plots a sigmoid derivative. Discussion: Actually, we simply create two files for processing by gnuplot(). Licensing: This code is distributed under the MIT license. Modified: 23 May 2024 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 < nvec; i++ ) { fprintf ( data_unit, "%14.6g %14.6g\n", xvec[i], yvec[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; } /******************************************************************************/ void sigmoid_poly_print ( int n, double *a, char *title ) /******************************************************************************/ /* Purpose: sigmoid_poly_print() prints a polynomial in s(x). Discussion: The power sum form is: p(x) = a(0) + a(1) * x + ... + a(n-1) * x^(n-1) + a(n) * x^(n) Licensing: This code is distributed under the MIT license. Modified: 22 May 2024 Author: John Burkardt Input: int N, the dimension of A. double A[0:N-1], the polynomial coefficients. A(0) is the constant term and A(N-1) is the coefficient of X^N. char *TITLE, a title. */ { int i; double mag; char plus_minus; printf ( "\n" ); printf ( "%s = ", title ); for ( i = 0; i < n; i++ ) { if ( a[i] < 0.0 ) { plus_minus = '-'; } else { plus_minus = '+'; } mag = fabs ( a[i] ); if ( mag != 0.0 ) { if ( 2 <= i ) { printf ( " %c %14.6g * s(x) ^ %d\n", plus_minus, mag, i ); } else if ( i == 1 ) { printf ( " %c %14.6g * s(x)\n", plus_minus, mag ); } else if ( i == 0 ) { printf ( " %c %14.6g\n", plus_minus, mag ); } } } 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 }