# include # include # include # include # include # include # include using namespace std; # include "sigmoid_derivative.hpp" int main ( ); void sigmoid_derivative_coef_test ( ); void sigmoid_derivative_value_test ( ); void sigmoid_derivative_plot ( int nvec, double *xvec, double *yvec, string header ); void sigmoid_poly_print ( int n, double *a, string title ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // sigmoid_derivative_test() tests sigmoid_derivative(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 May 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "sigmoid_derivative_test():\n"; cout << " C++ version\n"; cout << " Test sigmoid_derivative.\n"; sigmoid_derivative_coef_test ( ); sigmoid_derivative_value_test ( ); // // Terminate. // cout << "\n"; cout << "sigmoid_derivative_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void sigmoid_derivative_coef_test ( ) //****************************************************************************80 // // 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; string label; int n; cout << "\n"; cout << "sigmoid_derivative_coef_test():\n"; cout << " sigmoid_derivative_coef() returns the coefficients of\n"; cout << " the expansion of the nth derivative of the sigmoid\n"; cout << " function in terms of powers of the sigmoid function.\n"; for ( n = 0; n <= 4; n++ ) { coef = sigmoid_derivative_coef ( n ); label = " s^(" + to_string ( n ) + ")(x)"; sigmoid_poly_print ( n + 2, coef, label ); delete [] coef; } return; } //****************************************************************************80 void sigmoid_derivative_value_test ( ) //****************************************************************************80 // // 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 // { string header; int i; int n; int nvec = 51; double *xvec; double *yvec; cout << "\n"; cout << "sigmoid_derivative_value_test():\n"; cout << " sigmoid_derivative_value() evaluates the nth derivative\n"; cout << " of the sigmoid function at the location x.\n"; xvec = r8vec_linspace_new ( nvec, -5.0, +5.0 ); yvec = new double[nvec]; for ( n = 0; n <= 3; n++ ) { for ( i = 0; i < nvec; i++ ) { yvec[i] = sigmoid_derivative_value ( n, xvec[i] ); } header = "sigmoid_derivative_" + to_string ( n ); sigmoid_derivative_plot ( nvec, xvec, yvec, header ); } delete [] xvec; delete [] yvec; return; } //****************************************************************************80 void sigmoid_derivative_plot ( int nvec, double *xvec, double *yvec, string header ) //****************************************************************************80 // // 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 // { 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 < nvec; i++ ) { data_unit << xvec[i] << " " << yvec[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 void sigmoid_poly_print ( int n, double *a, string title ) //****************************************************************************80 // // 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. // // string TITLE, a title. // { int i; double mag; char plus_minus; cout << "\n"; cout << title << " = \n"; 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 ) { cout << " " << plus_minus << " " << mag << " * s(x) ^ " << i << "\n"; } else if ( i == 1 ) { cout << " " << plus_minus << " " << mag << " * s(x)\n"; } else if ( i == 0 ) { cout << " " << plus_minus << " " << mag << "\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 }