source: Sophya/trunk/SophyaProg/Tests/lpk.cc@ 3606

Last change on this file since 3606 was 2647, checked in by ansari, 21 years ago

Ajout programme test inversion matrice par SimpleMatrixOperation et ajout test inversion par lapack ds lpk.cc - Reza 7 Fev 2005

File size: 8.9 KB
RevLine 
[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
[2647]14int lpk_linsolve_mtx(int n); // ErrorCode = 8
15int lpk_svd_mtx(int l, int c, int wsf, bool covu=true); // ErrorCode = 16
16int lpk_leastsquare_mtx(int l, int c); // ErrorCode = 32
17int lpk_inverse_mtx(int l); // ErrorCode = 64
[812]18
[1248]19static double TOLERANCE = 1.e-6;
[2647]20static int nprt = 100;
21static int prtlev = 1;
[1100]22
[778]23int main(int narg, char* arg[])
24{
25
26 SophyaInit();
27 InitTim(); // Initializing the CPU timer
28
29
[812]30
31 if (narg < 2) {
[1343]32 cout << " lpk - LinAlg/LapackServer test \n"
[2647]33 << " Usage: lpk select [sizeL,C=5,5] [prtlev=1] \n"
[1343]34 << " [nprtmax=100] [WorkSpaceSizeFactor=2] \n"
[2647]35 << " select= linsolve svd svds lss inverse / all= \n"
[1343]36 << " linsolve: lpk_linsolve_mtx() LapackServer::LinSolve with TMatrix<r_8> \n"
37 << " svd: lpk_svd_mtx() LapackServer::SVD(a,s,u,vt) with TMatrix<r_8> \n"
[1567]38 << " svds: lpk_svd_mtx() LapackServer::SVD(a,s); with TMatrix<r_8> \n"
39 << " lss: lpk_leastsquare_mtx() LapackServer::LeastSquareSolve() r_8 \n"
[2647]40 << " inverse: LapackServer::ComputeInverse() r_8 \n" << endl;
[812]41 exit(0);
42 }
[1343]43 int l,c;
44 l = c = 5;
45 int wsf = 2;
46 string opt = arg[1];
47 if (narg > 2) sscanf(arg[2], "%d,%d", &l, &c);
[812]48 if (narg > 3) prtlev = atoi(arg[3]);
49 if (narg > 4) nprt = atoi(arg[4]);
[1343]50 if (narg > 5) wsf = atoi(arg[5]);
[812]51
[2647]52 cout << " lpk - LinAlg/LapackServer_Test sizeL,C=" << l << "," << c
53 << " opt= " << opt << endl;
[1248]54 int rc = 0;
[812]55 BaseArray::SetMaxPrint(nprt, prtlev);
56 try {
[2647]57 if (opt == "all") {
58 rc += lpk_linsolve_mtx(l);
59 rc += lpk_svd_mtx(l,c,wsf,true);
60 rc += lpk_leastsquare_mtx(l,c);
61 rc += lpk_inverse_mtx(l);
62 }
63 else if (opt == "linsolve") rc = lpk_linsolve_mtx(l);
[1343]64 else if (opt == "svd") rc = lpk_svd_mtx(l,c,wsf,true);
65 else if (opt == "svds") rc = lpk_svd_mtx(l,c,wsf,false);
[1567]66 else if (opt == "lss") rc = lpk_leastsquare_mtx(l,c);
[2647]67 else if (opt == "inverse") rc = lpk_inverse_mtx(l);
[1343]68 else { cout << " Unknown option " << opt << " ! " << endl; rc = 66; }
[812]69 }
70 catch (PThrowable exc) {
71 cerr << " catched Exception (lpk.cc) " << exc.Msg() << endl;
[1248]72 rc = 77;
[812]73 }
74 catch (...) {
75 cerr << " catched unknown (...) exception (lpk.cc) " << endl;
[1248]76 rc = 78;
[812]77 }
78
79 PrtTim(" End of lpk LinAlg/Lapack test ");
[1343]80 cout << " --------------- END of Programme -------- (Rc= "
81 << rc << ") --- " << endl;
[1248]82 return(rc);
[812]83}
84
[1343]85// -----------------------------------------------------------------------
86/* Nouvelle-Fonction */
87int lpk_linsolve_mtx(int n)
[812]88{
[1343]89 int i,j;
[812]90 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
[1343]91 cout << " lpk_linsolve_mtx() - Test of LapackServer::LinSolve() " << endl;
[812]92 Matrix a(n,n);
93 for(i=0; i<n; i++)
94 for(j=0; j<n; j++) a(j,i) = GauRnd(0., 1.);
95
96 Vector x(n), b;
97 // Matrix x(n,1), b;
[2647]98 if (prtlev > 0)
99 cout << " ------------ Vector X = \n " << x << "\n" << endl;
[812]100 for(i=0; i<n; i++) x(i) = GauRnd(2., 1.5);
101 b = a*x;
[778]102
[2647]103 if (prtlev > 0) {
104 cout << " ---- lpk_tmtx() LapackServer::LinSolve Test Using TMatrix<r_8> ----- " << endl;
105 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
106 cout << " ------------ Matrix X = \n " << x << "\n" << endl;
107 cout << " ------------ Matrix B = \n " << b << "\n" << endl;
108 }
[812]109 cout << "\n Calling LapackLinSolve(a,b) .... " << endl;
[1100]110 PrtTim(" Calling LapackLinSolve(a,b) ");
[812]111 LapackLinSolve(a,b);
[1100]112 PrtTim(" End LapackLinSolve(a,b) ");
[812]113
[2647]114 if (prtlev > 0)
115 cout << " ------------ Result B(=X ?) = \n " << b << "\n" << endl;
[812]116 Vector diff = b-x;
[1343]117 PrtTim(" End of Compute(diff)");
[2647]118 if (prtlev > 0)
119 cout << " ------------ Vector diff B-X = \n " << diff << "\n" << endl;
[1248]120 double min,max;
121 diff.MinMax(min, max);
122 cout << " Min/Max difference Vector (?=0) , Min= " << min
123 << " Max= " << max << endl;
124 if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) {
125 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
126 << endl;
[2647]127 return(8);
[1248]128 }
129 return(0);
[812]130
131}
132
[1343]133// -----------------------------------------------------------------------
134/* Nouvelle-Fonction */
135int lpk_svd_mtx(int m, int n, int wsf, bool covu)
[812]136{
[1343]137 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
138 cout << " lpk_svd_mtx() - Test of LapackServer::SVD " << endl;
139
140 Matrix a(m , n), aa;
141 a = RandomSequence(RandomSequence::Gaussian, 0., 4.);
142 aa = a;
143 Vector s;
144 Matrix u, vt;
145 cout << " ---- lpk_svd_tmtx() LapackServer::SVD Test Using TMatrix<r_8> ---- " << endl;
[2647]146 if (prtlev > 0)
147 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
[1343]148
149 cout << "\n Calling LapackSVD(a,s,u,vt) .... " << endl;
150 PrtTim(" Calling LapackSVD() ");
151 LapackServer<r_8> lpks;
152 lpks.SetWorkSpaceSizeFactor(wsf);
153 if (covu) lpks.SVD(aa, s, u, vt);
154 else lpks.SVD(aa, s);
155 PrtTim(" End LapackSVD() ");
156
[2647]157 if (prtlev > 0)
158 cout << " ------------ Result S = \n " << s << "\n" << endl;
[1343]159 if (!covu) return(0);
[2647]160 if (prtlev > 0) {
161 cout << " ------------ Result U = \n " << u << "\n" << endl;
162 cout << " ------------ Result VT = \n " << vt << "\n" << endl;
163 }
[1343]164 Matrix sm(m,n);
165 int minmn = (m<n) ? m : n ;
166 for(int k=0; k< minmn ; k++) sm(k,k) = s(k);
167 Matrix diff = u*(sm*vt) - a;
168 PrtTim(" End of Compute(diff)");
[2647]169 if (prtlev > 0)
170 cout << " ------------ Matrix diff U*S*Vt - A = \n " << diff << "\n" << endl;
[1343]171 double min,max;
172 diff.MinMax(min, max);
173 cout << " Min/Max difference Matrix (?=0) , Min= " << min
174 << " Max= " << max << endl;
175 if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) {
176 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
177 << endl;
[2647]178 return(16);
[1343]179 }
180 return(0);
181
182}
183
[1567]184// -----------------------------------------------------------------------
185/* Nouvelle-Fonction */
186int lpk_leastsquare_mtx(int m, int n)
187{
188 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
189 cout << " lpk_leastsquare_mtx() - Test of LapackLeastSquareSolve " << endl;
190
191 Matrix a(m , n), aa;
192 a = RandomSequence(RandomSequence::Gaussian, 0., 4.);
193 aa = a;
194 Vector x(n),noise(m),b,bx;
195 x = RandomSequence(RandomSequence::Gaussian, 2., 1.);
196 double signoise = 0.1;
197 noise = RandomSequence(RandomSequence::Gaussian, 0., 0.1);
198 b = a*x;
199 bx = b+noise;
200
201 cout << " ---- lpk_leastsquare_tmtx() LapackLeastSquareSolve<r_8> ---- " << endl;
[2647]202 if (prtlev > 0) {
203 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
204 cout << " ------------ Vector B = \n " << bx << "\n" << endl;
205 cout << " ------------ Vector X = \n " << x << "\n" << endl;
206 }
[1567]207 cout << "\n Calling LapackLeastSquareSolve(a,bx) .... " << endl;
208 PrtTim(" Calling LapackLeastSquareSolve() ");
209 LapackLeastSquareSolve(aa, bx);
210 PrtTim(" End LapackLeastSquareSolve() ");
211 bx.Share(bx(Range(0,0,n)));
[2647]212 if (prtlev > 0) {
213 cout << " ------------ Result X = \n " << bx << "\n" << endl;
214 cout << " ------------ X-X_Real = \n " << bx-x << "\n" << endl;
215 }
[1567]216 Vector diff = b-a*bx;
217 PrtTim(" End of Compute(diff)");
[2647]218 if (prtlev > 0)
219 cout << " ------------ Matrix diff b-a*x = \n " << diff << "\n" << endl;
[1567]220 double mean,sigma;
221 MeanSigma(diff, mean, sigma);
222 cout << " MeanSigma(diff) , Mean= " << mean << " Sigma=" << sigma
223 << " SigNoise= " << signoise << endl;
224 if ((fabs(mean) > signoise) || (fabs(sigma-signoise) > signoise)) {
225 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
226 << endl;
[2647]227 return 32;
[1567]228 }
[2647]229 return 0;
[1567]230
231}
[2647]232// -----------------------------------------------------------------------
233/* Nouvelle-Fonction */
234int lpk_inverse_mtx(int m)
[1343]235{
[2647]236 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
237 cout << " lpk_inverse_mtx() - Test of LapackComputeInverse<r_8> " << endl;
[778]238
[2647]239 Matrix a(m , m), aa, ainv, idmx(m,m), mxprod, diff;
240 a = RandomSequence(RandomSequence::Gaussian, 0., 4.);
241 aa = a; // We make a copy of a, as it is modified by LapackServer
242 PrtTim(" End of a_inint");
[778]243
[2647]244 cout << "\n Calling LapackInverse() .... " << endl;
245 ainv = LapackInverse(aa);
246 PrtTim(" End of LapackInverse(a)");
247 mxprod = a*ainv;
248 PrtTim(" End of mxprod = a*ainv");
249 idmx = IdentityMatrix();
250 PrtTim(" End of idmx = IdentityMatrix()");
251 diff = mxprod-idmx;
252 PrtTim(" End of Compute(diff)");
[778]253
[2647]254 if (prtlev > 0) {
255 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
256 cout << " ------------ Matrix Inverse(A) = \n " << ainv << "\n" << endl;
257 cout << " ------------ Matrix mxprod = A*Inverse(A) = \n " << mxprod << "\n" << endl;
258 }
259 double min,max;
260 diff.MinMax(min, max);
261 PrtTim(" End of diffCheck");
262 cout << " Min/Max difference Matrix (?=0) , Min= " << min
263 << " Max= " << max << endl;
264 if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) {
265 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
266 << endl;
267 return 64;
268 }
269 return 0;
[778]270
[2647]271}
[778]272
Note: See TracBrowser for help on using the repository browser.