# include # include # include # include # include using namespace std; # include "matrix_chain_dynamic.hpp" int main ( ); void timestamp ( ); //****************************************************************************// int main ( ) //****************************************************************************// // // Purpose: // // matrix_chain_dynamic_test() tests matrix_chain_dynamic(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 June 2024 // // Author: // // John Burkardt // { int cost; int *dims; int dims1[] = { 40, 20, 30, 10, 30 }; int dims2[] = { 1, 2, 3, 4, 3 }; int dims3[] = { 10, 20, 30 }; int dims4[] = { 10, 30, 5, 60 }; int dims5[] = { 10, 20 }; int dims6[] = { 40, 20, 0, 10, 30 }; int dims7[] = { 1, 100, 1, 100, 1 }; int dims8[] = { 100, 50, 1, 50, 100 }; int dims9[] = { 1, 50, 100, 50, 1 }; int dims10[] = { 4, 10, 3, 12, 20, 7 }; int i; int n; int orderings; int test; timestamp ( ); cout << "\n"; cout << "matrix_chain_dynamic_test():\n"; cout << " C++ version\n"; cout << " Test matrix_chain_dynamic().\n"; for ( test = 1; test <= 10; test++ ) { if ( test == 1 ) { n = 5; dims = dims1; } else if ( test == 2 ) { n = 5; dims = dims2; } else if ( test == 3 ) { n = 3; dims = dims3; } else if ( test == 4 ) { n = 4; dims = dims4; } else if ( test == 5 ) { n = 2; dims = dims5; } else if ( test == 6 ) { n = 5; dims = dims6; } else if ( test == 7 ) { n = 5; dims = dims7; } else if ( test == 8 ) { n = 5; dims = dims8; } else if ( test == 9 ) { n = 5; dims = dims9; } else if ( test == 10 ) { n = 6; dims = dims10; } cout << "\n"; cout << " Number of matrices = " << n - 1 << "\n"; cout << " Matrix dimensions"; for ( i = 0; i < n; i++ ) { cout << " " << setw(3) << dims[i]; } cout << "\n"; orderings = catalan_number ( n - 2 ); cout << " Number of possible orderings is " << orderings << "\n"; cost = matrix_chain_dynamic ( n - 1, dims ); cout << " Minimal cost is " << cost << "\n"; } // // Terminate. // cout << "\n"; cout << "matrix_chain_dynamic_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************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 }