# include # include # include # include # include "asa183.hpp" using namespace std; int main ( void ); void test01 ( void ); void test02 ( void ); void test03 ( void ); void test04 ( void ); void test05 ( void ); void test06 ( void ); void test07 ( void ); void test08 ( void ); void test09 ( void ); void test10 ( void ); void test11 ( void ); void test12 ( void ); //****************************************************************************80 int main ( void ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for ASA183_PRB. // // Discussion: // // ASA183_PRB tests the ASA183 library. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "ASA183_PRB\n"; cout << " C++ version\n"; cout << " Test the ASA183 library.\n"; test01 ( ); test02 ( ); test03 ( ); test04 ( ); test05 ( ); test06 ( ); test07 ( ); test08 ( ); test09 ( ); test10 ( ); test11 ( ); test12 ( ); // // Terminate. // cout << "\n"; cout << "ASA183_PRB\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void test01 ( ) //****************************************************************************80 // // Purpose: // // TEST01 tests R4_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; float r; int s1; int s2; int s3; cout << "\n"; cout << "TEST01\n"; cout << " R4_RANDOM computes pseudorandom values.\n"; cout << " Three seeds, S1, S2, and S3, are used.\n"; s1 = 12345; s2 = 34567; s3 = 56789; cout << "\n"; cout << " R S1 S2 S3\n"; cout << "\n"; cout << " " << " " << setw(8) << s1 << " " << setw(8) << s2 << " " << setw(8) << s3 << "\n"; for ( i = 0; i < 10; i++ ) { r = r4_random ( &s1, &s2, &s3 ); cout << " " << setw(14) << r << " " << setw(8) << s1 << " " << setw(8) << s2 << " " << setw(8) << s3 << "\n"; } return; } //****************************************************************************80 void test02 ( ) //****************************************************************************80 // // Purpose: // // TEST02 tests R4_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; int n = 100000; int s1; int s2; int s3; float *u; float u_avg; float u_var; u = new float[n]; cout << "\n"; cout << "TEST02\n"; cout << " Examine the average and variance of a\n"; cout << " sequence generated by R4_RANDOM.\n"; // // Start with known seeds. // s1 = 12345; s2 = 34567; s3 = 56789; cout << "\n"; cout << " Now compute " << n << " elements.\n"; u_avg = 0.0; for ( i = 0; i < n; i++ ) { u[i] = r4_random ( &s1, &s2, &s3 ); u_avg = u_avg + u[i]; } u_avg = u_avg / ( float ) ( n ); u_var = 0.0; for ( i = 0; i < n; i++ ) { u_var = u_var + ( u[i] - u_avg ) * ( u[i] - u_avg ); } u_var = u_var / ( float ) ( n - 1 ); cout << "\n"; cout << " Average value = " << u_avg << "\n"; cout << " Expecting " << 0.5 << "\n";; cout << "\n"; cout << " Variance = " << u_var << "\n"; cout << " Expecting " << 1.0 / 12.0 << "\n"; delete [] u; return; } //****************************************************************************80 void test03 ( ) //****************************************************************************80 // // Purpose: // // TEST03 tests R4_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; float r; int s1; int s1_save; int s2; int s2_save; int s3; int s3_save; cout << "\n"; cout << "TEST03\n"; cout << " Show how the seeds used by R4_RANDOM,\n"; cout << " which change on each step, can be reset to\n"; cout << " restore any part of the sequence.\n"; s1_save = 12345; s2_save = 34567; s3_save = 56789; s1 = s1_save; s2 = s2_save; s3 = s3_save; cout << "\n"; cout << " Begin sequence with following seeds\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << " S3 = " << s3 << "\n"; cout << "\n"; cout << " I R S1 S2 S3\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r4_random ( &s1, &s2, &s3 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << " " << setw(12) << s3 << "\n"; if ( i == 5 ) { s1_save = s1; s2_save = s2; s3_save = s3; } } s1 = s1_save; s2 = s2_save; s3 = s3_save; cout << "\n"; cout << " Restart the sequence, using the seeds\n"; cout << " produced after step 5:\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << " S3 = " << s3 << "\n"; cout << "\n"; cout << " I R S1 S2 S3\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r4_random ( &s1, &s2, &s3 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << " " << setw(12) << s3 << "\n"; } return; } //****************************************************************************80 void test04 ( ) //****************************************************************************80 // // Purpose: // // TEST04 tests R4_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; float r; int s1; int s2; cout << "\n"; cout << "TEST04\n"; cout << " R4_UNI computes pseudorandom values.\n"; cout << " Two seeds, S1 and S2, are used.\n"; s1 = 12345; s2 = 34567; cout << "\n"; cout << " R S1 S2\n"; cout << "\n"; cout << " " << " " << setw(8) << s1 << " " << setw(8) << s2<< "\n"; for ( i = 0; i < 10; i++ ) { r = r4_uni ( &s1, &s2 ); cout << " " << setw(14) << r << " " << setw(8) << s1 << " " << setw(8) << s2<< "\n"; } return; } //****************************************************************************80 void test05 ( ) //****************************************************************************80 // // Purpose: // // TEST05 tests R4_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; int n = 100000; int s1; int s2; float *u; float u_avg; float u_var; u = new float[n]; cout << "\n"; cout << "TEST05\n"; cout << " Examine the average and variance of a\n"; cout << " sequence generated by R4_UNI.\n"; // // Start with known seeds. // s1 = 12345; s2 = 34567; cout << "\n"; cout << " Now compute " << n << " elements.\n"; u_avg = 0.0; for ( i = 0; i < n; i++ ) { u[i] = r4_uni ( &s1, &s2 ); u_avg = u_avg + u[i]; } u_avg = u_avg / ( float ) ( n ); u_var = 0.0; for ( i = 0; i < n; i++ ) { u_var = u_var + ( u[i] - u_avg ) * ( u[i] - u_avg ); } u_var = u_var / ( float ) ( n - 1 ); cout << "\n"; cout << " Average value = " << u_avg << "\n"; cout << " Expecting " << 0.5 << "\n";; cout << "\n"; cout << " Variance = " << u_var << "\n"; cout << " Expecting " << 1.0 / 12.0 << "\n"; delete [] u; return; } //****************************************************************************80 void test06 ( ) //****************************************************************************80 // // Purpose: // // TEST06 tests R4_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; float r; int s1; int s1_save; int s2; int s2_save; cout << "\n"; cout << "TEST06\n"; cout << " Show how the seeds used by R4_UNI,\n"; cout << " which change on each step, can be reset to\n"; cout << " restore any part of the sequence.\n"; s1_save = 12345; s2_save = 34567; s1 = s1_save; s2 = s2_save; cout << "\n"; cout << " Begin sequence with following seeds\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << "\n"; cout << " I R S1 S2\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r4_uni ( &s1, &s2 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << "\n"; if ( i == 5 ) { s1_save = s1; s2_save = s2; } } s1 = s1_save; s2 = s2_save; cout << "\n"; cout << " Restart the sequence, using the seeds\n"; cout << " produced after step 5:\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << "\n"; cout << " I R S1 S2\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r4_uni ( &s1, &s2 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << "\n"; } return; } //****************************************************************************80 void test07 ( ) //****************************************************************************80 // // Purpose: // // TEST07 tests R8_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; double r; int s1; int s2; int s3; cout << "\n"; cout << "TEST07\n"; cout << " R8_RANDOM computes pseudorandom values.\n"; cout << " Three seeds, S1, S2, and S3, are used.\n"; s1 = 12345; s2 = 34567; s3 = 56789; cout << "\n"; cout << " R S1 S2 S3\n"; cout << "\n"; cout << " " << " " << setw(8) << s1 << " " << setw(8) << s2 << " " << setw(8) << s3 << "\n"; for ( i = 0; i < 10; i++ ) { r = r8_random ( &s1, &s2, &s3 ); cout << " " << setw(14) << r << " " << setw(8) << s1 << " " << setw(8) << s2 << " " << setw(8) << s3 << "\n"; } return; } //****************************************************************************80 void test08 ( ) //****************************************************************************80 // // Purpose: // // TEST08 tests R8_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; int n = 100000; int s1; int s2; int s3; double *u; double u_avg; double u_var; u = new double[n]; cout << "\n"; cout << "TEST08\n"; cout << " Examine the average and variance of a\n"; cout << " sequence generated by R8_RANDOM.\n"; // // Start with known seeds. // s1 = 12345; s2 = 34567; s3 = 56789; cout << "\n"; cout << " Now compute " << n << " elements.\n"; u_avg = 0.0; for ( i = 0; i < n; i++ ) { u[i] = r8_random ( &s1, &s2, &s3 ); u_avg = u_avg + u[i]; } u_avg = u_avg / ( double ) ( n ); u_var = 0.0; for ( i = 0; i < n; i++ ) { u_var = u_var + ( u[i] - u_avg ) * ( u[i] - u_avg ); } u_var = u_var / ( double ) ( n - 1 ); cout << "\n"; cout << " Average value = " << u_avg << "\n"; cout << " Expecting " << 0.5 << "\n";; cout << "\n"; cout << " Variance = " << u_var << "\n"; cout << " Expecting " << 1.0 / 12.0 << "\n"; delete [] u; return; } //****************************************************************************80 void test09 ( ) //****************************************************************************80 // // Purpose: // // TEST09 tests R8_RANDOM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; double r; int s1; int s1_save; int s2; int s2_save; int s3; int s3_save; cout << "\n"; cout << "TEST09\n"; cout << " Show how the seeds used by R8_RANDOM,\n"; cout << " which change on each step, can be reset to\n"; cout << " restore any part of the sequence.\n"; s1_save = 12345; s2_save = 34567; s3_save = 56789; s1 = s1_save; s2 = s2_save; s3 = s3_save; cout << "\n"; cout << " Begin sequence with following seeds\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << " S3 = " << s3 << "\n"; cout << "\n"; cout << " I R S1 S2 S3\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r8_random ( &s1, &s2, &s3 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << " " << setw(12) << s3 << "\n"; if ( i == 5 ) { s1_save = s1; s2_save = s2; s3_save = s3; } } s1 = s1_save; s2 = s2_save; s3 = s3_save; cout << "\n"; cout << " Restart the sequence, using the seeds\n"; cout << " produced after step 5:\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << " S3 = " << s3 << "\n"; cout << "\n"; cout << " I R S1 S2 S3\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r8_random ( &s1, &s2, &s3 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << " " << setw(12) << s3 << "\n"; } return; } //****************************************************************************80 void test10 ( ) //****************************************************************************80 // // Purpose: // // TEST10 tests R8_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; double r; int s1; int s2; cout << "\n"; cout << "TEST10\n"; cout << " R8_UNI computes pseudorandom values.\n"; cout << " Two seeds, S1 and S2, are used.\n"; s1 = 12345; s2 = 34567; cout << "\n"; cout << " R S1 S2\n"; cout << "\n"; cout << " " << " " << setw(8) << s1 << " " << setw(8) << s2<< "\n"; for ( i = 0; i < 10; i++ ) { r = r8_uni ( &s1, &s2 ); cout << " " << setw(14) << r << " " << setw(8) << s1 << " " << setw(8) << s2 << "\n"; } return; } //****************************************************************************80 void test11 ( ) //****************************************************************************80 // // Purpose: // // TEST11 tests R8_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; int n = 100000; int s1; int s2; double *u; double u_avg; double u_var; u = new double[n]; cout << "\n"; cout << "TEST11\n"; cout << " Examine the average and variance of a\n"; cout << " sequence generated by R8_UNI.\n"; // // Start with known seeds. // s1 = 12345; s2 = 34567; cout << "\n"; cout << " Now compute " << n << " elements.\n"; u_avg = 0.0; for ( i = 0; i < n; i++ ) { u[i] = r8_uni ( &s1, &s2 ); u_avg = u_avg + u[i]; } u_avg = u_avg / ( float ) ( n ); u_var = 0.0; for ( i = 0; i < n; i++ ) { u_var = u_var + ( u[i] - u_avg ) * ( u[i] - u_avg ); } u_var = u_var / ( float ) ( n - 1 ); cout << "\n"; cout << " Average value = " << u_avg << "\n"; cout << " Expecting " << 0.5 << "\n";; cout << "\n"; cout << " Variance = " << u_var << "\n"; cout << " Expecting " << 1.0 / 12.0 << "\n"; delete [] u; return; } //****************************************************************************80 void test12 ( ) //****************************************************************************80 // // Purpose: // // TEST12 tests R8_UNI. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 July 2008 // // Author: // // John Burkardt // { int i; double r; int s1; int s1_save; int s2; int s2_save; cout << "\n"; cout << "TEST12\n"; cout << " Show how the seeds used by R8_UNI,\n"; cout << " which change on each step, can be reset to\n"; cout << " restore any part of the sequence.\n"; s1_save = 12345; s2_save = 34567; s1 = s1_save; s2 = s2_save; cout << "\n"; cout << " Begin sequence with following seeds\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << "\n"; cout << " I R S1 S2\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r8_uni ( &s1, &s2 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << "\n"; if ( i == 5 ) { s1_save = s1; s2_save = s2; } } s1 = s1_save; s2 = s2_save; cout << "\n"; cout << " Restart the sequence, using the seeds\n"; cout << " produced after step 5:\n"; cout << "\n"; cout << " S1 = " << s1 << "\n"; cout << " S2 = " << s2 << "\n"; cout << "\n"; cout << " I R S1 S2\n"; cout << "\n"; for ( i = 1; i <= 10; i++ ) { r = r8_uni ( &s1, &s2 ); cout << " " << setw(8) << i << " " << setw(14) << r << " " << setw(12) << s1 << " " << setw(12) << s2 << "\n"; } return; }