00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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;
00035 Matrix B(2,2);
00036 Matrix C;
00037 Matrix S;
00038
00039
00040 A = "[1 3; -2 4]";
00041
00042
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
00049 cout << "A =" << endl;
00050 A.PrintStdout();
00051
00052
00053 cout << "B =" << endl;
00054 A.PrintStdout();
00055
00056
00057 result = A.Print( "A.txt", 4 );
00058
00059
00060 result = C.ReadFromFile( "A.txt" );
00061
00062
00063 result = A.Save( "A.mtx" );
00064
00065
00066 result = C.ReadFromFile( "A.mtx" );
00067
00068
00069 C = A+B;
00070 cout << "C = A+B;\nC = " << endl;
00071 C.PrintStdout();
00072
00073
00074 C = A-B;
00075 cout << "C = A-B;\nC = " << endl;
00076 C.PrintStdout();
00077
00078
00079 C = A*B;
00080 cout << "C = A*B;\nC = " << endl;
00081 C.PrintStdout();
00082
00083
00084 C = A.Transpose();
00085 cout << "C = transpose(A);\nC = " << endl;
00086 C.PrintStdout();
00087
00088
00089 C = A.Inverse();
00090 cout << "C = inverse(A);\nC = " << endl;
00091 C.PrintStdout();
00092
00093
00094 C = A^3;
00095 cout << "C = A^3;\nC = " << endl;
00096 C.PrintStdout();
00097
00098
00099 result = A.GetDeterminant(re,im);
00100 cout << "determinant(A) = \n" << re << endl;
00101
00102
00103 A.GetTrace( re, im );
00104 cout << "trace(A) = \n" << re << endl;
00105
00106
00107 C.Identity(12);
00108 cout << "C = 12x12 Identity;\nC =" << endl;
00109 C.PrintStdout();
00110
00111
00112 C *= -1.0;
00113 C.Inplace_sqrt();
00114 cout << "C = sqrt(-1) * 12x12 Identity;\nC =" << endl;
00115 C.PrintStdout();
00116
00117
00118 C = A.Diagonal();
00119 cout << "C = diagonal(A);\nC =" << endl;
00120 C.PrintStdout();
00121
00122
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
00136 A.Resize( 3 );
00137 A[0] = 1.0;
00138 A[1] = 2.0;
00139 A[2] = 3.0;
00140
00141
00142 cout << "A =" << endl;
00143 A.PrintStdout();
00144
00145
00146 B.Redim( 3, 3 );
00147 cout << "B =" << endl;
00148 B.PrintStdout();
00149
00150
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;
00155 A(1) = c2;
00156 A(2) = c3;
00157
00158 cout << "A =" << endl;
00159 A.PrintStdout();
00160
00161
00162 A.Resize(2,2);
00163 A(0,0) = c1;
00164 A(0,1) = c2;
00165 A(1,0) = c2;
00166 A(1,1) = 10.0;
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);
00173 cout << "A =" << endl;
00174 A.PrintStdout();
00175
00176
00177 A = "[1-2i 2+i; 3-7i 4-2i]";
00178 cout << "A =" << endl;
00179 A.PrintStdout();
00180
00181
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
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
00198 S.Resize(512);
00199
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
00206 C.Inplace_colon( 1.0/512.0, 1.0/512.0, 1.0 );
00207 C.Concatonate( S.Real() );
00208 C.Concatonate( S.Imag() );
00209
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
00213 S.Inplace_FFT();
00214
00215 std::complex<double> cplxResult;
00216 cplxResult = S(1);
00217 re = S(1).real();
00218 im = S(1).imag();
00219
00220
00221 S.Print( "S.txt", 6 );
00222
00223
00224 S.PrintDelimited( "S.csv", 6, ',' );
00225
00226
00227 result = S.Save("S.mtx");
00228
00229
00230 result = S.ReadFromFile( "S.csv" );
00231
00232
00233 result = S.ReadFromFile( "S.txt" );
00234
00235
00236 result = S.ReadFromFile( "S.mtx");
00237
00238
00239
00240
00241
00242
00243
00244
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