# include # include # include # include "ppc_array.h" # include "ppc_image.h" # include "ppc_image_analysis.h" # include "ppc_xmalloc.h" int main ( int argc, char **argv ); void clip_matrix_test ( ); void prune_pgm_test ( ); void timestamp ( ); void truncation_error_test ( ); /******************************************************************************/ int main ( int argc, char **argv ) /******************************************************************************/ /* Purpose: ppc_image_analysis() analyzes images. Licensing: This code is distributed under the MIT license. Modified: 23 April 2024 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 */ { timestamp ( ); printf ( "\n" ); printf ( "ppc_image_analysis_test():\n" ); printf ( " C version\n" ); printf ( " Test ppc_image_analysis().\n" ); clip_matrix_test ( ); prune_pgm_test ( ); truncation_error_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "ppc_image_analysis_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ void clip_matrix_test ( ) /******************************************************************************/ /* Purpose: clip_matrix_test() tests clip_matrix(). Licensing: This code is distributed under the MIT license. Modified: 17 September 2023 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 */ { double **a; int i; int j; int M; int m; int n; printf ( "\n" ); printf ( "clip_matrix_test():\n" ); printf ( " clip matrix() forces the entries of a matrix to be\n" ); printf ( " integers within a given range.\n" ); m = 6; n = 5; M = 255; make_matrix ( a, m, n ); for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a[i][j] = 255 * ( double ) rand ( ) / ( double ) ( RAND_MAX ); } } printf ( "\n" ); printf ( " Random matrix:\n" ); printf ( "\n" ); print_matrix ( " %g", a, m, n ); clip_matrix ( a, m, n, M ); printf ( "\n" ); printf ( " After clipping:\n" ); printf ( "\n" ); print_matrix ( " %g", a, m, n ); return; } /******************************************************************************/ void prune_pgm_test ( ) /******************************************************************************/ /* Purpose: prune_pgm_test() tests prune_pgm(). Licensing: This code is distributed under the MIT license. Modified: 17 September 2023 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: Output: */ { double **a; int deleted; int i; int j; int m = 5; int n = 5; double norm_new; double norm_old; double ratio; double rel_err; printf ( "\n" ); printf ( "prune_pgm_test():\n" ); printf ( " prune pgm() drops small matrix elements\n" ); printf ( " whose norm is less than some percentage of the matrix norm.\n" ); make_matrix ( a, m, n ); for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a[i][j] = ( double ) rand ( ) / ( double ) ( RAND_MAX ); } } printf ( "\n" ); printf ( " Random matrix:\n" ); printf ( "\n" ); print_matrix ( " %g", a, m, n ); norm_old = matrix_norm_frobenius ( a, m, n ); printf ( "\n" ); printf ( " Matrix Frobenius norm: %g\n", norm_old ); rel_err = 0.10; deleted = prune_pgm ( a, m, n, rel_err ); printf ( "\n" ); printf ( " Pruned matrix:\n" ); printf ( "\n" ); print_matrix ( " %g", a, m, n ); printf ( " Number of elements deleted was %d\n", deleted ); norm_new = matrix_norm_frobenius ( a, m, n ); printf ( "\n" ); printf ( " Matrix Frobenius norm after pruning: %g\n", norm_new ); ratio = fabs ( pow ( norm_new, 2 ) - pow ( norm_old, 2 ) ) / pow ( norm_old, 2 ); printf ( " Ratio norm_new - norm_old / norm_old = %g\n", ratio ); free_matrix ( a ); return; } /******************************************************************************/ 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 } /******************************************************************************/ void truncation_error_test ( ) /******************************************************************************/ /* Purpose: truncation_error_test() tests truncation_error(). Licensing: This code is distributed under the MIT license. Modified: 17 September 2023 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 */ { double **a; double e; int i; int j; int m = 5; int n = 5; double t; printf ( "\n" ); printf ( "truncation_error_test():\n" ); printf ( " truncation_error() measures error in neglecting matrix\n" ); printf ( " elements smaller than a given threshold.\n" ); make_matrix ( a, m, n ); t = 0.25; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a[i][j] = ( double ) rand ( ) / ( double ) ( RAND_MAX ); } } printf ( "\n" ); printf ( " Random matrix:\n" ); printf ( "\n" ); print_matrix ( " %g", a, m, n ); printf ( "\n" ); printf ( " Threshold Truncation error:\n" ); printf ( "\n" ); t = 1.0; for ( i = 0; i < 4; i++ ) { t = t / 2.0; e = truncation_error ( t, a, m, n ); printf ( " For threshold t = %g, truncation error = %g\n", t, e ); } free_matrix ( a ); return; }