| [778] | 1 | #include "machdefs.h" | 
|---|
|  | 2 |  | 
|---|
|  | 3 | #include <math.h> | 
|---|
|  | 4 | #include <iostream.h> | 
|---|
|  | 5 |  | 
|---|
| [857] | 6 | #include "srandgen.h" | 
|---|
| [778] | 7 | #include "tarrinit.h" | 
|---|
| [857] | 8 | #include "array.h" | 
|---|
| [778] | 9 | #include "timing.h" | 
|---|
|  | 10 | #include "intflapack.h" | 
|---|
|  | 11 |  | 
|---|
|  | 12 |  | 
|---|
| [1343] | 13 | int lpk_rzt_arr(int n); | 
|---|
|  | 14 | int lpk_linsolve_mtx(int n); | 
|---|
|  | 15 | int lpk_svd_mtx(int l, int c, int wsf, bool covu=true); | 
|---|
| [812] | 16 |  | 
|---|
| [1248] | 17 | static double TOLERANCE = 1.e-6; | 
|---|
| [1100] | 18 |  | 
|---|
| [778] | 19 | int main(int narg, char* arg[]) | 
|---|
|  | 20 | { | 
|---|
|  | 21 |  | 
|---|
|  | 22 | SophyaInit(); | 
|---|
|  | 23 | InitTim();   // Initializing the CPU timer | 
|---|
|  | 24 |  | 
|---|
|  | 25 |  | 
|---|
| [812] | 26 |  | 
|---|
|  | 27 | if (narg < 2) { | 
|---|
| [1343] | 28 | cout << " lpk - LinAlg/LapackServer test \n" | 
|---|
|  | 29 | << " Usage: lpk linsolve/svd/rzt [sizeL,C=5,5] [prtlev=0] \n" | 
|---|
|  | 30 | << "             [nprtmax=100] [WorkSpaceSizeFactor=2] \n" | 
|---|
|  | 31 | << "   linsolve:  lpk_linsolve_mtx() LapackServer::LinSolve with TMatrix<r_8> \n" | 
|---|
|  | 32 | << "   svd:  lpk_svd_mtx() LapackServer::SVD(a,s,u,vt) with TMatrix<r_8> \n" | 
|---|
|  | 33 | << "   svds:  lpk_svd_mtx() LapackServer::SVD(a,s); with TMatrix<r_8> \n" | 
|---|
|  | 34 | << "   rzt:  lpk_rzt_tarr() rztest_lapack with TArray<r_4> \n" << endl; | 
|---|
| [812] | 35 | exit(0); | 
|---|
|  | 36 | } | 
|---|
| [1343] | 37 | int l,c; | 
|---|
|  | 38 | l = c = 5; | 
|---|
|  | 39 | int wsf = 2; | 
|---|
|  | 40 | string opt = arg[1]; | 
|---|
|  | 41 | if (narg > 2) sscanf(arg[2], "%d,%d", &l, &c); | 
|---|
| [812] | 42 | int nprt = 100; | 
|---|
|  | 43 | int prtlev = 1; | 
|---|
|  | 44 | if (narg > 3) prtlev = atoi(arg[3]); | 
|---|
|  | 45 | if (narg > 4) nprt = atoi(arg[4]); | 
|---|
| [1343] | 46 | if (narg > 5) wsf = atoi(arg[5]); | 
|---|
| [812] | 47 |  | 
|---|
| [1248] | 48 | int rc = 0; | 
|---|
| [812] | 49 | BaseArray::SetMaxPrint(nprt, prtlev); | 
|---|
|  | 50 | try { | 
|---|
| [1343] | 51 | if (opt == "linsolve") rc = lpk_linsolve_mtx(l); | 
|---|
|  | 52 | else if (opt == "svd") rc = lpk_svd_mtx(l,c,wsf,true); | 
|---|
|  | 53 | else if (opt == "svds") rc = lpk_svd_mtx(l,c,wsf,false); | 
|---|
|  | 54 | else if (opt == "rzt") rc = lpk_rzt_arr(l); | 
|---|
|  | 55 | else { cout << " Unknown option " << opt << " ! " << endl; rc = 66; } | 
|---|
| [812] | 56 | } | 
|---|
|  | 57 | catch (PThrowable exc) { | 
|---|
|  | 58 | cerr << " catched Exception (lpk.cc) " << exc.Msg() << endl; | 
|---|
| [1248] | 59 | rc = 77; | 
|---|
| [812] | 60 | } | 
|---|
|  | 61 | catch (...) { | 
|---|
|  | 62 | cerr << " catched unknown (...) exception (lpk.cc) " << endl; | 
|---|
| [1248] | 63 | rc = 78; | 
|---|
| [812] | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | PrtTim(" End of lpk LinAlg/Lapack test "); | 
|---|
| [1343] | 67 | cout << " ---------------  END of Programme -------- (Rc= " | 
|---|
|  | 68 | << rc << ") --- " << endl; | 
|---|
| [1248] | 69 | return(rc); | 
|---|
| [812] | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [1343] | 72 | // ----------------------------------------------------------------------- | 
|---|
|  | 73 | /* Nouvelle-Fonction */ | 
|---|
|  | 74 | int lpk_linsolve_mtx(int n) | 
|---|
| [812] | 75 | { | 
|---|
| [1343] | 76 | int i,j; | 
|---|
| [812] | 77 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping); | 
|---|
| [1343] | 78 | cout << " lpk_linsolve_mtx() - Test of LapackServer::LinSolve()  " << endl; | 
|---|
| [812] | 79 | Matrix a(n,n); | 
|---|
|  | 80 | for(i=0; i<n; i++) | 
|---|
|  | 81 | for(j=0; j<n; j++)  a(j,i) = GauRnd(0., 1.); | 
|---|
|  | 82 |  | 
|---|
|  | 83 | Vector x(n), b; | 
|---|
|  | 84 | //  Matrix  x(n,1), b; | 
|---|
|  | 85 | cout << " ------------ Vector X = \n " << x << "\n" << endl; | 
|---|
|  | 86 | for(i=0; i<n; i++) x(i) = GauRnd(2., 1.5); | 
|---|
|  | 87 | b = a*x; | 
|---|
| [778] | 88 |  | 
|---|
| [1343] | 89 | cout << " ---- lpk_tmtx() LapackServer::LinSolve Test Using TMatrix<r_8> ----- " << endl; | 
|---|
| [812] | 90 | cout << " ------------ Matrix A = \n " << a << "\n" << endl; | 
|---|
|  | 91 | cout << " ------------ Matrix X = \n " << x << "\n" << endl; | 
|---|
|  | 92 | cout << " ------------ Matrix B = \n " << b << "\n" << endl; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | cout << "\n   Calling LapackLinSolve(a,b) .... " << endl; | 
|---|
| [1100] | 95 | PrtTim(" Calling LapackLinSolve(a,b) "); | 
|---|
| [812] | 96 | LapackLinSolve(a,b); | 
|---|
| [1100] | 97 | PrtTim(" End LapackLinSolve(a,b) "); | 
|---|
| [812] | 98 |  | 
|---|
|  | 99 | cout << " ------------ Result B(=X ?) = \n " << b << "\n" << endl; | 
|---|
|  | 100 | Vector diff = b-x; | 
|---|
| [1343] | 101 | PrtTim(" End of Compute(diff)"); | 
|---|
| [812] | 102 | cout << " ------------ Vector diff B-X = \n " << diff << "\n" << endl; | 
|---|
| [1248] | 103 | double min,max; | 
|---|
|  | 104 | diff.MinMax(min, max); | 
|---|
|  | 105 | cout << " Min/Max difference Vector (?=0) , Min= " << min | 
|---|
|  | 106 | << " Max= " << max << endl; | 
|---|
|  | 107 | if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) { | 
|---|
|  | 108 | cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!" | 
|---|
|  | 109 | << endl; | 
|---|
|  | 110 | return(99); | 
|---|
|  | 111 | } | 
|---|
|  | 112 | return(0); | 
|---|
| [812] | 113 |  | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [1343] | 116 | // ----------------------------------------------------------------------- | 
|---|
|  | 117 | /* Nouvelle-Fonction */ | 
|---|
|  | 118 | int lpk_svd_mtx(int m, int n, int wsf, bool covu) | 
|---|
| [812] | 119 | { | 
|---|
| [1343] | 120 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping); | 
|---|
|  | 121 | cout << " lpk_svd_mtx() - Test of LapackServer::SVD " << endl; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | Matrix a(m , n), aa; | 
|---|
|  | 124 | a = RandomSequence(RandomSequence::Gaussian, 0., 4.); | 
|---|
|  | 125 | aa = a; | 
|---|
|  | 126 | Vector s; | 
|---|
|  | 127 | Matrix u, vt; | 
|---|
|  | 128 | cout << " ---- lpk_svd_tmtx() LapackServer::SVD Test Using TMatrix<r_8> ---- " << endl; | 
|---|
|  | 129 | cout << " ------------ Matrix A = \n " << a << "\n" << endl; | 
|---|
|  | 130 |  | 
|---|
|  | 131 | cout << "\n   Calling LapackSVD(a,s,u,vt) .... " << endl; | 
|---|
|  | 132 | PrtTim(" Calling LapackSVD() "); | 
|---|
|  | 133 | LapackServer<r_8> lpks; | 
|---|
|  | 134 | lpks.SetWorkSpaceSizeFactor(wsf); | 
|---|
|  | 135 | if (covu) lpks.SVD(aa, s, u, vt); | 
|---|
|  | 136 | else lpks.SVD(aa, s); | 
|---|
|  | 137 | PrtTim(" End LapackSVD() "); | 
|---|
|  | 138 |  | 
|---|
|  | 139 | cout << " ------------ Result S  = \n " << s << "\n" << endl; | 
|---|
|  | 140 | if (!covu) return(0); | 
|---|
|  | 141 | cout << " ------------ Result U  = \n " << u << "\n" << endl; | 
|---|
|  | 142 | cout << " ------------ Result VT = \n " << vt << "\n" << endl; | 
|---|
|  | 143 | Matrix sm(m,n); | 
|---|
|  | 144 | int minmn = (m<n) ? m : n ; | 
|---|
|  | 145 | for(int k=0; k< minmn ; k++) sm(k,k) = s(k); | 
|---|
|  | 146 | Matrix diff = u*(sm*vt) - a; | 
|---|
|  | 147 | PrtTim(" End of Compute(diff)"); | 
|---|
|  | 148 | cout << " ------------ Matrix diff U*S*Vt - A = \n " << diff << "\n" << endl; | 
|---|
|  | 149 | double min,max; | 
|---|
|  | 150 | diff.MinMax(min, max); | 
|---|
|  | 151 | cout << " Min/Max difference Matrix (?=0) , Min= " << min | 
|---|
|  | 152 | << " Max= " << max << endl; | 
|---|
|  | 153 | if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) { | 
|---|
|  | 154 | cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!" | 
|---|
|  | 155 | << endl; | 
|---|
|  | 156 | return(99); | 
|---|
|  | 157 | } | 
|---|
|  | 158 | return(0); | 
|---|
|  | 159 |  | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
| [1425] | 162 | void rztest_lapack(TArray<r_4>& aa, TArray<r_4>& bb); | 
|---|
|  | 163 |  | 
|---|
| [1343] | 164 | int  lpk_rzt_arr(int n) | 
|---|
|  | 165 | { | 
|---|
|  | 166 | int i,j; | 
|---|
| [778] | 167 | TArray<r_4> a(n,n); | 
|---|
|  | 168 | for(i=0; i<n; i++) | 
|---|
|  | 169 | for(j=0; j<n; j++)  a(i,j,0) = GauRnd(0., 1.); | 
|---|
|  | 170 |  | 
|---|
| [789] | 171 | TArray<r_4> x(n,1), b(n,1); | 
|---|
| [778] | 172 | r_4 sum ; | 
|---|
| [789] | 173 | for(i=0; i<n; i++) x(i,0,0) = GauRnd(2., 1.5); | 
|---|
| [778] | 174 | for(i=0; i<n; i++) { | 
|---|
|  | 175 | sum = 0.; | 
|---|
| [789] | 176 | for(j=0; j<n; j++) sum +=  a(i,j,0)*x(j,0,0); | 
|---|
|  | 177 | b(i,0,0) = sum; | 
|---|
| [778] | 178 | } | 
|---|
|  | 179 |  | 
|---|
| [812] | 180 | //  cout << ":::::::: rztest_lapack - Size=" << n << "  ::::::::: " << endl; | 
|---|
|  | 181 | cout << " ------- lpk_tarr() LapaackTest Using TArray<r_4> -------- " << endl; | 
|---|
| [807] | 182 | cout << " ------------ Array A = \n " << a << "\n" << endl; | 
|---|
|  | 183 | cout << " ------------ Array X = \n " << x << "\n" << endl; | 
|---|
|  | 184 | cout << " ------------ Array B = \n " << b << "\n" << endl; | 
|---|
| [778] | 185 |  | 
|---|
|  | 186 | cout << "\n     Calling rztest_lapack ... " << endl; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | rztest_lapack(a, b); | 
|---|
|  | 189 |  | 
|---|
|  | 190 | cout << " ------------ Result B(=X ?) = \n " << b << "\n" << endl; | 
|---|
| [1248] | 191 | return(0); | 
|---|
| [778] | 192 | } | 
|---|