example_main.cpp

Go to the documentation of this file.
00001 /**
00002 \file     example_main.cpp
00003 \brief    An example program using the Zenautics Matrix Class.
00004 \author   Glenn D. MacGougan (GDM)
00005 \date     2008-04-23
00006 \version  0.02 Beta
00007 
00008 \b License \b Information \n
00009 Copyright (c) 2008, Glenn D. MacGougan, Zenautics Technologies Inc. \n
00010 This file may be used, copied, modified and redistributed without restriction.
00011 */
00012 
00013 #include <iostream>
00014 #include <string>
00015 #include <complex>
00016 #include "Matrix.h"
00017 
00018 using namespace Zenautics;
00019 using namespace std;
00020 
00021 #ifndef PI
00022 #define PI  (3.1415926535897932384626433832795) 
00023 #endif
00024  
00025 int main( int argc, char* argv[] )                            
00026 {  
00027   try
00028   {
00029     bool result = false;
00030     unsigned i;
00031     double re;
00032     double im;
00033 
00034     Matrix A;       // Create an empty matrix.
00035     Matrix B(2,2);  // Create a 2x2 matrix. 
00036     Matrix C;
00037     Matrix S;
00038 
00039     // Fill A from a matrix string.
00040     A = "[1 3; -2 4]"; 
00041 
00042     // Fill B with real data. A and B are now equal.
00043     B[0][0] = 1.0;
00044     B[0][1] = 3.0;
00045     B[1][0] = -2.0;
00046     B[1][1] = 4.0;
00047 
00048     // Output A to stdout.
00049     cout << "A =" << endl;
00050     A.PrintStdout();
00051 
00052     // Output B to stdout.
00053     cout << "B =" << endl;
00054     A.PrintStdout();
00055 
00056     // Output A to file.
00057     result = A.Print( "A.txt", 4 );
00058 
00059     // Load C from file.
00060     result = C.ReadFromFile( "A.txt" );
00061 
00062     // Save A to a binary file. 
00063     result = A.Save( "A.mtx" );
00064 
00065     // Load C from the binary file.
00066     result = C.ReadFromFile( "A.mtx" );
00067 
00068     // Add
00069     C = A+B;
00070     cout << "C = A+B;\nC = " << endl;
00071     C.PrintStdout();
00072     
00073     // Subtract
00074     C = A-B;
00075     cout << "C = A-B;\nC = " << endl;
00076     C.PrintStdout();
00077 
00078     // Multiply
00079     C = A*B;
00080     cout << "C = A*B;\nC = " << endl;
00081     C.PrintStdout();
00082 
00083     // Transpose
00084     C = A.Transpose();
00085     cout << "C = transpose(A);\nC = " << endl;
00086     C.PrintStdout();
00087 
00088     // Inversion
00089     C = A.Inverse();
00090     cout << "C = inverse(A);\nC = " << endl;
00091     C.PrintStdout();
00092 
00093     // Power
00094     C = A^3;
00095     cout << "C = A^3;\nC = " << endl;
00096     C.PrintStdout();
00097 
00098     // Determinant
00099     result = A.GetDeterminant(re,im); // im will be zero since A is real.
00100     cout << "determinant(A) = \n" << re << endl;
00101 
00102     // Trace
00103     A.GetTrace( re, im );// im will be zero since A is real.
00104     cout << "trace(A) = \n" << re << endl;
00105 
00106     // Identity
00107     C.Identity(12); // Set C to 12x12 Identity matrix;
00108     cout << "C = 12x12 Identity;\nC =" << endl;
00109     C.PrintStdout();
00110 
00111     // Make C = sqrt(-1) * I
00112     C *= -1.0; // multiply by a constant
00113     C.Inplace_sqrt();
00114     cout << "C = sqrt(-1) * 12x12 Identity;\nC =" << endl;
00115     C.PrintStdout();
00116 
00117     // Diagonal
00118     C = A.Diagonal();
00119     cout << "C = diagonal(A);\nC =" << endl;
00120     C.PrintStdout();
00121     
00122     // Tests
00123     cout << "A =" << endl;
00124     A.PrintStdout();
00125     if( A.isSquare() )
00126       cout << "A is a square matrix." << endl;
00127     else
00128       cout << "A is not a square matrix." << endl;
00129 
00130     if( A.isReal() )
00131       cout << "A is a real matrix." << endl;
00132     else
00133       cout << "A is a complex matrix." << endl;
00134     
00135     // Resize A to a 3x1 vector.
00136     A.Resize( 3 ); // Equivalently A.Resize(1,3) is a vector and is accessed the same way as below.
00137     A[0] = 1.0;
00138     A[1] = 2.0;
00139     A[2] = 3.0;
00140 
00141     // Output A to stdout.
00142     cout << "A =" << endl;
00143     A.PrintStdout();
00144 
00145     // Redimension B to a 3x3 matrix. The original data is maintained.
00146     B.Redim( 3, 3 );
00147     cout << "B =" << endl;
00148     B.PrintStdout();
00149 
00150     // Set A using complex values.
00151     complex<double> c1(1.0,1.0);
00152     complex<double> c2(-2.0,-2.0);
00153     complex<double> c3(3.0,3.0);
00154     A(0) = c1; // A is now a complex vector.
00155     A(1) = c2;
00156     A(2) = c3;
00157 
00158     cout << "A =" << endl;
00159     A.PrintStdout();
00160 
00161     // Demonstrate A as a complex matrix.
00162     A.Resize(2,2);
00163     A(0,0) = c1; // Set a matrix element to a complex value.
00164     A(0,1) = c2;
00165     A(1,0) = c2;
00166     A(1,1) = 10.0; // Set a matrix element to a double value.    
00167     cout << "A =" << endl;
00168     A.PrintStdout();
00169     re = A(0,0).real();
00170     im = A(1,1).imag();
00171 
00172     A.Redim(3,3);   // Change the dimensions of A to 3x3 and retain previous data.    
00173     cout << "A =" << endl;
00174     A.PrintStdout();
00175 
00176     // Set a matrix using a complex matrix string.
00177     A = "[1-2i 2+i; 3-7i 4-2i]";    
00178     cout << "A =" << endl;
00179     A.PrintStdout();
00180 
00181     // Plot columns of the matrix.
00182     A = "[1 2 3 4 5 6 7 8 9 10; 1 4 9 16 25 36 49 64 81 100;]";
00183     A.Inplace_transpose();
00184     cout << "A =" << endl;
00185     A.PrintStdout();
00186     A.Plot(0,1,"testplot.bmp","testing plot", "X", "Y", "Y=X^2" );
00187 
00188     // Plot is also a friend function in Matrix.h
00189     Matrix X;
00190     Matrix Y1;
00191     Matrix Y2;
00192     X = A.Column(0);
00193     Y1 = X^2;
00194     Y2 = X^3;
00195     Plot( "testplot2.bmp", "testing plot", "X (m)", "Y (m)", X, Y1, "Y=X^2", "(m)", X, Y2, "Y=X^3", "(m)", false, MTX_BLUE, false, MTX_RED, false, false, false );
00196 
00197     // Set S to a vector.
00198     S.Resize(512);
00199     // Set A to a complex tone sinusoid.
00200     for( i = 0; i < S.GetNrRows(); i++ )
00201     { 
00202       complex<double> cplx( cos(i*2*PI/512.0), sin(i*2*PI/512.0) );
00203       S(i) = cplx;
00204     }
00205     // Using the member plotting function, plot the real and imaginary components of S.
00206     C.Inplace_colon( 1.0/512.0, 1.0/512.0, 1.0 ); // Set C as a column vector from 1.0/512.0:1.0/512.0:1.0.
00207     C.Concatonate( S.Real() ); // Concatonte the real part of A to make C = C | real(S)
00208     C.Concatonate( S.Imag() ); // C = C | real(S) | imag(S)
00209     // Use the member plotting function.
00210     C.Plot( 0, 1, 2, "testplot3.bmp", "The real and imaginary parts of a complex sinusoid", "time (s)", "amplitude", "real part", "(V)", "imag part", "(V)" ); 
00211     
00212     // Take the fft of S inplace
00213     S.Inplace_FFT();  // Perform an fft of the columns in S. S is now complex. 
00214 
00215     std::complex<double> cplxResult;
00216     cplxResult = S(1);   // Get a complex value from the matrix.
00217     re = S(1).real();    // or re = S[1];
00218     im = S(1).imag();
00219 
00220     // Output A to a the specified ASCII file name.
00221     S.Print( "S.txt", 6 );
00222 
00223     // Output A to a the specified ASCII file name.
00224     S.PrintDelimited( "S.csv", 6, ',' );
00225 
00226     // Output A to a compressed binary matrix file.
00227     result = S.Save("S.mtx");
00228       
00229     // Read real or complex data from an ascii comma delimited file.          
00230     result = S.ReadFromFile( "S.csv" );
00231 
00232     // Read data from an ascii white space delimited file.    
00233     result = S.ReadFromFile( "S.txt" );                     
00234 
00235     // Read data from a binary .mtx format file.
00236     result = S.ReadFromFile( "S.mtx");
00237 
00238     // Force an exception with invalid index usage.
00239     // Uncomment the following lines to demonstrate matrix exception handling.
00240     //cout << "Causing an intentional exception to be thrown." << endl;
00241     //double q = A(2000,1).real();   // This will cause an exception.  
00242     //double r = A[2000][1];         // This will cause an exception.  
00243     //double s = A[2000];            // This will cause an exception.  
00244     //double t = A(2000).real();     // This will cause an exception.      
00245   }                                                           
00246   catch( MatrixException& matrix_exception )
00247   {
00248     cout << matrix_exception << endl;
00249   }
00250   catch ( ... )
00251   {
00252     cout << "Caught unknown exception" << endl;
00253   }
00254 
00255   char anykey;
00256   cout << "You have successfully executed the Zenautics Matrix Project example progam.\n" << endl;
00257   cout << "Type something and hit enter: " << endl;
00258   cin >> anykey;
00259   return 0;
00260 }                                   
00261 

Generated on Sun Feb 8 15:31:09 2009 for The Zenautics Matrix Project by  doxygen 1.5.4