00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <string>
00014 #include <complex>
00015 #include "Matrix.h"
00016 #include <time.h>
00017
00018 #if defined _MSC_VER && _MSC_VER < 1400
00019 #define _CRT_SECURE_NO_DEPRECATE
00020 #endif
00021
00022 #ifndef _MSC_VER
00023 #define _CRT_SECURE_NO_DEPRECATE
00024 #endif
00025
00026
00027 using namespace Zenautics;
00028 using namespace std;
00029
00030 int main( int argc, char* argv[] )
00031 {
00032 try
00033 {
00034
00035 Matrix A;
00036 Matrix B;
00037 Matrix C;
00038 char c;
00039 int i;
00040 int nr;
00041 char linestr[256];
00042 char MatrixBuffer[65536];
00043 int scount = 0;
00044 int nrTimes;
00045 time_t t0, t1;
00046 clock_t c0, c1;
00047
00048
00049 cout << "A = B*C" << endl;
00050 cout << "(Press U): User input matrix B and matrix C." << endl;
00051 cout << "(Press S): B = 1:1:N Column Matrix" << endl;
00052 cout << " C = 0.1:0.1:N/10 Column Matrix" << endl;
00053 cout << " B = B*B.Transpose()" << endl;
00054 cout << " C = C*C.Transpose()" << endl;
00055 cout << "Enter Choice:";
00056 cin.get( c );
00057 if( cin.fail() )
00058 {
00059 cout << "Bad input. Aborting." << endl;
00060 return 1;
00061 }
00062 c = toupper( c );
00063
00064 cout << "Enter the number of times to perform the multiplication:";
00065 cin >> nrTimes;
00066 if( nrTimes <= 0 )
00067 {
00068 cout << "Bad input. Aborting." << endl;
00069 return 1;
00070 }
00071
00072 if( c == 'U' )
00073 {
00074 cout << "You will enter Matrix B and Matrix C." << endl;
00075 cout << "Ensure that the matrices are conformal for multiplication." << endl;
00076
00077 cout << "Enter the number of rows B:";
00078 cin >> nr;
00079 if( nr < 0 )
00080 {
00081 cout << "Bad input. Aborting." << endl;
00082 return 1;
00083 }
00084 cout << "\nThe number of columns is determined automatically." << endl;
00085 cout << "Enter each row of the matrix then press Enter." << endl;
00086 cin.clear();
00087 for( i = 0; i < nr; i++ )
00088 {
00089 cin.getline( linestr, 256 );
00090 if( cin.fail() )
00091 {
00092 cout << "Bad input. Aborting." << endl;
00093 return 1;
00094 }
00095 if( strlen( linestr ) == 0 )
00096 {
00097
00098 i--;
00099 continue;
00100 }
00101 #ifndef _CRT_SECURE_NO_DEPRECATE
00102 scount += sprintf_s( MatrixBuffer+scount, 65536-scount, "%s\r\n", linestr );
00103 #else
00104 scount += sprintf( MatrixBuffer+scount, "%s\r\n", linestr );
00105 #endif
00106 }
00107 if( !B.SetFromMatrixString( MatrixBuffer ) )
00108 {
00109 cout << "Bad input. Aborting." << endl;
00110 return 1;
00111 }
00112
00113 cout << "Enter the number of rows C:";
00114 cin >> nr;
00115 if( nr < 0 )
00116 {
00117 cout << "Bad input. Aborting." << endl;
00118 return 1;
00119 }
00120 cout << "\nThe number of columns is determined automatically." << endl;
00121 cout << "Enter each row of the matrix then press Enter." << endl;
00122 scount = 0;
00123 cin.clear();
00124 for( i = 0; i < nr; i++ )
00125 {
00126 cin.getline( linestr, 256 );
00127 if( cin.fail() )
00128 {
00129 cout << "Bad input. Aborting." << endl;
00130 return 1;
00131 }
00132 if( strlen( linestr ) == 0 )
00133 {
00134
00135 i--;
00136 continue;
00137 }
00138 #ifndef _CRT_SECURE_NO_DEPRECATE
00139 scount += sprintf_s( MatrixBuffer+scount, 65536-scount, "%s\n", linestr );
00140 #else
00141 scount += sprintf( MatrixBuffer+scount, "%s\n", linestr );
00142 #endif
00143 }
00144 if( !C.SetFromMatrixString( MatrixBuffer ) )
00145 {
00146 cout << "Bad input. Aborting." << endl;
00147 return 1;
00148 }
00149
00150 cout << "B = " << endl;
00151 B.PrintStdout();
00152 cout << "C = " << endl;
00153 C.PrintStdout();
00154 }
00155 else
00156 {
00157 cout << "Enter the square dimensions of Matrix B and C: " << endl;
00158 cin >> nr;
00159 if( nr < 0 )
00160 {
00161 cout << "Bad input. Aborting." << endl;
00162 return 1;
00163 }
00164 if( !B.Inplace_colon( 1.0, 1.0, nr ) )
00165 {
00166 cout << "B.Inplace_colon returned false. Bad input. Aborting." << endl;
00167 return 1;
00168 }
00169 if( !C.Inplace_colon( 0.1, 0.1, nr/10.0+1e-09 ) )
00170 {
00171 cout << "B.Inplace_colon returned false. Bad input. Aborting." << endl;
00172 return 1;
00173 }
00174 B = B * B.Transpose();
00175 C = C * C.Transpose();
00176
00177 cout << "A = B*C;" << endl;
00178 if( B.nrows() < 20 )
00179 {
00180 cout << "B = " << endl;
00181 B.PrintStdout();
00182 }
00183 if( C.nrows() < 20 )
00184 {
00185 cout << "C = " << endl;
00186 C.PrintStdout();
00187 }
00188
00189
00190 }
00191
00192 t0 = time(NULL);
00193 c0 = clock();
00194
00195 for( i = 0; i < nrTimes; i++ )
00196 {
00197 A = B*C;
00198 if( i == 0 )
00199 {
00200 cout << "A = B*C;" << endl;
00201 cout << "A = " << endl;
00202 A.PrintStdout();
00203 }
00204 }
00205
00206 t1 = time(NULL);
00207 c1 = clock();
00208 printf( "Multipliciation testing took %10.1f ms\n", (float) (c1 - c0)/CLOCKS_PER_SEC*1000.0 );
00209 }
00210 catch( MatrixException& matrix_exception )
00211 {
00212 cout << matrix_exception << endl;
00213 }
00214 catch ( ... )
00215 {
00216 cout << "Caught unknown exception" << endl;
00217 }
00218
00219 char anykey;
00220 cout << "\nProgram complete.\n" << endl;
00221 cout << "Type something and hit enter: " << endl;
00222 cin.get( anykey );
00223 cin.get( anykey );
00224 return 0;
00225 }
00226