# include # include # include # include # include # include using namespace std; int main ( ); void test01 ( ); void test02 ( ); void test03 ( ); void test04 ( ); void test05 ( ); void test06 ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // complex_numbers_test demonstrates the use of complex numbers. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "complex_numbers_test\n"; cout << " C++ version\n"; cout << " Demonstrate complex number usage.\n"; // // Single precision complex numbers: "complex ". // test01 ( ); test02 ( ); test03 ( ); // // Double precision complex numbers: "complex ". // test04 ( ); test05 ( ); test06 ( ); // // Terminate. // cout << "\n"; cout << "complex_numbers_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void test01 ( ) //****************************************************************************80 // // Purpose: // // TEST01 demonstrate declaration and assignment for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { // // Declare a complex number A. // Declare a complex vector B. // Declare a complex array C. // complex a; complex b[3]; complex c[2][2]; int i; int j; cout << "\n"; cout << "TEST01\n"; cout << " Declare a COMPLEX variable.\n"; cout << " Assign value with an = statement.\n"; // // Assign values to A, B, and C. // a = complex ( 1.0, 2.0 ); b[0] = complex ( 1.0, 2.0 ); b[1] = complex ( 3.0, 4.0 ); b[2] = complex ( 5.0, 6.0 ); c[0][0] = complex ( 1.0, 0.1 ); c[0][1] = complex ( 1.0, 0.2 ); c[1][0] = complex ( 2.0, 0.1 ); c[1][1] = complex ( 2.0, 0.2 ); // // Print them. // cout << "\n"; cout << " Scalar A:\n"; cout << "\n"; cout << " " << a << "\n"; cout << "\n"; cout << " Vector B:\n"; cout << "\n"; for ( i = 0; i < 3; i++ ) { cout << " " << b[i] << "\n"; } cout << "\n"; cout << " Array C:\n"; cout << "\n"; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 2; j++ ) { cout << " " << c[i][j]; } cout << "\n"; } return; } //****************************************************************************80 void test02 ( ) //****************************************************************************80 // // Purpose: // // TEST02: declaration and iniitialization for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { // // Declare and initialize a complex number A. // Declare and initialize a complex vector B. // Declare and initialize a complex array C. // complex a = complex ( 1.0, 2.0 ); complex b[3] = { complex ( 1.0, 2.0 ), complex ( 3.0, 4.0 ), complex ( 5.0, 6.0 ) }; complex c[2][2] = { { complex ( 1.0, 0.1 ), complex ( 1.0, 0.2 ) }, { complex ( 2.0, 0.1 ), complex ( 2.0, 0.2 ) } }; int i; int j; cout << "\n"; cout << "TEST02\n"; cout << " Declare a COMPLEX variable.\n"; cout << " Initialize as part of the declaration.\n"; // // Print them. // cout << "\n"; cout << " Scalar A:\n"; cout << "\n"; cout << " " << a << "\n"; cout << "\n"; cout << " Vector B:\n"; cout << "\n"; for ( i = 0; i < 3; i++ ) { cout << " " << b[i] << "\n"; } cout << "\n"; cout << " Array C:\n"; cout << "\n"; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 2; j++ ) { cout << " " << c[i][j]; } cout << "\n"; } return; } //****************************************************************************80 void test03 ( ) //****************************************************************************80 // // Purpose: // // TEST03: intrinsic functions for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 07 November 2010 // // Author: // // John Burkardt // { complex a = complex ( 1.0, 2.0 ); float pi = 3.141592653589793; cout << "\n"; cout << "TEST03\n"; cout << " Apply operations and intrinsic functions \n"; cout << " to COMPLEX variable.\n"; cout << "\n"; cout << " a = " << a << "\n"; cout << " - a = " << - a << "\n"; //cout << " a + 3 = " << a + 3 << "\n"; cout << " a + complex (0,5) = " << a + complex (0,5) << "\n"; //cout << " 4 * a = " << 4 * a << "\n"; //cout << " a / 8 = " << a / 8 << "\n"; cout << " pow ( a, 2 ) = " << pow ( a, 2 ) << "\n"; //cout << " pow ( 2, a ) = " << pow ( 2, a ) << "\n"; //cout << " pow ( 2.1, a ) = " << pow ( 2, a ) << "\n"; cout << " pow ( a, a ) = " << pow ( a, a ) << "\n"; //cout << " 1 / a = " << 1 / a << "\n"; cout << "\n"; cout << " abs ( a ) = " << abs ( a ) << "\n"; cout << " arg ( a ) = " << arg ( a ) << "\n"; cout << " conj ( a ) = " << conj ( a ) << "\n"; cout << " cos ( a ) = " << cos ( a ) << "\n"; cout << " cosh ( a ) = " << cosh ( a ) << "\n"; cout << " exp ( a ) = " << exp ( a ) << "\n"; cout << " imag ( a ) = " << imag ( a ) << "\n"; cout << " log ( a ) = " << log ( a ) << "\n"; cout << " log10 ( a ) = " << log10 ( a ) << "\n"; cout << " norm ( a ) = " << norm ( a ) << "\n"; cout << " polar (10.0,pi/4) = " << polar ( 10.0, pi/4.0 ) << "\n"; cout << " real ( a ) = " << real ( a ) << "\n"; cout << " sin ( a ) = " << sin ( a ) << "\n"; cout << " sinh ( a ) = " << sinh ( a ) << "\n"; cout << " sqrt ( a ) = " << sqrt ( a ) << "\n"; cout << " tan ( a ) = " << tan ( a ) << "\n"; cout << " tanh ( a ) = " << tanh ( a ) << "\n"; return; } //****************************************************************************80 void test04 ( ) //****************************************************************************80 // // Purpose: // // TEST04 demonstrate declaration and assignment for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { // // Declare a complex number A. // Declare a complex vector B. // Declare a complex array C. // complex a; complex b[3]; complex c[2][2]; int i; int j; cout << "\n"; cout << "TEST04\n"; cout << " Declare a COMPLEX variable.\n"; cout << " Assign value with an = statement.\n"; // // Assign values to A, B, and C. // a = complex ( 1.0, 2.0 ); b[0] = complex ( 1.0, 2.0 ); b[1] = complex ( 3.0, 4.0 ); b[2] = complex ( 5.0, 6.0 ); c[0][0] = complex ( 1.0, 0.1 ); c[0][1] = complex ( 1.0, 0.2 ); c[1][0] = complex ( 2.0, 0.1 ); c[1][1] = complex ( 2.0, 0.2 ); // // Print them. // cout << "\n"; cout << " Scalar A:\n"; cout << "\n"; cout << " " << a << "\n"; cout << "\n"; cout << " Vector B:\n"; cout << "\n"; for ( i = 0; i < 3; i++ ) { cout << " " << b[i] << "\n"; } cout << "\n"; cout << " Array C:\n"; cout << "\n"; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 2; j++ ) { cout << " " << c[i][j]; } cout << "\n"; } return; } //****************************************************************************80 void test05 ( ) //****************************************************************************80 // // Purpose: // // TEST05: declaration and iniitialization for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { // // Declare and initialize a complex number A. // Declare and initialize a complex vector B. // Declare and initialize a complex array C. // complex a = complex ( 1.0, 2.0 ); complex b[3] = { complex ( 1.0, 2.0 ), complex ( 3.0, 4.0 ), complex ( 5.0, 6.0 ) }; complex c[2][2] = { { complex ( 1.0, 0.1 ), complex ( 1.0, 0.2 ) }, { complex ( 2.0, 0.1 ), complex ( 2.0, 0.2 ) } }; int i; int j; cout << "\n"; cout << "TEST05\n"; cout << " Declare a COMPLEX variable.\n"; cout << " Initialize as part of the declaration.\n"; // // Print them. // cout << "\n"; cout << " Scalar A:\n"; cout << "\n"; cout << " " << a << "\n"; cout << "\n"; cout << " Vector B:\n"; cout << "\n"; for ( i = 0; i < 3; i++ ) { cout << " " << b[i] << "\n"; } cout << "\n"; cout << " Array C:\n"; cout << "\n"; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 2; j++ ) { cout << " " << c[i][j]; } cout << "\n"; } return; } //****************************************************************************80 void test06 ( ) //****************************************************************************80 // // Purpose: // // TEST06: intrinsic functions for complex variables. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 November 2010 // // Author: // // John Burkardt // { complex a = complex ( 1.0, 2.0 ); double pi = 3.141592653589793; cout << "\n"; cout << "TEST06\n"; cout << " Apply operations and intrinsic functions \n"; cout << " to COMPLEX variable.\n"; cout << "\n"; cout << " a = " << a << "\n"; cout << " - a = " << - a << "\n"; //cout << " a + 3 = " << a + 3 << "\n"; cout << " a + complex (0,5) = " << a + complex (0,5) << "\n"; //cout << " 4 * a = " << 4 * a << "\n"; //cout << " a / 8 = " << a / 8 << "\n"; cout << " pow ( a, 2 ) = " << pow ( a, 2 ) << "\n"; //cout << " pow ( 2, a ) = " << pow ( 2, a ) << "\n"; //cout << " pow ( 2.1, a ) = " << pow ( 2, a ) << "\n"; cout << " pow ( a, a ) = " << pow ( a, a ) << "\n"; //cout << " 1 / a = " << 1 / a << "\n"; cout << "\n"; cout << " abs ( a ) = " << abs ( a ) << "\n"; cout << " arg ( a ) = " << arg ( a ) << "\n"; cout << " conj ( a ) = " << conj ( a ) << "\n"; cout << " cos ( a ) = " << cos ( a ) << "\n"; cout << " cosh ( a ) = " << cosh ( a ) << "\n"; cout << " exp ( a ) = " << exp ( a ) << "\n"; cout << " imag ( a ) = " << imag ( a ) << "\n"; cout << " log ( a ) = " << log ( a ) << "\n"; cout << " log10 ( a ) = " << log10 ( a ) << "\n"; cout << " norm ( a ) = " << norm ( a ) << "\n"; cout << " polar (10.0,pi/4) = " << polar ( 10.0, pi/4.0 ) << "\n"; cout << " real ( a ) = " << real ( a ) << "\n"; cout << " sin ( a ) = " << sin ( a ) << "\n"; cout << " sinh ( a ) = " << sinh ( a ) << "\n"; cout << " sqrt ( a ) = " << sqrt ( a ) << "\n"; cout << " tan ( a ) = " << tan ( a ) << "\n"; cout << " tanh ( a ) = " << tanh ( a ) << "\n"; 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 // // Parameters: // // None // { # 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 }