00001 #pragma ident "$Id: MatrixTest.cpp 3140 2012-06-18 15:03:02Z susancummins $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Vector.hpp"
00026 #include "Matrix.hpp"
00027 #include "PolyFit.hpp"
00028 #include "Stats.hpp"
00029
00030 #include <iostream>
00031 #include <iomanip>
00032 #include <fstream>
00033
00039 using namespace std;
00040
00041 int ReadMatrix(gpstk::Matrix<double>& M, string& file)
00042 {
00043 ifstream infile(file.c_str());
00044 if(!infile) return -1;
00045
00046 enum Type{GEN,LOW,UPT,DIA,SYM,SQU};
00047 string labels[6]={"GEN","LOW","UPT","DIA","SYM","SQU"};
00048 bool dimmed=false;
00049 char ch;
00050 int r=0,c=0,i=0,j=0;
00051 string buffer;
00052 Type type=GEN;
00053 while(infile >> buffer) {
00054 if(buffer[0] == '#') {
00055 while(infile.get(ch)) { if(ch=='\n') break; }
00056 }
00057 else {
00058 string::size_type in;
00059 if((in=buffer.find("r=")) != string::npos) {
00060 r = strtol(buffer.substr(in+2).c_str(),0,10);
00061
00062 if(type != GEN) c=r;
00063 }
00064 else if((in=buffer.find("c=")) != string::npos) {
00065 c = strtol(buffer.substr(in+2).c_str(),0,10);
00066
00067 if(type != GEN) r=c;
00068 }
00069 else if((in=buffer.find("t=")) != string::npos) {
00070 if((in=buffer.find("LOW")) != string::npos) type=LOW;
00071 if((in=buffer.find("UPT")) != string::npos) type=UPT;
00072 if((in=buffer.find("DIA")) != string::npos) type=DIA;
00073 if((in=buffer.find("SYM")) != string::npos) type=SYM;
00074 if((in=buffer.find("SQU")) != string::npos) type=SQU;
00075
00076 }
00077 else if(buffer == string(":::")) {
00078 if(r*c == 0) return 0;
00079 M = gpstk::Matrix<double>(r,c,double(0));
00080 dimmed = true;
00081 }
00082 else {
00083 if(!dimmed) continue;
00084 M(i,j) = strtod(buffer.c_str(),0);
00085 j++;
00086 switch(type) {
00087 case LOW: if(j>i) { i++; j=0; } break;
00088 case UPT: if(j>=c) { i++; j=i; } break;
00089 case DIA: i=j; break;
00090 case SYM: M(j-1,i) = M(i,j-1); if(j>i) { i++; j=0; } break;
00091 case SQU: case GEN: default: if(j>=c) { i++; j=0; } break;
00092 }
00093 }
00094 }
00095 }
00096 infile.close();
00097 if(!dimmed) return -2;
00098 return 0;
00099 }
00100
00101 void VectorTest(void)
00102 {
00103 cout << "\n -------------- Vector Test ---------------------------------\n";
00104 gpstk::Vector<double> V(10);
00105 V += 3.1415;
00106 cout << "V = " << V << endl;
00107
00108 double dat[10]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1};
00109 V = dat;
00110 cout << "V = " << V << endl;
00111
00112 cout << "V min " << min(V) << ", max " << max(V) << ", sum " << sum(V) << endl;
00113
00114
00115 gpstk::VectorSlice<double> Vodd(V,std::slice(0,5,2));
00116 gpstk::ConstVectorSlice<double> Veve(V,std::slice(1,5,2));
00117 cout << "Vodd = " << Vodd << endl;
00118 cout << "Veve = " << Veve << endl;
00119 Vodd[1] = Vodd[3] = 0;
00120
00121 V[3] = V[7] = 99;
00122 cout << "Vodd = " << Vodd << endl;
00123 cout << "Veve = " << Veve << endl;
00124 cout << "Minkowski of Vodd and Veve " << Minkowski(Vodd,Veve) << endl;
00125 cout << "dot of Vodd and Veve " << dot(Vodd,Veve) << endl;
00126 cout << "V = " << V << endl;
00127 cout << "V min " << min(V) << ", max " << max(V) << ", sum " << sum(V) << ", norm " << norm(V) << endl;
00128
00129 gpstk::Vector<double> W=V;
00130 V.zeroTolerance = 1.e-15;
00131 cout << "Zero tolerance for V is " << V.zeroTolerance << endl;
00132 V.zeroTolerance = 1.e-5;
00133 cout << "Zero tolerance for W is " << W.zeroTolerance << endl;
00134
00135 W += V;
00136 cout << "Here is W the usual way :\n";
00137 cout << ' ' << fixed << setfill('.') << setw(8) << setprecision(3) << W << endl;
00138
00139 cout << "Here is W the saved way :\n";
00140 cout << fixed << setfill('.') << setw(8) << setprecision(3);
00141 ofstream savefmt;
00142 savefmt.copyfmt(cout);
00143 for(int i=0; i<W.size(); i++) {
00144 cout.copyfmt(savefmt);
00145 cout << W[i];
00146 }
00147 cout << setfill(' ') << endl;
00148
00149 gpstk::Vector<double> Sum;
00150 Sum = V + W;
00151 cout << "Sum = " << setw(8) << Sum << endl;
00152
00153 gpstk::ConstVectorSlice<double> CVS(V,std::slice(0,V.size(),1));
00154 cout << "CVS = " << setw(13) << CVS << endl;
00155 }
00156
00157 void MatrixTest1(int argc, char **argv)
00158 {
00159 cout << "\n -------------- Matrix Test 1 ---------------------------------\n";
00160 gpstk::Matrix<double> MD(2,5);
00161 double dat[10]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1};
00162 MD = dat;
00163 cout << "Matrix (" << MD.rows() << "," << MD.cols() << ") from double* :\n" << fixed << setprecision(1) << setw(5) << MD << endl;
00164
00165 string filename(argv[5]);
00166 gpstk::Matrix<double> MF;
00167 int iret=ReadMatrix(MF,filename);
00168 if(iret < 0) {
00169 cerr << "Error: could not open file " << filename << endl;
00170 return;
00171 }
00172
00173
00174
00175 gpstk::Vector<double> B(MF.colCopy(MF.cols()-1));
00176
00177
00178 gpstk::Matrix<double> A(MF,0,0,MF.rows(),MF.cols()-1);
00179 cout << "Partials Matrix (" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << A << endl;
00180 cout << "Data vector (" << B.size() << ") :\n" << fixed << setprecision(3) << setw(10) << B << endl;
00181
00182
00183 gpstk::Matrix<double> AT,ATA;
00184 AT = transpose(A);
00185
00186 ATA = AT * A;
00187
00188 gpstk::Matrix<double> Ainv;
00189 try { Ainv=inverse(ATA); }
00190 catch (gpstk::Exception& e) { cout << e << endl; return;}
00191 cout << "Covariance matrix (" << Ainv.rows() << "," << Ainv.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << Ainv << endl;
00192
00193 gpstk::Vector<double> Sol;
00194 Sol = Ainv * AT * B;
00195 cout << "Solution vector (" << Sol.size() << ") :\n" << fixed << setprecision(3) << setw(10) << Sol << endl;
00196 gpstk::Vector<double> Two;
00197 Two = B - A*Sol;
00198 cout << "Residual vector (" << Two.size() << ") :\n" << scientific << setprecision(3) << setw(10) << Two << endl;
00199
00200 Two = Sol + Sol;
00201 cout << "2*Solution vector (" << Two.size() << ") :\n" << fixed << setprecision(3) << setw(10) << Two << endl;
00202
00203 gpstk::Matrix<double> TA;
00204 TA = A + A;
00205 cout << "2*Partials Matrix (" << TA.rows() << "," << TA.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << TA << endl;
00206
00207 int i;
00208 gpstk::Vector<double> c(8),b(7);
00209 gpstk::Matrix<double> W;
00210 for(i=0; i<8; i++) c(i)=3*i;
00211 for(i=0; i<7; i++) b(i)=i+1;
00212 W = outer(c,b);
00213 cout << "Vector c(" << c.size() << ") :\n" << fixed << setprecision(3) << setw(7) << c << endl;
00214 cout << "Vector b(" << b.size() << ") :\n" << fixed << setprecision(3) << setw(7) << b << endl;
00215 cout << "Their outer product (" << W.rows() << "," << W.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << W << endl;
00216 cout << "Their norms " << norm(b) << " and " << norm(c) << " and dot " << dot(c,b) << " and cosine " << cosVec(c,b) << endl;
00217 }
00218
00219 void MatrixTest2(void)
00220 {
00221 cout << "\n -------------- Matrix Test 2 ---------------------------------\n";
00222 for(int n=2; n<13; n++) {
00223 double big,small,cond;
00224 gpstk::Matrix<double> MM(n,n),Minv,Prod;
00225 for(int i=0; i<n; i++) {
00226 for(int j=0; j<n; j++) {
00227 MM(i,j) = 1.0/(i+j+1.0);
00228 }
00229 }
00230 cout << "Tough matrix (" << MM.rows() << "," << MM.cols() << ") :\n" << fixed << setprecision(6) << setw(9) << MM << endl;
00231 Minv = inverse(MM);
00232 try { Minv=inverse(MM); }
00233 catch (gpstk::Exception& e) { cout << e << endl; break;}
00234 cond = condNum(MM,big,small);
00235 cout << "Condition number for " << n << " is " << fixed << setprecision(3) << big << "/" << scientific << setprecision(3) << small;
00236 if(small > 0.0) cout << " = " << setprecision(3) << big/small;
00237 cout << endl;
00238 cout << "Inverse matrix (" << Minv.rows() << "," << Minv.cols() << ") :\n" << fixed << setprecision(3) << setw(10+(n>5?n:0)) << Minv << endl;
00239 Prod = Minv * MM;
00240 gpstk::Vector<double>V;
00241 V.zeroTolerance = 1.e-3;
00242 Prod.zeroize();
00243 cout << "Unity matrix (" << Prod.rows() << "," << Prod.cols() << ") ? :\n" << fixed << setprecision(9) << setw(12) << Prod << endl;
00244 }
00245 }
00246
00247 void MatrixTest3(int argc, char **argv)
00248 {
00249 cout << "\n -------------- Matrix Test 3 ---------------------------------\n";
00250 cout << "Read and print matrix from file\n";
00251 double cond,big,small;
00252 gpstk::Matrix<double> A,Ainv,P;
00253 for(int i=1; i<argc; i++) {
00254 string filename=argv[i];
00255 cout << "File " << filename;
00256 int iret=ReadMatrix(A,filename);
00257 cout << " (" << iret << ") Matrix(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << A << endl;
00258
00259 if(A.rows() != A.cols()) {
00260 gpstk::Matrix<double> AT;
00261 AT = gpstk::transpose(A);
00262 A = AT*A;
00263 cout << " ATA Matrix(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << A << endl;
00264 }
00265 cond = condNum(A,big,small);
00266 cout << "Condition number is " << fixed << setprecision(3) << big << "/" << scientific << setprecision(3) << small;
00267 cout << " = " << fixed << big/small << endl;
00268 try { Ainv=gpstk::inverse(A); }
00269 catch (gpstk::Exception& e) { cout << e << endl; continue;}
00270 cout << "Inverse matrix (" << Ainv.rows() << "," << Ainv.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << Ainv << endl;
00271 P = Ainv * A;
00272 cout << "Unity matrix (" << P.rows() << "," << P.cols() << ") ? :\n" << fixed << setprecision(9) << setw(12) << P << endl;
00273 }
00274 }
00275
00276 void MatrixTest4(void)
00277 {
00278 cout << "\n -------------- Matrix Test 4 ---------------------------------\n";
00279 const int N=7;
00280 double mat[N*N]={
00281 8.317, 6.212, 2.574, 5.317, 2.080, -9.133, -2.755,
00282 0.212, 3.292, 1.574, 1.028, 3.370, -2.077, -2.739,
00283 5.740, 1.574, 1.911, 1.390, 8.544, 8.930, 9.216,
00284 4.317, 1.028, 1.039, 7.126, 4.512, 8.538, 5.226,
00285 -1.109, 7.438, 7.236, 6.783, 0.356, -9.509, -0.109,
00286 0.174, 5.408, -9.503, -6.527, -6.589, -6.375, -7.239,
00287 1.960, 6.592, 9.440, 4.428, -4.531, 5.084, 4.296
00288 };
00289 double dat[N]={ 14.289, 9.284, -1.128, 8.389, -6.929, 4.664, 7.590 };
00290
00291 int i,j;
00292 gpstk::Matrix<double> A(N,N);
00293 gpstk::Vector<double> b(N);
00294 A = mat;
00295 A = transpose(A);
00296 b = dat;
00297
00298 cout << "Matrix A(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << A << endl;
00299 cout << "Vector b(" << b.size() << ") :\n" << fixed << setprecision(3) << setw(7) << b << endl;
00300 cout << "\nNow solve using SVD\n";
00301
00302 gpstk::SVD<double> Asvd;
00303 gpstk::Matrix<double> U,V;
00304 gpstk::Vector<double> S;
00305
00306 Asvd(A);
00307 U = Asvd.U;
00308 V = Asvd.V;
00309 S = Asvd.S;
00310 cout << "Singular Values (" << S.size() << ") :\n" << fixed << setprecision(3) << setw(7) << S << endl;
00311 cout << "Matrix U(" << U.rows() << "," << U.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << U << endl;
00312 cout << "Matrix V(" << V.rows() << "," << V.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << V << endl;
00313
00314 gpstk::Matrix<double> P,W(A);
00315 W = 0.0;
00316 for(i=0; i<S.size(); i++) W(i,i)=S(i);
00317 P = U * W * transpose(V);
00318 P = P - A;
00319 P.zeroize();
00320 cout << "Difference (" << P.rows() << "," << P.cols() << ") :\n" << scientific << setprecision(3) << setw(10) << P << endl;
00321
00322 cout << "Determinant of A = " << scientific << setprecision(3) << Asvd.det();
00323 double d=1;
00324 for(i=0; i<N; i++) d *= S(i);
00325 cout << " -- Compare to " << scientific << setprecision(3) << d << endl;
00326 gpstk::Vector<double> X(b);
00327 Asvd.backSub(X);
00328 cout << "Solution via backsubstitution (" << X.size() << ") :\n" << fixed << setprecision(3) << setw(7) << X << endl;
00329 S = b-A*X;
00330 cout << "Solution residuals (" << S.size() << ") :\n" << scientific << setprecision(3) << setw(7) << S << endl;
00331
00332 cout << "\nSort in ascending order\n";
00333 Asvd.sort(false);
00334 U = Asvd.U;
00335 V = Asvd.V;
00336 S = Asvd.S;
00337 cout << "Singular Values (" << S.size() << ") :\n" << fixed << setprecision(3) << setw(7) << S << endl;
00338 cout << "Matrix U(" << U.rows() << "," << U.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << U << endl;
00339 cout << "Matrix V(" << V.rows() << "," << V.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << V << endl;
00340
00341
00342 cout << "\n\nNow reduce A by one column and repeat\n";
00343 A = gpstk::Matrix<double>(A,0,0,A.rows(),A.cols()-1);
00344 cout << "Matrix A(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << A << endl;
00345 Asvd(A);
00346 S = Asvd.S;
00347 U = Asvd.U;
00348 V = Asvd.V;
00349 cout << "Singular Values (" << S.size() << ") :\n" << fixed << setprecision(3) << setw(7) << S << endl;
00350 cout << "Matrix U(" << U.rows() << "," << U.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << U << endl;
00351 cout << "Matrix V(" << V.rows() << "," << V.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << V << endl;
00352 W = A;
00353 W = 0.0;
00354 for(i=0; i<S.size(); i++) W(i,i)=S(i);
00355 P = U * W * transpose(V);
00356 P = P - A;
00357 P.zeroize();
00358 cout << "Difference (" << P.rows() << "," << P.cols() << ") :\n" << scientific << setprecision(3) << setw(10) << P << endl;
00359 }
00360
00361 void MatrixTest5(void)
00362 {
00363 cout << "\n -------------- Matrix Test 5 ---------------------------------\n";
00364 const int N=7;
00365 double mat[N*N]={
00366 8.317, 6.212, 2.574, 5.317, 2.080, -9.133, -2.755,
00367 0.212, 3.292, 1.574, 1.028, 3.370, -2.077, -2.739,
00368 5.740, 1.574, 1.911, 1.390, 8.544, 8.930, 9.216,
00369 4.317, 1.028, 1.039, 7.126, 4.512, 8.538, 5.226,
00370 -1.109, 7.438, 7.236, 6.783, 0.356, -9.509, -0.109,
00371 0.174, 5.408, -9.503, -6.527, -6.589, -6.375, -7.239,
00372 1.960, 6.592, 9.440, 4.428, -4.531, 5.084, 4.296
00373 };
00374 double dat[N]={ 14.289, 9.284, -1.128, 8.389, -6.929, 4.664, 7.590 };
00375
00376 int i,j;
00377 gpstk::Matrix<double> A(N,N);
00378 gpstk::Vector<double> b(N);
00379 A = mat;
00380 A = gpstk::transpose(A);
00381 b = dat;
00382
00383 cout << "Matrix A(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << A << endl;
00384 cout << "Vector b(" << b.size() << ") :\n" << fixed << setprecision(3) << setw(7) << b << endl;
00385 cout << "\nNow solve using LUD\n";
00386
00387 gpstk::LUDecomp<double> LUA;
00388 LUA(A);
00389
00390
00391
00392 cout << "Matrix LU(" << LUA.LU.rows() << "," << LUA.LU.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << LUA.LU << endl;
00393 cout << "Determinant of A = " << scientific << setprecision(3) << LUA.det() << endl;
00394
00395 gpstk::Vector<double> X(b),S;
00396 try { LUA.backSub(X); }
00397 catch (gpstk::Exception& e) { cout << e << endl; return;}
00398 cout << "Solution via backsubstitution (" << X.size() << ") :\n" << fixed << setprecision(3) << setw(7) << X << endl;
00399 S = b-A*X;
00400 cout << "Solution residuals (" << S.size() << ") :\n" << scientific << setprecision(3) << setw(7) << S << endl;
00401 }
00402
00403 void MatrixTest6(int argc, char **argv)
00404 {
00405 cout << "\n -------------- Matrix Test 6 ---------------------------------\n";
00406 string filename(argv[7]);
00407 gpstk::Matrix<double> A;
00408 int iret=ReadMatrix(A,filename);
00409 if(iret < 0) {
00410 cerr << "Error: could not open file " << filename << endl;
00411 return;
00412 }
00413 double dat[4]={1.,2.,3.,4.};
00414
00415 gpstk::Vector<double> b(4);
00416 b = dat;
00417 cout << "Matrix A(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << A << endl;
00418 cout << "Vector b(" << b.size() << ") :\n" << fixed << setprecision(3) << setw(7) << b << endl;
00419 cout << "\nNow compute Cholesky\n";
00420
00421 gpstk::Cholesky<double> CA;
00422 CA(A);
00423 cout << "\nCholesky of A (U) (" << CA.U.rows() << "," << CA.U.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << CA.U << endl;
00424 cout << "\nCholesky of A (L) (" << CA.L.rows() << "," << CA.L.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << CA.L << endl;
00425
00426 gpstk::Matrix<double> B;
00427 B = A - CA.U * gpstk::transpose(CA.U);
00428 cout << "\nDifference U*UT with matrix A(" << B.rows() << "," << B.cols() << ") :\n" << scientific << setprecision(3) << setw(7) << B << endl;
00429 B = A - CA.L * gpstk::transpose(CA.L);
00430 cout << "\nDifference L*LT with matrix A(" << B.rows() << "," << B.cols() << ") :\n" << scientific << setprecision(3) << setw(7) << B << endl;
00431
00432 gpstk::Vector<double> X(b);
00433 CA.backSub(X);
00434 cout << "\nSolution via backsubstitution (" << X.size() << ") :\n" << fixed << setprecision(3) << setw(7) << X << endl;
00435 X = b-A*X;
00436 cout << "Solution residuals (" << X.size() << ") :\n" << scientific << setprecision(3) << setw(7) << X << endl;
00437 }
00438
00439 void MatrixTest7(int argc, char **argv)
00440 {
00441 cout << "\n -------------- Matrix Test 7 ---------------------------------\n";
00442 string filename(argv[8]);
00443 gpstk::Matrix<double> A;
00444 int iret=ReadMatrix(A,filename);
00445 if(iret < 0) {
00446 cerr << "Error: could not open file " << filename << endl;
00447 return;
00448 }
00449 cout << "Matrix A(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << A << endl;
00450 cout << "\nNow compute the Householder transformation\n";
00451
00452 gpstk::Householder<double> HHA;
00453 HHA(A);
00454 cout << "HH: (" << HHA.A.rows() << "," << HHA.A.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << HHA.A << endl;
00455 }
00456
00457 void MatrixTest8(void)
00458 {
00459 cout << "\n -------------- Matrix Test 8 ---------------------------------\n";
00460 const int n=6;
00461 int i;
00462 double big,small,cond;
00463 gpstk::Vector<double>V;
00464 gpstk::Matrix<double> M(n,n),Minv,Prod;
00465
00466 for(i=0; i<n; i++) {
00467 for(int j=0; j<n; j++) {
00468 M(i,j) = 1.0/(i+j+1.0);
00469 }
00470 }
00471 cout << "Tough matrix (" << M.rows() << "," << M.cols() << ") :\n" << fixed << setprecision(6) << setw(9) << M << endl;
00472
00473 cond = gpstk::condNum(M,big,small);
00474 cout << "Condition number is " << fixed << setprecision(3) << big << "/" << scientific << setprecision(3) << small;
00475 if(small > 0.0) cout << " = " << setprecision(3) << big/small;
00476 cout << endl;
00477
00478 for(i=0; i<3; i++) {
00479 try {
00480 if(i==0) { cout << "Gaussian elimiation:\n"; Minv = inverse(M); }
00481 else if(i==1) { cout << "LUD:\n"; Minv = inverseLUD(M); }
00482 else if(i==2) { cout << "SVD:\n"; Minv = inverseSVD(M); }
00483 }
00484 catch (gpstk::Exception& e) { cout << e << endl; continue;}
00485 cout << "Inverse matrix (" << Minv.rows() << "," << Minv.cols() << ") :\n" << fixed << setprecision(3) << setw(13) << Minv << endl;
00486 Prod = Minv * M;
00487
00488 Prod.zeroize();
00489 cout << "Unity matrix ? (" << Prod.rows() << "," << Prod.cols() << ") ? :\n" << fixed << setprecision(9) << setw(12) << Prod << endl;
00490 }
00491 }
00492
00493 void MatrixTest9(int argc, char **argv)
00494 {
00495 cout << "\n -------------- Matrix Test 9 ---------------------------------\n";
00496 cout << "Read matrix and vector (in form M||V) from file, invert and solve\n";
00497 double cond,big,small;
00498 gpstk::Matrix<double> R,A,Ainv,P;
00499 gpstk::Vector<double> B,X;
00500 for(int i=1; i<argc; i++) {
00501 string filename=argv[i];
00502 cout << "File " << filename;
00503 int iret=ReadMatrix(R,filename);
00504 cout << " (" << iret << ") Matrix(" << R.rows() << "," << R.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << R << endl;
00505
00506 B = gpstk::Vector<double>(R.colCopy(R.cols()-1));
00507
00508 A = gpstk::Matrix<double>(R,0,0,R.rows(),R.cols()-1);
00509 cout << "Partials Matrix (" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << A << endl;
00510 cout << "Data vector (" << B.size() << ") :\n" << fixed << setprecision(3) << setw(10) << B << endl << endl;
00511
00512 if(A.rows() != A.cols()) {
00513 gpstk::Matrix<double> AT;
00514 AT = gpstk::transpose(A);
00515 A = AT*A;
00516 cout << " ATA Matrix(" << A.rows() << "," << A.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << A << endl;
00517 }
00518 cond = gpstk::condNum(A,big,small);
00519 cout << "Condition number is " << fixed << setprecision(3) << big << "/" << scientific << setprecision(3) << small;
00520 cout << " = " << fixed << big/small << endl;
00521 try
00522 {
00523 Ainv=gpstk::inverse(A);
00524 cout << "Inverse matrix (" << Ainv.rows() << "," << Ainv.cols() << ") :\n" << fixed << setprecision(3) << setw(10) << Ainv << endl;
00525 P = Ainv * A;
00526 cout << "Unity matrix (" << P.rows() << "," << P.cols() << ") ? :\n" << fixed << setprecision(9) << setw(12) << P << endl << endl;
00527
00528
00529
00530 }
00531 catch (gpstk::Exception& e) { cout << e << endl; continue;}
00532 }
00533 }
00534
00535 void PolyTest(void)
00536 {
00537 cout << "\n -------------- Poly Test ---------------------------------\n";
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 double t[33] = { -1.0000, -0.9375, -0.8750, -0.8125, -0.7500, -0.6875,
00609 -0.6250, -0.5625, -0.5000, -0.4375, -0.3750, -0.3125, -0.2500, -0.1875,
00610 -0.1250, -0.0625, 0.0000, 0.0625, 0.1250, 0.1875, 0.2500, 0.3125, 0.3750,
00611 0.4375, 0.5000, 0.5625, 0.6250, 0.6875, 0.7500, 0.8125, 0.8750, 0.9375,
00612 1.0000 };
00613 double d1[33] = { -1.750, -1.702, -1.663, -1.639, -1.590, -1.579, -1.536,
00614 -1.502, -1.441, -1.445, -1.412, -1.370, -1.328, -1.286, -1.266, -1.270,
00615 -1.249, -1.235, -1.197, -1.214, -1.183, -1.156, -1.135, -1.134, -1.098,
00616 -1.102, -1.118, -1.103, -1.115, -1.076, -1.101, -1.068, -1.088 };
00617 double d2[33] = { -1.088, -1.119, -1.118, -1.158, -1.146, -1.201, -1.207,
00618 -1.222, -1.230, -1.269, -1.269, -1.335, -1.359, -1.391, -1.391, -1.432,
00619 -1.440, -1.483, -1.520, -1.514, -1.514, -1.478, -1.496, -1.508, -1.507,
00620 -1.437, -1.439, -1.419, -1.388, -1.408, -1.390, -1.391, -1.385 };
00621
00622 gpstk::Vector<double> T(33),D1(33),D2(33);
00623 T = t;
00624 D1 = d1;
00625 D2 = d2;
00626
00627
00628
00629
00630 size_t i,rev=1;
00631 gpstk::Vector<double> Fit,Resid,Sol;
00632 gpstk::PolyFit<double> PF(3);
00633 gpstk::Stats<double> St1,St2;
00634 gpstk::TwoSampleStats<double> Tss;
00635 do {
00636 if(rev==1) PF.Add(D1,T);
00637 else for(i=0; i<T.size(); i++) PF.Add(D2[i],T[i]);
00638 gpstk::Matrix<double> C;
00639 C = PF.Covariance();
00640 cout << "Matrix Cov(" << C.rows() << "," << C.cols() << ") :\n" << fixed << setprecision(3) << setw(8) << C << endl;
00641 Sol = PF.Solution();
00642 cout << "Vector Sol(" << Sol.size() << ") :\n" << fixed << setprecision(3) << setw(8) << Sol << endl;
00643 Fit = PF.Evaluate(T);
00644 Resid = (rev==1?D1:D2)-Fit;
00645
00646 cout << " t data fit resid\n";
00647 for(i=0; i<T.size(); i++) {
00648 cout << setw(2) << i;
00649 cout << fixed << setw(8) << setprecision(4) << T(i);
00650 cout << fixed << setw(8) << setprecision(3) << (rev==1?D1(i):D2(i));
00651 cout << fixed << setw(8) << setprecision(3) << Fit(i);
00652 cout << fixed << setw(8) << setprecision(3) << Resid(i);
00653 cout << endl;
00654 St1.Add(Resid(i));
00655 St2.Add(rev==1?D1(i):D2(i));
00656 Tss.Add(T(i),(rev==1?D1(i):D2(i)));
00657 }
00658
00659 cout << "Stats on residuals\n";
00660 cout << scientific << setw(10) << setprecision(3) << St1 << endl;
00661
00662 cout << "Stats on data\n";
00663 cout << scientific << setw(10) << setprecision(3) << St2 << endl;
00664
00665 cout << "2-sample Stats on time,data\n";
00666 cout << scientific << setw(10) << setprecision(3) << Tss << endl;
00667
00668 if(++rev>2) break;
00669 PF.Reset();
00670 St1.Reset();
00671 St2.Reset();
00672 Tss.Reset();
00673 } while(1);
00674 }
00675
00676 int main(int argc, char **argv)
00677 {
00678 if (argc!=10)
00679 {
00680 cout << " Need 9 files to chew on" << endl;
00681 exit(-1);
00682 }
00683
00684 VectorTest();
00685 MatrixTest1(argc, argv);
00686 MatrixTest2();
00687 MatrixTest3(argc, argv);
00688 MatrixTest4();
00689 MatrixTest5();
00690 MatrixTest6(argc, argv);
00691 MatrixTest7(argc, argv);
00692 MatrixTest8();
00693 MatrixTest9(argc, argv);
00694 PolyTest();
00695 return 0;
00696 }
00697