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