# include # include # include # include # include "matrix_chain_dynamic.h" 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: 23 April 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 ( ); printf ( "\n" ); printf ( "matrix_chain_dynamic_test():\n" ); printf ( " C version\n" ); printf ( " 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; } printf ( "\n" ); printf ( " Number of matrices = %d\n", n - 1 ); printf ( " Matrix dimensions" ); for ( i = 0; i < n; i++ ) { printf ( " %3d", dims[i] ); } printf ( "\n" ); orderings = catalan_number ( n - 2 ); printf ( " Number of possible orderings is %d\n", orderings ); cost = matrix_chain_dynamic ( n - 1, dims ); printf ( " Minimal cost is %d\n", cost ); } /* Terminate. */ printf ( "\n" ); printf ( "matrix_chain_dynamic_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ 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: 01 May 2021 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 }