# include # include # include # include # include "r83t.h" int main ( ); void r83t_cg_test ( ); void r83t_dif2_test ( ); void r83t_gs_sl_test ( ); void r83t_indicator_test ( ); void r83t_jac_sl_test ( ); void r83t_mtv_test ( ); void r83t_mv_test ( ); void r83t_print_test ( ); void r83t_print_some_test ( ); void r83t_random_test ( ); void r83t_res_test ( ); void r83t_to_r8ge_test ( ); void r83t_zeros_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: r83t_test() tests r83t(). Licensing: This code is distributed under the MIT license. Modified: 17 August 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "r83t_test():\n" ); printf ( " C version\n" ); printf ( " Test r83t().\n" ); r83t_cg_test ( ); r83t_dif2_test ( ); r83t_gs_sl_test ( ); r83t_indicator_test ( ); r83t_jac_sl_test ( ); r83t_mtv_test ( ); r83t_mv_test ( ); r83t_print_test ( ); r83t_print_some_test ( ); r83t_random_test ( ); r83t_res_test ( ); r83t_to_r8ge_test ( ); r83t_zeros_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "r83t_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void r83t_cg_test ( ) /******************************************************************************/ /* Purpose: r83t_cg_test() tests r83t_cg(). Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; double *b; double e_norm; int i; int n; double *r; double r_norm; int seed; double *x1; double *x2; printf ( "\n" ); printf ( "R83T_CG_TEST\n" ); printf ( " R83T_CG applies CG to an R83 matrix.\n" ); n = 10; /* Let A be the -1 2 -1 matrix. */ a = r83t_dif2 ( n, n ); /* Choose a random solution. */ seed = 123456789; x1 = r8vec_uniform_01_new ( n, &seed ); /* Compute the corresponding right hand side. */ b = r83t_mv ( n, n, a, x1 ); /* Call the CG routine. */ x2 = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { x2[i] = 1.0; } r83t_cg ( n, a, b, x2 ); /* Compute the residual. */ r = r83t_res ( n, n, a, x2, b ); r_norm = r8vec_norm ( n, r ); /* Compute the error. */ e_norm = r8vec_norm_affine ( n, x1, x2 ); /* Report. */ printf ( "\n" ); printf ( " Number of variables N = %d\n", n ); printf ( " Norm of residual ||Ax-b|| = %g\n", r_norm ); printf ( " Norm of error ||x1-x2|| = %g\n", e_norm ); /* Free memory. */ free ( a ); free ( b ); free ( r ); free ( x1 ); free ( x2 ); return; } /******************************************************************************/ void r83t_dif2_test ( ) /******************************************************************************/ /* Purpose: R83T_DIF2_TEST tests R83T_DIF2. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int i; int m; int n; printf ( "\n" ); printf ( "R83T_DIF2_TEST\n" ); printf ( " R83T_DIF2 sets up the second difference matrix.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 1; i <= 3; i++ ) { if ( i == 1 ) { m = 3; n = 5; } else if ( i == 2 ) { m = 5; n = 5; } else if ( i == 3 ) { m = 5; n = 3; } a = r83t_dif2 ( m, n ); r83t_print ( m, n, a, " The R83T matrix:" ); free ( a ); } return; } /******************************************************************************/ void r83t_gs_sl_test ( ) /******************************************************************************/ /* Purpose: R83T_GS_SL_TEST tests R83T_GS_SL. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; double *b; int i; int maxit = 25; int n = 10; double *x; printf ( "\n" ); printf ( "R83T_GS_SL_TEST\n" ); printf ( " R83T_GS_SL solves a linear system using Gauss-Seidel iteration.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); printf ( " Iterations per call = %d\n", maxit ); /* Set the matrix values. */ a = r83t_dif2 ( n, n ); /* Set the desired solution. */ x = r8vec_indicator1_new ( n ); /* Compute the corresponding right hand side. */ b = r83t_mv ( n, n, a, x ); r8vec_print ( n, b, " Right hand side b:" ); /* Set the starting solution. */ for ( i = 0; i < n; i++ ) { x[i] = 0.0; } /* Solve the linear system. */ for ( i = 1; i <= 3; i++ ) { r83t_gs_sl ( n, a, b, x, maxit ); r8vec_print ( n, x, " Current estimated solution:" ); } free ( a ); free ( b ); free ( x ); return; } /******************************************************************************/ void r83t_indicator_test ( ) /******************************************************************************/ /* Purpose: R83T_INDICATOR_TEST tests R83T_INDICATOR. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int i; int m; int n; printf ( "\n" ); printf ( "R83T_INDICATOR_TEST\n" ); printf ( " R83T_INDICATOR sets up an R83T indicator matrix.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 1; i <= 3; i++ ) { if ( i == 1 ) { m = 3; n = 5; } else if ( i == 2 ) { m = 5; n = 5; } else if ( i == 3 ) { m = 5; n = 3; } a = r83t_indicator ( m, n ); r83t_print ( m, n, a, " The R83T indicator matrix:" ); free ( a ); } return; } /******************************************************************************/ void r83t_jac_sl_test ( ) /******************************************************************************/ /* Purpose: R83T_JAC_SL_TEST tests R83T_JAC_SL. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; double *b; int i; int maxit = 25; int n = 10; double *x; printf ( "\n" ); printf ( "R83T_JAC_SL_TEST\n" ); printf ( " R83T_JAC_SL solves a linear system using Jacobi iteration,\n" ); printf ( " for an R83T matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); printf ( " Iterations per call = %d\n", maxit ); /* Set the matrix values. */ a = r83t_dif2 ( n, n ); /* Set the desired solution. */ x = r8vec_indicator1_new ( n ); /* Compute the corresponding right hand side. */ b = r83t_mv ( n, n, a, x ); r8vec_print ( n, b, " The right hand side b:" ); /* Set the starting solution. */ for ( i = 0; i < n; i++ ) { x[i] = 0.0; } /* Solve the linear system. */ for ( i = 1; i <= 3; i++ ) { r83t_jac_sl ( n, a, b, x, maxit ); r8vec_print ( n, x, " Current estimated solution:" ); } free ( a ); free ( b ); free ( x ); return; } /******************************************************************************/ void r83t_mtv_test ( ) /******************************************************************************/ /* Purpose: R83T_MTV_TEST tests R83T_MTV. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a_83; double *a_ge; double *ax_83; double *ax_ge; int i; int m; int n; int seed; double *x; printf ( "\n" ); printf ( "R83T_MTV_TEST\n" ); printf ( " R83T_MTV computes b=A'*x, where A is an R83T matrix.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 1; i <= 3; i++ ) { if ( i == 1 ) { m = 3; n = 5; } else if ( i == 2 ) { m = 5; n = 5; } else if ( i == 3 ) { m = 5; n = 3; } seed = 123456789; a_83 = r83t_random ( m, n, &seed ); x = r8vec_indicator1_new ( m ); ax_83 = r83t_mtv ( m, n, a_83, x ); a_ge = r83t_to_r8ge ( m, n, a_83 ); ax_ge = r8ge_mtv ( m, n, a_ge, x ); r8vec2_print ( n, ax_83, ax_ge, " Product comparison:" ); free ( a_83 ); free ( a_ge ); free ( ax_83 ); free ( ax_ge ); free ( x ); } return; } /******************************************************************************/ void r83t_mv_test ( ) /******************************************************************************/ /* Purpose: R83T_MV_TEST tests R83T_MV. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a_83; double *a_ge; double *ax_83; double *ax_ge; int i; int m; int n; int seed; double *x; printf ( "\n" ); printf ( "R83T_MV_TEST\n" ); printf ( " R83T_MV computes b=A*x, where A is an R83T matrix.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 1; i <= 3; i++ ) { if ( i == 1 ) { m = 3; n = 5; } else if ( i == 2 ) { m = 5; n = 5; } else if ( i == 3 ) { m = 5; n = 3; } seed = 123456789; a_83 = r83t_random ( m, n, &seed ); x = r8vec_indicator1_new ( n ); ax_83 = r83t_mv ( m, n, a_83, x ); a_ge = r83t_to_r8ge ( m, n, a_83 ); ax_ge = r8ge_mv ( m, n, a_ge, x ); r8vec2_print ( m, ax_83, ax_ge, " Product comparison:" ); free ( a_83 ); free ( a_ge ); free ( ax_83 ); free ( ax_ge ); free ( x ); } return; } /******************************************************************************/ void r83t_print_test ( ) /******************************************************************************/ /* Purpose: R83T_PRINT_TEST tests R83T_PRINT. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int m = 5; int n = 4; printf ( "\n" ); printf ( "R83T_PRINT_TEST\n" ); printf ( " R83T_PRINT prints an R83T matrix.\n" ); a = r83t_indicator ( m, n ); r83t_print ( m, n, a, " The R83T matrix:" ); free ( a ); return; } /******************************************************************************/ void r83t_print_some_test ( ) /******************************************************************************/ /* Purpose: R83T_PRINT_SOME_TEST tests R83T_PRINT_SOME. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int m = 5; int n = 5; printf ( "\n" ); printf ( "R83T_PRINT_SOME_TEST\n" ); printf ( " R83T_PRINT_SOME prints some of an R83T matrix.\n" ); a = r83t_indicator ( m, n ); r83t_print_some ( m, n, a, 1, 1, 4, 3, " Rows 1-4, Cols 1-3:" ); free ( a ); return; } /******************************************************************************/ void r83t_random_test ( ) /******************************************************************************/ /* Purpose: R83T_RANDOM_TEST tests R83T_RANDOM. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int m = 5; int n = 4; int seed; printf ( "\n" ); printf ( "R83T_RANDOM_TEST\n" ); printf ( " R83T_RANDOM randomizes an R83T matrix.\n" ); seed = 123456789; a = r83t_random ( m, n, &seed ); r83t_print ( m, n, a, " The R83T matrix:" ); free ( a ); return; } /******************************************************************************/ void r83t_res_test ( ) /******************************************************************************/ /* Purpose: R83T_RES_TEST tests R83T_RES. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; double *b; int i; int m; int n; double *r; int seed; double *x; printf ( "\n" ); printf ( "R83T_RES_TEST\n" ); printf ( " R83T_RES computes b-A*x, where A is an R83T matrix.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 1; i <= 3; i++ ) { if ( i == 1 ) { m = 3; n = 5; } else if ( i == 2 ) { m = 5; n = 5; } else if ( i == 3 ) { m = 5; n = 3; } seed = 123456789; a = r83t_random ( m, n, &seed ); x = r8vec_indicator1_new ( n ); b = r83t_mv ( m, n, a, x ); r = r83t_res ( m, n, a, x, b ); r8vec_print ( m, r, " Residual A*x-b:" ); free ( a ); free ( b ); free ( r ); free ( x ); } return; } /******************************************************************************/ void r83t_to_r8ge_test ( ) /******************************************************************************/ /* Purpose: R83T_TO_R8GE_TEST tests R83T_TO_R8GE. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a_83t; double *a_ge; int i; int m; int n; int seed; printf ( "\n" ); printf ( "R83T_TO_R8GE_TEST\n" ); printf ( " R83T_TO_R8GE converts an R83T matrix to R8GE format.\n" ); printf ( " We check three cases, MN.\n" ); for ( i = 0; i < 3; i++ ) { if ( i == 0 ) { m = 3; n = 5; } else if ( i == 1 ) { m = 5; n = 5; } else if ( i == 2 ) { m = 5; n = 3; } seed = 123456789; a_83t = r83t_random ( m, n, &seed ); r83t_print ( m, n, a_83t, " The R83T matrix:" ); a_ge = r83t_to_r8ge ( m, n, a_83t ); r8ge_print ( m, n, a_ge, " The R8GE matrix:" ); free ( a_83t ); free ( a_ge ); } return; } /******************************************************************************/ void r83t_zeros_test ( ) /******************************************************************************/ /* Purpose: R83T_ZEROS_TEST tests R83T_ZEROS. Licensing: This code is distributed under the MIT license. Modified: 29 May 2016 Author: John Burkardt */ { double *a; int m = 5; int n = 4; printf ( "\n" ); printf ( "R83T_ZEROS_TEST\n" ); printf ( " R83T_ZEROS zeros an R83T matrix.\n" ); a = r83t_zeros ( m, n ); r83t_print ( m, n, a, " The R83T matrix:" ); free ( a ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* 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: 24 September 2003 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 }