MatrixTest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: MatrixTest.cpp 3140 2012-06-18 15:03:02Z susancummins $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00020 //  
00021 //  Copyright 2009, The University of Texas at Austin
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>   // for copyfmt
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] == '#') {        // skip to end of line
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             //cout << "Found r = " << r << endl;
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             //cout << "Found c = " << c << endl;
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             //cout << "Found type " << labels[type] << endl;
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    //cout << "Median of V " << median(V) << endl;
00112    cout << "V min " << min(V) << ", max " << max(V) << ", sum " << sum(V) << endl;
00113 
00114       // slice is (init,number,stride)
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    //Veve[1] = Veve[4] = 99; compiler won't let you have Veve as l-value
00121    V[3] = V[7] = 99;    // but you can change V, and that will show in Veve
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    //cout << "Matrix (" << MF.rows() << "," << MF.cols() << ") from file :\n" << fixed << setprecision(1) << setw(7) << MF << endl;
00173 
00174       // pick off last column
00175    gpstk::Vector<double> B(MF.colCopy(MF.cols()-1));
00176 
00177       // copy all but last column
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       // take ATA, then invert
00183    gpstk::Matrix<double> AT,ATA;
00184    AT = transpose(A);
00185    //cout << "Transposed matrix (" << AT.rows() << "," << AT.cols() << ") :\n" << fixed << setprecision(1) << setw(7) << AT << endl;
00186    ATA = AT * A;
00187    //cout << "ATA matrix (" << ATA.rows() << "," << ATA.cols() << ") :\n" << fixed << setprecision(3) << setw(13) << ATA << endl;
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; // rows = columns
00280    double mat[N*N]={       // storage by columns
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    // now chop off the last column of A and repeat
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; // rows = columns
00365    double mat[N*N]={       // storage by columns
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    //cout << "LU: Matrix L(" << LUA.L.rows() << "," << LUA.L.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << LUA.L << endl;
00390    //cout << "LU: Matrix U(" << LUA.U.rows() << "," << LUA.U.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << LUA.U << endl;
00391    //cout << "LU: Matrix P(" << LUA.P.rows() << "," << LUA.P.cols() << ") :\n" << fixed << setprecision(3) << setw(7) << LUA.P << endl;
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       //V.zeroTolerance = 1.e-6;
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          // pick off last column
00506       B = gpstk::Vector<double>(R.colCopy(R.cols()-1));
00507          // copy all but last column
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          //X = Ainv * B;
00529          //cout << "Solution vector (" << X.size() << ") :\n" << fixed << setprecision(3) << setw(10) << X << endl;
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    /*  33 points in each of two fits:
00539     * timetag      t     data    fit  resid
00540     * 351569.981 -1.0000 -1.750 -1.755  0.005
00541     * 351580.003 -0.9375 -1.702 -1.713  0.011
00542     * 351590.026 -0.8750 -1.663 -1.672  0.009
00543     * 351599.962 -0.8125 -1.639 -1.633 -0.006
00544     * 351609.984 -0.7500 -1.590 -1.595  0.005
00545     * 351620.006 -0.6875 -1.579 -1.558 -0.020
00546     * 351630.029 -0.6250 -1.536 -1.523 -0.013
00547     * 351639.965 -0.5625 -1.502 -1.489 -0.013
00548     * 351649.987 -0.5000 -1.441 -1.457  0.015
00549     * 351660.010 -0.4375 -1.445 -1.426 -0.019
00550     * 351670.032 -0.3750 -1.412 -1.396 -0.016
00551     * 351679.968 -0.3125 -1.370 -1.368 -0.002
00552     * 351689.990 -0.2500 -1.328 -1.340  0.012
00553     * 351700.013 -0.1875 -1.286 -1.315  0.029
00554     * 351710.035 -0.1250 -1.266 -1.290  0.025
00555     * 351719.971 -0.0625 -1.270 -1.267 -0.003
00556     * 351729.994  0.0000 -1.249 -1.246 -0.003
00557     * 351740.016  0.0625 -1.235 -1.226 -0.010
00558     * 351750.038  0.1250 -1.197 -1.207  0.010
00559     * 351759.974  0.1875 -1.214 -1.189 -0.025
00560     * 351769.997  0.2500 -1.183 -1.173 -0.010
00561     * 351780.019  0.3125 -1.156 -1.158  0.003
00562     * 351790.042  0.3750 -1.135 -1.145  0.010
00563     * 351799.978  0.4375 -1.134 -1.133 -0.001
00564     * 351810.000  0.5000 -1.098 -1.122  0.024
00565     * 351820.022  0.5625 -1.102 -1.113  0.011
00566     * 351829.958  0.6250 -1.118 -1.105 -0.013
00567     * 351839.981  0.6875 -1.103 -1.098 -0.005
00568     * 351850.003  0.7500 -1.115 -1.093 -0.022
00569     * 351860.026  0.8125 -1.076 -1.089  0.013
00570     * 351869.962  0.8750 -1.101 -1.086 -0.015
00571     * 351879.984  0.9375 -1.068 -1.085  0.017
00572     * 351890.006  1.0000 -1.088 -1.085 -0.003
00573     * timetag      t     data    fit  resid
00574     * 351900.029 -1.0000 -1.088 -1.029 -0.060
00575     * 351909.965 -0.9375 -1.119 -1.068 -0.051
00576     * 351919.987 -0.8750 -1.118 -1.105 -0.013
00577     * 351930.010 -0.8125 -1.158 -1.140 -0.018
00578     * 351940.032 -0.7500 -1.146 -1.174  0.028
00579     * 351949.968 -0.6875 -1.201 -1.206  0.004
00580     * 351959.990 -0.6250 -1.207 -1.235  0.029
00581     * 351970.013 -0.5625 -1.222 -1.264  0.041
00582     * 351980.035 -0.5000 -1.230 -1.290  0.059
00583     * 351989.971 -0.4375 -1.269 -1.314  0.045
00584     * 351999.994 -0.3750 -1.269 -1.337  0.068
00585     * 352010.016 -0.3125 -1.335 -1.358  0.022
00586     * 352020.038 -0.2500 -1.359 -1.377  0.018
00587     * 352029.974 -0.1875 -1.391 -1.394  0.003
00588     * 352039.997 -0.1250 -1.391 -1.410  0.019
00589     * 352050.019 -0.0625 -1.432 -1.423 -0.009
00590     * 352060.042  0.0000 -1.440 -1.435 -0.004
00591     * 352069.978  0.0625 -1.483 -1.445 -0.038
00592     * 352080.000  0.1250 -1.520 -1.453 -0.067
00593     * 352090.022  0.1875 -1.514 -1.460 -0.055
00594     * 352099.958  0.2500 -1.514 -1.464 -0.050
00595     * 352109.981  0.3125 -1.478 -1.467 -0.010
00596     * 352120.003  0.3750 -1.496 -1.468 -0.028
00597     * 352130.026  0.4375 -1.508 -1.467 -0.040
00598     * 352139.962  0.5000 -1.507 -1.465 -0.043
00599     * 352149.984  0.5625 -1.437 -1.460  0.023
00600     * 352160.006  0.6250 -1.439 -1.454  0.015
00601     * 352170.029  0.6875 -1.419 -1.446  0.027
00602     * 352179.965  0.7500 -1.388 -1.436  0.048
00603     * 352189.987  0.8125 -1.408 -1.424  0.016
00604     * 352200.010  0.8750 -1.390 -1.411  0.021
00605     * 352210.032  0.9375 -1.391 -1.395  0.005
00606     * 352219.968  1.0000 -1.385 -1.378 -0.007
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    //cout << "Vector T(" << T.size() << ") :\n" << fixed << setprecision(3) << setw(7) << T << endl;
00627    //cout << "Vector D1(" << D1.size() << ") :\n" << fixed << setprecision(3) << setw(7) << D1 << endl;
00628    //cout << "Vector D2(" << D2.size() << ") :\n" << fixed << setprecision(3) << setw(7) << D2 << endl;
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);    // general stuff
00686    MatrixTest2();    // condition number and inverse
00687    MatrixTest3(argc, argv);      // Read and condition number
00688    MatrixTest4();    // SVD
00689    MatrixTest5();    // LUD
00690    MatrixTest6(argc, argv);    // Cholesky
00691    MatrixTest7(argc, argv);    // Householder
00692    MatrixTest8();    // Inverse via Gauss,LUD,SVD
00693    MatrixTest9(argc, argv);    // Read a matrix and data vector and solve Ax=b
00694    PolyTest();       // PolyFit
00695         return 0;
00696 }
00697 

Generated on Thu May 23 03:31:09 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1