# include # include # include # include # include # include "r8pbu.h" int main ( ); void r8pbu_cg_test ( ); void r8pbu_det_test ( ); void r8pbu_dif2_test ( ); void r8pbu_fa_test ( ); void r8pbu_indicator_test ( ); void r8pbu_ml_test ( ); void r8pbu_mv_test ( ); void r8pbu_print_test ( ); void r8pbu_print_some_test ( ); void r8pbu_random_test ( ); void r8pbu_res_test ( ); void r8pbu_sl_test ( ); void r8pbu_sor_test ( ); void r8pbu_to_r8ge_test ( ); void r8pbu_zeros_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: r8pbu_test() tests r8pbu(). Licensing: This code is distributed under the MIT license. Modified: 25 August 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "r8pbu_test():\n" ); printf ( " C version\n" ); printf ( " Test r8pbu().\n" ); r8pbu_cg_test ( ); r8pbu_det_test ( ); r8pbu_dif2_test ( ); r8pbu_fa_test ( ); r8pbu_indicator_test ( ); r8pbu_ml_test ( ); r8pbu_mv_test ( ); r8pbu_print_test ( ); r8pbu_print_some_test ( ); r8pbu_random_test ( ); r8pbu_res_test ( ); r8pbu_sl_test ( ); r8pbu_sor_test ( ); r8pbu_to_r8ge_test ( ); r8pbu_zeros_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "r8pbu_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void r8pbu_cg_test ( ) /******************************************************************************/ /* Purpose: R8PBU_CG_TEST tests R8PBU_CG. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 50 # define MU 1 double a[(MU+1)*N]; double *b; int i; int j; double *r; double res_max; double *x; double *x_init; printf ( "\n" ); printf ( "R8PBU_CG_TEST\n" ); printf ( " R8PBU_CG applies the conjugate gradient method\n" ); printf ( " to a symmetric positive definite banded\n" ); printf ( " linear system.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); /* Set the matrix values. */ a[0+0*(MU+1)] = 0.0; for ( j = 1; j < N; j++ ) { a[0+j*(MU+1)] = -1.0; } for ( j = 0; j < N; j++ ) { a[1+j*(MU+1)] = 2.0; } r8pbu_print_some ( N, MU, a, 1, 1, 10, 10, "The symmetric banded matrix:" ); /* Set the desired solution. */ x = r8vec_indicator1_new ( N ); /* Compute the right hand side. */ b = r8pbu_mv ( N, N, MU, a, x ); /* Set the approximate solution. */ x_init = ( double * ) malloc ( N * sizeof ( double ) ); for ( i = 0; i < N; i++ ) { x_init[i] = 1.0; } /* Call the conjugate gradient method. */ free ( x ); x = r8pbu_cg ( N, MU, a, b, x_init ); r8vec_print_some ( N, x, 10, " Solution:" ); /* Compute the residual, A*x-b */ r = r8pbu_mv ( N, N, MU, a, x ); res_max = 0.0; for ( i = 0; i < N; i++ ) { res_max = r8_max ( res_max, fabs ( r[i] - b[i] ) ); } printf ( "\n" ); printf ( " Maximum residual = %g\n", res_max ); free ( r ); free ( x ); free ( x_init ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_det_test ( ) /******************************************************************************/ /* Purpose: R8PBU_DET_TEST tests R8PBU_DET. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 10 # define MU 3 double *a; double *a2; double *a_lu; double det; int info; int pivot[N]; int seed = 123456789; printf ( " \n" ); printf ( "R8PBU_DET_TEST\n" ); printf ( " R8PBU_DET, determinant of a positive definite\n" ); printf ( " symmetric banded matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); /* Set the matrix. */ a = r8pbu_random ( N, MU, &seed ); r8pbu_print ( N, MU, a, " The R8PBU matrix:" ); /* Copy the matrix into a general array. */ a2 = r8pbu_to_r8ge ( N, MU, a ); /* Factor the matrix. */ a_lu = r8pbu_fa ( N, MU, a ); r8pbu_print ( N, MU, a_lu, " The factored R8PBU matrix:" ); /* Compute the determinant. */ det = r8pbu_det ( N, MU, a_lu ); printf ( "\n" ); printf ( " R8PBU_DET computes the determinant = %g\n", det ); /* Factor the general matrix. */ info = r8ge_fa ( N, a2, pivot ); if ( info != 0 ) { printf ( "\n" ); printf ( "r8pbu_det_test(): Fatal error!\n" ); printf ( " r8ge_fa() returns nonzero value of info!\n" ); exit ( 1 ); } /* Compute the determinant. */ det = r8ge_det ( N, a2, pivot ); printf ( " R8GE_DET computes the determinant = %g\n", det ); free ( a ); free ( a2 ); free ( a_lu ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_dif2_test ( ) /******************************************************************************/ /* Purpose: R8PBU_DIF2_TEST tests R8PBU_DIF2. Licensing: This code is distributed under the MIT license. Modified: 04 June 2016 Author: John Burkardt */ { # define N 5 # define MU 1 double *a; printf ( " \n" ); printf ( "R8PBU_DIF2_TEST\n" ); printf ( " R8PBU_DIF2 sets up an R8PBU second difference matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_dif2 ( N, N, MU ); r8pbu_print ( N, MU, a, " The R8PBU second difference matrix:" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_fa_test ( ) /******************************************************************************/ /* Purpose: R8PBU_FA_TEST tests R8PBU_FA. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 50 # define MU 1 double *a; double *a_lu; double *b; int seed = 123456789; double *x; printf ( "\n" ); printf ( "R8PBU_FA_TEST\n" ); printf ( " R8PBU_FA factors a matrix in R8PBU format.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); /* Set the matrix values. */ a = r8pbu_random ( N, MU, &seed ); /* Set the desired solution. */ x = r8vec_indicator1_new ( N ); /* Compute the right hand side. */ b = r8pbu_mv ( N, N, MU, a, x ); /* Factor the matrix. */ a_lu = r8pbu_fa ( N, MU, a ); /* Solve the linear system. */ free ( x ); x = r8pbu_sl ( N, MU, a_lu, b ); r8vec_print_some ( N, x, 10, " Solution:" ); free ( a ); free ( a_lu ); free ( b ); free ( x ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_indicator_test ( ) /******************************************************************************/ /* Purpose: R8PBU_INDICATOR_TEST tests R8PBU_INDICATOR. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 5 # define MU 3 double *a; printf ( " \n" ); printf ( "R8PBU_INDICATOR_TEST\n" ); printf ( " R8PBU_INDICATOR sets up an R8PBU indicator matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_indicator ( N, MU ); r8pbu_print ( N, MU, a, " The R8PBU indicator matrix:" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_ml_test ( ) /******************************************************************************/ /* Purpose: R8PBU_ML_TEST tests R8PBU_ML. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 10 # define MU 3 double *a; double *a_lu; double *b; double *b2; int seed = 123456789; double *x; printf ( "\n" ); printf ( "R8PBU_ML_TEST\n" ); printf ( " R8PBU_ML computes A*x \n" ); printf ( " where A has been factored by R8PBU_FA.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); /* Set the matrix. */ a = r8pbu_random ( N, MU, &seed ); /* Set the desired solution. */ x = r8vec_indicator1_new ( N ); /* Compute the corresponding right hand side. */ b = r8pbu_mv ( N, N, MU, a, x ); /* Factor the matrix. */ a_lu = r8pbu_fa ( N, MU, a ); /* Now multiply the factored matrix times solution to get right hand side again. */ b2 = r8pbu_ml ( N, MU, a_lu, x ); r8vec2_print_some ( N, b, b2, 10, " A*x and PLU*x" ); free ( a ); free ( a_lu ); free ( b ); free ( b2 ); free ( x ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_mv_test ( ) /******************************************************************************/ /* Purpose: R8PBU_MV_TEST tests R8PBU_MV. Licensing: This code is distributed under the MIT license. Modified: 04 June 2016 Author: John Burkardt */ { double *a; double *b; int mu = 2; int n = 5; int seed = 123456789; double *x; printf ( "\n" ); printf ( "R8PBU_MV_TEST\n" ); printf ( " R8PBU_MV computes A*x where A is an R8PBU matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); printf ( " Upper bandwidth MU = %d\n", mu ); /* Set the matrix. */ a = r8pbu_random ( n, mu, &seed ); r8pbu_print ( n, mu, a, " Matrix A:" ); /* Set the desired solution. */ x = r8vec_indicator1_new ( n ); r8vec_print ( n, x, " Vector x:" ); /* Compute the right hand side. */ b = r8pbu_mv ( n, n, mu, a, x ); r8vec_print ( n, b, " Product b=A*x:" ); free ( a ); free ( b ); free ( x ); return; } /******************************************************************************/ void r8pbu_print_test ( ) /******************************************************************************/ /* Purpose: R8PBU_PRINT_TEST tests R8PBU_PRINT. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 5 # define MU 3 double *a; printf ( " \n" ); printf ( "R8PBU_PRINT_TEST\n" ); printf ( " R8PBU_PRINT prints an R8PBU matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_indicator ( N, MU ); r8pbu_print ( N, MU, a, " The R8PBU indicator matrix:" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_print_some_test ( ) /******************************************************************************/ /* Purpose: R8PBU_PRINT_SOME_TEST tests R8PBU_PRINT_SOME. Licensing: This code is distributed under the MIT license. Modified: 04 June 2016 Author: John Burkardt */ { # define N 9 # define MU 4 double *a; printf ( " \n" ); printf ( "R8PBU_PRINT_SOME_TEST\n" ); printf ( " R8PBU_PRINT_SOME prints some of an R8PBU matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_indicator ( N, MU ); r8pbu_print_some ( N, MU, a, 3, 4, 7, 8, " Row(3:7), Col(4:8):" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_random_test ( ) /******************************************************************************/ /* Purpose: R8PBU_RANDOM_TEST tests R8PBU_RANDOM. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 5 # define MU 3 double *a; int seed = 123456789; printf ( "\n" ); printf ( "R8PBU_RANDOM_TEST\n" ); printf ( " R8PBU_RANDOM sets a random R8PBU matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); printf ( "\n" ); /* Set the matrix values. */ a = r8pbu_random ( N, MU, &seed ); r8pbu_print ( N, MU, a, " The R8PBU random matrix:" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_res_test ( ) /******************************************************************************/ /* Purpose: R8PBU_RES_TEST tests R8PBU_RES. Licensing: This code is distributed under the MIT license. Modified: 04 June 2016 Author: John Burkardt */ { double *a; double *b; double *e; int i; int mu = 2; int n = 5; double *r; int seed = 123456789; double *x; double *x2; printf ( "\n" ); printf ( "R8PBU_RES_TEST\n" ); printf ( " R8PBU_RES computes the residual b-A*x where A is an R8PBU matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); printf ( " Upper bandwidth MU = %d\n", mu ); /* Set the matrix. */ a = r8pbu_random ( n, mu, &seed ); r8pbu_print ( n, mu, a, " Matrix A:" ); /* Set the desired solution. */ x = r8vec_indicator1_new ( n ); r8vec_print ( n, x, " Vector x:" ); /* Compute the right hand side. */ b = r8pbu_mv ( n, n, mu, a, x ); r8vec_print ( n, b, " Product b=A*x:" ); /* Jostle the solution. */ e = r8vec_uniform_01_new ( n, &seed ); x2 = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++) { x2[i] = x[i] + 0.01 * e[i]; } r8vec_print ( n, x2, " Approximate solution x2:" ); /* Compute the residual. */ r = r8pbu_res ( n, n, mu, a, x2, b ); r8vec_print ( n, r, " Residual r = b-A*x2:" ); /* Free memory. */ free ( a ); free ( b ); free ( e ); free ( r ); free ( x ); free ( x2 ); return; } /******************************************************************************/ void r8pbu_sl_test ( ) /******************************************************************************/ /* Purpose: R8PBU_SL_TEST tests R8PBU_SL. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 50 # define MU 1 double *a; double *a_lu; double *b; int seed = 123456789; double *x; printf ( "\n" ); printf ( "R8PBU_SL_TEST\n" ); printf ( " R8PBU_SL solves a linear system factored by R8PBU_FA.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); /* Set the matrix values. */ a = r8pbu_random ( N, MU, &seed ); /* Set the desired solution. */ x = r8vec_indicator1_new ( N ); /* Compute the right hand side. */ b = r8pbu_mv ( N, N, MU, a, x ); /* Factor the matrix. */ a_lu = r8pbu_fa ( N, MU, a ); /* Solve the linear system. */ free ( x ); x = r8pbu_sl ( N, MU, a_lu, b ); r8vec_print_some ( N, x, 10, " Solution:" ); free ( a ); free ( a_lu ); free ( b ); free ( x ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_sor_test ( ) /******************************************************************************/ /* Purpose: R8PBU_SOR_TEST tests R8PBU_SOR. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 50 # define MU 1 double a[(MU+1)*N]; double *b; double *b2; double eps; double err; int i; int itchk; int itmax; int j; int k; double omega; double pi = 3.14159265; double t; double *x; double x_init[N]; printf ( "\n" ); printf ( "R8PBU_SOR_TEST\n" ); printf ( " R8PBU_SOR, SOR routine for iterative\n" ); printf ( " solution of A*x=b.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Upper bandwidth MU = %d\n", MU ); for ( k = 1; k <= 3; k++ ) { if ( k == 1 ) { omega = 0.25; } else if ( k == 2 ) { omega = 0.75; } else { omega = 1.00; } /* Set matrix values. */ a[0+0*(MU+1)] = 0.0; for ( j = 1; j < N; j++ ) { a[0+j*(MU+1)] = -1.0; } for ( j = 0; j < N; j++ ) { a[1+j*(MU+1)] = 2.0; } /* Set the desired solution. */ x = ( double * ) malloc ( N * sizeof ( double ) ); for ( i = 0; i < N; i++ ) { t = pi * ( double ) ( i ) / ( double ) ( N - 1 ); x[i] = sin ( t ); } /* Compute the right hand side. */ b = r8pbu_mv ( N, N, MU, a, x ); /* Set the initial solution estimate. */ for ( i = 0; i < N; i++ ) { x_init[i] = 1.0; } itchk = 1; itmax = 8000; eps = 0.0001; free ( x ); x = r8pbu_sor ( N, MU, a, b, eps, itchk, itmax, omega, x_init ); /* Compute residual, A*x-b */ b2 = r8pbu_mv ( N, N, MU, a, x ); err = 0.0; for ( i = 0; i < N; i++ ) { err = r8_max ( err, fabs ( b2[i] - b[i] ) ); } printf ( "\n" ); printf ( "SOR iteration.\n" ); printf ( "\n" ); printf ( " Relaxation factor OMEGA = %g\n", omega ); r8vec_print_some ( N, x, 10, " Solution:" ); printf ( "\n" ); printf ( " Maximum error = %g\n", err ); free ( b ); free ( b2 ); free ( x ); } return; # undef MU # undef N } /******************************************************************************/ void r8pbu_to_r8ge_test ( ) /******************************************************************************/ /* Purpose: R8PBU_TO_R8GE_TEST tests R8PBU_TO_R8GE. Licensing: This code is distributed under the MIT license. Modified: 14 April 2013 Author: John Burkardt */ { # define N 5 # define MU 3 double *a; double *a_r8ge; printf ( " \n" ); printf ( "R8PBU_TO_R8GE_TEST\n" ); printf ( " R8PBU_TO_R8GE converts an R8PBU matrix to R8GE format.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_indicator ( N, MU ); r8pbu_print ( N, MU, a, " The R8PBU matrix:" ); a_r8ge = r8pbu_to_r8ge ( N, MU, a ); r8ge_print ( N, N, a_r8ge, " The R8GE matrix:" ); free ( a ); free ( a_r8ge ); return; # undef MU # undef N } /******************************************************************************/ void r8pbu_zeros_test ( ) /******************************************************************************/ /* Purpose: R8PBU_ZEROS_TEST tests R8PBU_ZEROS. Licensing: This code is distributed under the MIT license. Modified: 03 June 2016 Author: John Burkardt */ { # define N 9 # define MU 3 double *a; printf ( " \n" ); printf ( "R8PBU_ZEROS_TEST\n" ); printf ( " R8PBU_ZEROS sets up an R8PBU zero matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); printf ( " Bandwidth MU = %d\n", MU ); a = r8pbu_zeros ( N, MU ); r8pbu_print ( N, MU, a, " The R8PBU zero matrix:" ); free ( a ); return; # undef MU # undef N } /******************************************************************************/ 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 }