# include # include # include # include # include # include # include # include using namespace std; # include "lagrange.hpp" int main ( ); void lagrange_basis_antideriv_test ( ); void lagrange_basis_coef_test ( ); void lagrange_basis_deriv_test ( ); void lagrange_basis_deriv2_test ( ); void lagrange_basis_value_test ( ); double *r8vec_linspace_new ( int n, double a, double b ); string s_escape_tex ( string s1 ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // lagrange_test() tests lagrange(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 August 2025 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "lagrange_test():\n"; cout << " C++ version\n"; cout << " Test lagrange()\n"; lagrange_basis_antideriv_test ( ); lagrange_basis_coef_test ( ); lagrange_basis_deriv_test ( ); lagrange_basis_deriv2_test ( ); lagrange_basis_value_test ( ); // // Terminate. // cout << "\n"; cout << "lagrange_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void lagrange_basis_antideriv_test ( ) //****************************************************************************80 // // Purpose: // // lagrange_basis_antideriv_test() tests lagrange_basis_antideriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 August 2025 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string data2_filename; ofstream data2_unit; int i; int ipol; int nplot; int npol; string output_filename; string prefix; string prefix2; double *pcof_antideriv; double *xpol; double *xplot; double *yplot; cout << "\n"; cout << "lagrange_basis_antideriv_test():\n"; cout << " lagrange_basis_antideriv() evaluates the antiderivative\n"; cout << " of a Lagrange basis polynomial.\n"; npol = 7; ipol = 2; xpol = r8vec_linspace_new ( npol, -3.0, +3.0 ); // // Get coefficients of antiderivative. // pcof_antideriv = lagrange_basis_antideriv ( npol, ipol, xpol ); nplot = 101; xplot = r8vec_linspace_new ( nplot, -3.3, +3.5 ); yplot = new double[nplot]; for ( i = 0; i < nplot; i++ ) { yplot[i] = r8poly_value ( npol, pcof_antideriv, xplot[i] ); } prefix = "lagrange_basis_antideriv"; // // Create the data file. // data_filename = prefix + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < nplot; i++ ) { data_unit << xplot[i] << " " << yplot[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; // // Create the data2 file. // data2_filename = prefix + "_data2.txt"; data2_unit.open ( data2_filename.c_str ( ) ); for ( i = 0; i < npol; i++ ) { data2_unit << xpol[i] << " " << 0.0 << "\n"; } data2_unit.close ( ); cout << " Created graphics data file '" << data2_filename << "'\n"; // // Plot the selected data. // command_filename = prefix + "_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 nokey\n"; output_filename = prefix + ".png"; command_unit << "set output '" << output_filename << "'\n"; command_unit << "set xlabel '<---X--->'\n"; command_unit << "set ylabel '<---Antideriv(L(i)(X))--->'\n"; prefix2 = s_escape_tex ( prefix ); command_unit << "set title '" << prefix2 << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "$AXIS << EOL\n"; command_unit << "-3.3 0.0\n"; command_unit << " 3.3 0.0\n"; command_unit << "EOL\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue', \\\n"; command_unit << " '" << data2_filename << "' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n"; command_unit << " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n"; command_unit.close ( ); cout << " Created graphics command file '" << command_filename << "'."; // // Free memory. // delete [] pcof_antideriv; delete [] xplot; delete [] xpol; delete [] yplot; return; } //****************************************************************************80 void lagrange_basis_coef_test ( ) //****************************************************************************80 // // Purpose: // // lagrange_basis_coef_test() tests lagrange_basis_coef(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 August 2025 // // Author: // // John Burkardt // { int ipol; int npol; double *pcof; string s; double *xpol; cout << "\n"; cout << "lagrange_basis_coef_test():\n"; cout << " lagrange_basis_coef() returns the coefficients\n"; cout << " for a Lagrange basis polynomial.\n"; npol = 7; xpol = r8vec_linspace_new ( npol, -3.0, +3.0 ); r8vec_print ( npol, xpol, " Abscissas:" ); for ( ipol = 0; ipol < npol; ipol++ ) { pcof = lagrange_basis_coef ( npol, ipol, xpol ); s = "L(" + to_string ( ipol ) + ")(x)"; r8poly_print ( npol, pcof, s ); delete [] pcof; } // // Free memory. // delete [] xpol; return; } //****************************************************************************80 void lagrange_basis_deriv_test ( ) //****************************************************************************80 // // Purpose: // // lagrange_basis_deriv_test() tests lagrange_basis_deriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 August 2025 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string data2_filename; ofstream data2_unit; int i; int ipol; int nplot; int npol; string output_filename; string prefix; string prefix2; double *xpol; double *xplot; double *yplot; cout << "\n"; cout << "lagrange_basis_deriv_test():\n"; cout << " lagrange_basis_deriv() evaluates the derivative\n"; cout << " of a Lagrange basis polynomial.\n"; npol = 7; ipol = 2; xpol = r8vec_linspace_new ( npol, -3.0, +3.0 ); nplot = 101; xplot = r8vec_linspace_new ( nplot, -3.3, +3.5 ); yplot = new double[nplot]; for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_deriv ( npol, ipol, xpol, xplot[i] ); } prefix = "lagrange_basis_deriv"; // // Create the data file. // data_filename = prefix + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < nplot; i++ ) { data_unit << xplot[i] << " " << yplot[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; // // Create the data2 file. // data2_filename = prefix + "_data2.txt"; data2_unit.open ( data2_filename.c_str ( ) ); for ( i = 0; i < npol; i++ ) { data2_unit << xpol[i] << " " << 0.0 << "\n"; } data2_unit.close ( ); cout << " Created graphics data file '" << data2_filename << "'\n"; // // Plot the selected data. // command_filename = prefix + "_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 nokey\n"; output_filename = prefix + ".png"; command_unit << "set output '" << output_filename << "\n"; command_unit << "set xlabel '<---X--->'\n"; command_unit << "set ylabel '<---dL(i)(X)/dx)--->'\n"; prefix2 = s_escape_tex ( prefix ); command_unit << "set title '" << prefix2 << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "$AXIS << EOL\n"; command_unit << "-3.3 0.0\n"; command_unit << " 3.3 0.0\n"; command_unit << "EOL\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue', \\\n"; command_unit << " '" << data2_filename << "' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n"; command_unit << " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n"; command_unit.close ( ); cout << " Created graphics command file '" << command_filename << "'."; // // Free memory. // delete [] xplot; delete [] xpol; delete [] yplot; return; } //****************************************************************************80 void lagrange_basis_deriv2_test ( ) //****************************************************************************80 // // Purpose: // // lagrange_basis_deriv2_test() tests lagrange_basis_deriv2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 August 2025 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string data2_filename; ofstream data2_unit; int i; int ipol; int nplot; int npol; string output_filename; string prefix; string prefix2; double *xpol; double *xplot; double *yplot; cout << "\n"; cout << "lagrange_basis_deriv2_test():\n"; cout << " lagrange_basis_deriv2() evaluates the second derivative\n"; cout << " of a Lagrange basis polynomial.\n"; npol = 7; ipol = 2; xpol = r8vec_linspace_new ( npol, -3.0, +3.0 ); nplot = 101; xplot = r8vec_linspace_new ( nplot, -3.3, +3.5 ); yplot = new double[nplot]; for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_deriv2 ( npol, ipol, xpol, xplot[i] ); } prefix = "lagrange_basis_deriv2"; // // Create the data file. // data_filename = prefix + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < nplot; i++ ) { data_unit << xplot[i] << " " << yplot[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; // // Create the data2 file. // data2_filename = prefix + "_data2.txt"; data2_unit.open ( data2_filename.c_str ( ) ); for ( i = 0; i < npol; i++ ) { data2_unit << xpol[i] << " " << 0.0 << "\n"; } data2_unit.close ( ); cout << " Created graphics data file '" << data2_filename << "'\n"; // // Plot the selected data. // command_filename = prefix + "_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 nokey\n"; output_filename = prefix + ".png"; command_unit << "set output '" << output_filename << "\n"; command_unit << "set xlabel '<---X--->'\n"; command_unit << "set ylabel '<---d2L(i)(X)/dx2)--->'\n"; prefix2 = s_escape_tex ( prefix ); command_unit << "set title '" << prefix2 << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "$AXIS << EOL\n"; command_unit << "-3.3 0.0\n"; command_unit << " 3.3 0.0\n"; command_unit << "EOL\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue', \\\n"; command_unit << " '" << data2_filename << "' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n"; command_unit << " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n"; command_unit.close ( ); cout << " Created graphics command file '" << command_filename << "'."; // // Free memory. // delete [] xplot; delete [] xpol; delete [] yplot; return; } //****************************************************************************80 void lagrange_basis_value_test ( ) //****************************************************************************80 // // Purpose: // // lagrange_basis_value_test() tests lagrange_basis_value(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 August 2025 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; string data2_filename; ofstream data2_unit; int i; int ipol; int nplot; int npol; string output_filename; string prefix; string prefix2; double *xpol; double *xplot; double *yplot; cout << "\n"; cout << "lagrange_basis_value_test():\n"; cout << " lagrange_basis_value() evaluates\n"; cout << " a Lagrange basis polynomial.\n"; npol = 7; ipol = 2; xpol = r8vec_linspace_new ( npol, -3.0, +3.0 ); nplot = 101; xplot = r8vec_linspace_new ( nplot, -3.3, +3.5 ); yplot = new double[nplot]; for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_value ( npol, ipol, xpol, xplot[i] ); } prefix = "lagrange_basis_value"; // // Create the data file. // data_filename = prefix +"_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < nplot; i++ ) { data_unit << xplot[i] << " " << yplot[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; // // Create the data2 file. // data2_filename = prefix + "_data2.txt"; data2_unit.open ( data2_filename.c_str ( ) ); for ( i = 0; i < npol; i++ ) { data2_unit << xpol[i] << " " << 0.0 << "\n"; } data2_unit.close ( ); cout << " Created graphics data file '" << data2_filename << "'\n"; // // Plot the selected data. // command_filename = prefix + "_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 nokey\n"; output_filename = prefix + ".png"; command_unit << "set output '" << output_filename << "\n"; command_unit << "set xlabel '<---X--->'\n"; command_unit << "set ylabel '<---L(i)(X)--->'\n"; prefix2 = s_escape_tex ( prefix ); command_unit << "set title '" << prefix2 << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "$AXIS << EOL\n"; command_unit << "-3.3 0.0\n"; command_unit << " 3.3 0.0\n"; command_unit << "EOL\n"; command_unit << "$BLIP << EOL\n"; command_unit << "-1.0 0.0\n"; command_unit << "-1.0 1.0\n"; command_unit << "EOL\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue', \\\n"; command_unit << " '" << data2_filename << "' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n"; command_unit << " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black', \\\n"; command_unit << " $BLIP using 1:2 with lines lw 3 linecolor rgb 'red'\n"; command_unit.close ( ); cout << " Created graphics command file '" << command_filename << "'."; // // Free memory. // delete [] xplot; delete [] xpol; delete [] yplot; 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 string s_escape_tex ( string s1 ) //****************************************************************************80 // // Purpose: // // s_escape_tex() de-escapes TeX escape sequences. // // Discussion: // // In particular, every occurrence of the characters '\', '_', // '^', '{' and '}' will be replaced by '\\', '\_', '\^', // '\{' and '\}'. A TeX interpreter, on seeing these character // strings, is then likely to return the original characters. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 August 2009 // // Author: // // John Burkardt // // Input: // // string S1, the string to be de-escaped. // // Output: // // string S_ESCAPE_TEX, a copy of the string, // modified to avoid TeX escapes. // { char ch; int s1_length; int s1_pos; char *s2; int s2_pos; string s3; int slash_count; s1_length = s1.length ( ); slash_count = 0; for ( s1_pos = 0; s1_pos < s1_length; s1_pos++ ) { ch = s1[s1_pos]; if ( ch == '\\' || ch == '_' || ch == '^' || ch == '{' || ch == '}' ) { slash_count = slash_count + 1; } } s2 = new char[s1_length + slash_count + 1]; // // Now copy S1 into S2. // s1_pos = 0; s2_pos = 0; for ( s1_pos = 0; s1_pos < s1_length; s1_pos++ ) { ch = s1[s1_pos]; if ( ch == '\\' || ch == '_' || ch == '^' || ch == '{' || ch == '}' ) { s2[s2_pos] = '\\'; s2_pos = s2_pos + 1; } s2[s2_pos] = ch; s2_pos = s2_pos + 1; } s2[s2_pos] = '\0'; s2_pos = s2_pos + 1; s3 = string ( s2 ); return s3; } //****************************************************************************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 }