# include # include # include # include # include # include using namespace std; int main ( int argc, char *argv[] ); char ch_cap ( char ch ); bool ch_eqi ( char ch1, char ch2 ); int ch_to_digit ( char ch ); void dtable_close_write ( ofstream &output ); double *dtable_data_read ( char *input_filename, int m, int n ); void dtable_data_write ( ofstream &output, int m, int n, double table[] ); void dtable_header_read ( char *input_filename, int *m, int *n ); void dtable_header_write ( char *output_filename, ofstream &output, int m, int n ); void dtable_write ( char *output_filename, int m, int n, double table[], bool header ); int file_column_count ( char *input_filename ); int file_row_count ( char *input_filename ); int i4_power ( int i, int j ); void product_rule_set ( int point_num_1d, double x_1d[], double w_1d[], double r_1d[], int dim_num, int point_num, double x[], double w[], double r[] ); int product_rule_size ( int point_num_1d, int dim_num ); double r8_epsilon ( void ); int s_len_trim ( char *s ); int s_to_i4 ( char *s, int *last, bool *error ); double s_to_r8 ( char *s, int *lchar, bool *error ); bool s_to_r8vec ( char *s, int n, double rvec[] ); int s_word_count ( char *s ); void timestamp ( void ); char *timestring ( void ); void tuple_next ( int m1, int m2, int n, int *rank, int x[] ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for PRODUCT_RULE. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 May 2007 // // Author: // // John Burkardt // { int dim; int dim_num; int dim_num_1d; bool error; bool header; int last; bool more; int point_num; int point_num_1d; int point_num_1d2; char quad_1d_filename[81]; char quad_filename[] = "product"; char quad_r_1d_filename[81]; char quad_r_filename[81]; char quad_w_1d_filename[81]; char quad_w_filename[81]; char quad_x_1d_filename[81]; char quad_x_filename[81]; double *r; double *r_1d; double *w; double *w_1d; double *x; double *x_1d; timestamp ( ); cout << "\n"; cout << "PRODUCT_RULE\n"; cout << " C++ version\n"; cout << "\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; cout << "\n"; cout << " Create a multidimensional product rule\n"; cout << " as a product of identical 1D integration rules.\n"; // // Get the quadrature file root name: // if ( 1 < argc ) { strcpy ( quad_1d_filename, argv[1] ); } else { cout << "\n"; cout << "PRODUCT_RULE:\n"; cout << " Enter the \"root\" name of the 1D quadrature files.\n"; cin.getline ( quad_1d_filename, sizeof ( quad_1d_filename ) ); } // // Create the names of: // the quadrature X file; // the quadrature W file; // the quadrature R file; // if ( 0 < s_len_trim ( quad_1d_filename ) ) { strcpy ( quad_x_1d_filename, quad_1d_filename ); strcat ( quad_x_1d_filename, "_x.txt" ); strcpy ( quad_w_1d_filename, quad_1d_filename ); strcat ( quad_w_1d_filename, "_w.txt" ); strcpy ( quad_r_1d_filename, quad_1d_filename ); strcat ( quad_r_1d_filename, "_r.txt" ); } else { strcpy ( quad_x_1d_filename, "x.txt" ); strcpy ( quad_w_1d_filename, "w.txt" ); strcpy ( quad_r_1d_filename, "r.txt" ); } // // The second command line argument is the spatial dimension of the product. // if ( 2 < argc ) { dim_num = s_to_i4 ( argv[2], &last, &error ); } else { cout << "\n"; cout << "PRODUCT_RULE:\n"; cout << " Please enter the desired spatial dimension of the product rule.\n"; cin >> dim_num; } // // Summarize the input. // cout << "\n"; cout << "PRODUCT_RULE: User input:\n"; cout << " Quadrature rule X file = \"" << quad_x_1d_filename << "\".\n"; cout << " Quadrature rule W file = \"" << quad_w_1d_filename << "\".\n"; cout << " Quadrature rule R file = \"" << quad_r_1d_filename << "\".\n"; cout << " Spatial dimension of product = " << dim_num << "\n"; // // Read the X file. // dtable_header_read ( quad_x_1d_filename, &dim_num_1d, &point_num_1d ); if ( dim_num_1d != 1 ) { cout << "\n"; cout << "PRODUCT_RULE - Fatal error!\n"; cout << " The 1D quadrature abscissa file should have exactly\n"; cout << " one value on each line.\n"; exit ( 1 ); } cout << "\n"; cout << " Number of points in 1D rule = " << point_num_1d << "\n"; x_1d = dtable_data_read ( quad_x_1d_filename, dim_num_1d, point_num_1d ); // // Read the W file. // dtable_header_read ( quad_w_1d_filename, &dim_num_1d, &point_num_1d2 ); if ( dim_num_1d != 1 ) { cout << "\n"; cout << "PRODUCT_RULE - Fatal error!\n"; cout << " The 1D quadrature weight file should have exactly\n"; cout << " one value on each line.\n"; exit ( 1 ); } if ( point_num_1d2 != point_num_1d ) { cout << "\n"; cout << "PRODUCT_RULE - Fatal error!\n"; cout << " The 1D quadrature weight file should have exactly\n"; cout << " the same number of lines as the abscissa file.\n"; exit ( 1 ); } w_1d = dtable_data_read ( quad_w_1d_filename, dim_num_1d, point_num_1d ); // // Read the R file. // dtable_header_read ( quad_r_1d_filename, &dim_num_1d, &point_num_1d2 ); if ( dim_num_1d != 1 ) { cout << "\n"; cout << "PRODUCT_RULE - Fatal error!\n"; cout << " The 1D quadrature region file should have exactly\n"; cout << " one value on each line.\n"; exit ( 1 ); } if ( point_num_1d2 != 2 ) { cout << "\n"; cout << "PRODUCT_RULE - Fatal error!\n"; cout << " The 1D quadrature region file should have two lines.\n"; exit ( 1 ); } r_1d = dtable_data_read ( quad_r_1d_filename, 1, 2 ); // // Determine size of product rule. // point_num = product_rule_size ( point_num_1d, dim_num ); cout << "\n"; cout << " Number of points in product rule = " << point_num << "\n"; // // Compute the product rule. // w = new double [point_num]; x = new double [dim_num*point_num]; r = new double [dim_num*2]; product_rule_set ( point_num_1d, x_1d, w_1d, r_1d, dim_num, point_num, x, w, r ); // // Write product rule to files. // strcpy ( quad_x_filename, quad_filename ); strcat ( quad_x_filename, "_x.txt" ); strcpy ( quad_w_filename, quad_filename ); strcat ( quad_w_filename, "_w.txt" ); strcpy ( quad_r_filename, quad_filename ); strcat ( quad_r_filename, "_r.txt" ); cout << "\n"; cout << " Creating product quadrature rule X file = \"" << quad_x_filename << "\".\n"; header = false; dtable_write ( quad_x_filename, dim_num, point_num, x, header ); cout << "\n"; cout << " Creating product quadrature rule W file = \"" << quad_w_filename << "\".\n"; header = false; dtable_write ( quad_w_filename, 1, point_num, w, header ); cout << "\n"; cout << " Creating product quadrature rule R file = \"" << quad_r_filename << "\".\n"; header = false; dtable_write ( quad_r_filename, dim_num, 2, r, header ); delete [] w; delete [] w_1d; delete [] x; delete [] x_1d; delete [] r; delete [] r_1d; cout << "\n"; cout << "PRODUCT_RULE:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 char ch_cap ( char ch ) //****************************************************************************80 // // Purpose: // // CH_CAP capitalizes a single character. // // Discussion: // // This routine should be equivalent to the library "toupper" function. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 19 July 1998 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the character to capitalize. // // Output, char CH_CAP, the capitalized character. // { if ( 97 <= ch && ch <= 122 ) { ch = ch - 32; } return ch; } //****************************************************************************80 bool ch_eqi ( char ch1, char ch2 ) //****************************************************************************80 // // Purpose: // // CH_EQI is true if two characters are equal, disregarding case. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH1, CH2, the characters to compare. // // Output, bool CH_EQI, is true if the two characters are equal, // disregarding case. // { if ( 97 <= ch1 && ch1 <= 122 ) { ch1 = ch1 - 32; } if ( 97 <= ch2 && ch2 <= 122 ) { ch2 = ch2 - 32; } return ( ch1 == ch2 ); } //****************************************************************************80 int ch_to_digit ( char ch ) //****************************************************************************80 // // Purpose: // // CH_TO_DIGIT returns the integer value of a base 10 digit. // // Example: // // CH DIGIT // --- ----- // '0' 0 // '1' 1 // ... ... // '9' 9 // ' ' 0 // 'X' -1 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the decimal digit, '0' through '9' or blank are legal. // // Output, int CH_TO_DIGIT, the corresponding integer value. If the character was // 'illegal', then DIGIT is -1. // { int digit; if ( '0' <= ch && ch <= '9' ) { digit = ch - '0'; } else if ( ch == ' ' ) { digit = 0; } else { digit = -1; } return digit; } //****************************************************************************80 void dtable_close_write ( ofstream &output ) //****************************************************************************80 // // Purpose: // // DTABLE_CLOSE_WRITE closes a file to which a DTABLE was to be written. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 27 September 2006 // // Author: // // John Burkardt // // Parameters: // // Input, ofstream output, the output file stream. // { output.close ( ); return; } //****************************************************************************80 double *dtable_data_read ( char *input_filename, int m, int n ) //****************************************************************************80 // // Purpose: // // DTABLE_DATA_READ reads the data from a DTABLE file. // // Discussion: // // The file is assumed to contain one record per line. // // Records beginning with the '#' character are comments, and are ignored. // Blank lines are also ignored. // // Each line that is not ignored is assumed to contain exactly (or at least) // M real numbers, representing the coordinates of a point. // // There are assumed to be exactly (or at least) N such records. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 27 January 2005 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Input, int M, the number of spatial dimensions. // // Input, int N, the number of points. The program // will stop reading data once N values have been read. // // Output, double DTABLE_DATA_READ[M*N], the table data. // { bool error; ifstream input; int i; int j; char line[255]; double *table; double *x; input.open ( input_filename ); if ( !input ) { cout << "\n"; cout << "DTABLE_DATA_READ - Fatal error!\n"; cout << " Could not open the input file: \"" << input_filename << "\"\n"; return NULL; } table = new double[m*n]; x = new double[m]; j = 0; while ( j < n ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( line[0] == '#' || s_len_trim ( line ) == 0 ) { continue; } error = s_to_r8vec ( line, m, x ); if ( error ) { continue; } for ( i = 0; i < m; i++ ) { table[i+j*m] = x[i]; } j = j + 1; } input.close ( ); delete [] x; return table; } //****************************************************************************80 void dtable_data_write ( ofstream &output, int m, int n, double table[] ) //****************************************************************************80 // // Purpose: // // DTABLE_DATA_WRITE writes data to a DTABLE file. // // Discussion: // // The file should already be open. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 December 2003 // // Author: // // John Burkardt // // Parameters: // // Input, ofstream &OUTPUT, a pointer to the output stream. // // Input, int M, the spatial dimension. // // Input, int N, the number of points. // // Input, double TABLE[M*N], the table data. // { int i; int j; char *s; for ( j = 0; j < n; j++ ) { for ( i = 0; i < m; i++ ) { output << setprecision(16) << setw(24) << table[i+j*m] << " "; } output << "\n"; } output.close ( ); return; } //****************************************************************************80 void dtable_header_read ( char *input_filename, int *m, int *n ) //****************************************************************************80 // // Purpose: // // DTABLE_HEADER_READ reads the header from a DTABLE file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 04 June 2004 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Output, int *M, the number of spatial dimensions. // // Output, int *N, the number of points. // { *m = file_column_count ( input_filename ); if ( *m <= 0 ) { cout << "\n"; cout << "DTABLE_HEADER_READ - Fatal error!\n"; cout << " FILE_COLUMN_COUNT failed.\n"; *n = -1; return; } *n = file_row_count ( input_filename ); if ( *n <= 0 ) { cout << "\n"; cout << "DTABLE_HEADER_READ - Fatal error!\n"; cout << " FILE_ROW_COUNT failed.\n"; return; } return; } //****************************************************************************80 void dtable_header_write ( char *output_filename, ofstream &output, int m, int n ) //****************************************************************************80 // // Purpose: // // DTABLE_HEADER_WRITE writes the header of a DTABLE file. // // Discussion: // // The file should already be open. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 March 2006 // // Author: // // John Burkardt // // Parameters: // // Input, char *OUTPUT_FILENAME, the output filename. // // Input, ofstream &OUTPUT, the output stream. // // Input, int M, the spatial dimension. // // Input, int N, the number of points. // { char *s; s = timestring ( ); output << "# " << output_filename << "\n"; output << "# created by dtable_write in table_io.C" << "\n"; output << "# at " << s << "\n"; output << "#\n"; output << "# Spatial dimension M = " << m << "\n"; output << "# Number of points N = " << n << "\n"; output << "# EPSILON (unit roundoff) = " << r8_epsilon ( ) << "\n"; output << "#\n"; delete [] s; return; } //****************************************************************************80 void dtable_write ( char *output_filename, int m, int n, double table[], bool header ) //****************************************************************************80 // // Purpose: // // DTABLE_WRITE writes information to a DTABLE file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 20 July 2005 // // Author: // // John Burkardt // // Parameters: // // Input, char *OUTPUT_FILENAME, the output filename. // // Input, int M, the spatial dimension. // // Input, int N, the number of points. // // Input, double TABLE[M*N], the table data. // // Input, bool HEADER, is TRUE if the header is to be included. // { ofstream output; output.open ( output_filename ); if ( !output ) { cout << "\n"; cout << "DTABLE_WRITE - Fatal error!\n"; cout << " Could not open the output file.\n"; return; } if ( header ) { dtable_header_write ( output_filename, output, m, n ); } dtable_data_write ( output, m, n, table ); dtable_close_write ( output ); return; } //****************************************************************************80 int file_column_count ( char *input_filename ) //****************************************************************************80 // // Purpose: // // FILE_COLUMN_COUNT counts the number of columns in the first line of a file. // // Discussion: // // The file is assumed to be a simple text file. // // Most lines of the file is presumed to consist of COLUMN_NUM words, separated // by spaces. There may also be some blank lines, and some comment lines, // which have a "#" in column 1. // // The routine tries to find the first non-comment non-blank line and // counts the number of words in that line. // // If all lines are blanks or comments, it goes back and tries to analyze // a comment line. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the file. // // Output, int FILE_COLUMN_COUNT, the number of columns assumed // to be in the file. // { int column_num; ifstream input; bool got_one; char line[256]; // // Open the file. // input.open ( input_filename ); if ( !input ) { column_num = -1; cout << "\n"; cout << "FILE_COLUMN_COUNT - Fatal error!\n"; cout << " Could not open the file:\n"; cout << " \"" << input_filename << "\"\n"; return column_num; } // // Read one line, but skip blank lines and comment lines. // got_one = false; for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( line ) == 0 ) { continue; } if ( line[0] == '#' ) { continue; } got_one = true; break; } if ( !got_one ) { input.close ( ); input.open ( input_filename ); for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( line ) == 0 ) { continue; } got_one = true; break; } } input.close ( ); if ( !got_one ) { cout << "\n"; cout << "FILE_COLUMN_COUNT - Warning!\n"; cout << " The file does not seem to contain any data.\n"; return -1; } column_num = s_word_count ( line ); return column_num; } //****************************************************************************80 int file_row_count ( char *input_filename ) //****************************************************************************80 // // Purpose: // // FILE_ROW_COUNT counts the number of row records in a file. // // Discussion: // // It does not count lines that are blank, or that begin with a // comment symbol '#'. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Output, int FILE_ROW_COUNT, the number of rows found. // { int bad_num; int comment_num; ifstream input; int i; char line[100]; int record_num; int row_num; row_num = 0; comment_num = 0; record_num = 0; bad_num = 0; input.open ( input_filename ); if ( !input ) { cout << "\n"; cout << "FILE_ROW_COUNT - Fatal error!\n"; cout << " Could not open the input file: \"" << input_filename << "\"\n"; return (-1); } for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } record_num = record_num + 1; if ( line[0] == '#' ) { comment_num = comment_num + 1; continue; } if ( s_len_trim ( line ) == 0 ) { comment_num = comment_num + 1; continue; } row_num = row_num + 1; } input.close ( ); return row_num; } //****************************************************************************80 int i4_power ( int i, int j ) //****************************************************************************80 // // Purpose: // // I4_POWER returns the value of I^J. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 01 April 2004 // // Author: // // John Burkardt // // Parameters: // // Input, int I, J, the base and the power. J should be nonnegative. // // Output, int I4_POWER, the value of I^J. // { int k; int value; if ( j < 0 ) { if ( i == 1 ) { value = 1; } else if ( i == 0 ) { cout << "\n"; cout << "I4_POWER - Fatal error!\n"; cout << " I^J requested, with I = 0 and J negative.\n"; exit ( 1 ); } else { value = 0; } } else if ( j == 0 ) { if ( i == 0 ) { cout << "\n"; cout << "I4_POWER - Fatal error!\n"; cout << " I^J requested, with I = 0 and J = 0.\n"; exit ( 1 ); } else { value = 1; } } else if ( j == 1 ) { value = i; } else { value = 1; for ( k = 1; k <= j; k++ ) { value = value * i; } } return value; } //****************************************************************************80 void product_rule_set ( int point_num_1d, double x_1d[], double w_1d[], double r_1d[], int dim_num, int point_num, double x[], double w[], double r[] ) //****************************************************************************80 // // Purpose: // // PRODUCT_RULE_SET sets up a product rule. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 May 2007 // // Author: // // John Burkardt // // Parameters: // // Input, int POINT_NUM_1D, the order of the 1D rule. // // Input, double X_1D[POINT_NUM_1D], the points of the 1D rule. // // Input, double W_1D[POINT_NUM_1D], the weights of the // 1D rule. // // Input, double R_1D[2], the extreme points that define // the range of the 1D region. // // Input, int DIM_NUM, the spatial dimension. // // Input, int POINT_NUM, the number of points in the product rule. // // Output, double X[DIM_NUM*POINT_NUM], the points of the // product rule. // // Output, double W[POINT_NUM], the weights of the // product rule. // // Output, double R[DIM_NUM*2], the extreme points // that define the range of the product rule region. // { int dim; int *indx; int k; indx = new int[dim_num]; k = 0; for ( ; ; ) { tuple_next ( 0, point_num_1d-1, dim_num, &k, indx ); if ( k == 0 ) { break; } w[k-1] = 1.0; for ( dim = 0; dim < dim_num; dim++ ) { w[k-1] = w[k-1] * w_1d[indx[dim]]; } for ( dim = 0; dim < dim_num; dim++ ) { x[dim+(k-1)*dim_num] = x_1d[indx[dim]]; } } delete [] indx; for ( dim = 0; dim < dim_num; dim++ ) { r[dim+0*dim_num] = r_1d[0]; r[dim+1*dim_num] = r_1d[1]; } return; } //****************************************************************************80 int product_rule_size ( int point_num_1d, int dim_num ) //****************************************************************************80 // // Purpose: // // PRODUCT_RULE_SIZE returns the size of a product rule. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 05 May 2007 // // Author: // // John Burkardt // // Parameters: // // Input, int POINT_NUM_1D, the number of points in the 1D rule. // // Input, int DIM_NUM, the spatial dimension. // // Output, int PRODUCT_RULE_SIZE, the number of points in the product rule. // { int point_num; point_num = i4_power ( point_num_1d, dim_num ); return point_num; } //****************************************************************************80 double r8_epsilon ( void ) //****************************************************************************80 // // Purpose: // // R8_EPSILON returns the R8 roundoff unit. // // Discussion: // // The roundoff unit is a number R which is a power of 2 with the // property that, to the precision of the computer's arithmetic, // 1 < 1 + R // but // 1 = ( 1 + R / 2 ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 01 July 2004 // // Author: // // John Burkardt // // Parameters: // // Output, double R8_EPSILON, the R8 round-off unit. // { double value; value = 1.0; while ( 1.0 < ( double ) ( 1.0 + value ) ) { value = value / 2.0; } value = 2.0 * value; return value; } //****************************************************************************80 int s_len_trim ( char *s ) //****************************************************************************80 // // Purpose: // // S_LEN_TRIM returns the length of a string to the last nonblank. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 26 April 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a pointer to a string. // // Output, int S_LEN_TRIM, the length of the string to the last nonblank. // If S_LEN_TRIM is 0, then the string is entirely blank. // { int n; char *t; n = strlen ( s ); t = s + strlen ( s ) - 1; while ( 0 < n ) { if ( *t != ' ' ) { return n; } t--; n--; } return n; } //****************************************************************************80 int s_to_i4 ( char *s, int *last, bool *error ) //****************************************************************************80 // // Purpose: // // S_TO_I4 reads an I4 from a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a string to be examined. // // Output, int *LAST, the last character of S used to make IVAL. // // Output, bool *ERROR is TRUE if an error occurred. // // Output, int *S_TO_I4, the integer value read from the string. // If the string is blank, then IVAL will be returned 0. // { char c; int i; int isgn; int istate; int ival; *error = false; istate = 0; isgn = 1; i = 0; ival = 0; while ( *s ) { c = s[i]; i = i + 1; // // Haven't read anything. // if ( istate == 0 ) { if ( c == ' ' ) { } else if ( c == '-' ) { istate = 1; isgn = -1; } else if ( c == '+' ) { istate = 1; isgn = + 1; } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read the sign, expecting digits. // else if ( istate == 1 ) { if ( c == ' ' ) { } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read at least one digit, expecting more. // else if ( istate == 2 ) { if ( '0' <= c && c <= '9' ) { ival = 10 * (ival) + c - '0'; } else { ival = isgn * ival; *last = i - 1; return ival; } } } // // If we read all the characters in the string, see if we're OK. // if ( istate == 2 ) { ival = isgn * ival; *last = s_len_trim ( s ); } else { *error = true; *last = 0; } return ival; } //****************************************************************************80 double s_to_r8 ( char *s, int *lchar, bool *error ) //****************************************************************************80 // // Purpose: // // S_TO_R8 reads an R8 value from a string. // // Discussion: // // We have had some trouble with input of the form 1.0E-312. // For now, let's assume anything less than 1.0E-20 is zero. // // This routine will read as many characters as possible until it reaches // the end of the string, or encounters a character which cannot be // part of the real number. // // Legal input is: // // 1 blanks, // 2 '+' or '-' sign, // 2.5 spaces // 3 integer part, // 4 decimal point, // 5 fraction part, // 6 'E' or 'e' or 'D' or 'd', exponent marker, // 7 exponent sign, // 8 exponent integer part, // 9 exponent decimal point, // 10 exponent fraction part, // 11 blanks, // 12 final comma or semicolon. // // with most quantities optional. // // Example: // // S R // // '1' 1.0 // ' 1 ' 1.0 // '1A' 1.0 // '12,34,56' 12.0 // ' 34 7' 34.0 // '-1E2ABCD' -100.0 // '-1X2ABCD' -1.0 // ' 2E-1' 0.2 // '23.45' 23.45 // '-4.2E+2' -420.0 // '17d2' 1700.0 // '-14e-2' -0.14 // 'e2' 100.0 // '-12.73e-9.23' -12.73 * 10.0**(-9.23) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, the string containing the // data to be read. Reading will begin at position 1 and // terminate at the end of the string, or when no more // characters can be read to form a legal real. Blanks, // commas, or other nonnumeric data will, in particular, // cause the conversion to halt. // // Output, int *LCHAR, the number of characters read from // the string to form the number, including any terminating // characters such as a trailing comma or blanks. // // Output, bool *ERROR, is true if an error occurred. // // Output, double S_TO_R8, the value that was read from the string. // { char c; int ihave; int isgn; int iterm; int jbot; int jsgn; int jtop; int nchar; int ndig; double r; double rbot; double rexp; double rtop; char TAB = 9; nchar = s_len_trim ( s ); *error = false; r = 0.0; *lchar = -1; isgn = 1; rtop = 0.0; rbot = 1.0; jsgn = 1; jtop = 0; jbot = 1; ihave = 1; iterm = 0; for ( ; ; ) { c = s[*lchar+1]; *lchar = *lchar + 1; // // Blank or TAB character. // if ( c == ' ' || c == TAB ) { if ( ihave == 2 ) { } else if ( ihave == 6 || ihave == 7 ) { iterm = 1; } else if ( 1 < ihave ) { ihave = 11; } } // // Comma. // else if ( c == ',' || c == ';' ) { if ( ihave != 1 ) { iterm = 1; ihave = 12; *lchar = *lchar + 1; } } // // Minus sign. // else if ( c == '-' ) { if ( ihave == 1 ) { ihave = 2; isgn = -1; } else if ( ihave == 6 ) { ihave = 7; jsgn = -1; } else { iterm = 1; } } // // Plus sign. // else if ( c == '+' ) { if ( ihave == 1 ) { ihave = 2; } else if ( ihave == 6 ) { ihave = 7; } else { iterm = 1; } } // // Decimal point. // else if ( c == '.' ) { if ( ihave < 4 ) { ihave = 4; } else if ( 6 <= ihave && ihave <= 8 ) { ihave = 9; } else { iterm = 1; } } // // Exponent marker. // else if ( ch_eqi ( c, 'E' ) || ch_eqi ( c, 'D' ) ) { if ( ihave < 6 ) { ihave = 6; } else { iterm = 1; } } // // Digit. // else if ( ihave < 11 && '0' <= c && c <= '9' ) { if ( ihave <= 2 ) { ihave = 3; } else if ( ihave == 4 ) { ihave = 5; } else if ( ihave == 6 || ihave == 7 ) { ihave = 8; } else if ( ihave == 9 ) { ihave = 10; } ndig = ch_to_digit ( c ); if ( ihave == 3 ) { rtop = 10.0 * rtop + ( double ) ndig; } else if ( ihave == 5 ) { rtop = 10.0 * rtop + ( double ) ndig; rbot = 10.0 * rbot; } else if ( ihave == 8 ) { jtop = 10 * jtop + ndig; } else if ( ihave == 10 ) { jtop = 10 * jtop + ndig; jbot = 10 * jbot; } } // // Anything else is regarded as a terminator. // else { iterm = 1; } // // If we haven't seen a terminator, and we haven't examined the // entire string, go get the next character. // if ( iterm == 1 || nchar <= *lchar + 1 ) { break; } } // // If we haven't seen a terminator, and we have examined the // entire string, then we're done, and LCHAR is equal to NCHAR. // if ( iterm != 1 && (*lchar) + 1 == nchar ) { *lchar = nchar; } // // Number seems to have terminated. Have we got a legal number? // Not if we terminated in states 1, 2, 6 or 7! // if ( ihave == 1 || ihave == 2 || ihave == 6 || ihave == 7 ) { *error = true; return r; } // // Number seems OK. Form it. // // We have had some trouble with input of the form 1.0E-312. // For now, let's assume anything less than 1.0E-20 is zero. // if ( jtop == 0 ) { rexp = 1.0; } else { if ( jbot == 1 ) { if ( jsgn * jtop < -20 ) { rexp = 0.0; } else { rexp = pow ( ( double ) 10.0, ( double ) ( jsgn * jtop ) ); } } else { if ( jsgn * jtop < -20 * jbot ) { rexp = 0.0; } else { rexp = jsgn * jtop; rexp = rexp / jbot; rexp = pow ( ( double ) 10.0, ( double ) rexp ); } } } r = isgn * rexp * rtop / rbot; return r; } //****************************************************************************80 bool s_to_r8vec ( char *s, int n, double rvec[] ) //****************************************************************************80 // // Purpose: // // S_TO_R8VEC reads an R8VEC from a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 19 February 2001 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, the string to be read. // // Input, int N, the number of values expected. // // Output, double RVEC[N], the values read from the string. // // Output, bool S_TO_R8VEC, is true if an error occurred. // { bool error; int i; int lchar; double x; error = false; for ( i = 0; i < n; i++ ) { rvec[i] = s_to_r8 ( s, &lchar, &error ); if ( error ) { return error; } s = s + lchar; } return error; } //****************************************************************************80 int s_word_count ( char *s ) //****************************************************************************80 // // Purpose: // // S_WORD_COUNT counts the number of "words" in a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 30 January 2006 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, the string to be examined. // // Output, int S_WORD_COUNT, the number of "words" in the string. // Words are presumed to be separated by one or more blanks. // { bool blank; int i; int word_num; word_num = 0; blank = true; while ( *s ) { if ( *s == ' ' ) { blank = true; } else if ( blank ) { word_num = word_num + 1; blank = false; } *s++; } return word_num; } //****************************************************************************80 void timestamp ( void ) //****************************************************************************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 GNU LGPL license. // // Modified: // // 24 September 2003 // // Author: // // John Burkardt // // Parameters: // // None // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; size_t len; time_t now; now = time ( NULL ); tm = localtime ( &now ); len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); cout << time_buffer << "\n"; return; # undef TIME_SIZE } //****************************************************************************80 char *timestring ( void ) //****************************************************************************80 // // Purpose: // // TIMESTRING returns the current YMDHMS date as a string. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 September 2003 // // Author: // // John Burkardt // // Parameters: // // Output, char *TIMESTRING, a string containing the current YMDHMS date. // { # define TIME_SIZE 40 const struct tm *tm; size_t len; time_t now; char *s; now = time ( NULL ); tm = localtime ( &now ); s = new char[TIME_SIZE]; len = strftime ( s, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); return s; # undef TIME_SIZE } //****************************************************************************80 void tuple_next ( int m1, int m2, int n, int *rank, int x[] ) //****************************************************************************80 // // Purpose: // // TUPLE_NEXT computes the next element of a tuple space. // // Discussion: // // The elements are N vectors. Each entry is constrained to lie // between M1 and M2. The elements are produced one at a time. // The first element is // (M1,M1,...,M1), // the second element is // (M1,M1,...,M1+1), // and the last element is // (M2,M2,...,M2) // Intermediate elements are produced in lexicographic order. // // Example: // // N = 2, M1 = 1, M2 = 3 // // INPUT OUTPUT // ------- ------- // Rank X Rank X // ---- --- ----- --- // 0 * * 1 1 1 // 1 1 1 2 1 2 // 2 1 2 3 1 3 // 3 1 3 4 2 1 // 4 2 1 5 2 2 // 5 2 2 6 2 3 // 6 2 3 7 3 1 // 7 3 1 8 3 2 // 8 3 2 9 3 3 // 9 3 3 0 0 0 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 29 April 2003 // // Author: // // John Burkardt // // Parameters: // // Input, int M1, M2, the minimum and maximum entries. // // Input, int N, the number of components. // // Input/output, int *RANK, counts the elements. // On first call, set RANK to 0. Thereafter, the output value of RANK // will indicate the order of the element returned. When there are no // more elements, RANK will be returned as 0. // // Input/output, int X[N], on input the previous tuple. // On output, the next tuple. // { int i; int j; if ( m2 < m1 ) { *rank = 0; return; } if ( *rank <= 0 ) { for ( i = 0; i < n; i++ ) { x[i] = m1; } *rank = 1; } else { *rank = *rank + 1; i = n - 1; for ( ; ; ) { if ( x[i] < m2 ) { x[i] = x[i] + 1; break; } x[i] = m1; if ( i == 0 ) { *rank = 0; for ( j = 0; j < n; j++ ) { x[j] = m1; } break; } i = i - 1; } } return; }