# include # include # include # include # include # include using namespace std; # include "test_eigen.hpp" int main ( ); void r8nsymm_gen_test ( ); void r8symm_gen_test ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // test_eigen_test() tests test_eigen(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 June 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "test_eigen_test():\n"; cout << " C++ version\n"; cout << " Test test_eigen().\n"; r8symm_gen_test ( ); r8nsymm_gen_test ( ); // // Terminate. // cout << "\n"; cout << "test_eigen_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void r8symm_gen_test ( ) //****************************************************************************80 // // Purpose: // // r8symm_gen_test() tests r8symm_gen(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 June 2024 // // Author: // // John Burkardt // { double *A; double *aq; int i; int j; double *lambda; double *lambda2; double lambda_dev = 5.0; double lambda_mean = 10.0; int n = 5; double *Q; int seed = 123456789; cout << "\n"; cout << "r8symm_gen_test()\n"; cout << " r8symm_gen() generates an arbitrary size symmetric matrix\n"; cout << " with known eigenvalues and eigenvectors.\n"; A = new double[n*n]; Q = new double[n*n]; lambda = new double[n]; r8symm_gen ( n, lambda_mean, lambda_dev, seed, A, Q, lambda ); r8mat_print ( n, n, A, " The matrix A:" ); r8mat_print ( n, n, Q, " The eigenvector matrix Q:" ); r8vec_print ( n, lambda, " The eigenvalue vector LAMBDA:" ); aq = r8mat_mm_new ( n, n, n, A, Q ); lambda2 = new double[n]; for ( j = 0; j < n; j++ ) { lambda2[j] = 0.0; for ( i = 0; i < n; i++ ) { lambda2[j] = lambda2[j] + pow ( aq[i+j*n], 2 ); } lambda2[j] = sqrt ( lambda2[j] ); } r8vec2_print ( n, lambda, lambda2, " LAMBDA versus the column norms of A*Q:" ); // // Free memory. // delete [] A; delete [] aq; delete [] lambda; delete [] lambda2; delete [] Q; return; } //****************************************************************************80 void r8nsymm_gen_test ( ) //****************************************************************************80 // // Purpose: // // r8nsymm_gen_test() tests r8nsymm_gen(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 June 2024 // // Author: // // John Burkardt // { double *A; int i; double *lambda; double lambda_dev = 5.0; double lambda_mean = 10.0; int n = 5; double *Q; int seed = 123456789; double *T; cout << "\n"; cout << "r8nsymm_gen_test():\n"; cout << " r8nsymm_gen() generates an arbitrary size nonsymmetric matrix\n"; cout << " with known eigenvalues and eigenvectors.\n"; A = new double[n*n]; Q = new double[n*n]; T = new double[n*n]; r8nsymm_gen ( n, lambda_mean, lambda_dev, seed, A, Q, T ); lambda = new double[n]; for ( i = 0; i < n; i++ ) { lambda[i] = T[i+i*n]; } r8vec_sort_bubble_a ( n, lambda ); r8mat_print ( n, n, A, " The matrix A:" ); r8mat_print ( n, n, Q, " The orthogonal factor Q:" ); r8mat_print ( n, n, T, " The upper triangular factor T:" ); r8vec_print ( n, lambda, " The eigenvalue vector LAMBDA:" ); // // Free memory. // delete [] A; delete [] lambda; delete [] Q; delete [] T; return; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // 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: // // 08 July 2009 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }