# include # include # include # include # include # include # include "lagrange.h" 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 ); char *s_escape_tex ( char *s1 ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: lagrange_test() tests lagrange(). Licensing: This code is distributed under the MIT license. Modified: 06 August 2025 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "lagrange_test():\n" ); printf ( " C version\n" ); printf ( " Test lagrange()\n" ); lagrange_basis_antideriv_test ( ); lagrange_basis_coef_test ( ); lagrange_basis_deriv_test ( ); lagrange_basis_deriv2_test ( ); lagrange_basis_value_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "lagrange_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void lagrange_basis_antideriv_test ( ) /******************************************************************************/ /* Purpose: lagrange_basis_antideriv_test() tests lagrange_basis_antideriv(). Licensing: This code is distributed under the MIT license. Modified: 07 August 2025 Author: John Burkardt */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char data2_filename[255]; FILE *data2_unit; int i; int ipol; int nplot; int npol; char output_filename[255]; char prefix[100]; char *prefix2; double *pcof_antideriv; double *xpol; double *xplot; double *yplot; printf ( "\n" ); printf ( "lagrange_basis_antideriv_test():\n" ); printf ( " lagrange_basis_antideriv() evaluates the antiderivative\n" ); printf ( " 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 = ( double * ) malloc ( nplot * sizeof ( double ) ); for ( i = 0; i < nplot; i++ ) { yplot[i] = r8poly_value ( npol, pcof_antideriv, xplot[i] ); } strcpy ( prefix, "lagrange_basis_antideriv" ); /* Create the data file. */ sprintf ( data_filename, "%s_data.txt", prefix ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < nplot; i++ ) { fprintf ( data_unit, "%g %g\n", xplot[i], yplot[i] ); } fclose ( data_unit ); printf ( " Created graphics data file '%s'\n", data_filename ); /* Create the data2 file. */ sprintf ( data2_filename, "%s_data2.txt", prefix ); data2_unit = fopen ( data2_filename, "wt" ); for ( i = 0; i < npol; i++ ) { fprintf ( data2_unit, "%g %g\n", xpol[i], 0.0 ); } fclose ( data2_unit ); printf ( " Created graphics data file '%s'\n", data2_filename ); /* Plot the selected data. */ sprintf ( command_filename, "%s_commands.txt", prefix ); 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 nokey\n" ); sprintf ( output_filename, "%s.png", prefix ); fprintf ( command_unit, "set output '%s\n", output_filename ); fprintf ( command_unit, "set xlabel '<---X--->'\n" ); fprintf ( command_unit, "set ylabel '<---Antideriv(L(i)(X))--->'\n" ); prefix2 = s_escape_tex ( prefix ); fprintf ( command_unit, "set title '%s'\n", prefix2 ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "$AXIS << EOL\n" ); fprintf ( command_unit, "-3.3 0.0\n" ); fprintf ( command_unit, " 3.3 0.0\n" ); fprintf ( command_unit, "EOL\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n", data2_filename ); fprintf ( command_unit, " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n" ); fclose ( command_unit ); printf ( " Created graphics command file '%s'\n", command_filename ); /* Free memory. */ free ( pcof_antideriv ); free ( xplot ); free ( xpol ); free ( yplot ); return; } /******************************************************************************/ void lagrange_basis_coef_test ( ) /******************************************************************************/ /* 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; char s[15]; double *xpol; printf ( "\n" ); printf ( "lagrange_basis_coef_test():\n" ); printf ( " lagrange_basis_coef() returns the coefficients\n" ); printf ( " 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 ); sprintf ( s, "L(%d)(x)", ipol ); r8poly_print ( npol, pcof, s ); free ( pcof ); } /* Free memory. */ free ( xpol ); return; } /******************************************************************************/ void lagrange_basis_deriv_test ( ) /******************************************************************************/ /* 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 */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char data2_filename[255]; FILE *data2_unit; int i; int ipol; int nplot; int npol; char output_filename[255]; char prefix[100]; char *prefix2; double *xpol; double *xplot; double *yplot; printf ( "\n" ); printf ( "lagrange_basis_deriv_test():\n" ); printf ( " lagrange_basis_deriv() evaluates the derivative\n" ); printf ( " 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 = ( double * ) malloc ( nplot * sizeof ( double ) ); for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_deriv ( npol, ipol, xpol, xplot[i] ); } strcpy ( prefix, "lagrange_basis_deriv" ); /* Create the data file. */ sprintf ( data_filename, "%s_data.txt", prefix ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < nplot; i++ ) { fprintf ( data_unit, "%g %g\n", xplot[i], yplot[i] ); } fclose ( data_unit ); printf ( " Created graphics data file '%s'\n", data_filename ); /* Create the data2 file. */ sprintf ( data2_filename, "%s_data2.txt", prefix ); data2_unit = fopen ( data2_filename, "wt" ); for ( i = 0; i < npol; i++ ) { fprintf ( data2_unit, "%g %g\n", xpol[i], 0.0 ); } fclose ( data2_unit ); printf ( " Created graphics data file '%s'\n", data2_filename ); /* Plot the selected data. */ sprintf ( command_filename, "%s_commands.txt", prefix ); 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 nokey\n" ); sprintf ( output_filename, "%s.png", prefix ); fprintf ( command_unit, "set output '%s\n", output_filename ); fprintf ( command_unit, "set xlabel '<---X--->'\n" ); fprintf ( command_unit, "set ylabel '<---dL(i)(X)/dx)--->'\n" ); prefix2 = s_escape_tex ( prefix ); fprintf ( command_unit, "set title '%s'\n", prefix2 ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "$AXIS << EOL\n" ); fprintf ( command_unit, "-3.3 0.0\n" ); fprintf ( command_unit, " 3.3 0.0\n" ); fprintf ( command_unit, "EOL\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n", data2_filename ); fprintf ( command_unit, " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n" ); fclose ( command_unit ); printf ( " Created graphics command file '%s'\n", command_filename ); /* Free memory. */ free ( xplot ); free ( xpol ); free ( yplot ); return; } /******************************************************************************/ void lagrange_basis_deriv2_test ( ) /******************************************************************************/ /* 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 */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char data2_filename[255]; FILE *data2_unit; int i; int ipol; int nplot; int npol; char output_filename[255]; char prefix[100]; char *prefix2; double *xpol; double *xplot; double *yplot; printf ( "\n" ); printf ( "lagrange_basis_deriv2_test():\n" ); printf ( " lagrange_basis_deriv2() evaluates the second derivative\n" ); printf ( " 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 = ( double * ) malloc ( nplot * sizeof ( double ) ); for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_deriv2 ( npol, ipol, xpol, xplot[i] ); } strcpy ( prefix, "lagrange_basis_deriv2" ); /* Create the data file. */ sprintf ( data_filename, "%s_data.txt", prefix ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < nplot; i++ ) { fprintf ( data_unit, "%g %g\n", xplot[i], yplot[i] ); } fclose ( data_unit ); printf ( " Created graphics data file '%s'\n", data_filename ); /* Create the data2 file. */ sprintf ( data2_filename, "%s_data2.txt", prefix ); data2_unit = fopen ( data2_filename, "wt" ); for ( i = 0; i < npol; i++ ) { fprintf ( data2_unit, "%g %g\n", xpol[i], 0.0 ); } fclose ( data2_unit ); printf ( " Created graphics data file '%s'\n", data2_filename ); /* Plot the selected data. */ sprintf ( command_filename, "%s_commands.txt", prefix ); 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 nokey\n" ); sprintf ( output_filename, "%s.png", prefix ); fprintf ( command_unit, "set output '%s\n", output_filename ); fprintf ( command_unit, "set xlabel '<---X--->'\n" ); fprintf ( command_unit, "set ylabel '<---d2L(i)(X)/dx2)--->'\n" ); prefix2 = s_escape_tex ( prefix ); fprintf ( command_unit, "set title '%s'\n", prefix2 ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "$AXIS << EOL\n" ); fprintf ( command_unit, "-3.3 0.0\n" ); fprintf ( command_unit, " 3.3 0.0\n" ); fprintf ( command_unit, "EOL\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n", data2_filename ); fprintf ( command_unit, " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black'\n" ); fclose ( command_unit ); printf ( " Created graphics command file '%s'\n", command_filename ); /* Free memory. */ free ( xplot ); free ( xpol ); free ( yplot ); return; } /******************************************************************************/ void lagrange_basis_value_test ( ) /******************************************************************************/ /* 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 */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; char data2_filename[255]; FILE *data2_unit; int i; int ipol; int nplot; int npol; char output_filename[255]; char prefix[100]; char *prefix2; double *xpol; double *xplot; double *yplot; printf ( "\n" ); printf ( "lagrange_basis_value_test():\n" ); printf ( " lagrange_basis_value() evaluates\n" ); printf ( " 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 = ( double * ) malloc ( nplot * sizeof ( double ) ); for ( i = 0; i < nplot; i++ ) { yplot[i] = lagrange_basis_value ( npol, ipol, xpol, xplot[i] ); } strcpy ( prefix, "lagrange_basis_value" ); /* Create the data file. */ sprintf ( data_filename, "%s_data.txt", prefix ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < nplot; i++ ) { fprintf ( data_unit, "%g %g\n", xplot[i], yplot[i] ); } fclose ( data_unit ); printf ( " Created graphics data file '%s'\n", data_filename ); /* Create the data2 file. */ sprintf ( data2_filename, "%s_data2.txt", prefix ); data2_unit = fopen ( data2_filename, "wt" ); for ( i = 0; i < npol; i++ ) { fprintf ( data2_unit, "%g %g\n", xpol[i], 0.0 ); } fclose ( data2_unit ); printf ( " Created graphics data file '%s'\n", data2_filename ); /* Plot the selected data. */ sprintf ( command_filename, "%s_commands.txt", prefix ); 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 nokey\n" ); sprintf ( output_filename, "%s.png", prefix ); fprintf ( command_unit, "set output '%s\n", output_filename ); fprintf ( command_unit, "set xlabel '<---X--->'\n" ); fprintf ( command_unit, "set ylabel '<---L(i)(X)--->'\n" ); prefix2 = s_escape_tex ( prefix ); fprintf ( command_unit, "set title '%s'\n", prefix2 ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "$AXIS << EOL\n" ); fprintf ( command_unit, "-3.3 0.0\n" ); fprintf ( command_unit, " 3.3 0.0\n" ); fprintf ( command_unit, "EOL\n" ); fprintf ( command_unit, "$BLIP << EOL\n" ); fprintf ( command_unit, "-1.0 0.0\n" ); fprintf ( command_unit, "-1.0 1.0\n" ); fprintf ( command_unit, "EOL\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command_unit, " '%s' using 1:2 with points pt 7 ps 2 lc rgb 'red', \\\n", data2_filename ); fprintf ( command_unit, " $AXIS using 1:2 with lines lw 3 linecolor rgb 'black', \\\n" ); fprintf ( command_unit, " $BLIP using 1:2 with lines lw 3 linecolor rgb 'red'\n" ); fclose ( command_unit ); printf ( " Created graphics command file '%s'\n", command_filename ); /* Free memory. */ free ( xplot ); free ( xpol ); free ( yplot ); 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; } /******************************************************************************/ char *s_escape_tex ( char *s1 ) /******************************************************************************/ /* 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: 19 January 2007 Author: John Burkardt Input: char *S1, the string to be de-escaped. Output: char *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_length; int s2_pos; int slash_count; s1_length = strlen ( s1 ); /* We need to know how many slashes occur in S1, so we can allocate S2. Note that, alas, the backslash is also the escape in C++, so we have to say '\\' when we mean '\'! */ 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_length = s1_length + slash_count; s2 = ( char * ) malloc ( ( s2_length + 1 ) * sizeof ( char ) ); /* 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; return s2; } /******************************************************************************/ 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: 17 June 2014 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 }