# include # include # include # include # include "r8po.h" int main ( ); void r8ge_to_r8po_test ( ); void r8po_det_test ( ); void r8po_dif2_test ( ); void r8po_fa_test ( ); void r8po_indicator_test ( ); void r8po_inverse_test ( ); void r8po_ml_test ( ); void r8po_mm_test ( ); void r8po_mv_test ( ); void r8po_print_test ( ); void r8po_print_some_test ( ); void r8po_random_test ( ); void r8po_sl_test ( ); void r8po_to_r8ge_test ( ); void r8po_zeros_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: r8po_test() tests r8po(). Licensing: This code is distributed under the MIT license. Modified: 25 August 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "r8po_test():\n" ); printf ( " C version\n" ); printf ( " Test r8po().\n" ); r8ge_to_r8po_test ( ); r8po_det_test ( ); r8po_dif2_test ( ); r8po_fa_test ( ); r8po_indicator_test ( ); r8po_inverse_test ( ); r8po_ml_test ( ); r8po_mm_test ( ); r8po_mv_test ( ); r8po_print_test ( ); r8po_print_some_test ( ); r8po_random_test ( ); r8po_sl_test ( ); r8po_to_r8ge_test ( ); r8po_zeros_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "r8po_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void r8ge_to_r8po_test ( ) /******************************************************************************/ /* Purpose: R8GE_TO_R8PO_TEST tests R8GE_TO_R8PO. Licensing: This code is distributed under the MIT license. Modified: 02 August 2015 Author: John Burkardt */ { double *a; double *b; int n = 5; int seed = 123456789; printf ( "\n" ); printf ( "R8GE_TO_R8PO_TEST\n" ); printf ( " R8GE_TO_R8PO converts an R8GE matrix to R8PO format.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); a = r8ge_random ( n, n, &seed ); r8ge_print ( n, n, a, " The random R8GE matrix A:" ); b = r8ge_to_r8po ( n, a ); r8po_print ( n, b, " The R8PO matrix" ); free ( a ); free ( b ); return; } /******************************************************************************/ void r8po_det_test ( ) /******************************************************************************/ /* Purpose: R8PO_DET_TEST tests R8PO_DET. Licensing: This code is distributed under the MIT license. Modified: 29 July 2015 Author: John Burkardt */ { double *a; double det; int i; int j; int n = 4; double *r; printf ( "\n" ); printf ( "R8PO_DET_TEST\n" ); printf ( " R8PO_DET computes the determinant of\n" ); printf ( " a symmetric positive definite matrix\n" ); printf ( " factored by R8PO_FA.\n" ); /* Set the matrix. */ a = ( double * ) malloc ( n * n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { if ( j == i - 1 ) { a[i+j*n] = - 1.0; } else if ( j == i ) { a[i+j*n] = 2.0; } else if ( j == i + 1 ) { a[i+j*n] = - 1.0; } else { a[i+j*n] = 0.0; } } } r8po_print ( n, a, " Matrix A:" ); /* Factor the matrix. */ r = r8po_fa ( n, a ); /* Compute the determinant. */ det = r8po_det ( n, r ); printf ( "\n" ); printf ( " Determinant of A = %g\n", det ); free ( a ); free ( r ); return; } /******************************************************************************/ void r8po_dif2_test ( ) /******************************************************************************/ /* Purpose: R8PO_DIF2_TEST tests R8PO_DIF2. Licensing: This code is distributed under the MIT license. Modified: 20 August 2015 Author: John Burkardt */ { double *a; int n = 5; printf ( "\n" ); printf ( "R8PO_DIF2_TEST\n" ); printf ( " R8PO_DIF2 sets the second difference matrix in R8PO format.\n" ); a = r8po_dif2 ( n ); r8po_print ( n, a, " The R8PO DIF2 matrix:" ); free ( a ); return; } /******************************************************************************/ void r8po_fa_test ( ) /******************************************************************************/ /* Purpose: R8PO_FA_TEST tests R8PO_FA; Licensing: This code is distributed under the MIT license. Modified: 15 April 2013 Author: John Burkardt */ { double *a; int i; int j; int k; int n = 5; double *r; printf ( "\n" ); printf ( "R8PO_FA_TEST\n" ); printf ( " R8PO_FA factors a positive definite symmetric\n" ); printf ( " linear system,\n" ); a = ( double * ) malloc ( n * n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { a[i+j*n] = ( double ) ( i4_min ( i + 1, j + 1 ) ); } } r8po_print ( n, a, " The matrix A:" ); /* Factor the matrix. */ r = r8po_fa ( n, a ); if ( r == NULL ) { printf ( "\n" ); printf ( " Fatal error!\n" ); printf ( " R8PO_FA declares the matrix is singular!\n" ); return; } r8ut_print ( n, n, r, " The factor R (an R8UT matrix):" ); /* Compute the product R' * R. */ for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { a[i+j*n] = 0.0; for ( k = 0; k < n; k++ ) { a[i+j*n] = a[i+j*n] + r[k+i*n] * r[k+j*n]; } } } r8ge_print ( n, n, a, " The product R' * R:" ); free ( a ); free ( r ); return; } /******************************************************************************/ void r8po_indicator_test ( ) /******************************************************************************/ /* Purpose: R8PO_INDICATOR_TEST tests R8PO_INDICATOR. Licensing: This code is distributed under the MIT license. Modified: 01 August 2015 Author: John Burkardt */ { double *a; int n = 5; printf ( "\n" ); printf ( "R8PO_INDICATOR_TEST\n" ); printf ( " R8PO_INDICATOR sets up an R8PO indicator matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); /* Set the matrix. */ a = r8po_indicator ( n ); r8po_print ( n, a, " The R8PO indicator matrix:" ); free ( a ); return; } /******************************************************************************/ void r8po_inverse_test ( ) /******************************************************************************/ /* Purpose: R8PO_INVERSE_TEST tests R8PO_INVERSE. Licensing: This code is distributed under the MIT license. Modified: 29 July 2015 Author: John Burkardt */ { # define N 4 double a[N*N]; double *a_inv; double *c; int i; int j; double *r; printf ( "\n" ); printf ( "R8PO_INVERSE_TEST\n" ); printf ( " R8PO_INVERSE computes the inverse of\n" ); printf ( " a symmetric positive definite matrix\n" ); printf ( " factored by R8PO_FA.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", N ); /* Set the matrix. */ for ( i = 0; i < N; i++ ) { for ( j = 0; j < N; j++ ) { a[i+j*N] = ( double ) ( i4_min ( i + 1, j + 1 ) ); } } r8po_print ( N, a, " Matrix A:" ); /* Factor the matrix. */ r = r8po_fa ( N, a ); /* Compute the inverse. */ a_inv = r8po_inverse ( N, r ); r8po_print ( N, a_inv, " Inverse matrix A_INV:" ); /* Check. */ c = r8po_mm ( N, a, a_inv ); r8po_print ( N, c, " Product A * A_INV:" ); free ( a_inv ); free ( c ); free ( r ); return; # undef N } /******************************************************************************/ void r8po_ml_test ( ) /******************************************************************************/ /* Purpose: R8PO_ML_TEST tests R8PO_ML. Licensing: This code is distributed under the MIT license. Modified: 20 August 2015 Author: John Burkardt */ { double *a; double *b; int i; int j; int n = 10; double *r; double *x; printf ( "\n" ); printf ( "R8PO_ML_TEST\n" ); printf ( " R8PO_ML can compute A*x for an R8PO matrix A\n" ); printf ( " even after it has been factored by R8PO_FA.\n" ); a = ( double * ) malloc ( n * n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { a[i+j*n] = ( double ) ( i4_min ( i + 1, j + 1 ) ); } } /* Set the desired solution. */ x = r8vec_indicator1_new ( n ); /* Compute the corresponding right hand side. */ b = r8po_mv ( n, a, x ); /* Factor the matrix. */ r = r8po_fa ( n, a ); if ( r == NULL ) { printf ( "\n" ); printf ( " Fatal error!\n" ); printf ( " R8PO_FA declares the matrix is singular!\n" ); return; } /* Solve the linear system. */ free ( x ); x = r8po_sl ( n, r, b ); r8vec_print ( n, x, " Solution:" ); /* Set the desired solution. */ free ( x ); x = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { x[i] = 1.0; } /* Compute the corresponding right hand side, using the factored matrix. */ free ( b ); b = r8po_ml ( n, r, x ); /* Solve the linear system. */ free ( x ); x = r8po_sl ( n, r, b ); r8vec_print ( n, x, " Solution:" ); free ( a ); free ( b ); free ( r ); free ( x ); return; } /******************************************************************************/ void r8po_mm_test ( ) /******************************************************************************/ /* Purpose: R8PO_MM_TEST tests R8PO_MM. Licensing: This code is distributed under the MIT license. Modified: 31 July 2015 Author: John Burkardt */ { double *a; double *b; double *c; int i; int n = 5; printf ( "\n" ); printf ( "R8PO_MM_TEST\n" ); printf ( " R8PO_MM computes the product of two\n" ); printf ( " symmetric positive definite matrices.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); /* Set (the upper half of) matrix A. */ a = r8po_zeros ( n ); for ( i = 0; i < n; i++ ) { a[i+i*n] = 2.0; } for ( i = 0; i < n - 1; i++ ) { a[i+(i+1)*n] = -1.0; } r8po_print ( n, a, " Matrix A:" ); /* Set (the upper half of) matrix B. */ b = r8po_zeros ( n ); for ( i = 0; i < n; i++ ) { b[i+i*n] = ( double ) ( i + i + 1 ); } for ( i = 0; i < n - 1; i++ ) { b[i+(i+1)*n] = ( double ) ( i + i + 1 + 1 ); } r8po_print ( n, b, " Matrix B:" ); /* Compute the product. */ c = r8po_mm ( n, a, b ); r8po_print ( n, c, " Product matrix C = A * B:" ); free ( a ); free ( b ); free ( c ); return; } /******************************************************************************/ void r8po_mv_test ( ) /******************************************************************************/ /* Purpose: R8PO_MV_TEST tests R8PO_MV. Licensing: This code is distributed under the MIT license. Modified: 01 August 2015 Author: John Burkardt */ { double *a; int i; int n = 5; double *v; double *w; printf ( "\n" ); printf ( "R8PO_MV_TEST\n" ); printf ( " R8PO_MV computes the product of an R8PO matrix and a vector.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); a = r8po_zeros ( n ); for ( i = 0; i < n; i++ ) { a[i+i*n] = 2.0; } for ( i = 0; i < n - 1; i++ ) { a[i+(i+1)*n] = -1.0; } r8po_print ( n, a, " Matrix A:" ); /* Set the vector V. */ v = r8vec_indicator1_new ( n ); r8vec_print ( n, v, " Vector V:" ); /* Compute the product. */ w = r8po_mv ( n, a, v ); r8vec_print ( n, w, " Product w = A * v:" ); free ( a ); free ( v ); free ( w ); return; } /******************************************************************************/ void r8po_print_test ( ) /******************************************************************************/ /* Purpose: R8PO_PRINT_TEST tests R8PO_PRINT. Licensing: This code is distributed under the MIT license. Modified: 20 August 2015 Author: John Burkardt */ { double *a; int n = 5; printf ( "\n" ); printf ( "R8PO_PRINT_TEST\n" ); printf ( " R8PO_PRINT prints an R8PO matrix.\n" ); a = r8po_indicator ( n ); r8po_print ( n, a, " The R8PO matrix:" ); free ( a ); return; } /******************************************************************************/ void r8po_print_some_test ( ) /******************************************************************************/ /* Purpose: R8PO_PRINT_SOME_TEST tests R8PO_PRINT_SOME. Licensing: This code is distributed under the MIT license. Modified: 20 August 2015 Author: John Burkardt */ { double *a; int n = 10; printf ( "\n" ); printf ( "R8PO_PRINT_SOME_TEST\n" ); printf ( " R8PO_PRINT_SOME prints some of an R8PO matrix.\n" ); a = r8po_indicator ( n ); r8po_print_some ( n, a, 2, 5, 6, 7, " Rows 2-6, 5-7:" ); free ( a ); return; } /******************************************************************************/ void r8po_random_test ( ) /******************************************************************************/ /* Purpose: R8PO_RANDOM_TEST tests R8PO_RANDOM.. Licensing: This code is distributed under the MIT license. Modified: 01 August 2015 Author: John Burkardt */ { double *a; int n = 5; int seed = 123456789; printf ( "\n" ); printf ( "R8PO_RANDOM_TEST\n" ); printf ( " R8PO_RANDOM computes a random positive definite\n" ); printf ( " symmetric matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); a = r8po_random ( n, &seed ); r8po_print ( n, a, " The random R8PO matrix:" ); free ( a ); return; } /******************************************************************************/ void r8po_sl_test ( ) /******************************************************************************/ /* Purpose: R8PO_SL_TEST tests R8PO_SL. Licensing: This code is distributed under the MIT license. Modified: 02 August 2015 Author: John Burkardt */ { double *a; double *b; int i; int n = 5; double *r; double *x; printf ( "\n" ); printf ( "R8PO_SL_TEST\n" ); printf ( " R8PO_SL solves a linear system with R8PO matrix\n" ); printf ( " after it has been factored by R8PO_FA.\n" ); a = r8po_zeros ( n ); for ( i = 0; i < n; i++ ) { a[i+i*n] = 2.0; } for ( i = 0; i < n - 1; i++ ) { a[i+(i+1)*n] = -1.0; } r8po_print ( n, a, " Matrix A:" ); /* Factor the matrix. */ r = r8po_fa ( n, a ); if ( r == NULL ) { printf ( "\n" ); printf ( " R8PO_FA declares the matrix is singular!\n" ); return; } /* Set the right hand side. */ b = r8vec_zeros_new ( n ); b[n-1] = ( double ) ( n + 1 ); r8vec_print ( n, b, " Right hand side b:" ); /* Solve the linear system. */ x = r8po_sl ( n, r, b ); r8vec_print ( n, x, " Solution x:" ); /* Free memory. */ free ( a ); free ( b ); free ( r ); free ( x ); return; } /******************************************************************************/ void r8po_to_r8ge_test ( ) /******************************************************************************/ /* Purpose: R8PO_TO_R8GE_TEST tests R8PO_TO_R8GE. Licensing: This code is distributed under the MIT license. Modified: 01 August 2015 Author: John Burkardt */ { double *a; double *b; int n = 5; int seed = 123456789; printf ( "\n" ); printf ( "R8PO_TO_R8GE_TEST\n" ); printf ( " R8PO_TO_R8GE converts an R8PO matrix to R8GE format.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); a = r8po_random ( n, &seed ); r8po_print ( n, a, " The random R8PO matrix:" ); r8ge_print ( n, n, a, " The random R8PO matrix (printed by R8GE_PRINT):" ); b = r8po_to_r8ge ( n, a ); r8ge_print ( n, n, b, " The random R8GE matrix (printed by R8GE_PRINT):" ); free ( a ); free ( b ); return; } /******************************************************************************/ void r8po_zeros_test ( ) /******************************************************************************/ /* Purpose: R8PO_ZEROS_TEST tests R8PO_ZEROS. Licensing: This code is distributed under the MIT license. Modified: 01 August 2015 Author: John Burkardt */ { double *a; int n = 5; printf ( "\n" ); printf ( "R8PO_ZEROS_TEST\n" ); printf ( " R8PO_ZEROS returns a zeroed out R8PO matrix.\n" ); printf ( "\n" ); printf ( " Matrix order N = %d\n", n ); a = r8po_zeros ( n ); r8po_print ( n, a, " Matrix A:" ); 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 }