Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

complex.cc

Go to the documentation of this file.
00001 // complex.cc
00002 
00003 // Brief introduction to SuperMix's complex number class
00004 
00005 // We access the capabilities of the library with the following
00006 // #include statement:
00007 #include "supermix.h"
00008 
00009 // SuperMix header files are found in the SuperMix "include/"
00010 // directory.
00011 
00012 // The complex number class used by SuperMix is NOT the same as
00013 // the class provided with the standard C++ class library (usually
00014 // loaded with a statement like " #include <complex.h> " or some
00015 // such). DO NOT TRY TO USE BOTH THE SuperMix COMPLEX CLASS AND THE
00016 // STANDARD LIBRARY COMPLEX CLASS IN THE SAME PROGRAM!
00017 
00018 // The SuperMix header file "SIScmplx.h" defines the SuperMix
00019 // complex number class. It contains extensive comments describing
00020 // the capabilities of the class; this program provides a simple
00021 // introduction to some of these capabilities.
00022 
00023 int main()
00024 {
00025   // -----------------------------------------------------------------
00026   // BASIC CREATION AND MANIPULATION OF COMPLEX NUMBERS
00027 
00028   // Declaring a complex number is easy:
00029 
00030   complex z;
00031 
00032 
00033   // The number is stored internally in Cartesian form. We can
00034   // access and manipulate the two components directly (each component
00035   // is of type "double"):
00036 
00037   z.real = 3;
00038   z.imaginary = z.real + 1;
00039 
00040 
00041   // Here we output the value of z. The default output format writes
00042   // out the Cartesian representation:
00043 
00044   cout << "z = " << z << endl;
00045 
00046 
00047   //When we declare a complex variable we can give it an initialization:
00048 
00049   complex u(2,1);    // u = 2+i1
00050   complex v(3);      // v = 3+i0
00051   complex w = 5;     // w = 5+i0
00052 
00053   cout << "u = " << u << ", v = " << v << ", w = " << w << endl;
00054 
00055 
00056   // We can manipulate complex numbers using the same sorts of
00057   // formulas we'd use with doubles:
00058 
00059   z = u + 2*v;
00060   cout << "u = " << u << ", v = " << v << ", u + 2*v = " << z << endl;
00061 
00062   w += z/u;
00063   cout << "z = " << z << ", u = " << u << ", w += z/u = " << w << endl;
00064 
00065 
00066   // The predefined consant "I" (defined in "global.h") can be used in
00067   // formulas:
00068 
00069   w = 3.0 + I*4.0;   // w = 3+i4
00070 
00071 
00072   // A temporary, unnamed complex value can be created by using the
00073   // complex() constructor:
00074 
00075   u = complex(5,6);  // u = 5+i6
00076 
00077   // This can also be used to cast a double value to type complex,
00078   // when you need a complex argument for a function:
00079 
00080   z = sqrt(complex(-4.0)); // z = 0+i2
00081 
00082 
00083   // To construct a complex number from a magnitude and phase (in radians),
00084   // use the function polar():
00085 
00086   z = polar(1,Pi/2);       // "Pi" is another predefined constant
00087   v = polar(2,45*Degree);  // the constant "Degree" == Pi/180
00088 
00089   cout << "w = " << w << ", u = " << u
00090        << ", z = " << z << ", v = " << v << endl;
00091 
00092 
00093   // -----------------------------------------------------------------
00094   // OUTPUT FORMATTING OF COMPLEX NUMBERS, I/O INFORMATION
00095 
00096   // We can output complex numbers in several different formats. The
00097   // format affects only the way the number is displayed, not the
00098   // internal representation.
00099 
00100   complex::out_cartesian();   // this sets the default form used above
00101   cout << "Cartesian format: " << v << endl;
00102 
00103   complex::out_polar();       // magnitude phase(radians) format
00104   cout << "polar format:     " << v << endl;
00105 
00106   complex::out_degree();      // magnitude phase(degrees) format
00107   cout << "degree format:    " << v << endl;
00108 
00109   complex::out_db();          // magnitude(dB) phase(degrees) format
00110   cout << "db format:        " << v << endl;
00111 
00112   // Note that the decibel conversion is appropriate for wave
00113   // amplitude, not power. The formula used is 20*log10(magnitude).
00114 
00115   // Changing the leading and trailing characters as well as the
00116   // separator character is possible; see the comments in the file
00117   // "SIScmplx.h".
00118 
00119   // For example, to get rid of the "+i" between the Cartesian
00120   // components in the output and replace it with a single space,
00121   // just use:
00122 
00123   complex::out_space();      // separate output numbers with " ".
00124   complex::out_cartesian();  // back to Cartesian output mode.
00125 
00126   cout << "Cartesian, with just a space as a separator: " << v << endl;
00127 
00128   complex::out_default();    // back to using "+i" separator.
00129 
00130   // Similar conversions are available during complex number input.
00131   // See the "SIScmplx.h" header file for details, and the programs
00132   // "test_complex_io.cc" and "test_complex_input.cc" in the
00133   // Supermix "test/src/" directory.
00134 
00135 
00136   // -----------------------------------------------------------------
00137   // FUNCTIONS OF COMPLEX NUMBERS
00138 
00139   // Complex number versions of many of the standard C++ numerical
00140   // functions are implemented by SuperMix. Here are some examples;
00141   // a complete list is provided in "SIScmplx.h".
00142 
00143   z = I;
00144   cout << "z        = " << z        << endl
00145        << "sqrt(z)  = " << sqrt(z)  << endl
00146        << "log(z)   = " << log(z)   << endl     // natural logarithm
00147        << "log10(z) = " << log10(z) << endl     // common logarithm
00148        << "exp(z)   = " << exp(z)   << endl
00149        << "pow(z,I) = " << pow(z,I) << endl
00150        << "cos(z)   = " << cos(z)   << endl
00151        << "sinh(z)  = " << sinh(z)  << endl
00152        << "atanh(z) = " << atanh(z) << endl;
00153 
00154   // Other functions are especially for complex numbers:
00155   cout << "conj(z)  = " << conj(z)  << endl
00156        << "arg(z)   = " << arg(z)   << endl;    // in radians
00157 
00158   // The absolute value function returns the magnitude. Since abs(z)
00159   // is equivalent to the C++ standard fabs(double), SuperMix
00160   // defines abs(double) to return the correct absolute value. The
00161   // default C++ behavior would be to truncate the double to an int
00162   // if you used abs() on a double value.
00163   cout << "abs(z)   = " << abs(z)   << endl     // magnitude
00164        << "abs(-Pi) = " << abs(-Pi) << endl;    // works for doubles
00165   
00166     
00167 }

Please direct comments and corrections to supermix@submm.caltech.edu
Go to the supermix home page
Generated by doxygen1.2.7